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"
47 struct pa_protocol_native;
49 struct record_stream {
50 struct connection *connection;
52 struct pa_source_output *source_output;
53 struct pa_memblockq *memblockq;
57 struct playback_stream {
59 struct connection *connection;
61 struct pa_sink_input *sink_input;
62 struct pa_memblockq *memblockq;
63 size_t requested_bytes;
68 struct upload_stream {
70 struct connection *connection;
72 struct pa_memchunk memchunk;
75 struct pa_sample_spec sample_spec;
78 struct output_stream {
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;
97 struct pa_protocol_native {
98 struct pa_module *module;
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];
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);
111 static void request_bytes(struct playback_stream*s);
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);
116 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
117 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
118 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
119 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
120 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
121 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
122 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
123 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
124 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
125 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
126 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
127 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
128 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
129 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
130 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
131 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
133 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
134 [PA_COMMAND_ERROR] = { NULL },
135 [PA_COMMAND_TIMEOUT] = { NULL },
136 [PA_COMMAND_REPLY] = { NULL },
137 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
138 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_stream },
139 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
140 [PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
141 [PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_stream },
142 [PA_COMMAND_AUTH] = { command_auth },
143 [PA_COMMAND_REQUEST] = { NULL },
144 [PA_COMMAND_EXIT] = { command_exit },
145 [PA_COMMAND_SET_NAME] = { command_set_name },
146 [PA_COMMAND_LOOKUP_SINK] = { command_lookup },
147 [PA_COMMAND_LOOKUP_SOURCE] = { command_lookup },
148 [PA_COMMAND_STAT] = { command_stat },
149 [PA_COMMAND_GET_PLAYBACK_LATENCY] = { command_get_playback_latency },
150 [PA_COMMAND_CREATE_UPLOAD_STREAM] = { command_create_upload_stream },
151 [PA_COMMAND_DELETE_UPLOAD_STREAM] = { command_delete_stream },
152 [PA_COMMAND_FINISH_UPLOAD_STREAM] = { command_finish_upload_stream },
153 [PA_COMMAND_PLAY_SAMPLE] = { command_play_sample },
154 [PA_COMMAND_REMOVE_SAMPLE] = { command_remove_sample },
155 [PA_COMMAND_GET_SINK_INFO] = { command_get_info },
156 [PA_COMMAND_GET_SOURCE_INFO] = { command_get_info },
157 [PA_COMMAND_GET_SINK_INFO_LIST] = { command_get_info_list },
158 [PA_COMMAND_GET_SOURCE_INFO_LIST] = { command_get_info_list },
161 /* structure management */
163 static struct upload_stream* upload_stream_new(struct connection *c, const struct pa_sample_spec *ss, const char *name, size_t length) {
164 struct upload_stream *s;
165 assert(c && ss && name && length);
167 s = pa_xmalloc(sizeof(struct upload_stream));
168 s->type = UPLOAD_STREAM;
170 s->sample_spec = *ss;
171 s->name = pa_xstrdup(name);
173 s->memchunk.memblock = NULL;
174 s->memchunk.index = 0;
175 s->memchunk.length = 0;
179 pa_idxset_put(c->output_streams, s, &s->index);
183 static void upload_stream_free(struct upload_stream *o) {
184 assert(o && o->connection);
186 pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
190 if (o->memchunk.memblock)
191 pa_memblock_unref(o->memchunk.memblock);
196 static struct record_stream* record_stream_new(struct connection *c, struct pa_source *source, const struct pa_sample_spec *ss, const char *name, size_t maxlength, size_t fragment_size) {
197 struct record_stream *s;
198 struct pa_source_output *source_output;
200 assert(c && source && ss && name && maxlength);
202 if (!(source_output = pa_source_output_new(source, name, ss)))
205 s = pa_xmalloc(sizeof(struct record_stream));
207 s->source_output = source_output;
208 s->source_output->push = source_output_push_cb;
209 s->source_output->kill = source_output_kill_cb;
210 s->source_output->userdata = s;
211 s->source_output->owner = c->protocol->module;
212 s->source_output->client = c->client;
214 s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0);
215 assert(s->memblockq);
217 s->fragment_size = (fragment_size/base)*base;
218 if (!s->fragment_size)
219 s->fragment_size = base;
221 pa_idxset_put(c->record_streams, s, &s->index);
225 static void record_stream_free(struct record_stream* r) {
226 assert(r && r->connection);
228 pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
229 pa_source_output_free(r->source_output);
230 pa_memblockq_free(r->memblockq);
234 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name,
239 struct playback_stream *s;
240 struct pa_sink_input *sink_input;
241 assert(c && sink && ss && name && maxlength);
243 if (!(sink_input = pa_sink_input_new(sink, name, ss)))
246 s = pa_xmalloc(sizeof(struct playback_stream));
247 s->type = PLAYBACK_STREAM;
249 s->sink_input = sink_input;
251 s->sink_input->peek = sink_input_peek_cb;
252 s->sink_input->drop = sink_input_drop_cb;
253 s->sink_input->kill = sink_input_kill_cb;
254 s->sink_input->get_latency = sink_input_get_latency_cb;
255 s->sink_input->userdata = s;
256 s->sink_input->owner = c->protocol->module;
257 s->sink_input->client = c->client;
259 s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq);
260 assert(s->memblockq);
262 s->requested_bytes = 0;
263 s->drain_request = 0;
265 pa_idxset_put(c->output_streams, s, &s->index);
269 static void playback_stream_free(struct playback_stream* p) {
270 assert(p && p->connection);
272 if (p->drain_request)
273 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
275 pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
276 pa_sink_input_free(p->sink_input);
277 pa_memblockq_free(p->memblockq);
281 static void connection_free(struct connection *c) {
282 struct record_stream *r;
283 struct output_stream *o;
284 assert(c && c->protocol);
286 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
287 while ((r = pa_idxset_first(c->record_streams, NULL)))
288 record_stream_free(r);
289 pa_idxset_free(c->record_streams, NULL, NULL);
291 while ((o = pa_idxset_first(c->output_streams, NULL)))
292 if (o->type == PLAYBACK_STREAM)
293 playback_stream_free((struct playback_stream*) o);
295 upload_stream_free((struct upload_stream*) o);
296 pa_idxset_free(c->output_streams, NULL, NULL);
298 pa_pdispatch_free(c->pdispatch);
299 pa_pstream_free(c->pstream);
300 pa_client_free(c->client);
304 static void request_bytes(struct playback_stream *s) {
305 struct pa_tagstruct *t;
309 if (!(l = pa_memblockq_missing(s->memblockq)))
312 if (l <= s->requested_bytes)
315 l -= s->requested_bytes;
317 if (l < pa_memblockq_get_minreq(s->memblockq))
320 s->requested_bytes += l;
322 t = pa_tagstruct_new(NULL, 0);
324 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
325 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
326 pa_tagstruct_putu32(t, s->index);
327 pa_tagstruct_putu32(t, l);
328 pa_pstream_send_tagstruct(s->connection->pstream, t);
330 /*fprintf(stderr, "Requesting %u bytes\n", l);*/
333 static void send_memblock(struct connection *c) {
335 struct record_stream *r;
337 start = PA_IDXSET_INVALID;
339 struct pa_memchunk chunk;
341 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
344 if (start == PA_IDXSET_INVALID)
345 start = c->rrobin_index;
346 else if (start == c->rrobin_index)
349 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
350 if (chunk.length > r->fragment_size)
351 chunk.length = r->fragment_size;
353 pa_pstream_send_memblock(c->pstream, r->index, 0, &chunk);
354 pa_memblockq_drop(r->memblockq, chunk.length);
355 pa_memblock_unref(chunk.memblock);
362 static void send_playback_stream_killed(struct playback_stream *p) {
363 struct pa_tagstruct *t;
366 t = pa_tagstruct_new(NULL, 0);
368 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
369 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
370 pa_tagstruct_putu32(t, p->index);
371 pa_pstream_send_tagstruct(p->connection->pstream, t);
374 static void send_record_stream_killed(struct record_stream *r) {
375 struct pa_tagstruct *t;
378 t = pa_tagstruct_new(NULL, 0);
380 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
381 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
382 pa_tagstruct_putu32(t, r->index);
383 pa_pstream_send_tagstruct(r->connection->pstream, t);
387 /*** sinkinput callbacks ***/
389 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
390 struct playback_stream *s;
391 assert(i && i->userdata && chunk);
394 if (pa_memblockq_peek(s->memblockq, chunk) < 0)
400 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
401 struct playback_stream *s;
402 assert(i && i->userdata && length);
405 pa_memblockq_drop(s->memblockq, length);
408 if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
409 pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
410 s->drain_request = 0;
414 static void sink_input_kill_cb(struct pa_sink_input *i) {
415 assert(i && i->userdata);
416 send_playback_stream_killed((struct playback_stream *) i->userdata);
417 playback_stream_free((struct playback_stream *) i->userdata);
420 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
421 struct playback_stream *s;
422 assert(i && i->userdata);
425 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
428 /*** source_output callbacks ***/
430 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
431 struct record_stream *s;
432 assert(o && o->userdata && chunk);
435 pa_memblockq_push(s->memblockq, chunk, 0);
436 if (!pa_pstream_is_pending(s->connection->pstream))
437 send_memblock(s->connection);
440 static void source_output_kill_cb(struct pa_source_output *o) {
441 assert(o && o->userdata);
442 send_record_stream_killed((struct record_stream *) o->userdata);
443 record_stream_free((struct record_stream *) o->userdata);
446 /*** pdispatch callbacks ***/
448 static void protocol_error(struct connection *c) {
449 fprintf(stderr, __FILE__": protocol error, kicking client\n");
453 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
454 struct connection *c = userdata;
455 struct playback_stream *s;
456 size_t maxlength, tlength, prebuf, minreq;
458 const char *name, *sink_name;
459 struct pa_sample_spec ss;
460 struct pa_tagstruct *reply;
461 struct pa_sink *sink;
462 assert(c && t && c->protocol && c->protocol->core);
464 if (pa_tagstruct_gets(t, &name) < 0 ||
465 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
466 pa_tagstruct_getu32(t, &sink_index) < 0 ||
467 pa_tagstruct_gets(t, &sink_name) < 0 ||
468 pa_tagstruct_getu32(t, &maxlength) < 0 ||
469 pa_tagstruct_getu32(t, &tlength) < 0 ||
470 pa_tagstruct_getu32(t, &prebuf) < 0 ||
471 pa_tagstruct_getu32(t, &minreq) < 0 ||
472 !pa_tagstruct_eof(t)) {
477 if (!c->authorized) {
478 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
482 if (sink_index != (uint32_t) -1)
483 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
485 sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
488 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
492 if (!(s = playback_stream_new(c, sink, &ss, name, maxlength, tlength, prebuf, minreq))) {
493 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
497 reply = pa_tagstruct_new(NULL, 0);
499 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
500 pa_tagstruct_putu32(reply, tag);
501 pa_tagstruct_putu32(reply, s->index);
502 assert(s->sink_input);
503 pa_tagstruct_putu32(reply, s->sink_input->index);
504 pa_pstream_send_tagstruct(c->pstream, reply);
508 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
509 struct connection *c = userdata;
513 if (pa_tagstruct_getu32(t, &channel) < 0 ||
514 !pa_tagstruct_eof(t)) {
519 if (!c->authorized) {
520 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
524 if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
525 struct playback_stream *s;
526 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
527 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
531 playback_stream_free(s);
532 } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
533 struct record_stream *s;
534 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
535 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
539 record_stream_free(s);
541 struct upload_stream *s;
542 assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
543 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
544 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
548 upload_stream_free(s);
551 pa_pstream_send_simple_ack(c->pstream, tag);
554 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
555 struct connection *c = userdata;
556 struct record_stream *s;
557 size_t maxlength, fragment_size;
558 uint32_t source_index;
559 const char *name, *source_name;
560 struct pa_sample_spec ss;
561 struct pa_tagstruct *reply;
562 struct pa_source *source;
563 assert(c && t && c->protocol && c->protocol->core);
565 if (pa_tagstruct_gets(t, &name) < 0 ||
566 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
567 pa_tagstruct_getu32(t, &source_index) < 0 ||
568 pa_tagstruct_gets(t, &source_name) < 0 ||
569 pa_tagstruct_getu32(t, &maxlength) < 0 ||
570 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
571 !pa_tagstruct_eof(t)) {
576 if (!c->authorized) {
577 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
581 if (source_index != (uint32_t) -1)
582 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
584 source = pa_namereg_get(c->protocol->core, *source_name ? source_name : NULL, PA_NAMEREG_SOURCE, 1);
587 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
591 if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
592 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
596 reply = pa_tagstruct_new(NULL, 0);
598 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
599 pa_tagstruct_putu32(reply, tag);
600 pa_tagstruct_putu32(reply, s->index);
601 assert(s->source_output);
602 pa_tagstruct_putu32(reply, s->source_output->index);
603 pa_pstream_send_tagstruct(c->pstream, reply);
606 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
607 struct connection *c = userdata;
610 if (!pa_tagstruct_eof(t)) {
615 if (!c->authorized) {
616 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
620 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
621 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
622 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
626 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
627 struct connection *c = userdata;
631 if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
632 !pa_tagstruct_eof(t)) {
637 if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
638 fprintf(stderr, "protocol-native.c: Denied access to client with invalid authorization key.\n");
639 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
644 pa_pstream_send_simple_ack(c->pstream, tag);
648 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
649 struct connection *c = userdata;
653 if (pa_tagstruct_gets(t, &name) < 0 ||
654 !pa_tagstruct_eof(t)) {
659 pa_client_rename(c->client, name);
660 pa_pstream_send_simple_ack(c->pstream, tag);
664 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
665 struct connection *c = userdata;
667 uint32_t index = PA_IDXSET_INVALID;
670 if (pa_tagstruct_gets(t, &name) < 0 ||
671 !pa_tagstruct_eof(t)) {
676 if (!c->authorized) {
677 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
681 if (command == PA_COMMAND_LOOKUP_SINK) {
682 struct pa_sink *sink;
683 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
686 struct pa_source *source;
687 assert(command == PA_COMMAND_LOOKUP_SOURCE);
688 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
689 index = source->index;
692 if (index == PA_IDXSET_INVALID)
693 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
695 struct pa_tagstruct *reply;
696 reply = pa_tagstruct_new(NULL, 0);
698 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
699 pa_tagstruct_putu32(reply, tag);
700 pa_tagstruct_putu32(reply, index);
701 pa_pstream_send_tagstruct(c->pstream, reply);
705 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
706 struct connection *c = userdata;
708 struct playback_stream *s;
711 if (pa_tagstruct_getu32(t, &index) < 0 ||
712 !pa_tagstruct_eof(t)) {
717 if (!c->authorized) {
718 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
722 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
723 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
727 s->drain_request = 0;
729 if (!pa_memblockq_is_readable(s->memblockq))
730 pa_pstream_send_simple_ack(c->pstream, tag);
732 s->drain_request = 1;
737 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
738 struct connection *c = userdata;
740 struct pa_tagstruct *reply;
742 if (!pa_tagstruct_eof(t)) {
747 if (!c->authorized) {
748 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
752 reply = pa_tagstruct_new(NULL, 0);
754 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
755 pa_tagstruct_putu32(reply, tag);
756 pa_tagstruct_putu32(reply, pa_memblock_get_count());
757 pa_tagstruct_putu32(reply, pa_memblock_get_total());
758 pa_pstream_send_tagstruct(c->pstream, reply);
761 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
762 struct connection *c = userdata;
764 struct pa_tagstruct *reply;
765 struct playback_stream *s;
766 uint32_t index, latency;
768 if (pa_tagstruct_getu32(t, &index) < 0 ||
769 !pa_tagstruct_eof(t)) {
774 if (!c->authorized) {
775 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
779 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
780 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
784 latency = pa_sink_input_get_latency(s->sink_input);
785 reply = pa_tagstruct_new(NULL, 0);
787 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
788 pa_tagstruct_putu32(reply, tag);
789 pa_tagstruct_putu32(reply, latency);
790 pa_pstream_send_tagstruct(c->pstream, reply);
793 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
794 struct connection *c = userdata;
795 struct upload_stream *s;
798 struct pa_sample_spec ss;
799 struct pa_tagstruct *reply;
800 assert(c && t && c->protocol && c->protocol->core);
802 if (pa_tagstruct_gets(t, &name) < 0 ||
803 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
804 pa_tagstruct_getu32(t, &length) < 0 ||
805 !pa_tagstruct_eof(t)) {
810 if (!c->authorized) {
811 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
815 if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
816 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
820 if (!(s = upload_stream_new(c, &ss, name, length))) {
821 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
825 reply = pa_tagstruct_new(NULL, 0);
827 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
828 pa_tagstruct_putu32(reply, tag);
829 pa_tagstruct_putu32(reply, s->index);
830 pa_pstream_send_tagstruct(c->pstream, reply);
832 reply = pa_tagstruct_new(NULL, 0);
834 pa_tagstruct_putu32(reply, PA_COMMAND_REQUEST);
835 pa_tagstruct_putu32(reply, (uint32_t) -1); /* tag */
836 pa_tagstruct_putu32(reply, s->index);
837 pa_tagstruct_putu32(reply, length);
838 pa_pstream_send_tagstruct(c->pstream, reply);
841 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
842 struct connection *c = userdata;
844 struct upload_stream *s;
848 if (pa_tagstruct_getu32(t, &channel) < 0 ||
849 !pa_tagstruct_eof(t)) {
854 if (!c->authorized) {
855 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
859 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
860 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
864 pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
865 pa_pstream_send_simple_ack(c->pstream, tag);
866 upload_stream_free(s);
869 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
870 struct connection *c = userdata;
871 uint32_t sink_index, volume;
872 struct pa_sink *sink;
873 const char *name, *sink_name;
876 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
877 pa_tagstruct_gets(t, &sink_name) < 0 ||
878 pa_tagstruct_getu32(t, &volume) < 0 ||
879 pa_tagstruct_gets(t, &name) < 0 ||
880 !pa_tagstruct_eof(t)) {
885 if (!c->authorized) {
886 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
890 if (sink_index != (uint32_t) -1)
891 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
893 sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
896 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
900 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
901 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
905 pa_pstream_send_simple_ack(c->pstream, tag);
908 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
909 struct connection *c = userdata;
913 if (pa_tagstruct_gets(t, &name) < 0 ||
914 !pa_tagstruct_eof(t)) {
919 if (!c->authorized) {
920 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
924 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
925 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
929 pa_pstream_send_simple_ack(c->pstream, tag);
932 static void sink_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink *sink) {
934 pa_tagstruct_putu32(t, sink->index);
935 pa_tagstruct_puts(t, sink->name);
936 pa_tagstruct_puts(t, sink->description);
937 pa_tagstruct_put_sample_spec(t, &sink->sample_spec);
938 pa_tagstruct_putu32(t, sink->owner ? sink->owner->index : (uint32_t) -1);
939 pa_tagstruct_putu32(t, sink->volume);
940 pa_tagstruct_putu32(t, sink->monitor_source->index);
941 pa_tagstruct_puts(t, sink->monitor_source->name);
942 pa_tagstruct_putu32(t, pa_sink_get_latency(sink));
945 static void source_fill_tagstruct(struct pa_tagstruct *t, struct pa_source *source) {
947 pa_tagstruct_putu32(t, source->index);
948 pa_tagstruct_puts(t, source->name);
949 pa_tagstruct_puts(t, source->description);
950 pa_tagstruct_put_sample_spec(t, &source->sample_spec);
951 pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
952 pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
953 pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : "");
956 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
957 struct connection *c = userdata;
959 struct pa_sink *sink = NULL;
960 struct pa_source *source = NULL;
962 struct pa_tagstruct *reply;
965 if (pa_tagstruct_getu32(t, &index) < 0 ||
966 pa_tagstruct_gets(t, &name) < 0 ||
967 !pa_tagstruct_eof(t)) {
972 if (!c->authorized) {
973 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
977 if (command == PA_COMMAND_GET_SINK_INFO) {
978 if (index != (uint32_t) -1)
979 sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
981 sink = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SINK, 1);
983 assert(command == PA_COMMAND_GET_SOURCE_INFO);
984 if (index != (uint32_t) -1)
985 source = pa_idxset_get_by_index(c->protocol->core->sources, index);
987 source = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SOURCE, 1);
990 if (!sink && !source) {
991 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
995 reply = pa_tagstruct_new(NULL, 0);
997 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
998 pa_tagstruct_putu32(reply, tag);
1000 sink_fill_tagstruct(reply, sink);
1002 source_fill_tagstruct(reply, source);
1003 pa_pstream_send_tagstruct(c->pstream, reply);
1006 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1007 struct connection *c = userdata;
1008 struct pa_idxset *i;
1011 struct pa_tagstruct *reply;
1014 if (!pa_tagstruct_eof(t)) {
1019 if (!c->authorized) {
1020 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1024 reply = pa_tagstruct_new(NULL, 0);
1026 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1027 pa_tagstruct_putu32(reply, tag);
1029 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1030 i = c->protocol->core->sinks;
1032 assert(command == PA_COMMAND_GET_SOURCE_INFO_LIST);
1033 i = c->protocol->core->sources;
1036 for (p = pa_idxset_first(i, &index); p; p = pa_idxset_next(i, &index)) {
1037 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1038 sink_fill_tagstruct(reply, p);
1040 assert(command == PA_COMMAND_GET_SOURCE_INFO_LIST);
1041 source_fill_tagstruct(reply, p);
1045 pa_pstream_send_tagstruct(c->pstream, reply);
1048 /*** pstream callbacks ***/
1050 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
1051 struct connection *c = userdata;
1052 assert(p && packet && packet->data && c);
1054 if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1055 fprintf(stderr, "protocol-native: invalid packet.\n");
1060 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) {
1061 struct connection *c = userdata;
1062 struct output_stream *stream;
1063 assert(p && chunk && userdata);
1065 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1066 fprintf(stderr, "protocol-native: client sent block for invalid stream.\n");
1071 if (stream->type == PLAYBACK_STREAM) {
1072 struct playback_stream *p = (struct playback_stream*) stream;
1073 if (chunk->length >= p->requested_bytes)
1074 p->requested_bytes = 0;
1076 p->requested_bytes -= chunk->length;
1078 pa_memblockq_push_align(p->memblockq, chunk, delta);
1079 assert(p->sink_input);
1080 pa_sink_notify(p->sink_input->sink);
1081 /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
1083 struct upload_stream *u = (struct upload_stream*) stream;
1085 assert(u->type == UPLOAD_STREAM);
1087 if (!u->memchunk.memblock) {
1088 if (u->length == chunk->length) {
1089 u->memchunk = *chunk;
1090 pa_memblock_ref(u->memchunk.memblock);
1092 fprintf(stderr, "COPY\n");
1094 u->memchunk.memblock = pa_memblock_new(u->length);
1095 u->memchunk.index = u->memchunk.length = 0;
1099 assert(u->memchunk.memblock);
1102 if (l > chunk->length)
1106 memcpy(u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length, chunk->memblock->data+chunk->index, l);
1107 u->memchunk.length += l;
1113 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
1114 struct connection *c = userdata;
1118 fprintf(stderr, "protocol-native: connection died.\n");
1122 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
1123 struct connection *c = userdata;
1129 /*** client callbacks ***/
1131 static void client_kill_cb(struct pa_client *c) {
1132 assert(c && c->userdata);
1133 connection_free(c->userdata);
1136 /*** socket server callbacks ***/
1138 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
1139 struct pa_protocol_native *p = userdata;
1140 struct connection *c;
1141 assert(s && io && p);
1143 c = pa_xmalloc(sizeof(struct connection));
1144 c->authorized = p->public;
1147 c->client = pa_client_new(p->core, "NATIVE", "Client");
1149 c->client->kill = client_kill_cb;
1150 c->client->userdata = c;
1151 c->client->owner = p->module;
1153 c->pstream = pa_pstream_new(p->core->mainloop, io);
1156 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
1157 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
1158 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
1159 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
1161 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
1162 assert(c->pdispatch);
1164 c->record_streams = pa_idxset_new(NULL, NULL);
1165 c->output_streams = pa_idxset_new(NULL, NULL);
1166 assert(c->record_streams && c->output_streams);
1168 c->rrobin_index = PA_IDXSET_INVALID;
1170 pa_idxset_put(p->connections, c, NULL);
1173 /*** module entry points ***/
1175 struct pa_protocol_native* pa_protocol_native_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) {
1176 struct pa_protocol_native *p;
1178 assert(core && server && ma);
1180 if (pa_modargs_get_value_u32(ma, "public", &public) < 0) {
1181 fprintf(stderr, __FILE__": public= expects numeric argument.\n");
1185 p = pa_xmalloc(sizeof(struct pa_protocol_native));
1187 if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
1196 p->connections = pa_idxset_new(NULL, NULL);
1197 assert(p->connections);
1199 pa_socket_server_set_callback(p->server, on_connection, p);
1204 void pa_protocol_native_free(struct pa_protocol_native *p) {
1205 struct connection *c;
1208 while ((c = pa_idxset_first(p->connections, NULL)))
1210 pa_idxset_free(p->connections, NULL, NULL);
1211 pa_socket_server_free(p->server);