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"
51 struct pa_protocol_native;
53 struct record_stream {
54 struct connection *connection;
56 struct pa_source_output *source_output;
57 struct pa_memblockq *memblockq;
61 struct playback_stream {
63 struct connection *connection;
65 struct pa_sink_input *sink_input;
66 struct pa_memblockq *memblockq;
67 size_t requested_bytes;
72 struct upload_stream {
74 struct connection *connection;
76 struct pa_memchunk memchunk;
79 struct pa_sample_spec sample_spec;
82 struct output_stream {
93 struct pa_protocol_native *protocol;
94 struct pa_client *client;
95 struct pa_pstream *pstream;
96 struct pa_pdispatch *pdispatch;
97 struct pa_idxset *record_streams, *output_streams;
98 uint32_t rrobin_index;
99 struct pa_subscription *subscription;
102 struct pa_protocol_native {
103 struct pa_module *module;
105 struct pa_core *core;
106 struct pa_socket_server *server;
107 struct pa_idxset *connections;
108 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
111 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
112 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length);
113 static void sink_input_kill_cb(struct pa_sink_input *i);
114 static pa_usec_t sink_input_get_latency_cb(struct pa_sink_input *i);
116 static void request_bytes(struct playback_stream*s);
118 static void source_output_kill_cb(struct pa_source_output *o);
119 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
120 static pa_usec_t source_output_get_latency_cb(struct pa_source_output *o);
122 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
123 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
124 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
125 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
126 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
127 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
128 static void command_set_client_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
129 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
130 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
131 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
132 static void command_get_record_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
133 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
134 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
135 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
136 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
137 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
138 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
139 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
140 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
141 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
142 static void command_cork_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
143 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
144 static void command_set_default_sink_or_source(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
145 static void command_set_stream_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
146 static void command_kill(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
147 static void command_load_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
148 static void command_unload_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
149 static void command_add_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
150 static void command_remove_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
151 static void command_get_autoload_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
152 static void command_get_autoload_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
154 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
155 [PA_COMMAND_ERROR] = { NULL },
156 [PA_COMMAND_TIMEOUT] = { NULL },
157 [PA_COMMAND_REPLY] = { NULL },
158 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
159 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_stream },
160 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
161 [PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
162 [PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_stream },
163 [PA_COMMAND_AUTH] = { command_auth },
164 [PA_COMMAND_REQUEST] = { NULL },
165 [PA_COMMAND_EXIT] = { command_exit },
166 [PA_COMMAND_SET_CLIENT_NAME] = { command_set_client_name },
167 [PA_COMMAND_LOOKUP_SINK] = { command_lookup },
168 [PA_COMMAND_LOOKUP_SOURCE] = { command_lookup },
169 [PA_COMMAND_STAT] = { command_stat },
170 [PA_COMMAND_GET_PLAYBACK_LATENCY] = { command_get_playback_latency },
171 [PA_COMMAND_GET_RECORD_LATENCY] = { command_get_record_latency },
172 [PA_COMMAND_CREATE_UPLOAD_STREAM] = { command_create_upload_stream },
173 [PA_COMMAND_DELETE_UPLOAD_STREAM] = { command_delete_stream },
174 [PA_COMMAND_FINISH_UPLOAD_STREAM] = { command_finish_upload_stream },
175 [PA_COMMAND_PLAY_SAMPLE] = { command_play_sample },
176 [PA_COMMAND_REMOVE_SAMPLE] = { command_remove_sample },
177 [PA_COMMAND_GET_SINK_INFO] = { command_get_info },
178 [PA_COMMAND_GET_SOURCE_INFO] = { command_get_info },
179 [PA_COMMAND_GET_CLIENT_INFO] = { command_get_info },
180 [PA_COMMAND_GET_MODULE_INFO] = { command_get_info },
181 [PA_COMMAND_GET_SINK_INPUT_INFO] = { command_get_info },
182 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = { command_get_info },
183 [PA_COMMAND_GET_SAMPLE_INFO] = { command_get_info },
184 [PA_COMMAND_GET_SINK_INFO_LIST] = { command_get_info_list },
185 [PA_COMMAND_GET_SOURCE_INFO_LIST] = { command_get_info_list },
186 [PA_COMMAND_GET_MODULE_INFO_LIST] = { command_get_info_list },
187 [PA_COMMAND_GET_CLIENT_INFO_LIST] = { command_get_info_list },
188 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = { command_get_info_list },
189 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = { command_get_info_list },
190 [PA_COMMAND_GET_SAMPLE_INFO_LIST] = { command_get_info_list },
191 [PA_COMMAND_GET_SERVER_INFO] = { command_get_server_info },
192 [PA_COMMAND_SUBSCRIBE] = { command_subscribe },
193 [PA_COMMAND_SET_SINK_VOLUME] = { command_set_volume },
194 [PA_COMMAND_SET_SINK_INPUT_VOLUME] = { command_set_volume },
195 [PA_COMMAND_CORK_PLAYBACK_STREAM] = { command_cork_playback_stream },
196 [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = { command_flush_or_trigger_playback_stream },
197 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = { command_flush_or_trigger_playback_stream },
198 [PA_COMMAND_SET_DEFAULT_SINK] = { command_set_default_sink_or_source },
199 [PA_COMMAND_SET_DEFAULT_SOURCE] = { command_set_default_sink_or_source },
200 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME] = { command_set_stream_name },
201 [PA_COMMAND_SET_RECORD_STREAM_NAME] = { command_set_stream_name },
202 [PA_COMMAND_KILL_CLIENT] = { command_kill },
203 [PA_COMMAND_KILL_SINK_INPUT] = { command_kill },
204 [PA_COMMAND_KILL_SOURCE_OUTPUT] = { command_kill },
205 [PA_COMMAND_LOAD_MODULE] = { command_load_module },
206 [PA_COMMAND_UNLOAD_MODULE] = { command_unload_module },
207 [PA_COMMAND_GET_AUTOLOAD_INFO] = { command_get_autoload_info },
208 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST] = { command_get_autoload_info_list },
209 [PA_COMMAND_ADD_AUTOLOAD] = { command_add_autoload },
210 [PA_COMMAND_REMOVE_AUTOLOAD] = { command_remove_autoload },
213 /* structure management */
215 static struct upload_stream* upload_stream_new(struct connection *c, const struct pa_sample_spec *ss, const char *name, size_t length) {
216 struct upload_stream *s;
217 assert(c && ss && name && length);
219 s = pa_xmalloc(sizeof(struct upload_stream));
220 s->type = UPLOAD_STREAM;
222 s->sample_spec = *ss;
223 s->name = pa_xstrdup(name);
225 s->memchunk.memblock = NULL;
226 s->memchunk.index = 0;
227 s->memchunk.length = 0;
231 pa_idxset_put(c->output_streams, s, &s->index);
235 static void upload_stream_free(struct upload_stream *o) {
236 assert(o && o->connection);
238 pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
242 if (o->memchunk.memblock)
243 pa_memblock_unref(o->memchunk.memblock);
248 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) {
249 struct record_stream *s;
250 struct pa_source_output *source_output;
252 assert(c && source && ss && name && maxlength);
254 if (!(source_output = pa_source_output_new(source, name, ss, -1)))
257 s = pa_xmalloc(sizeof(struct record_stream));
259 s->source_output = source_output;
260 s->source_output->push = source_output_push_cb;
261 s->source_output->kill = source_output_kill_cb;
262 s->source_output->get_latency = source_output_get_latency_cb;
263 s->source_output->userdata = s;
264 s->source_output->owner = c->protocol->module;
265 s->source_output->client = c->client;
267 s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0, c->protocol->core->memblock_stat);
268 assert(s->memblockq);
270 s->fragment_size = (fragment_size/base)*base;
271 if (!s->fragment_size)
272 s->fragment_size = base;
274 pa_idxset_put(c->record_streams, s, &s->index);
278 static void record_stream_free(struct record_stream* r) {
279 assert(r && r->connection);
281 pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
282 pa_source_output_disconnect(r->source_output);
283 pa_source_output_unref(r->source_output);
284 pa_memblockq_free(r->memblockq);
288 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name,
293 pa_volume_t volume) {
294 struct playback_stream *s;
295 struct pa_sink_input *sink_input;
296 assert(c && sink && ss && name && maxlength);
298 if (!(sink_input = pa_sink_input_new(sink, name, ss, 0, -1)))
301 s = pa_xmalloc(sizeof(struct playback_stream));
302 s->type = PLAYBACK_STREAM;
304 s->sink_input = sink_input;
306 s->sink_input->peek = sink_input_peek_cb;
307 s->sink_input->drop = sink_input_drop_cb;
308 s->sink_input->kill = sink_input_kill_cb;
309 s->sink_input->get_latency = sink_input_get_latency_cb;
310 s->sink_input->userdata = s;
311 s->sink_input->owner = c->protocol->module;
312 s->sink_input->client = c->client;
314 s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq, c->protocol->core->memblock_stat);
315 assert(s->memblockq);
317 s->requested_bytes = 0;
318 s->drain_request = 0;
320 s->sink_input->volume = volume;
322 pa_idxset_put(c->output_streams, s, &s->index);
326 static void playback_stream_free(struct playback_stream* p) {
327 assert(p && p->connection);
329 if (p->drain_request)
330 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
332 pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
333 pa_sink_input_disconnect(p->sink_input);
334 pa_sink_input_unref(p->sink_input);
335 pa_memblockq_free(p->memblockq);
339 static void connection_free(struct connection *c) {
340 struct record_stream *r;
341 struct output_stream *o;
342 assert(c && c->protocol);
344 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
345 while ((r = pa_idxset_first(c->record_streams, NULL)))
346 record_stream_free(r);
347 pa_idxset_free(c->record_streams, NULL, NULL);
349 while ((o = pa_idxset_first(c->output_streams, NULL)))
350 if (o->type == PLAYBACK_STREAM)
351 playback_stream_free((struct playback_stream*) o);
353 upload_stream_free((struct upload_stream*) o);
354 pa_idxset_free(c->output_streams, NULL, NULL);
356 pa_pdispatch_unref(c->pdispatch);
357 pa_pstream_close(c->pstream);
358 pa_pstream_unref(c->pstream);
359 pa_client_free(c->client);
362 pa_subscription_free(c->subscription);
367 static void request_bytes(struct playback_stream *s) {
368 struct pa_tagstruct *t;
372 if (!(l = pa_memblockq_missing(s->memblockq)))
375 if (l <= s->requested_bytes)
378 l -= s->requested_bytes;
380 if (l < pa_memblockq_get_minreq(s->memblockq))
383 s->requested_bytes += l;
385 t = pa_tagstruct_new(NULL, 0);
387 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
388 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
389 pa_tagstruct_putu32(t, s->index);
390 pa_tagstruct_putu32(t, l);
391 pa_pstream_send_tagstruct(s->connection->pstream, t);
393 /* pa_log(__FILE__": Requesting %u bytes\n", l); */
396 static void send_memblock(struct connection *c) {
398 struct record_stream *r;
400 start = PA_IDXSET_INVALID;
402 struct pa_memchunk chunk;
404 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
407 if (start == PA_IDXSET_INVALID)
408 start = c->rrobin_index;
409 else if (start == c->rrobin_index)
412 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
413 struct pa_memchunk schunk = chunk;
415 if (schunk.length > r->fragment_size)
416 schunk.length = r->fragment_size;
418 pa_pstream_send_memblock(c->pstream, r->index, 0, &schunk);
419 pa_memblockq_drop(r->memblockq, &chunk, schunk.length);
420 pa_memblock_unref(schunk.memblock);
427 static void send_playback_stream_killed(struct playback_stream *p) {
428 struct pa_tagstruct *t;
431 t = pa_tagstruct_new(NULL, 0);
433 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
434 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
435 pa_tagstruct_putu32(t, p->index);
436 pa_pstream_send_tagstruct(p->connection->pstream, t);
439 static void send_record_stream_killed(struct record_stream *r) {
440 struct pa_tagstruct *t;
443 t = pa_tagstruct_new(NULL, 0);
445 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
446 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
447 pa_tagstruct_putu32(t, r->index);
448 pa_pstream_send_tagstruct(r->connection->pstream, t);
451 /*** sinkinput callbacks ***/
453 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
454 struct playback_stream *s;
455 assert(i && i->userdata && chunk);
458 if (pa_memblockq_peek(s->memblockq, chunk) < 0)
464 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
465 struct playback_stream *s;
466 assert(i && i->userdata && length);
469 pa_memblockq_drop(s->memblockq, chunk, length);
472 if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
473 pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
474 s->drain_request = 0;
477 /*pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq));*/
480 static void sink_input_kill_cb(struct pa_sink_input *i) {
481 assert(i && i->userdata);
482 send_playback_stream_killed((struct playback_stream *) i->userdata);
483 playback_stream_free((struct playback_stream *) i->userdata);
486 static pa_usec_t sink_input_get_latency_cb(struct pa_sink_input *i) {
487 struct playback_stream *s;
488 assert(i && i->userdata);
491 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
493 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
496 /*** source_output callbacks ***/
498 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
499 struct record_stream *s;
500 assert(o && o->userdata && chunk);
503 pa_memblockq_push_align(s->memblockq, chunk, 0);
504 if (!pa_pstream_is_pending(s->connection->pstream))
505 send_memblock(s->connection);
508 static void source_output_kill_cb(struct pa_source_output *o) {
509 assert(o && o->userdata);
510 send_record_stream_killed((struct record_stream *) o->userdata);
511 record_stream_free((struct record_stream *) o->userdata);
514 static pa_usec_t source_output_get_latency_cb(struct pa_source_output *o) {
515 struct record_stream *s;
516 assert(o && o->userdata);
519 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
521 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &o->sample_spec);
524 /*** pdispatch callbacks ***/
526 static void protocol_error(struct connection *c) {
527 pa_log(__FILE__": protocol error, kicking client\n");
531 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
532 struct connection *c = userdata;
533 struct playback_stream *s;
534 size_t maxlength, tlength, prebuf, minreq;
536 const char *name, *sink_name;
537 struct pa_sample_spec ss;
538 struct pa_tagstruct *reply;
539 struct pa_sink *sink;
541 assert(c && t && c->protocol && c->protocol->core);
543 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
544 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
545 pa_tagstruct_getu32(t, &sink_index) < 0 ||
546 pa_tagstruct_gets(t, &sink_name) < 0 ||
547 pa_tagstruct_getu32(t, &maxlength) < 0 ||
548 pa_tagstruct_getu32(t, &tlength) < 0 ||
549 pa_tagstruct_getu32(t, &prebuf) < 0 ||
550 pa_tagstruct_getu32(t, &minreq) < 0 ||
551 pa_tagstruct_getu32(t, &volume) < 0 ||
552 !pa_tagstruct_eof(t)) {
558 if (!c->authorized) {
559 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
563 if (sink_index != (uint32_t) -1)
564 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
566 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
569 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
573 if (!(s = playback_stream_new(c, sink, &ss, name, maxlength, tlength, prebuf, minreq, volume))) {
574 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
578 reply = pa_tagstruct_new(NULL, 0);
580 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
581 pa_tagstruct_putu32(reply, tag);
582 pa_tagstruct_putu32(reply, s->index);
583 assert(s->sink_input);
584 pa_tagstruct_putu32(reply, s->sink_input->index);
585 pa_tagstruct_putu32(reply, s->requested_bytes = pa_memblockq_missing(s->memblockq));
586 pa_pstream_send_tagstruct(c->pstream, reply);
590 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
591 struct connection *c = userdata;
595 if (pa_tagstruct_getu32(t, &channel) < 0 ||
596 !pa_tagstruct_eof(t)) {
601 if (!c->authorized) {
602 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
606 if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
607 struct playback_stream *s;
608 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
609 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
613 playback_stream_free(s);
614 } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
615 struct record_stream *s;
616 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
617 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
621 record_stream_free(s);
623 struct upload_stream *s;
624 assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
625 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
626 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
630 upload_stream_free(s);
633 pa_pstream_send_simple_ack(c->pstream, tag);
636 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
637 struct connection *c = userdata;
638 struct record_stream *s;
639 size_t maxlength, fragment_size;
640 uint32_t source_index;
641 const char *name, *source_name;
642 struct pa_sample_spec ss;
643 struct pa_tagstruct *reply;
644 struct pa_source *source;
645 assert(c && t && c->protocol && c->protocol->core);
647 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
648 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
649 pa_tagstruct_getu32(t, &source_index) < 0 ||
650 pa_tagstruct_gets(t, &source_name) < 0 ||
651 pa_tagstruct_getu32(t, &maxlength) < 0 ||
652 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
653 !pa_tagstruct_eof(t)) {
658 if (!c->authorized) {
659 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
663 if (source_index != (uint32_t) -1)
664 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
666 source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1);
669 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
673 if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
674 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
678 reply = pa_tagstruct_new(NULL, 0);
680 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
681 pa_tagstruct_putu32(reply, tag);
682 pa_tagstruct_putu32(reply, s->index);
683 assert(s->source_output);
684 pa_tagstruct_putu32(reply, s->source_output->index);
685 pa_pstream_send_tagstruct(c->pstream, reply);
688 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
689 struct connection *c = userdata;
692 if (!pa_tagstruct_eof(t)) {
697 if (!c->authorized) {
698 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
702 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
703 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
704 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
708 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
709 struct connection *c = userdata;
713 if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
714 !pa_tagstruct_eof(t)) {
719 if (!c->authorized) {
720 if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
721 pa_log(__FILE__": Denied access to client with invalid authorization key.\n");
722 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
729 pa_pstream_send_simple_ack(c->pstream, tag);
733 static void command_set_client_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
734 struct connection *c = userdata;
738 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
739 !pa_tagstruct_eof(t)) {
744 pa_client_set_name(c->client, name);
745 pa_pstream_send_simple_ack(c->pstream, tag);
749 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
750 struct connection *c = userdata;
752 uint32_t index = PA_IDXSET_INVALID;
755 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
756 !pa_tagstruct_eof(t)) {
761 if (!c->authorized) {
762 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
766 if (command == PA_COMMAND_LOOKUP_SINK) {
767 struct pa_sink *sink;
768 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
771 struct pa_source *source;
772 assert(command == PA_COMMAND_LOOKUP_SOURCE);
773 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
774 index = source->index;
777 if (index == PA_IDXSET_INVALID)
778 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
780 struct pa_tagstruct *reply;
781 reply = pa_tagstruct_new(NULL, 0);
783 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
784 pa_tagstruct_putu32(reply, tag);
785 pa_tagstruct_putu32(reply, index);
786 pa_pstream_send_tagstruct(c->pstream, reply);
790 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
791 struct connection *c = userdata;
793 struct playback_stream *s;
796 if (pa_tagstruct_getu32(t, &index) < 0 ||
797 !pa_tagstruct_eof(t)) {
802 if (!c->authorized) {
803 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
807 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
808 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
812 s->drain_request = 0;
814 pa_memblockq_prebuf_disable(s->memblockq);
816 if (!pa_memblockq_is_readable(s->memblockq)) {
817 pa_pstream_send_simple_ack(c->pstream, tag);
819 s->drain_request = 1;
822 pa_sink_notify(s->sink_input->sink);
826 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
827 struct connection *c = userdata;
828 struct pa_tagstruct *reply;
831 if (!pa_tagstruct_eof(t)) {
836 if (!c->authorized) {
837 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
841 reply = pa_tagstruct_new(NULL, 0);
843 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
844 pa_tagstruct_putu32(reply, tag);
845 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total);
846 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total_size);
847 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated);
848 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated_size);
849 pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
850 pa_pstream_send_tagstruct(c->pstream, reply);
853 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
854 struct connection *c = userdata;
855 struct pa_tagstruct *reply;
856 struct playback_stream *s;
857 struct timeval tv, now;
861 if (pa_tagstruct_getu32(t, &index) < 0 ||
862 pa_tagstruct_get_timeval(t, &tv) < 0 ||
863 !pa_tagstruct_eof(t)) {
868 if (!c->authorized) {
869 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
873 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
874 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
878 reply = pa_tagstruct_new(NULL, 0);
880 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
881 pa_tagstruct_putu32(reply, tag);
882 pa_tagstruct_put_usec(reply, pa_sink_input_get_latency(s->sink_input));
883 pa_tagstruct_put_usec(reply, pa_sink_get_latency(s->sink_input->sink));
884 pa_tagstruct_put_usec(reply, 0);
885 pa_tagstruct_put_boolean(reply, pa_memblockq_is_readable(s->memblockq));
886 pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
887 pa_tagstruct_put_timeval(reply, &tv);
888 gettimeofday(&now, NULL);
889 pa_tagstruct_put_timeval(reply, &now);
890 pa_pstream_send_tagstruct(c->pstream, reply);
893 static void command_get_record_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
894 struct connection *c = userdata;
895 struct pa_tagstruct *reply;
896 struct record_stream *s;
897 struct timeval tv, now;
901 if (pa_tagstruct_getu32(t, &index) < 0 ||
902 pa_tagstruct_get_timeval(t, &tv) < 0 ||
903 !pa_tagstruct_eof(t)) {
908 if (!c->authorized) {
909 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
913 if (!(s = pa_idxset_get_by_index(c->record_streams, index))) {
914 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
918 reply = pa_tagstruct_new(NULL, 0);
920 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
921 pa_tagstruct_putu32(reply, tag);
922 pa_tagstruct_put_usec(reply, pa_source_output_get_latency(s->source_output));
923 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
924 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
925 pa_tagstruct_put_boolean(reply, 0);
926 pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
927 pa_tagstruct_put_timeval(reply, &tv);
928 gettimeofday(&now, NULL);
929 pa_tagstruct_put_timeval(reply, &now);
930 pa_pstream_send_tagstruct(c->pstream, reply);
934 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
935 struct connection *c = userdata;
936 struct upload_stream *s;
939 struct pa_sample_spec ss;
940 struct pa_tagstruct *reply;
941 assert(c && t && c->protocol && c->protocol->core);
943 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
944 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
945 pa_tagstruct_getu32(t, &length) < 0 ||
946 !pa_tagstruct_eof(t)) {
951 if (!c->authorized) {
952 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
956 if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
957 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
961 if (!(s = upload_stream_new(c, &ss, name, length))) {
962 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
966 reply = pa_tagstruct_new(NULL, 0);
968 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
969 pa_tagstruct_putu32(reply, tag);
970 pa_tagstruct_putu32(reply, s->index);
971 pa_tagstruct_putu32(reply, length);
972 pa_pstream_send_tagstruct(c->pstream, reply);
975 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
976 struct connection *c = userdata;
978 struct upload_stream *s;
982 if (pa_tagstruct_getu32(t, &channel) < 0 ||
983 !pa_tagstruct_eof(t)) {
988 if (!c->authorized) {
989 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
993 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
994 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
998 pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
999 pa_pstream_send_simple_ack(c->pstream, tag);
1000 upload_stream_free(s);
1003 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1004 struct connection *c = userdata;
1005 uint32_t sink_index, volume;
1006 struct pa_sink *sink;
1007 const char *name, *sink_name;
1010 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
1011 pa_tagstruct_gets(t, &sink_name) < 0 ||
1012 pa_tagstruct_getu32(t, &volume) < 0 ||
1013 pa_tagstruct_gets(t, &name) < 0 || !name ||
1014 !pa_tagstruct_eof(t)) {
1019 if (!c->authorized) {
1020 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1024 if (sink_index != (uint32_t) -1)
1025 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1027 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1030 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1034 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
1035 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1039 pa_pstream_send_simple_ack(c->pstream, tag);
1042 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1043 struct connection *c = userdata;
1047 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1048 !pa_tagstruct_eof(t)) {
1053 if (!c->authorized) {
1054 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1058 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
1059 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1063 pa_pstream_send_simple_ack(c->pstream, tag);
1066 static void sink_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink *sink) {
1068 pa_tagstruct_putu32(t, sink->index);
1069 pa_tagstruct_puts(t, sink->name);
1070 pa_tagstruct_puts(t, sink->description);
1071 pa_tagstruct_put_sample_spec(t, &sink->sample_spec);
1072 pa_tagstruct_putu32(t, sink->owner ? sink->owner->index : (uint32_t) -1);
1073 pa_tagstruct_putu32(t, sink->volume);
1074 pa_tagstruct_putu32(t, sink->monitor_source->index);
1075 pa_tagstruct_puts(t, sink->monitor_source->name);
1076 pa_tagstruct_put_usec(t, pa_sink_get_latency(sink));
1079 static void source_fill_tagstruct(struct pa_tagstruct *t, struct pa_source *source) {
1080 assert(t && source);
1081 pa_tagstruct_putu32(t, source->index);
1082 pa_tagstruct_puts(t, source->name);
1083 pa_tagstruct_puts(t, source->description);
1084 pa_tagstruct_put_sample_spec(t, &source->sample_spec);
1085 pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
1086 pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
1087 pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : NULL);
1088 pa_tagstruct_put_usec(t, pa_source_get_latency(source));
1091 static void client_fill_tagstruct(struct pa_tagstruct *t, struct pa_client *client) {
1092 assert(t && client);
1093 pa_tagstruct_putu32(t, client->index);
1094 pa_tagstruct_puts(t, client->name);
1095 pa_tagstruct_puts(t, client->protocol_name);
1096 pa_tagstruct_putu32(t, client->owner ? client->owner->index : (uint32_t) -1);
1099 static void module_fill_tagstruct(struct pa_tagstruct *t, struct pa_module *module) {
1100 assert(t && module);
1101 pa_tagstruct_putu32(t, module->index);
1102 pa_tagstruct_puts(t, module->name);
1103 pa_tagstruct_puts(t, module->argument);
1104 pa_tagstruct_putu32(t, module->n_used);
1105 pa_tagstruct_put_boolean(t, module->auto_unload);
1108 static void sink_input_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink_input *s) {
1110 pa_tagstruct_putu32(t, s->index);
1111 pa_tagstruct_puts(t, s->name);
1112 pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1113 pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1114 pa_tagstruct_putu32(t, s->sink->index);
1115 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1116 pa_tagstruct_putu32(t, s->volume);
1117 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
1118 pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
1121 static void source_output_fill_tagstruct(struct pa_tagstruct *t, struct pa_source_output *s) {
1123 pa_tagstruct_putu32(t, s->index);
1124 pa_tagstruct_puts(t, s->name);
1125 pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1126 pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1127 pa_tagstruct_putu32(t, s->source->index);
1128 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1129 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
1130 pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
1133 static void scache_fill_tagstruct(struct pa_tagstruct *t, struct pa_scache_entry *e) {
1135 pa_tagstruct_putu32(t, e->index);
1136 pa_tagstruct_puts(t, e->name);
1137 pa_tagstruct_putu32(t, e->volume);
1138 pa_tagstruct_put_usec(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1139 pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1140 pa_tagstruct_putu32(t, e->memchunk.length);
1141 pa_tagstruct_put_boolean(t, e->lazy);
1142 pa_tagstruct_puts(t, e->filename);
1145 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1146 struct connection *c = userdata;
1148 struct pa_sink *sink = NULL;
1149 struct pa_source *source = NULL;
1150 struct pa_client *client = NULL;
1151 struct pa_module *module = NULL;
1152 struct pa_sink_input *si = NULL;
1153 struct pa_source_output *so = NULL;
1154 struct pa_scache_entry *sce = NULL;
1156 struct pa_tagstruct *reply;
1160 if (pa_tagstruct_getu32(t, &index) < 0 ||
1161 (command != PA_COMMAND_GET_CLIENT_INFO &&
1162 command != PA_COMMAND_GET_MODULE_INFO &&
1163 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1164 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1165 pa_tagstruct_gets(t, &name) < 0) ||
1166 !pa_tagstruct_eof(t)) {
1171 if (!c->authorized) {
1172 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1176 if (command == PA_COMMAND_GET_SINK_INFO) {
1177 if (index != (uint32_t) -1)
1178 sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1180 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1181 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1182 if (index != (uint32_t) -1)
1183 source = pa_idxset_get_by_index(c->protocol->core->sources, index);
1185 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1186 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1187 client = pa_idxset_get_by_index(c->protocol->core->clients, index);
1188 else if (command == PA_COMMAND_GET_MODULE_INFO)
1189 module = pa_idxset_get_by_index(c->protocol->core->modules, index);
1190 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1191 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1192 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1193 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, index);
1195 assert(command == PA_COMMAND_GET_SAMPLE_INFO);
1196 if (index != (uint32_t) -1)
1197 sce = pa_idxset_get_by_index(c->protocol->core->scache, index);
1199 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1202 if (!sink && !source && !client && !module && !si && !so && !sce) {
1203 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1207 reply = pa_tagstruct_new(NULL, 0);
1209 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1210 pa_tagstruct_putu32(reply, tag);
1212 sink_fill_tagstruct(reply, sink);
1214 source_fill_tagstruct(reply, source);
1216 client_fill_tagstruct(reply, client);
1218 module_fill_tagstruct(reply, module);
1220 sink_input_fill_tagstruct(reply, si);
1222 source_output_fill_tagstruct(reply, so);
1224 scache_fill_tagstruct(reply, sce);
1225 pa_pstream_send_tagstruct(c->pstream, reply);
1228 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1229 struct connection *c = userdata;
1230 struct pa_idxset *i;
1233 struct pa_tagstruct *reply;
1236 if (!pa_tagstruct_eof(t)) {
1241 if (!c->authorized) {
1242 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1246 reply = pa_tagstruct_new(NULL, 0);
1248 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1249 pa_tagstruct_putu32(reply, tag);
1251 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1252 i = c->protocol->core->sinks;
1253 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1254 i = c->protocol->core->sources;
1255 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1256 i = c->protocol->core->clients;
1257 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1258 i = c->protocol->core->modules;
1259 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1260 i = c->protocol->core->sink_inputs;
1261 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1262 i = c->protocol->core->source_outputs;
1264 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1265 i = c->protocol->core->scache;
1269 for (p = pa_idxset_first(i, &index); p; p = pa_idxset_next(i, &index)) {
1270 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1271 sink_fill_tagstruct(reply, p);
1272 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1273 source_fill_tagstruct(reply, p);
1274 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1275 client_fill_tagstruct(reply, p);
1276 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1277 module_fill_tagstruct(reply, p);
1278 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1279 sink_input_fill_tagstruct(reply, p);
1280 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1281 source_output_fill_tagstruct(reply, p);
1283 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1284 scache_fill_tagstruct(reply, p);
1289 pa_pstream_send_tagstruct(c->pstream, reply);
1292 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1293 struct connection *c = userdata;
1294 struct pa_tagstruct *reply;
1299 if (!pa_tagstruct_eof(t)) {
1304 if (!c->authorized) {
1305 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1309 reply = pa_tagstruct_new(NULL, 0);
1311 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1312 pa_tagstruct_putu32(reply, tag);
1313 pa_tagstruct_puts(reply, PACKAGE_NAME);
1314 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1315 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1316 pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
1317 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1319 n = pa_namereg_get_default_sink_name(c->protocol->core);
1320 pa_tagstruct_puts(reply, n);
1321 n = pa_namereg_get_default_source_name(c->protocol->core);
1322 pa_tagstruct_puts(reply, n);
1323 pa_pstream_send_tagstruct(c->pstream, reply);
1326 static void subscription_cb(struct pa_core *core, enum pa_subscription_event_type e, uint32_t index, void *userdata) {
1327 struct pa_tagstruct *t;
1328 struct connection *c = userdata;
1331 t = pa_tagstruct_new(NULL, 0);
1333 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1334 pa_tagstruct_putu32(t, (uint32_t) -1);
1335 pa_tagstruct_putu32(t, e);
1336 pa_tagstruct_putu32(t, index);
1337 pa_pstream_send_tagstruct(c->pstream, t);
1340 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1341 struct connection *c = userdata;
1342 enum pa_subscription_mask m;
1345 if (pa_tagstruct_getu32(t, &m) < 0 ||
1346 !pa_tagstruct_eof(t)) {
1351 if (!c->authorized) {
1352 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1356 if (c->subscription)
1357 pa_subscription_free(c->subscription);
1360 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1361 assert(c->subscription);
1363 c->subscription = NULL;
1365 pa_pstream_send_simple_ack(c->pstream, tag);
1368 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1369 struct connection *c = userdata;
1370 uint32_t index, volume;
1371 struct pa_sink *sink = NULL;
1372 struct pa_sink_input *si = NULL;
1373 const char *name = NULL;
1376 if (pa_tagstruct_getu32(t, &index) < 0 ||
1377 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1378 pa_tagstruct_getu32(t, &volume) ||
1379 !pa_tagstruct_eof(t)) {
1384 if (!c->authorized) {
1385 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1389 if (command == PA_COMMAND_SET_SINK_VOLUME) {
1390 if (index != (uint32_t) -1)
1391 sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1393 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1395 assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1396 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1400 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1405 pa_sink_set_volume(sink, volume);
1407 pa_sink_input_set_volume(si, volume);
1409 pa_pstream_send_simple_ack(c->pstream, tag);
1412 static void command_cork_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1413 struct connection *c = userdata;
1416 struct playback_stream *s;
1419 if (pa_tagstruct_getu32(t, &index) < 0 ||
1420 pa_tagstruct_getu32(t, &b) < 0 ||
1421 !pa_tagstruct_eof(t)) {
1426 if (!c->authorized) {
1427 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1431 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1432 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1436 pa_sink_input_cork(s->sink_input, b);
1437 pa_pstream_send_simple_ack(c->pstream, tag);
1440 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1441 struct connection *c = userdata;
1443 struct playback_stream *s;
1446 if (pa_tagstruct_getu32(t, &index) < 0 ||
1447 !pa_tagstruct_eof(t)) {
1452 if (!c->authorized) {
1453 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1457 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1458 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1462 if (command == PA_COMMAND_TRIGGER_PLAYBACK_STREAM)
1463 pa_memblockq_prebuf_disable(s->memblockq);
1465 assert(command == PA_COMMAND_FLUSH_PLAYBACK_STREAM);
1466 pa_memblockq_flush(s->memblockq);
1467 /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1470 pa_sink_notify(s->sink_input->sink);
1471 pa_pstream_send_simple_ack(c->pstream, tag);
1475 static void command_set_default_sink_or_source(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1476 struct connection *c = userdata;
1481 if (pa_tagstruct_getu32(t, &index) < 0 ||
1482 pa_tagstruct_gets(t, &s) < 0 || !s ||
1483 !pa_tagstruct_eof(t)) {
1488 if (!c->authorized) {
1489 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1493 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
1494 pa_pstream_send_simple_ack(c->pstream, tag);
1497 static void command_set_stream_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1498 struct connection *c = userdata;
1503 if (pa_tagstruct_getu32(t, &index) < 0 ||
1504 pa_tagstruct_gets(t, &name) < 0 || !name ||
1505 !pa_tagstruct_eof(t)) {
1510 if (!c->authorized) {
1511 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1515 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
1516 struct playback_stream *s;
1518 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1519 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1523 pa_sink_input_set_name(s->sink_input, name);
1526 struct record_stream *s;
1528 if (!(s = pa_idxset_get_by_index(c->record_streams, index))) {
1529 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1533 pa_source_output_set_name(s->source_output, name);
1536 pa_pstream_send_simple_ack(c->pstream, tag);
1539 static void command_kill(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1540 struct connection *c = userdata;
1544 if (pa_tagstruct_getu32(t, &index) < 0 ||
1545 !pa_tagstruct_eof(t)) {
1550 if (!c->authorized) {
1551 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1555 if (command == PA_COMMAND_KILL_CLIENT) {
1556 struct pa_client *client;
1558 if (!(client = pa_idxset_get_by_index(c->protocol->core->clients, index))) {
1559 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1563 pa_client_kill(client);
1564 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
1565 struct pa_sink_input *s;
1567 if (!(s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index))) {
1568 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1572 pa_sink_input_kill(s);
1574 struct pa_source_output *s;
1576 assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
1578 if (!(s = pa_idxset_get_by_index(c->protocol->core->source_outputs, index))) {
1579 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1583 pa_source_output_kill(s);
1586 pa_pstream_send_simple_ack(c->pstream, tag);
1589 static void command_load_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1590 struct connection *c = userdata;
1591 struct pa_module *m;
1592 const char *name, *argument;
1593 struct pa_tagstruct *reply;
1596 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1597 pa_tagstruct_gets(t, &argument) < 0 ||
1598 !pa_tagstruct_eof(t)) {
1603 if (!c->authorized) {
1604 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1608 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
1609 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INITFAILED);
1613 reply = pa_tagstruct_new(NULL, 0);
1614 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1615 pa_tagstruct_putu32(reply, tag);
1616 pa_tagstruct_putu32(reply, m->index);
1617 pa_pstream_send_tagstruct(c->pstream, reply);
1620 static void command_unload_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1621 struct connection *c = userdata;
1623 struct pa_module *m;
1626 if (pa_tagstruct_getu32(t, &index) < 0 ||
1627 !pa_tagstruct_eof(t)) {
1632 if (!c->authorized) {
1633 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1637 if (!(m = pa_idxset_get_by_index(c->protocol->core->modules, index))) {
1638 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1642 pa_module_unload_request(m);
1643 pa_pstream_send_simple_ack(c->pstream, tag);
1646 static void command_add_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1647 struct connection *c = userdata;
1648 const char *name, *module, *argument;
1652 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1653 pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1654 pa_tagstruct_gets(t, &module) < 0 || !module ||
1655 pa_tagstruct_gets(t, &argument) < 0 ||
1656 !pa_tagstruct_eof(t)) {
1661 if (!c->authorized) {
1662 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1666 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument) < 0) {
1667 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
1671 pa_pstream_send_simple_ack(c->pstream, tag);
1674 static void command_remove_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1675 struct connection *c = userdata;
1680 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1681 pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1682 !pa_tagstruct_eof(t)) {
1687 if (!c->authorized) {
1688 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1692 if (pa_autoload_remove(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
1693 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1697 pa_pstream_send_simple_ack(c->pstream, tag);
1700 static void autoload_fill_tagstruct(struct pa_tagstruct *t, struct pa_autoload_entry *e) {
1702 pa_tagstruct_puts(t, e->name);
1703 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
1704 pa_tagstruct_puts(t, e->module);
1705 pa_tagstruct_puts(t, e->argument);
1708 static void command_get_autoload_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1709 struct connection *c = userdata;
1710 struct pa_autoload_entry *a = NULL;
1713 struct pa_tagstruct *reply;
1716 if (pa_tagstruct_gets(t, &name) < 0 || name ||
1717 pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1718 !pa_tagstruct_eof(t)) {
1723 if (!c->authorized) {
1724 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1728 if (!c->protocol->core->autoload_hashmap || !(a = pa_hashmap_get(c->protocol->core->autoload_hashmap, name)) || (a->type == PA_NAMEREG_SINK && type != 0) || (a->type == PA_NAMEREG_SOURCE && type != 1)) {
1729 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1733 reply = pa_tagstruct_new(NULL, 0);
1735 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1736 pa_tagstruct_putu32(reply, tag);
1737 autoload_fill_tagstruct(reply, a);
1738 pa_pstream_send_tagstruct(c->pstream, reply);
1741 static void command_get_autoload_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1742 struct connection *c = userdata;
1743 struct pa_tagstruct *reply;
1746 if (!pa_tagstruct_eof(t)) {
1751 if (!c->authorized) {
1752 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1756 reply = pa_tagstruct_new(NULL, 0);
1757 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1758 pa_tagstruct_putu32(reply, tag);
1760 if (c->protocol->core->autoload_hashmap) {
1761 struct pa_autoload_entry *a;
1764 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state)))
1765 autoload_fill_tagstruct(reply, a);
1768 pa_pstream_send_tagstruct(c->pstream, reply);
1771 /*** pstream callbacks ***/
1773 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
1774 struct connection *c = userdata;
1775 assert(p && packet && packet->data && c);
1777 if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1778 pa_log(__FILE__": invalid packet.\n");
1783 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata) {
1784 struct connection *c = userdata;
1785 struct output_stream *stream;
1786 assert(p && chunk && userdata);
1788 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1789 pa_log(__FILE__": client sent block for invalid stream.\n");
1794 if (stream->type == PLAYBACK_STREAM) {
1795 struct playback_stream *p = (struct playback_stream*) stream;
1796 if (chunk->length >= p->requested_bytes)
1797 p->requested_bytes = 0;
1799 p->requested_bytes -= chunk->length;
1801 pa_memblockq_push_align(p->memblockq, chunk, delta);
1802 assert(p->sink_input);
1803 /*pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq));*/
1805 pa_sink_notify(p->sink_input->sink);
1806 /* pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1809 struct upload_stream *u = (struct upload_stream*) stream;
1811 assert(u->type == UPLOAD_STREAM);
1813 if (!u->memchunk.memblock) {
1814 if (u->length == chunk->length) {
1815 u->memchunk = *chunk;
1816 pa_memblock_ref(u->memchunk.memblock);
1819 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
1820 u->memchunk.index = u->memchunk.length = 0;
1824 assert(u->memchunk.memblock);
1827 if (l > chunk->length)
1831 memcpy((uint8_t*) u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length,
1832 (uint8_t*) chunk->memblock->data+chunk->index, l);
1833 u->memchunk.length += l;
1839 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
1840 struct connection *c = userdata;
1844 /* pa_log(__FILE__": connection died.\n");*/
1848 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
1849 struct connection *c = userdata;
1855 /*** client callbacks ***/
1857 static void client_kill_cb(struct pa_client *c) {
1858 assert(c && c->userdata);
1859 connection_free(c->userdata);
1862 /*** socket server callbacks ***/
1864 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
1865 struct pa_protocol_native *p = userdata;
1866 struct connection *c;
1869 c = pa_xmalloc(sizeof(struct connection));
1871 c->authorized =!! p->public;
1874 c->client = pa_client_new(p->core, "NATIVE", "Client");
1876 c->client->kill = client_kill_cb;
1877 c->client->userdata = c;
1878 c->client->owner = p->module;
1880 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
1883 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
1884 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
1885 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
1886 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
1888 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
1889 assert(c->pdispatch);
1891 c->record_streams = pa_idxset_new(NULL, NULL);
1892 c->output_streams = pa_idxset_new(NULL, NULL);
1893 assert(c->record_streams && c->output_streams);
1895 c->rrobin_index = PA_IDXSET_INVALID;
1896 c->subscription = NULL;
1898 pa_idxset_put(p->connections, c, NULL);
1901 /*** module entry points ***/
1903 static struct pa_protocol_native* protocol_new_internal(struct pa_core *c, struct pa_module *m, struct pa_modargs *ma) {
1904 struct pa_protocol_native *p;
1908 if (pa_modargs_get_value_boolean(ma, "public", &public) < 0) {
1909 pa_log(__FILE__": public= expects a boolean argument.\n");
1913 p = pa_xmalloc(sizeof(struct pa_protocol_native));
1915 if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
1924 p->connections = pa_idxset_new(NULL, NULL);
1925 assert(p->connections);
1930 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) {
1931 struct pa_protocol_native *p;
1933 if (!(p = protocol_new_internal(core, m, ma)))
1937 pa_socket_server_set_callback(p->server, on_connection, p);
1942 void pa_protocol_native_free(struct pa_protocol_native *p) {
1943 struct connection *c;
1946 while ((c = pa_idxset_first(p->connections, NULL)))
1948 pa_idxset_free(p->connections, NULL, NULL);
1951 pa_socket_server_unref(p->server);
1956 struct pa_protocol_native* pa_protocol_native_new_iochannel(struct pa_core*core, struct pa_iochannel *io, struct pa_module *m, struct pa_modargs *ma) {
1957 struct pa_protocol_native *p;
1959 if (!(p = protocol_new_internal(core, m, ma)))
1962 on_connection(NULL, io, p);