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 Lesser 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 Lesser 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
32 #include <polyp/version.h>
33 #include <polyp/utf8.h>
34 #include <polyp/xmalloc.h>
36 #include <polypcore/native-common.h>
37 #include <polypcore/packet.h>
38 #include <polypcore/client.h>
39 #include <polypcore/source-output.h>
40 #include <polypcore/sink-input.h>
41 #include <polypcore/pstream.h>
42 #include <polypcore/tagstruct.h>
43 #include <polypcore/pdispatch.h>
44 #include <polypcore/pstream-util.h>
45 #include <polypcore/authkey.h>
46 #include <polypcore/namereg.h>
47 #include <polypcore/core-scache.h>
48 #include <polypcore/util.h>
49 #include <polypcore/core-subscribe.h>
50 #include <polypcore/log.h>
51 #include <polypcore/autoload.h>
52 #include <polypcore/authkey-prop.h>
53 #include <polypcore/strlist.h>
54 #include <polypcore/props.h>
55 #include <polypcore/sample-util.h>
56 #include <polypcore/llist.h>
58 #include "protocol-native.h"
60 /* Kick a client if it doesn't authenticate within this time */
61 #define AUTH_TIMEOUT 60
63 /* Don't accept more connection than this */
64 #define MAX_CONNECTIONS 10
66 #define MAX_MEMBLOCKQ_LENGTH (4*1024*1024) /* 4MB */
69 struct pa_protocol_native;
71 struct record_stream {
72 struct connection *connection;
74 pa_source_output *source_output;
75 pa_memblockq *memblockq;
79 struct playback_stream {
81 struct connection *connection;
83 pa_sink_input *sink_input;
84 pa_memblockq *memblockq;
85 size_t requested_bytes;
91 /* Sync group members */
92 PA_LLIST_FIELDS(struct playback_stream);
95 struct upload_stream {
97 struct connection *connection;
102 pa_sample_spec sample_spec;
103 pa_channel_map channel_map;
106 struct output_stream {
118 pa_protocol_native *protocol;
121 pa_pdispatch *pdispatch;
122 pa_idxset *record_streams, *output_streams;
123 uint32_t rrobin_index;
124 pa_subscription *subscription;
125 pa_time_event *auth_timeout_event;
128 struct pa_protocol_native {
132 pa_socket_server *server;
133 pa_idxset *connections;
134 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
135 int auth_cookie_in_property;
136 #ifdef SCM_CREDENTIALS
141 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk);
142 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length);
143 static void sink_input_kill_cb(pa_sink_input *i);
144 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i);
146 static void request_bytes(struct playback_stream*s);
148 static void source_output_kill_cb(pa_source_output *o);
149 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
150 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
152 static void command_exit(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
153 static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
154 static void command_drain_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
155 static void command_create_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
156 static void command_delete_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
157 static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
158 static void command_set_client_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
159 static void command_lookup(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
160 static void command_stat(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
161 static void command_get_playback_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
162 static void command_get_record_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
163 static void command_create_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
164 static void command_finish_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
165 static void command_play_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
166 static void command_remove_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
167 static void command_get_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
168 static void command_get_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
169 static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
170 static void command_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
171 static void command_set_volume(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
172 static void command_set_mute(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
173 static void command_cork_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
174 static void command_flush_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
175 static void command_trigger_or_prebuf_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
176 static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
177 static void command_set_stream_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
178 static void command_kill(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
179 static void command_load_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
180 static void command_unload_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
181 static void command_add_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
182 static void command_remove_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
183 static void command_get_autoload_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
184 static void command_get_autoload_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
185 static void command_cork_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
186 static void command_flush_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
188 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
189 [PA_COMMAND_ERROR] = NULL,
190 [PA_COMMAND_TIMEOUT] = NULL,
191 [PA_COMMAND_REPLY] = NULL,
192 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = command_create_playback_stream,
193 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = command_delete_stream,
194 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = command_drain_playback_stream,
195 [PA_COMMAND_CREATE_RECORD_STREAM] = command_create_record_stream,
196 [PA_COMMAND_DELETE_RECORD_STREAM] = command_delete_stream,
197 [PA_COMMAND_AUTH] = command_auth,
198 [PA_COMMAND_REQUEST] = NULL,
199 [PA_COMMAND_EXIT] = command_exit,
200 [PA_COMMAND_SET_CLIENT_NAME] = command_set_client_name,
201 [PA_COMMAND_LOOKUP_SINK] = command_lookup,
202 [PA_COMMAND_LOOKUP_SOURCE] = command_lookup,
203 [PA_COMMAND_STAT] = command_stat,
204 [PA_COMMAND_GET_PLAYBACK_LATENCY] = command_get_playback_latency,
205 [PA_COMMAND_GET_RECORD_LATENCY] = command_get_record_latency,
206 [PA_COMMAND_CREATE_UPLOAD_STREAM] = command_create_upload_stream,
207 [PA_COMMAND_DELETE_UPLOAD_STREAM] = command_delete_stream,
208 [PA_COMMAND_FINISH_UPLOAD_STREAM] = command_finish_upload_stream,
209 [PA_COMMAND_PLAY_SAMPLE] = command_play_sample,
210 [PA_COMMAND_REMOVE_SAMPLE] = command_remove_sample,
211 [PA_COMMAND_GET_SINK_INFO] = command_get_info,
212 [PA_COMMAND_GET_SOURCE_INFO] = command_get_info,
213 [PA_COMMAND_GET_CLIENT_INFO] = command_get_info,
214 [PA_COMMAND_GET_MODULE_INFO] = command_get_info,
215 [PA_COMMAND_GET_SINK_INPUT_INFO] = command_get_info,
216 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = command_get_info,
217 [PA_COMMAND_GET_SAMPLE_INFO] = command_get_info,
218 [PA_COMMAND_GET_SINK_INFO_LIST] = command_get_info_list,
219 [PA_COMMAND_GET_SOURCE_INFO_LIST] = command_get_info_list,
220 [PA_COMMAND_GET_MODULE_INFO_LIST] = command_get_info_list,
221 [PA_COMMAND_GET_CLIENT_INFO_LIST] = command_get_info_list,
222 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = command_get_info_list,
223 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = command_get_info_list,
224 [PA_COMMAND_GET_SAMPLE_INFO_LIST] = command_get_info_list,
225 [PA_COMMAND_GET_SERVER_INFO] = command_get_server_info,
226 [PA_COMMAND_SUBSCRIBE] = command_subscribe,
228 [PA_COMMAND_SET_SINK_VOLUME] = command_set_volume,
229 [PA_COMMAND_SET_SINK_INPUT_VOLUME] = command_set_volume,
230 [PA_COMMAND_SET_SOURCE_VOLUME] = command_set_volume,
232 [PA_COMMAND_SET_SINK_MUTE] = command_set_mute,
233 [PA_COMMAND_SET_SOURCE_MUTE] = command_set_mute,
235 [PA_COMMAND_CORK_PLAYBACK_STREAM] = command_cork_playback_stream,
236 [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = command_flush_playback_stream,
237 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = command_trigger_or_prebuf_playback_stream,
238 [PA_COMMAND_PREBUF_PLAYBACK_STREAM] = command_trigger_or_prebuf_playback_stream,
240 [PA_COMMAND_CORK_RECORD_STREAM] = command_cork_record_stream,
241 [PA_COMMAND_FLUSH_RECORD_STREAM] = command_flush_record_stream,
243 [PA_COMMAND_SET_DEFAULT_SINK] = command_set_default_sink_or_source,
244 [PA_COMMAND_SET_DEFAULT_SOURCE] = command_set_default_sink_or_source,
245 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME] = command_set_stream_name,
246 [PA_COMMAND_SET_RECORD_STREAM_NAME] = command_set_stream_name,
247 [PA_COMMAND_KILL_CLIENT] = command_kill,
248 [PA_COMMAND_KILL_SINK_INPUT] = command_kill,
249 [PA_COMMAND_KILL_SOURCE_OUTPUT] = command_kill,
250 [PA_COMMAND_LOAD_MODULE] = command_load_module,
251 [PA_COMMAND_UNLOAD_MODULE] = command_unload_module,
252 [PA_COMMAND_GET_AUTOLOAD_INFO] = command_get_autoload_info,
253 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST] = command_get_autoload_info_list,
254 [PA_COMMAND_ADD_AUTOLOAD] = command_add_autoload,
255 [PA_COMMAND_REMOVE_AUTOLOAD] = command_remove_autoload
258 /* structure management */
260 static struct upload_stream* upload_stream_new(
261 struct connection *c,
262 const pa_sample_spec *ss,
263 const pa_channel_map *map,
264 const char *name, size_t length) {
266 struct upload_stream *s;
267 assert(c && ss && name && length);
269 s = pa_xnew(struct upload_stream, 1);
270 s->type = UPLOAD_STREAM;
272 s->sample_spec = *ss;
273 s->channel_map = *map;
274 s->name = pa_xstrdup(name);
276 s->memchunk.memblock = NULL;
277 s->memchunk.index = 0;
278 s->memchunk.length = 0;
282 pa_idxset_put(c->output_streams, s, &s->index);
286 static void upload_stream_free(struct upload_stream *o) {
287 assert(o && o->connection);
289 pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
293 if (o->memchunk.memblock)
294 pa_memblock_unref(o->memchunk.memblock);
299 static struct record_stream* record_stream_new(
300 struct connection *c,
302 const pa_sample_spec *ss,
303 const pa_channel_map *map,
306 size_t fragment_size) {
308 struct record_stream *s;
309 pa_source_output *source_output;
311 assert(c && source && ss && name && maxlength);
313 if (!(source_output = pa_source_output_new(source, __FILE__, name, ss, map, -1)))
316 s = pa_xnew(struct record_stream, 1);
318 s->source_output = source_output;
319 s->source_output->push = source_output_push_cb;
320 s->source_output->kill = source_output_kill_cb;
321 s->source_output->get_latency = source_output_get_latency_cb;
322 s->source_output->userdata = s;
323 s->source_output->owner = c->protocol->module;
324 s->source_output->client = c->client;
326 s->memblockq = pa_memblockq_new(
330 base = pa_frame_size(ss),
334 c->protocol->core->memblock_stat);
335 assert(s->memblockq);
337 s->fragment_size = (fragment_size/base)*base;
338 if (!s->fragment_size)
339 s->fragment_size = base;
341 pa_idxset_put(c->record_streams, s, &s->index);
345 static void record_stream_free(struct record_stream* r) {
346 assert(r && r->connection);
348 pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
349 pa_source_output_disconnect(r->source_output);
350 pa_source_output_unref(r->source_output);
351 pa_memblockq_free(r->memblockq);
355 static struct playback_stream* playback_stream_new(
356 struct connection *c,
358 const pa_sample_spec *ss,
359 const pa_channel_map *map,
368 struct playback_stream *s, *ssync;
369 pa_sink_input *sink_input;
370 pa_memblock *silence;
374 assert(c && sink && ss && name && maxlength);
376 /* Find syncid group */
377 for (ssync = pa_idxset_first(c->output_streams, &idx); ssync; ssync = pa_idxset_next(c->output_streams, &idx)) {
379 if (ssync->type != PLAYBACK_STREAM)
382 if (ssync->syncid == syncid)
386 /* Synced streams must connect to the same sink */
387 if (ssync && ssync->sink_input->sink != sink)
390 if (!(sink_input = pa_sink_input_new(sink, __FILE__, name, ss, map, volume, 0, -1)))
393 s = pa_xnew(struct playback_stream, 1);
394 s->type = PLAYBACK_STREAM;
397 s->sink_input = sink_input;
400 s->sink_input->peek = sink_input_peek_cb;
401 s->sink_input->drop = sink_input_drop_cb;
402 s->sink_input->kill = sink_input_kill_cb;
403 s->sink_input->get_latency = sink_input_get_latency_cb;
404 s->sink_input->userdata = s;
405 s->sink_input->owner = c->protocol->module;
406 s->sink_input->client = c->client;
409 /* Sync id found, now find head of list */
410 PA_LLIST_FIND_HEAD(struct playback_stream, ssync, &ssync);
412 /* Prepend ourselves */
413 PA_LLIST_PREPEND(struct playback_stream, ssync, s);
415 /* Set our start index to the current read index of the other grozp member(s) */
417 start_index = pa_memblockq_get_read_index(ssync->next->memblockq);
419 /* This ia a new sync group */
420 PA_LLIST_INIT(struct playback_stream, s);
424 silence = pa_silence_memblock_new(ss, 0, c->protocol->core->memblock_stat);
426 s->memblockq = pa_memblockq_new(
434 c->protocol->core->memblock_stat);
436 pa_memblock_unref(silence);
438 s->requested_bytes = 0;
439 s->drain_request = 0;
441 pa_idxset_put(c->output_streams, s, &s->index);
446 static void playback_stream_free(struct playback_stream* p) {
447 struct playback_stream *head;
448 assert(p && p->connection);
450 if (p->drain_request)
451 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERR_NOENTITY);
453 PA_LLIST_FIND_HEAD(struct playback_stream, p, &head);
454 PA_LLIST_REMOVE(struct playback_stream, head, p);
456 pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
457 pa_sink_input_disconnect(p->sink_input);
458 pa_sink_input_unref(p->sink_input);
459 pa_memblockq_free(p->memblockq);
463 static void connection_free(struct connection *c) {
464 struct record_stream *r;
465 struct output_stream *o;
466 assert(c && c->protocol);
468 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
469 while ((r = pa_idxset_first(c->record_streams, NULL)))
470 record_stream_free(r);
471 pa_idxset_free(c->record_streams, NULL, NULL);
473 while ((o = pa_idxset_first(c->output_streams, NULL)))
474 if (o->type == PLAYBACK_STREAM)
475 playback_stream_free((struct playback_stream*) o);
477 upload_stream_free((struct upload_stream*) o);
478 pa_idxset_free(c->output_streams, NULL, NULL);
480 pa_pdispatch_unref(c->pdispatch);
481 pa_pstream_close(c->pstream);
482 pa_pstream_unref(c->pstream);
483 pa_client_free(c->client);
486 pa_subscription_free(c->subscription);
488 if (c->auth_timeout_event)
489 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
494 static void request_bytes(struct playback_stream *s) {
499 if (!(l = pa_memblockq_missing(s->memblockq)))
502 if (l <= s->requested_bytes)
505 l -= s->requested_bytes;
507 if (l < pa_memblockq_get_minreq(s->memblockq))
510 s->requested_bytes += l;
512 t = pa_tagstruct_new(NULL, 0);
514 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
515 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
516 pa_tagstruct_putu32(t, s->index);
517 pa_tagstruct_putu32(t, l);
518 pa_pstream_send_tagstruct(s->connection->pstream, t);
520 /* pa_log(__FILE__": Requesting %u bytes", l); */
523 static void send_memblock(struct connection *c) {
525 struct record_stream *r;
527 start = PA_IDXSET_INVALID;
531 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
534 if (start == PA_IDXSET_INVALID)
535 start = c->rrobin_index;
536 else if (start == c->rrobin_index)
539 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
540 pa_memchunk schunk = chunk;
542 if (schunk.length > r->fragment_size)
543 schunk.length = r->fragment_size;
545 pa_pstream_send_memblock(c->pstream, r->index, 0, PA_SEEK_RELATIVE, &schunk);
546 pa_memblockq_drop(r->memblockq, &chunk, schunk.length);
547 pa_memblock_unref(schunk.memblock);
554 static void send_playback_stream_killed(struct playback_stream *p) {
558 t = pa_tagstruct_new(NULL, 0);
559 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
560 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
561 pa_tagstruct_putu32(t, p->index);
562 pa_pstream_send_tagstruct(p->connection->pstream, t);
565 static void send_record_stream_killed(struct record_stream *r) {
569 t = pa_tagstruct_new(NULL, 0);
570 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
571 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
572 pa_tagstruct_putu32(t, r->index);
573 pa_pstream_send_tagstruct(r->connection->pstream, t);
576 /*** sinkinput callbacks ***/
578 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
579 struct playback_stream *s;
580 assert(i && i->userdata && chunk);
583 if (pa_memblockq_get_length(s->memblockq) <= 0 && !s->underrun) {
586 /* Report that we're empty */
588 t = pa_tagstruct_new(NULL, 0);
589 pa_tagstruct_putu32(t, PA_COMMAND_UNDERFLOW);
590 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
591 pa_tagstruct_putu32(t, s->index);
592 pa_pstream_send_tagstruct(s->connection->pstream, t);
597 if (pa_memblockq_peek(s->memblockq, chunk) < 0) {
598 /* pa_log(__FILE__": peek: failure"); */
602 /* pa_log(__FILE__": peek: %u", chunk->length); */
607 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
608 struct playback_stream *s;
609 assert(i && i->userdata && length);
612 pa_memblockq_drop(s->memblockq, chunk, length);
616 if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
617 pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
618 s->drain_request = 0;
621 /* pa_log(__FILE__": after_drop: %u %u", pa_memblockq_get_length(s->memblockq), pa_memblockq_is_readable(s->memblockq)); */
624 static void sink_input_kill_cb(pa_sink_input *i) {
625 assert(i && i->userdata);
626 send_playback_stream_killed((struct playback_stream *) i->userdata);
627 playback_stream_free((struct playback_stream *) i->userdata);
630 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
631 struct playback_stream *s;
632 assert(i && i->userdata);
635 /*pa_log(__FILE__": get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
637 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
640 /*** source_output callbacks ***/
642 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
643 struct record_stream *s;
644 assert(o && o->userdata && chunk);
647 if (pa_memblockq_push_align(s->memblockq, chunk) < 0) {
648 pa_log_warn(__FILE__": Failed to push data into output queue.");
652 if (!pa_pstream_is_pending(s->connection->pstream))
653 send_memblock(s->connection);
656 static void source_output_kill_cb(pa_source_output *o) {
657 assert(o && o->userdata);
658 send_record_stream_killed((struct record_stream *) o->userdata);
659 record_stream_free((struct record_stream *) o->userdata);
662 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
663 struct record_stream *s;
664 assert(o && o->userdata);
667 /*pa_log(__FILE__": get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
669 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &o->sample_spec);
672 /*** pdispatch callbacks ***/
674 static void protocol_error(struct connection *c) {
675 pa_log(__FILE__": protocol error, kicking client");
679 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
680 if (!(expression)) { \
681 pa_pstream_send_error((pstream), (tag), (error)); \
686 static pa_tagstruct *reply_new(uint32_t tag) {
689 reply = pa_tagstruct_new(NULL, 0);
690 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
691 pa_tagstruct_putu32(reply, tag);
695 static void command_create_playback_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
696 struct connection *c = userdata;
697 struct playback_stream *s;
698 size_t maxlength, tlength, prebuf, minreq;
699 uint32_t sink_index, syncid;
700 const char *name, *sink_name;
708 assert(c && t && c->protocol && c->protocol->core);
710 if (pa_tagstruct_get(
712 PA_TAG_STRING, &name,
713 PA_TAG_SAMPLE_SPEC, &ss,
714 PA_TAG_CHANNEL_MAP, &map,
715 PA_TAG_U32, &sink_index,
716 PA_TAG_STRING, &sink_name,
717 PA_TAG_U32, &maxlength,
718 PA_TAG_BOOLEAN, &corked,
719 PA_TAG_U32, &tlength,
723 PA_TAG_CVOLUME, &volume,
724 PA_TAG_INVALID) < 0 ||
725 !pa_tagstruct_eof(t) ||
731 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
732 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
733 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
734 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
735 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
736 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
737 CHECK_VALIDITY(c->pstream, map.channels == ss.channels && volume.channels == ss.channels, tag, PA_ERR_INVALID);
738 CHECK_VALIDITY(c->pstream, maxlength <= MAX_MEMBLOCKQ_LENGTH, tag, PA_ERR_INVALID);
740 if (sink_index != PA_INVALID_INDEX)
741 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
743 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
745 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
747 s = playback_stream_new(c, sink, &ss, &map, name, maxlength, tlength, prebuf, minreq, &volume, syncid);
748 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
750 pa_sink_input_cork(s->sink_input, corked);
752 reply = reply_new(tag);
753 pa_tagstruct_putu32(reply, s->index);
754 assert(s->sink_input);
755 pa_tagstruct_putu32(reply, s->sink_input->index);
756 pa_tagstruct_putu32(reply, s->requested_bytes = pa_memblockq_missing(s->memblockq));
757 pa_pstream_send_tagstruct(c->pstream, reply);
761 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
762 struct connection *c = userdata;
766 if (pa_tagstruct_getu32(t, &channel) < 0 ||
767 !pa_tagstruct_eof(t)) {
772 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
774 if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
775 struct playback_stream *s;
776 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
777 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
781 playback_stream_free(s);
782 } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
783 struct record_stream *s;
784 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
785 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
789 record_stream_free(s);
791 struct upload_stream *s;
792 assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
793 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
794 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
798 upload_stream_free(s);
801 pa_pstream_send_simple_ack(c->pstream, tag);
804 static void command_create_record_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
805 struct connection *c = userdata;
806 struct record_stream *s;
807 uint32_t maxlength, fragment_size;
808 uint32_t source_index;
809 const char *name, *source_name;
815 assert(c && t && c->protocol && c->protocol->core);
817 if (pa_tagstruct_gets(t, &name) < 0 ||
818 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
819 pa_tagstruct_get_channel_map(t, &map) < 0 ||
820 pa_tagstruct_getu32(t, &source_index) < 0 ||
821 pa_tagstruct_gets(t, &source_name) < 0 ||
822 pa_tagstruct_getu32(t, &maxlength) < 0 ||
823 pa_tagstruct_get_boolean(t, &corked) < 0 ||
824 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
825 !pa_tagstruct_eof(t)) {
830 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
831 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
832 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
833 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
834 CHECK_VALIDITY(c->pstream, source_index != PA_INVALID_INDEX || !source_name || (*source_name && pa_utf8_valid(source_name)), tag, PA_ERR_INVALID);
835 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
836 CHECK_VALIDITY(c->pstream, maxlength <= MAX_MEMBLOCKQ_LENGTH, tag, PA_ERR_INVALID);
838 if (source_index != PA_INVALID_INDEX)
839 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
841 source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1);
843 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
845 s = record_stream_new(c, source, &ss, &map, name, maxlength, fragment_size);
846 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
848 pa_source_output_cork(s->source_output, corked);
850 reply = reply_new(tag);
851 pa_tagstruct_putu32(reply, s->index);
852 assert(s->source_output);
853 pa_tagstruct_putu32(reply, s->source_output->index);
854 pa_pstream_send_tagstruct(c->pstream, reply);
857 static void command_exit(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
858 struct connection *c = userdata;
861 if (!pa_tagstruct_eof(t)) {
866 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
868 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
869 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
870 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
873 static void command_auth(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
874 struct connection *c = userdata;
879 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
880 pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
881 !pa_tagstruct_eof(t)) {
886 /* Minimum supported version */
887 if (c->version < 8) {
888 pa_pstream_send_error(c->pstream, tag, PA_ERR_VERSION);
892 if (!c->authorized) {
895 #ifdef SCM_CREDENTIALS
896 const struct ucred *ucred = pa_pdispatch_creds(pd);
899 if (ucred->uid == getuid())
901 else if (c->protocol->auth_group) {
904 if ((r = pa_uid_in_group(ucred->uid, c->protocol->auth_group)) < 0)
905 pa_log_warn(__FILE__": failed to check group membership.");
910 pa_log_info(__FILE__": Got credentials: pid=%lu uid=%lu gid=%lu auth=%i",
911 (unsigned long) ucred->pid,
912 (unsigned long) ucred->uid,
913 (unsigned long) ucred->gid,
918 if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) == 0)
922 pa_log_warn(__FILE__": Denied access to client with invalid authorization data.");
923 pa_pstream_send_error(c->pstream, tag, PA_ERR_ACCESS);
928 if (c->auth_timeout_event) {
929 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
930 c->auth_timeout_event = NULL;
934 reply = reply_new(tag);
935 pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION);
936 pa_pstream_send_tagstruct(c->pstream, reply);
939 static void command_set_client_name(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
940 struct connection *c = userdata;
944 if (pa_tagstruct_gets(t, &name) < 0 ||
945 !pa_tagstruct_eof(t)) {
950 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
952 pa_client_set_name(c->client, name);
953 pa_pstream_send_simple_ack(c->pstream, tag);
956 static void command_lookup(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
957 struct connection *c = userdata;
959 uint32_t idx = PA_IDXSET_INVALID;
962 if (pa_tagstruct_gets(t, &name) < 0 ||
963 !pa_tagstruct_eof(t)) {
968 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
969 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
971 if (command == PA_COMMAND_LOOKUP_SINK) {
973 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
977 assert(command == PA_COMMAND_LOOKUP_SOURCE);
978 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
982 if (idx == PA_IDXSET_INVALID)
983 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
986 reply = reply_new(tag);
987 pa_tagstruct_putu32(reply, idx);
988 pa_pstream_send_tagstruct(c->pstream, reply);
992 static void command_drain_playback_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
993 struct connection *c = userdata;
995 struct playback_stream *s;
998 if (pa_tagstruct_getu32(t, &idx) < 0 ||
999 !pa_tagstruct_eof(t)) {
1004 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1005 s = pa_idxset_get_by_index(c->output_streams, idx);
1006 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1007 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1009 s->drain_request = 0;
1011 pa_memblockq_prebuf_disable(s->memblockq);
1013 if (!pa_memblockq_is_readable(s->memblockq)) {
1014 /* pa_log("immediate drain: %u", pa_memblockq_get_length(s->memblockq)); */
1015 pa_pstream_send_simple_ack(c->pstream, tag);
1017 /* pa_log("slow drain triggered"); */
1018 s->drain_request = 1;
1021 pa_sink_notify(s->sink_input->sink);
1025 static void command_stat(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1026 struct connection *c = userdata;
1027 pa_tagstruct *reply;
1030 if (!pa_tagstruct_eof(t)) {
1035 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1037 reply = reply_new(tag);
1038 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total);
1039 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total_size);
1040 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated);
1041 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated_size);
1042 pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
1043 pa_pstream_send_tagstruct(c->pstream, reply);
1046 static void command_get_playback_latency(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1047 struct connection *c = userdata;
1048 pa_tagstruct *reply;
1049 struct playback_stream *s;
1050 struct timeval tv, now;
1055 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1056 pa_tagstruct_get_timeval(t, &tv) < 0 ||
1057 !pa_tagstruct_eof(t)) {
1062 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1063 s = pa_idxset_get_by_index(c->output_streams, idx);
1064 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1065 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1067 reply = reply_new(tag);
1069 latency = pa_sink_get_latency(s->sink_input->sink);
1070 if (s->sink_input->resampled_chunk.memblock)
1071 latency += pa_bytes_to_usec(s->sink_input->resampled_chunk.length, &s->sink_input->sample_spec);
1072 pa_tagstruct_put_usec(reply, latency);
1074 pa_tagstruct_put_usec(reply, 0);
1075 pa_tagstruct_put_boolean(reply, pa_memblockq_is_readable(s->memblockq));
1076 pa_tagstruct_put_timeval(reply, &tv);
1077 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
1078 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
1079 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
1080 pa_pstream_send_tagstruct(c->pstream, reply);
1083 static void command_get_record_latency(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1084 struct connection *c = userdata;
1085 pa_tagstruct *reply;
1086 struct record_stream *s;
1087 struct timeval tv, now;
1091 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1092 pa_tagstruct_get_timeval(t, &tv) < 0 ||
1093 !pa_tagstruct_eof(t)) {
1098 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1099 s = pa_idxset_get_by_index(c->record_streams, idx);
1100 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1102 reply = reply_new(tag);
1103 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
1104 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
1105 pa_tagstruct_put_boolean(reply, 0);
1106 pa_tagstruct_put_timeval(reply, &tv);
1107 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
1108 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
1109 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
1110 pa_pstream_send_tagstruct(c->pstream, reply);
1113 static void command_create_upload_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1114 struct connection *c = userdata;
1115 struct upload_stream *s;
1120 pa_tagstruct *reply;
1121 assert(c && t && c->protocol && c->protocol->core);
1123 if (pa_tagstruct_gets(t, &name) < 0 ||
1124 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
1125 pa_tagstruct_get_channel_map(t, &map) < 0 ||
1126 pa_tagstruct_getu32(t, &length) < 0 ||
1127 !pa_tagstruct_eof(t)) {
1132 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1133 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1134 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1135 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
1136 CHECK_VALIDITY(c->pstream, (length % pa_frame_size(&ss)) == 0 && length > 0, tag, PA_ERR_INVALID);
1137 CHECK_VALIDITY(c->pstream, length <= PA_SCACHE_ENTRY_SIZE_MAX, tag, PA_ERR_TOOLARGE);
1138 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1140 s = upload_stream_new(c, &ss, &map, name, length);
1141 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1143 reply = reply_new(tag);
1144 pa_tagstruct_putu32(reply, s->index);
1145 pa_tagstruct_putu32(reply, length);
1146 pa_pstream_send_tagstruct(c->pstream, reply);
1149 static void command_finish_upload_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1150 struct connection *c = userdata;
1152 struct upload_stream *s;
1156 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1157 !pa_tagstruct_eof(t)) {
1162 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1164 s = pa_idxset_get_by_index(c->output_streams, channel);
1165 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1166 CHECK_VALIDITY(c->pstream, s->type == UPLOAD_STREAM, tag, PA_ERR_NOENTITY);
1168 if (pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->channel_map, &s->memchunk, &idx) < 0)
1169 pa_pstream_send_error(c->pstream, tag, PA_ERR_INTERNAL);
1171 pa_pstream_send_simple_ack(c->pstream, tag);
1173 upload_stream_free(s);
1176 static void command_play_sample(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1177 struct connection *c = userdata;
1178 uint32_t sink_index;
1181 const char *name, *sink_name;
1184 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
1185 pa_tagstruct_gets(t, &sink_name) < 0 ||
1186 pa_tagstruct_getu32(t, &volume) < 0 ||
1187 pa_tagstruct_gets(t, &name) < 0 ||
1188 !pa_tagstruct_eof(t)) {
1193 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1194 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1195 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1197 if (sink_index != PA_INVALID_INDEX)
1198 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1200 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1202 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
1204 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
1205 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1209 pa_pstream_send_simple_ack(c->pstream, tag);
1212 static void command_remove_sample(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1213 struct connection *c = userdata;
1217 if (pa_tagstruct_gets(t, &name) < 0 ||
1218 !pa_tagstruct_eof(t)) {
1223 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1224 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1226 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
1227 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1231 pa_pstream_send_simple_ack(c->pstream, tag);
1234 static void sink_fill_tagstruct(pa_tagstruct *t, pa_sink *sink) {
1238 PA_TAG_U32, sink->index,
1239 PA_TAG_STRING, sink->name,
1240 PA_TAG_STRING, sink->description,
1241 PA_TAG_SAMPLE_SPEC, &sink->sample_spec,
1242 PA_TAG_CHANNEL_MAP, &sink->channel_map,
1243 PA_TAG_U32, sink->owner ? sink->owner->index : PA_INVALID_INDEX,
1244 PA_TAG_CVOLUME, pa_sink_get_volume(sink, PA_MIXER_HARDWARE),
1245 PA_TAG_BOOLEAN, pa_sink_get_mute(sink, PA_MIXER_HARDWARE),
1246 PA_TAG_U32, sink->monitor_source->index,
1247 PA_TAG_STRING, sink->monitor_source->name,
1248 PA_TAG_USEC, pa_sink_get_latency(sink),
1249 PA_TAG_STRING, sink->driver,
1250 PA_TAG_U32, (sink->get_hw_volume ? PA_SINK_HW_VOLUME_CTRL : 0) | (sink->get_latency ? PA_SINK_LATENCY : 0),
1254 static void source_fill_tagstruct(pa_tagstruct *t, pa_source *source) {
1255 assert(t && source);
1258 PA_TAG_U32, source->index,
1259 PA_TAG_STRING, source->name,
1260 PA_TAG_STRING, source->description,
1261 PA_TAG_SAMPLE_SPEC, &source->sample_spec,
1262 PA_TAG_CHANNEL_MAP, &source->channel_map,
1263 PA_TAG_U32, source->owner ? source->owner->index : PA_INVALID_INDEX,
1264 PA_TAG_CVOLUME, pa_source_get_volume(source, PA_MIXER_HARDWARE),
1265 PA_TAG_BOOLEAN, pa_source_get_mute(source, PA_MIXER_HARDWARE),
1266 PA_TAG_U32, source->monitor_of ? source->monitor_of->index : PA_INVALID_INDEX,
1267 PA_TAG_STRING, source->monitor_of ? source->monitor_of->name : NULL,
1268 PA_TAG_USEC, pa_source_get_latency(source),
1269 PA_TAG_STRING, source->driver,
1270 PA_TAG_U32, (source->get_hw_volume ? PA_SOURCE_HW_VOLUME_CTRL : 0) | (source->get_latency ? PA_SOURCE_LATENCY : 0),
1274 static void client_fill_tagstruct(pa_tagstruct *t, pa_client *client) {
1275 assert(t && client);
1276 pa_tagstruct_putu32(t, client->index);
1277 pa_tagstruct_puts(t, client->name);
1278 pa_tagstruct_putu32(t, client->owner ? client->owner->index : PA_INVALID_INDEX);
1279 pa_tagstruct_puts(t, client->driver);
1282 static void module_fill_tagstruct(pa_tagstruct *t, pa_module *module) {
1283 assert(t && module);
1284 pa_tagstruct_putu32(t, module->index);
1285 pa_tagstruct_puts(t, module->name);
1286 pa_tagstruct_puts(t, module->argument);
1287 pa_tagstruct_putu32(t, module->n_used);
1288 pa_tagstruct_put_boolean(t, module->auto_unload);
1291 static void sink_input_fill_tagstruct(pa_tagstruct *t, pa_sink_input *s) {
1293 pa_tagstruct_putu32(t, s->index);
1294 pa_tagstruct_puts(t, s->name);
1295 pa_tagstruct_putu32(t, s->owner ? s->owner->index : PA_INVALID_INDEX);
1296 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
1297 pa_tagstruct_putu32(t, s->sink->index);
1298 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1299 pa_tagstruct_put_channel_map(t, &s->channel_map);
1300 pa_tagstruct_put_cvolume(t, &s->volume);
1301 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
1302 pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
1303 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
1304 pa_tagstruct_puts(t, s->driver);
1307 static void source_output_fill_tagstruct(pa_tagstruct *t, pa_source_output *s) {
1309 pa_tagstruct_putu32(t, s->index);
1310 pa_tagstruct_puts(t, s->name);
1311 pa_tagstruct_putu32(t, s->owner ? s->owner->index : PA_INVALID_INDEX);
1312 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
1313 pa_tagstruct_putu32(t, s->source->index);
1314 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1315 pa_tagstruct_put_channel_map(t, &s->channel_map);
1316 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
1317 pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
1318 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
1319 pa_tagstruct_puts(t, s->driver);
1322 static void scache_fill_tagstruct(pa_tagstruct *t, pa_scache_entry *e) {
1324 pa_tagstruct_putu32(t, e->index);
1325 pa_tagstruct_puts(t, e->name);
1326 pa_tagstruct_put_cvolume(t, &e->volume);
1327 pa_tagstruct_put_usec(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1328 pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1329 pa_tagstruct_put_channel_map(t, &e->channel_map);
1330 pa_tagstruct_putu32(t, e->memchunk.length);
1331 pa_tagstruct_put_boolean(t, e->lazy);
1332 pa_tagstruct_puts(t, e->filename);
1335 static void command_get_info(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1336 struct connection *c = userdata;
1338 pa_sink *sink = NULL;
1339 pa_source *source = NULL;
1340 pa_client *client = NULL;
1341 pa_module *module = NULL;
1342 pa_sink_input *si = NULL;
1343 pa_source_output *so = NULL;
1344 pa_scache_entry *sce = NULL;
1346 pa_tagstruct *reply;
1349 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1350 (command != PA_COMMAND_GET_CLIENT_INFO &&
1351 command != PA_COMMAND_GET_MODULE_INFO &&
1352 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1353 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1354 pa_tagstruct_gets(t, &name) < 0) ||
1355 !pa_tagstruct_eof(t)) {
1360 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1361 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1363 if (command == PA_COMMAND_GET_SINK_INFO) {
1364 if (idx != PA_INVALID_INDEX)
1365 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1367 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1368 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1369 if (idx != PA_INVALID_INDEX)
1370 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1372 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1373 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1374 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
1375 else if (command == PA_COMMAND_GET_MODULE_INFO)
1376 module = pa_idxset_get_by_index(c->protocol->core->modules, idx);
1377 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1378 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1379 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1380 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
1382 assert(command == PA_COMMAND_GET_SAMPLE_INFO);
1383 if (idx != PA_INVALID_INDEX)
1384 sce = pa_idxset_get_by_index(c->protocol->core->scache, idx);
1386 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1389 if (!sink && !source && !client && !module && !si && !so && !sce) {
1390 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1394 reply = reply_new(tag);
1396 sink_fill_tagstruct(reply, sink);
1398 source_fill_tagstruct(reply, source);
1400 client_fill_tagstruct(reply, client);
1402 module_fill_tagstruct(reply, module);
1404 sink_input_fill_tagstruct(reply, si);
1406 source_output_fill_tagstruct(reply, so);
1408 scache_fill_tagstruct(reply, sce);
1409 pa_pstream_send_tagstruct(c->pstream, reply);
1412 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1413 struct connection *c = userdata;
1417 pa_tagstruct *reply;
1420 if (!pa_tagstruct_eof(t)) {
1425 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1427 reply = reply_new(tag);
1429 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1430 i = c->protocol->core->sinks;
1431 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1432 i = c->protocol->core->sources;
1433 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1434 i = c->protocol->core->clients;
1435 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1436 i = c->protocol->core->modules;
1437 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1438 i = c->protocol->core->sink_inputs;
1439 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1440 i = c->protocol->core->source_outputs;
1442 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1443 i = c->protocol->core->scache;
1447 for (p = pa_idxset_first(i, &idx); p; p = pa_idxset_next(i, &idx)) {
1448 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1449 sink_fill_tagstruct(reply, p);
1450 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1451 source_fill_tagstruct(reply, p);
1452 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1453 client_fill_tagstruct(reply, p);
1454 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1455 module_fill_tagstruct(reply, p);
1456 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1457 sink_input_fill_tagstruct(reply, p);
1458 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1459 source_output_fill_tagstruct(reply, p);
1461 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1462 scache_fill_tagstruct(reply, p);
1467 pa_pstream_send_tagstruct(c->pstream, reply);
1470 static void command_get_server_info(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1471 struct connection *c = userdata;
1472 pa_tagstruct *reply;
1477 if (!pa_tagstruct_eof(t)) {
1482 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1484 reply = reply_new(tag);
1485 pa_tagstruct_puts(reply, PACKAGE_NAME);
1486 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1487 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1488 pa_tagstruct_puts(reply, pa_get_fqdn(txt, sizeof(txt)));
1489 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1491 n = pa_namereg_get_default_sink_name(c->protocol->core);
1492 pa_tagstruct_puts(reply, n);
1493 n = pa_namereg_get_default_source_name(c->protocol->core);
1494 pa_tagstruct_puts(reply, n);
1496 pa_tagstruct_putu32(reply, c->protocol->core->cookie);
1498 pa_pstream_send_tagstruct(c->pstream, reply);
1501 static void subscription_cb(pa_core *core, pa_subscription_event_type_t e, uint32_t idx, void *userdata) {
1503 struct connection *c = userdata;
1506 t = pa_tagstruct_new(NULL, 0);
1507 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1508 pa_tagstruct_putu32(t, (uint32_t) -1);
1509 pa_tagstruct_putu32(t, e);
1510 pa_tagstruct_putu32(t, idx);
1511 pa_pstream_send_tagstruct(c->pstream, t);
1514 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1515 struct connection *c = userdata;
1516 pa_subscription_mask_t m;
1519 if (pa_tagstruct_getu32(t, &m) < 0 ||
1520 !pa_tagstruct_eof(t)) {
1525 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1526 CHECK_VALIDITY(c->pstream, (m & ~PA_SUBSCRIPTION_MASK_ALL) == 0, tag, PA_ERR_INVALID);
1528 if (c->subscription)
1529 pa_subscription_free(c->subscription);
1532 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1533 assert(c->subscription);
1535 c->subscription = NULL;
1537 pa_pstream_send_simple_ack(c->pstream, tag);
1540 static void command_set_volume(
1541 PA_GCC_UNUSED pa_pdispatch *pd,
1547 struct connection *c = userdata;
1550 pa_sink *sink = NULL;
1551 pa_source *source = NULL;
1552 pa_sink_input *si = NULL;
1553 const char *name = NULL;
1556 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1557 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1558 (command == PA_COMMAND_SET_SOURCE_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1559 pa_tagstruct_get_cvolume(t, &volume) ||
1560 !pa_tagstruct_eof(t)) {
1565 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1566 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1567 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
1569 if (command == PA_COMMAND_SET_SINK_VOLUME) {
1570 if (idx != PA_INVALID_INDEX)
1571 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1573 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1574 } else if (command == PA_COMMAND_SET_SOURCE_VOLUME) {
1575 if (idx != (uint32_t) -1)
1576 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1578 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1580 assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1581 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1584 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
1587 pa_sink_set_volume(sink, PA_MIXER_HARDWARE, &volume);
1589 pa_source_set_volume(source, PA_MIXER_HARDWARE, &volume);
1591 pa_sink_input_set_volume(si, &volume);
1593 pa_pstream_send_simple_ack(c->pstream, tag);
1596 static void command_set_mute(
1597 PA_GCC_UNUSED pa_pdispatch *pd,
1603 struct connection *c = userdata;
1606 pa_sink *sink = NULL;
1607 pa_source *source = NULL;
1608 const char *name = NULL;
1611 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1612 pa_tagstruct_gets(t, &name) < 0 ||
1613 pa_tagstruct_get_boolean(t, &mute) ||
1614 !pa_tagstruct_eof(t)) {
1619 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1620 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1622 if (command == PA_COMMAND_SET_SINK_MUTE) {
1623 if (idx != PA_INVALID_INDEX)
1624 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1626 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1628 assert(command == PA_COMMAND_SET_SOURCE_MUTE);
1629 if (idx != (uint32_t) -1)
1630 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1632 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1635 CHECK_VALIDITY(c->pstream, sink || source, tag, PA_ERR_NOENTITY);
1638 pa_sink_set_mute(sink, PA_MIXER_HARDWARE, mute);
1640 pa_source_set_mute(source, PA_MIXER_HARDWARE, mute);
1642 pa_pstream_send_simple_ack(c->pstream, tag);
1645 static void command_cork_playback_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1646 struct connection *c = userdata;
1649 struct playback_stream *s, *ssync;
1652 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1653 pa_tagstruct_get_boolean(t, &b) < 0 ||
1654 !pa_tagstruct_eof(t)) {
1659 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1660 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1661 s = pa_idxset_get_by_index(c->output_streams, idx);
1662 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1663 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1665 pa_sink_input_cork(s->sink_input, b);
1666 pa_memblockq_prebuf_force(s->memblockq);
1668 /* Do the same for all other members in the sync group */
1669 for (ssync = s->prev; ssync; ssync = ssync->prev) {
1670 pa_sink_input_cork(ssync->sink_input, b);
1671 pa_memblockq_prebuf_force(ssync->memblockq);
1674 for (ssync = s->next; ssync; ssync = ssync->next) {
1675 pa_sink_input_cork(ssync->sink_input, b);
1676 pa_memblockq_prebuf_force(ssync->memblockq);
1679 pa_pstream_send_simple_ack(c->pstream, tag);
1682 static void command_flush_playback_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1683 struct connection *c = userdata;
1685 struct playback_stream *s, *ssync;
1688 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1689 !pa_tagstruct_eof(t)) {
1694 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1695 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1696 s = pa_idxset_get_by_index(c->output_streams, idx);
1697 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1698 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1700 pa_memblockq_flush(s->memblockq);
1703 /* Do the same for all other members in the sync group */
1704 for (ssync = s->prev; ssync; ssync = ssync->prev) {
1705 pa_memblockq_flush(ssync->memblockq);
1706 ssync->underrun = 0;
1709 for (ssync = s->next; ssync; ssync = ssync->next) {
1710 pa_memblockq_flush(ssync->memblockq);
1711 ssync->underrun = 0;
1714 pa_pstream_send_simple_ack(c->pstream, tag);
1715 pa_sink_notify(s->sink_input->sink);
1718 for (ssync = s->prev; ssync; ssync = ssync->prev)
1719 request_bytes(ssync);
1721 for (ssync = s->next; ssync; ssync = ssync->next)
1722 request_bytes(ssync);
1725 static void command_trigger_or_prebuf_playback_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1726 struct connection *c = userdata;
1728 struct playback_stream *s;
1731 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1732 !pa_tagstruct_eof(t)) {
1737 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1738 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1739 s = pa_idxset_get_by_index(c->output_streams, idx);
1740 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1741 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1744 case PA_COMMAND_PREBUF_PLAYBACK_STREAM:
1745 pa_memblockq_prebuf_force(s->memblockq);
1748 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM:
1749 pa_memblockq_prebuf_disable(s->memblockq);
1756 pa_sink_notify(s->sink_input->sink);
1757 pa_pstream_send_simple_ack(c->pstream, tag);
1761 static void command_cork_record_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1762 struct connection *c = userdata;
1764 struct record_stream *s;
1768 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1769 pa_tagstruct_get_boolean(t, &b) < 0 ||
1770 !pa_tagstruct_eof(t)) {
1775 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1776 s = pa_idxset_get_by_index(c->record_streams, idx);
1777 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1779 pa_source_output_cork(s->source_output, b);
1780 pa_memblockq_prebuf_force(s->memblockq);
1781 pa_pstream_send_simple_ack(c->pstream, tag);
1784 static void command_flush_record_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1785 struct connection *c = userdata;
1787 struct record_stream *s;
1790 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1791 !pa_tagstruct_eof(t)) {
1796 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1797 s = pa_idxset_get_by_index(c->record_streams, idx);
1798 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1800 pa_memblockq_flush(s->memblockq);
1801 pa_pstream_send_simple_ack(c->pstream, tag);
1804 static void command_set_default_sink_or_source(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1805 struct connection *c = userdata;
1809 if (pa_tagstruct_gets(t, &s) < 0 ||
1810 !pa_tagstruct_eof(t)) {
1815 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1816 CHECK_VALIDITY(c->pstream, !s || (*s && pa_utf8_valid(s)), tag, PA_ERR_INVALID);
1818 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
1819 pa_pstream_send_simple_ack(c->pstream, tag);
1822 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1823 struct connection *c = userdata;
1828 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1829 pa_tagstruct_gets(t, &name) < 0 ||
1830 !pa_tagstruct_eof(t)) {
1835 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1836 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1838 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
1839 struct playback_stream *s;
1841 s = pa_idxset_get_by_index(c->output_streams, idx);
1842 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1843 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1845 pa_sink_input_set_name(s->sink_input, name);
1848 struct record_stream *s;
1850 s = pa_idxset_get_by_index(c->record_streams, idx);
1851 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1853 pa_source_output_set_name(s->source_output, name);
1856 pa_pstream_send_simple_ack(c->pstream, tag);
1859 static void command_kill(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1860 struct connection *c = userdata;
1864 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1865 !pa_tagstruct_eof(t)) {
1870 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1872 if (command == PA_COMMAND_KILL_CLIENT) {
1875 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
1876 CHECK_VALIDITY(c->pstream, client, tag, PA_ERR_NOENTITY);
1877 pa_client_kill(client);
1879 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
1882 s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1883 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1885 pa_sink_input_kill(s);
1887 pa_source_output *s;
1889 assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
1891 s = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
1892 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1894 pa_source_output_kill(s);
1897 pa_pstream_send_simple_ack(c->pstream, tag);
1900 static void command_load_module(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1901 struct connection *c = userdata;
1903 const char *name, *argument;
1904 pa_tagstruct *reply;
1907 if (pa_tagstruct_gets(t, &name) < 0 ||
1908 pa_tagstruct_gets(t, &argument) < 0 ||
1909 !pa_tagstruct_eof(t)) {
1914 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1915 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name) && !strchr(name, '/'), tag, PA_ERR_INVALID);
1916 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
1918 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
1919 pa_pstream_send_error(c->pstream, tag, PA_ERR_MODINITFAILED);
1923 reply = reply_new(tag);
1924 pa_tagstruct_putu32(reply, m->index);
1925 pa_pstream_send_tagstruct(c->pstream, reply);
1928 static void command_unload_module(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1929 struct connection *c = userdata;
1934 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1935 !pa_tagstruct_eof(t)) {
1940 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1941 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
1942 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOENTITY);
1944 pa_module_unload_request(m);
1945 pa_pstream_send_simple_ack(c->pstream, tag);
1948 static void command_add_autoload(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1949 struct connection *c = userdata;
1950 const char *name, *module, *argument;
1953 pa_tagstruct *reply;
1956 if (pa_tagstruct_gets(t, &name) < 0 ||
1957 pa_tagstruct_getu32(t, &type) < 0 ||
1958 pa_tagstruct_gets(t, &module) < 0 ||
1959 pa_tagstruct_gets(t, &argument) < 0 ||
1960 !pa_tagstruct_eof(t)) {
1965 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1966 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1967 CHECK_VALIDITY(c->pstream, type == 0 || type == 1, tag, PA_ERR_INVALID);
1968 CHECK_VALIDITY(c->pstream, module && *module && pa_utf8_valid(module), tag, PA_ERR_INVALID);
1969 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
1971 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument, &idx) < 0) {
1972 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1976 reply = reply_new(tag);
1977 pa_tagstruct_putu32(reply, idx);
1978 pa_pstream_send_tagstruct(c->pstream, reply);
1981 static void command_remove_autoload(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1982 struct connection *c = userdata;
1983 const char *name = NULL;
1984 uint32_t type, idx = PA_IDXSET_INVALID;
1988 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
1989 (pa_tagstruct_gets(t, &name) < 0 ||
1990 pa_tagstruct_getu32(t, &type) < 0)) ||
1991 !pa_tagstruct_eof(t)) {
1996 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1997 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
1998 CHECK_VALIDITY(c->pstream, !name || (*name && pa_utf8_valid(name) && (type == 0 || type == 1)), tag, PA_ERR_INVALID);
2001 r = pa_autoload_remove_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
2003 r = pa_autoload_remove_by_index(c->protocol->core, idx);
2005 CHECK_VALIDITY(c->pstream, r >= 0, tag, PA_ERR_NOENTITY);
2007 pa_pstream_send_simple_ack(c->pstream, tag);
2010 static void autoload_fill_tagstruct(pa_tagstruct *t, const pa_autoload_entry *e) {
2013 pa_tagstruct_putu32(t, e->index);
2014 pa_tagstruct_puts(t, e->name);
2015 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
2016 pa_tagstruct_puts(t, e->module);
2017 pa_tagstruct_puts(t, e->argument);
2020 static void command_get_autoload_info(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2021 struct connection *c = userdata;
2022 const pa_autoload_entry *a = NULL;
2025 pa_tagstruct *reply;
2028 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
2029 (pa_tagstruct_gets(t, &name) < 0 ||
2030 pa_tagstruct_getu32(t, &type) < 0)) ||
2031 !pa_tagstruct_eof(t)) {
2036 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2037 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
2038 CHECK_VALIDITY(c->pstream, !name || (*name && (type == 0 || type == 1) && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2041 a = pa_autoload_get_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
2043 a = pa_autoload_get_by_index(c->protocol->core, idx);
2045 CHECK_VALIDITY(c->pstream, a, tag, PA_ERR_NOENTITY);
2047 reply = reply_new(tag);
2048 autoload_fill_tagstruct(reply, a);
2049 pa_pstream_send_tagstruct(c->pstream, reply);
2052 static void command_get_autoload_info_list(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2053 struct connection *c = userdata;
2054 pa_tagstruct *reply;
2057 if (!pa_tagstruct_eof(t)) {
2062 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2064 reply = reply_new(tag);
2066 if (c->protocol->core->autoload_hashmap) {
2067 pa_autoload_entry *a;
2070 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state, NULL)))
2071 autoload_fill_tagstruct(reply, a);
2074 pa_pstream_send_tagstruct(c->pstream, reply);
2077 /*** pstream callbacks ***/
2079 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const void *creds, void *userdata) {
2080 struct connection *c = userdata;
2081 assert(p && packet && packet->data && c);
2083 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0) {
2084 pa_log(__FILE__": invalid packet.");
2089 static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t offset, pa_seek_mode_t seek, const pa_memchunk *chunk, void *userdata) {
2090 struct connection *c = userdata;
2091 struct output_stream *stream;
2092 assert(p && chunk && userdata);
2094 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
2095 pa_log(__FILE__": client sent block for invalid stream.");
2100 if (stream->type == PLAYBACK_STREAM) {
2101 struct playback_stream *ps = (struct playback_stream*) stream;
2102 if (chunk->length >= ps->requested_bytes)
2103 ps->requested_bytes = 0;
2105 ps->requested_bytes -= chunk->length;
2107 pa_memblockq_seek(ps->memblockq, offset, seek);
2109 if (pa_memblockq_push_align(ps->memblockq, chunk) < 0) {
2112 pa_log_warn(__FILE__": failed to push data into queue");
2114 /* Pushing this block into the queue failed, so we simulate
2115 * it by skipping ahead */
2117 pa_memblockq_seek(ps->memblockq, chunk->length, PA_SEEK_RELATIVE);
2119 /* Notify the user */
2120 t = pa_tagstruct_new(NULL, 0);
2121 pa_tagstruct_putu32(t, PA_COMMAND_OVERFLOW);
2122 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
2123 pa_tagstruct_putu32(t, ps->index);
2124 pa_pstream_send_tagstruct(p, t);
2129 pa_sink_notify(ps->sink_input->sink);
2132 struct upload_stream *u = (struct upload_stream*) stream;
2134 assert(u->type == UPLOAD_STREAM);
2136 if (!u->memchunk.memblock) {
2137 if (u->length == chunk->length) {
2138 u->memchunk = *chunk;
2139 pa_memblock_ref(u->memchunk.memblock);
2142 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
2143 u->memchunk.index = u->memchunk.length = 0;
2147 assert(u->memchunk.memblock);
2150 if (l > chunk->length)
2154 memcpy((uint8_t*) u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length,
2155 (uint8_t*) chunk->memblock->data+chunk->index, l);
2156 u->memchunk.length += l;
2162 static void pstream_die_callback(pa_pstream *p, void *userdata) {
2163 struct connection *c = userdata;
2167 /* pa_log(__FILE__": connection died.");*/
2171 static void pstream_drain_callback(pa_pstream *p, void *userdata) {
2172 struct connection *c = userdata;
2178 /*** client callbacks ***/
2180 static void client_kill_cb(pa_client *c) {
2181 assert(c && c->userdata);
2182 connection_free(c->userdata);
2185 /*** socket server callbacks ***/
2187 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
2188 struct connection *c = userdata;
2189 assert(m && tv && c && c->auth_timeout_event == e);
2195 static void on_connection(PA_GCC_UNUSED pa_socket_server*s, pa_iochannel *io, void *userdata) {
2196 pa_protocol_native *p = userdata;
2197 struct connection *c;
2200 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
2201 pa_log_warn(__FILE__": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
2202 pa_iochannel_free(io);
2206 c = pa_xmalloc(sizeof(struct connection));
2208 c->authorized =!! p->public;
2210 if (!c->authorized) {
2212 pa_gettimeofday(&tv);
2213 tv.tv_sec += AUTH_TIMEOUT;
2214 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
2216 c->auth_timeout_event = NULL;
2220 c->client = pa_client_new(p->core, __FILE__, "Client");
2222 c->client->kill = client_kill_cb;
2223 c->client->userdata = c;
2224 c->client->owner = p->module;
2226 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
2229 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
2230 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
2231 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
2232 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
2234 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
2235 assert(c->pdispatch);
2237 c->record_streams = pa_idxset_new(NULL, NULL);
2238 c->output_streams = pa_idxset_new(NULL, NULL);
2239 assert(c->record_streams && c->output_streams);
2241 c->rrobin_index = PA_IDXSET_INVALID;
2242 c->subscription = NULL;
2244 pa_idxset_put(p->connections, c, NULL);
2247 #ifdef SCM_CREDENTIALS
2248 if (pa_iochannel_creds_supported(io))
2249 pa_iochannel_creds_enable(io);
2254 /*** module entry points ***/
2256 static int load_key(pa_protocol_native*p, const char*fn) {
2259 p->auth_cookie_in_property = 0;
2261 if (!fn && pa_authkey_prop_get(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0) {
2262 pa_log_info(__FILE__": using already loaded auth cookie.");
2263 pa_authkey_prop_ref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2264 p->auth_cookie_in_property = 1;
2269 fn = PA_NATIVE_COOKIE_FILE;
2271 if (pa_authkey_load_auto(fn, p->auth_cookie, sizeof(p->auth_cookie)) < 0)
2274 pa_log_info(__FILE__": loading cookie from disk.");
2276 if (pa_authkey_prop_put(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0)
2277 p->auth_cookie_in_property = 1;
2282 static pa_protocol_native* protocol_new_internal(pa_core *c, pa_module *m, pa_modargs *ma) {
2283 pa_protocol_native *p;
2287 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
2288 pa_log(__FILE__": auth-anonymous= expects a boolean argument.");
2292 p = pa_xnew(pa_protocol_native, 1);
2298 #ifdef SCM_CREDENTIALS
2299 p->auth_group = pa_xstrdup(pa_modargs_get_value(ma, "auth-group", NULL));
2302 if (load_key(p, pa_modargs_get_value(ma, "cookie", NULL)) < 0) {
2307 p->connections = pa_idxset_new(NULL, NULL);
2308 assert(p->connections);
2313 pa_protocol_native* pa_protocol_native_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
2315 pa_protocol_native *p;
2317 if (!(p = protocol_new_internal(core, m, ma)))
2321 pa_socket_server_set_callback(p->server, on_connection, p);
2323 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
2325 l = pa_property_get(core, PA_NATIVE_SERVER_PROPERTY_NAME);
2326 l = pa_strlist_prepend(l, t);
2327 pa_property_replace(core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
2333 void pa_protocol_native_free(pa_protocol_native *p) {
2334 struct connection *c;
2337 while ((c = pa_idxset_first(p->connections, NULL)))
2339 pa_idxset_free(p->connections, NULL, NULL);
2344 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
2346 l = pa_property_get(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
2347 l = pa_strlist_remove(l, t);
2350 pa_property_replace(p->core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
2352 pa_property_remove(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
2355 pa_socket_server_unref(p->server);
2358 if (p->auth_cookie_in_property)
2359 pa_authkey_prop_unref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2361 #ifdef SCM_CREDENTIALS
2362 pa_xfree(p->auth_group);
2367 pa_protocol_native* pa_protocol_native_new_iochannel(pa_core*core, pa_iochannel *io, pa_module *m, pa_modargs *ma) {
2368 pa_protocol_native *p;
2370 if (!(p = protocol_new_internal(core, m, ma)))
2373 on_connection(NULL, io, p);