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
31 #include <polypcore/native-common.h>
32 #include <polypcore/packet.h>
33 #include <polypcore/client.h>
34 #include <polypcore/source-output.h>
35 #include <polypcore/sink-input.h>
36 #include <polypcore/pstream.h>
37 #include <polypcore/tagstruct.h>
38 #include <polypcore/pdispatch.h>
39 #include <polypcore/pstream-util.h>
40 #include <polypcore/authkey.h>
41 #include <polypcore/namereg.h>
42 #include <polypcore/core-scache.h>
43 #include <polypcore/xmalloc.h>
44 #include <polypcore/util.h>
45 #include <polypcore/core-subscribe.h>
46 #include <polypcore/log.h>
47 #include <polypcore/autoload.h>
48 #include <polypcore/authkey-prop.h>
49 #include <polypcore/strlist.h>
50 #include <polypcore/props.h>
52 #include "protocol-native.h"
54 /* Kick a client if it doesn't authenticate within this time */
55 #define AUTH_TIMEOUT 5
57 /* Don't accept more connection than this */
58 #define MAX_CONNECTIONS 10
61 struct pa_protocol_native;
63 struct record_stream {
64 struct connection *connection;
66 pa_source_output *source_output;
67 pa_memblockq *memblockq;
71 struct playback_stream {
73 struct connection *connection;
75 pa_sink_input *sink_input;
76 pa_memblockq *memblockq;
77 size_t requested_bytes;
82 struct upload_stream {
84 struct connection *connection;
89 pa_sample_spec sample_spec;
90 pa_channel_map channel_map;
93 struct output_stream {
104 pa_protocol_native *protocol;
107 pa_pdispatch *pdispatch;
108 pa_idxset *record_streams, *output_streams;
109 uint32_t rrobin_index;
110 pa_subscription *subscription;
111 pa_time_event *auth_timeout_event;
114 struct pa_protocol_native {
118 pa_socket_server *server;
119 pa_idxset *connections;
120 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
121 int auth_cookie_in_property;
124 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk);
125 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length);
126 static void sink_input_kill_cb(pa_sink_input *i);
127 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i);
129 static void request_bytes(struct playback_stream*s);
131 static void source_output_kill_cb(pa_source_output *o);
132 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
133 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
135 static void command_exit(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
136 static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
137 static void command_drain_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
138 static void command_create_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
139 static void command_delete_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
140 static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
141 static void command_set_client_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
142 static void command_lookup(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
143 static void command_stat(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
144 static void command_get_playback_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
145 static void command_get_record_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
146 static void command_create_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
147 static void command_finish_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
148 static void command_play_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
149 static void command_remove_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
150 static void command_get_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
151 static void command_get_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
152 static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
153 static void command_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
154 static void command_set_volume(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
155 static void command_cork_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
156 static void command_flush_or_trigger_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
157 static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
158 static void command_set_stream_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
159 static void command_kill(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
160 static void command_load_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
161 static void command_unload_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
162 static void command_add_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
163 static void command_remove_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
164 static void command_get_autoload_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
165 static void command_get_autoload_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
166 static void command_cork_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
167 static void command_flush_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
169 static const pa_pdispatch_callback command_table[PA_COMMAND_MAX] = {
170 [PA_COMMAND_ERROR] = NULL,
171 [PA_COMMAND_TIMEOUT] = NULL,
172 [PA_COMMAND_REPLY] = NULL,
173 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = command_create_playback_stream,
174 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = command_delete_stream,
175 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = command_drain_playback_stream,
176 [PA_COMMAND_CREATE_RECORD_STREAM] = command_create_record_stream,
177 [PA_COMMAND_DELETE_RECORD_STREAM] = command_delete_stream,
178 [PA_COMMAND_AUTH] = command_auth,
179 [PA_COMMAND_REQUEST] = NULL,
180 [PA_COMMAND_EXIT] = command_exit,
181 [PA_COMMAND_SET_CLIENT_NAME] = command_set_client_name,
182 [PA_COMMAND_LOOKUP_SINK] = command_lookup,
183 [PA_COMMAND_LOOKUP_SOURCE] = command_lookup,
184 [PA_COMMAND_STAT] = command_stat,
185 [PA_COMMAND_GET_PLAYBACK_LATENCY] = command_get_playback_latency,
186 [PA_COMMAND_GET_RECORD_LATENCY] = command_get_record_latency,
187 [PA_COMMAND_CREATE_UPLOAD_STREAM] = command_create_upload_stream,
188 [PA_COMMAND_DELETE_UPLOAD_STREAM] = command_delete_stream,
189 [PA_COMMAND_FINISH_UPLOAD_STREAM] = command_finish_upload_stream,
190 [PA_COMMAND_PLAY_SAMPLE] = command_play_sample,
191 [PA_COMMAND_REMOVE_SAMPLE] = command_remove_sample,
192 [PA_COMMAND_GET_SINK_INFO] = command_get_info,
193 [PA_COMMAND_GET_SOURCE_INFO] = command_get_info,
194 [PA_COMMAND_GET_CLIENT_INFO] = command_get_info,
195 [PA_COMMAND_GET_MODULE_INFO] = command_get_info,
196 [PA_COMMAND_GET_SINK_INPUT_INFO] = command_get_info,
197 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = command_get_info,
198 [PA_COMMAND_GET_SAMPLE_INFO] = command_get_info,
199 [PA_COMMAND_GET_SINK_INFO_LIST] = command_get_info_list,
200 [PA_COMMAND_GET_SOURCE_INFO_LIST] = command_get_info_list,
201 [PA_COMMAND_GET_MODULE_INFO_LIST] = command_get_info_list,
202 [PA_COMMAND_GET_CLIENT_INFO_LIST] = command_get_info_list,
203 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = command_get_info_list,
204 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = command_get_info_list,
205 [PA_COMMAND_GET_SAMPLE_INFO_LIST] = command_get_info_list,
206 [PA_COMMAND_GET_SERVER_INFO] = command_get_server_info,
207 [PA_COMMAND_SUBSCRIBE] = command_subscribe,
209 [PA_COMMAND_SET_SINK_VOLUME] = command_set_volume,
210 [PA_COMMAND_SET_SINK_INPUT_VOLUME] = command_set_volume,
212 [PA_COMMAND_CORK_PLAYBACK_STREAM] = command_cork_playback_stream,
213 [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = command_flush_or_trigger_playback_stream,
214 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = command_flush_or_trigger_playback_stream,
215 [PA_COMMAND_PREBUF_PLAYBACK_STREAM] = command_flush_or_trigger_playback_stream,
217 [PA_COMMAND_CORK_RECORD_STREAM] = command_cork_record_stream,
218 [PA_COMMAND_FLUSH_RECORD_STREAM] = command_flush_record_stream,
220 [PA_COMMAND_SET_DEFAULT_SINK] = command_set_default_sink_or_source,
221 [PA_COMMAND_SET_DEFAULT_SOURCE] = command_set_default_sink_or_source,
222 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME] = command_set_stream_name,
223 [PA_COMMAND_SET_RECORD_STREAM_NAME] = command_set_stream_name,
224 [PA_COMMAND_KILL_CLIENT] = command_kill,
225 [PA_COMMAND_KILL_SINK_INPUT] = command_kill,
226 [PA_COMMAND_KILL_SOURCE_OUTPUT] = command_kill,
227 [PA_COMMAND_LOAD_MODULE] = command_load_module,
228 [PA_COMMAND_UNLOAD_MODULE] = command_unload_module,
229 [PA_COMMAND_GET_AUTOLOAD_INFO] = command_get_autoload_info,
230 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST] = command_get_autoload_info_list,
231 [PA_COMMAND_ADD_AUTOLOAD] = command_add_autoload,
232 [PA_COMMAND_REMOVE_AUTOLOAD] = command_remove_autoload,
236 /* structure management */
238 static struct upload_stream* upload_stream_new(
239 struct connection *c,
240 const pa_sample_spec *ss,
241 const pa_channel_map *map,
242 const char *name, size_t length) {
244 struct upload_stream *s;
245 assert(c && ss && name && length);
247 s = pa_xmalloc(sizeof(struct upload_stream));
248 s->type = UPLOAD_STREAM;
250 s->sample_spec = *ss;
251 s->channel_map = *map;
252 s->name = pa_xstrdup(name);
254 s->memchunk.memblock = NULL;
255 s->memchunk.index = 0;
256 s->memchunk.length = 0;
260 pa_idxset_put(c->output_streams, s, &s->index);
264 static void upload_stream_free(struct upload_stream *o) {
265 assert(o && o->connection);
267 pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
271 if (o->memchunk.memblock)
272 pa_memblock_unref(o->memchunk.memblock);
277 static struct record_stream* record_stream_new(
278 struct connection *c,
280 const pa_sample_spec *ss,
281 const pa_channel_map *map,
284 size_t fragment_size) {
286 struct record_stream *s;
287 pa_source_output *source_output;
289 assert(c && source && ss && name && maxlength);
291 if (!(source_output = pa_source_output_new(source, __FILE__, name, ss, map, -1)))
294 s = pa_xmalloc(sizeof(struct record_stream));
296 s->source_output = source_output;
297 s->source_output->push = source_output_push_cb;
298 s->source_output->kill = source_output_kill_cb;
299 s->source_output->get_latency = source_output_get_latency_cb;
300 s->source_output->userdata = s;
301 s->source_output->owner = c->protocol->module;
302 s->source_output->client = c->client;
304 s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0, c->protocol->core->memblock_stat);
305 assert(s->memblockq);
307 s->fragment_size = (fragment_size/base)*base;
308 if (!s->fragment_size)
309 s->fragment_size = base;
311 pa_idxset_put(c->record_streams, s, &s->index);
315 static void record_stream_free(struct record_stream* r) {
316 assert(r && r->connection);
318 pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
319 pa_source_output_disconnect(r->source_output);
320 pa_source_output_unref(r->source_output);
321 pa_memblockq_free(r->memblockq);
325 static struct playback_stream* playback_stream_new(
326 struct connection *c,
328 const pa_sample_spec *ss,
329 const pa_channel_map *map,
335 pa_cvolume *volume) {
337 struct playback_stream *s;
338 pa_sink_input *sink_input;
339 assert(c && sink && ss && name && maxlength);
341 if (!(sink_input = pa_sink_input_new(sink, __FILE__, name, ss, map, 0, -1)))
344 s = pa_xmalloc(sizeof(struct playback_stream));
345 s->type = PLAYBACK_STREAM;
347 s->sink_input = sink_input;
349 s->sink_input->peek = sink_input_peek_cb;
350 s->sink_input->drop = sink_input_drop_cb;
351 s->sink_input->kill = sink_input_kill_cb;
352 s->sink_input->get_latency = sink_input_get_latency_cb;
353 s->sink_input->userdata = s;
354 s->sink_input->owner = c->protocol->module;
355 s->sink_input->client = c->client;
357 s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq, c->protocol->core->memblock_stat);
358 assert(s->memblockq);
360 s->requested_bytes = 0;
361 s->drain_request = 0;
363 s->sink_input->volume = *volume;
365 pa_idxset_put(c->output_streams, s, &s->index);
369 static void playback_stream_free(struct playback_stream* p) {
370 assert(p && p->connection);
372 if (p->drain_request)
373 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
375 pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
376 pa_sink_input_disconnect(p->sink_input);
377 pa_sink_input_unref(p->sink_input);
378 pa_memblockq_free(p->memblockq);
382 static void connection_free(struct connection *c) {
383 struct record_stream *r;
384 struct output_stream *o;
385 assert(c && c->protocol);
387 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
388 while ((r = pa_idxset_first(c->record_streams, NULL)))
389 record_stream_free(r);
390 pa_idxset_free(c->record_streams, NULL, NULL);
392 while ((o = pa_idxset_first(c->output_streams, NULL)))
393 if (o->type == PLAYBACK_STREAM)
394 playback_stream_free((struct playback_stream*) o);
396 upload_stream_free((struct upload_stream*) o);
397 pa_idxset_free(c->output_streams, NULL, NULL);
399 pa_pdispatch_unref(c->pdispatch);
400 pa_pstream_close(c->pstream);
401 pa_pstream_unref(c->pstream);
402 pa_client_free(c->client);
405 pa_subscription_free(c->subscription);
407 if (c->auth_timeout_event)
408 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
413 static void request_bytes(struct playback_stream *s) {
418 if (!(l = pa_memblockq_missing(s->memblockq)))
421 if (l <= s->requested_bytes)
424 l -= s->requested_bytes;
426 if (l < pa_memblockq_get_minreq(s->memblockq))
429 s->requested_bytes += l;
431 t = pa_tagstruct_new(NULL, 0);
433 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
434 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
435 pa_tagstruct_putu32(t, s->index);
436 pa_tagstruct_putu32(t, l);
437 pa_pstream_send_tagstruct(s->connection->pstream, t);
439 /* pa_log(__FILE__": Requesting %u bytes\n", l); */
442 static void send_memblock(struct connection *c) {
444 struct record_stream *r;
446 start = PA_IDXSET_INVALID;
450 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
453 if (start == PA_IDXSET_INVALID)
454 start = c->rrobin_index;
455 else if (start == c->rrobin_index)
458 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
459 pa_memchunk schunk = chunk;
461 if (schunk.length > r->fragment_size)
462 schunk.length = r->fragment_size;
464 pa_pstream_send_memblock(c->pstream, r->index, 0, &schunk);
465 pa_memblockq_drop(r->memblockq, &chunk, schunk.length);
466 pa_memblock_unref(schunk.memblock);
473 static void send_playback_stream_killed(struct playback_stream *p) {
477 t = pa_tagstruct_new(NULL, 0);
479 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
480 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
481 pa_tagstruct_putu32(t, p->index);
482 pa_pstream_send_tagstruct(p->connection->pstream, t);
485 static void send_record_stream_killed(struct record_stream *r) {
489 t = pa_tagstruct_new(NULL, 0);
491 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
492 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
493 pa_tagstruct_putu32(t, r->index);
494 pa_pstream_send_tagstruct(r->connection->pstream, t);
497 /*** sinkinput callbacks ***/
499 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
500 struct playback_stream *s;
501 assert(i && i->userdata && chunk);
504 if (pa_memblockq_peek(s->memblockq, chunk) < 0)
510 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
511 struct playback_stream *s;
512 assert(i && i->userdata && length);
515 pa_memblockq_drop(s->memblockq, chunk, length);
518 if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
519 pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
520 s->drain_request = 0;
523 /* pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq)); */
526 static void sink_input_kill_cb(pa_sink_input *i) {
527 assert(i && i->userdata);
528 send_playback_stream_killed((struct playback_stream *) i->userdata);
529 playback_stream_free((struct playback_stream *) i->userdata);
532 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
533 struct playback_stream *s;
534 assert(i && i->userdata);
537 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
539 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
542 /*** source_output callbacks ***/
544 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
545 struct record_stream *s;
546 assert(o && o->userdata && chunk);
549 pa_memblockq_push_align(s->memblockq, chunk, 0);
550 if (!pa_pstream_is_pending(s->connection->pstream))
551 send_memblock(s->connection);
554 static void source_output_kill_cb(pa_source_output *o) {
555 assert(o && o->userdata);
556 send_record_stream_killed((struct record_stream *) o->userdata);
557 record_stream_free((struct record_stream *) o->userdata);
560 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
561 struct record_stream *s;
562 assert(o && o->userdata);
565 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
567 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &o->sample_spec);
570 /*** pdispatch callbacks ***/
572 static void protocol_error(struct connection *c) {
573 pa_log(__FILE__": protocol error, kicking client\n");
577 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) {
578 struct connection *c = userdata;
579 struct playback_stream *s;
580 size_t maxlength, tlength, prebuf, minreq;
582 const char *name, *sink_name;
590 assert(c && t && c->protocol && c->protocol->core);
592 if (pa_tagstruct_get(
594 PA_TAG_STRING, &name,
595 PA_TAG_SAMPLE_SPEC, &ss,
596 PA_TAG_CHANNEL_MAP, &map,
597 PA_TAG_U32, &sink_index,
598 PA_TAG_STRING, &sink_name,
599 PA_TAG_U32, &maxlength,
600 PA_TAG_BOOLEAN, &corked,
601 PA_TAG_U32, &tlength,
604 PA_TAG_CVOLUME, &volume,
605 PA_TAG_INVALID) < 0 ||
606 !pa_tagstruct_eof(t) ||
612 if (!c->authorized) {
613 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
617 if (sink_index != (uint32_t) -1)
618 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
620 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
623 pa_log("%s: Can't find a suitable sink.\n", __FILE__);
624 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
628 if (!(s = playback_stream_new(c, sink, &ss, &map, name, maxlength, tlength, prebuf, minreq, &volume))) {
629 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
633 pa_sink_input_cork(s->sink_input, corked);
635 reply = pa_tagstruct_new(NULL, 0);
637 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
638 pa_tagstruct_putu32(reply, tag);
639 pa_tagstruct_putu32(reply, s->index);
640 assert(s->sink_input);
641 pa_tagstruct_putu32(reply, s->sink_input->index);
642 pa_tagstruct_putu32(reply, s->requested_bytes = pa_memblockq_missing(s->memblockq));
643 pa_pstream_send_tagstruct(c->pstream, reply);
647 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
648 struct connection *c = userdata;
652 if (pa_tagstruct_getu32(t, &channel) < 0 ||
653 !pa_tagstruct_eof(t)) {
658 if (!c->authorized) {
659 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
663 if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
664 struct playback_stream *s;
665 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
666 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
670 playback_stream_free(s);
671 } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
672 struct record_stream *s;
673 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
674 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
678 record_stream_free(s);
680 struct upload_stream *s;
681 assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
682 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
683 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
687 upload_stream_free(s);
690 pa_pstream_send_simple_ack(c->pstream, tag);
693 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) {
694 struct connection *c = userdata;
695 struct record_stream *s;
696 size_t maxlength, fragment_size;
697 uint32_t source_index;
698 const char *name, *source_name;
704 assert(c && t && c->protocol && c->protocol->core);
706 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
707 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
708 pa_tagstruct_get_channel_map(t, &map) < 0 ||
709 pa_tagstruct_getu32(t, &source_index) < 0 ||
710 pa_tagstruct_gets(t, &source_name) < 0 ||
711 pa_tagstruct_getu32(t, &maxlength) < 0 ||
712 pa_tagstruct_get_boolean(t, &corked) < 0 ||
713 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
714 !pa_tagstruct_eof(t)) {
719 if (!c->authorized) {
720 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
724 if (source_index != (uint32_t) -1)
725 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
727 source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1);
730 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
734 if (!(s = record_stream_new(c, source, &ss, &map, name, maxlength, fragment_size))) {
735 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
739 pa_source_output_cork(s->source_output, corked);
741 reply = pa_tagstruct_new(NULL, 0);
743 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
744 pa_tagstruct_putu32(reply, tag);
745 pa_tagstruct_putu32(reply, s->index);
746 assert(s->source_output);
747 pa_tagstruct_putu32(reply, s->source_output->index);
748 pa_pstream_send_tagstruct(c->pstream, reply);
751 static void command_exit(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
752 struct connection *c = userdata;
755 if (!pa_tagstruct_eof(t)) {
760 if (!c->authorized) {
761 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
765 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
766 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
767 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
771 static void command_auth(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
772 struct connection *c = userdata;
776 if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
777 !pa_tagstruct_eof(t)) {
782 if (!c->authorized) {
783 if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
784 pa_log(__FILE__": Denied access to client with invalid authorization key.\n");
785 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
790 if (c->auth_timeout_event) {
791 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
792 c->auth_timeout_event = NULL;
796 pa_pstream_send_simple_ack(c->pstream, tag);
800 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) {
801 struct connection *c = userdata;
805 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
806 !pa_tagstruct_eof(t)) {
811 pa_client_set_name(c->client, name);
812 pa_pstream_send_simple_ack(c->pstream, tag);
816 static void command_lookup(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
817 struct connection *c = userdata;
819 uint32_t idx = PA_IDXSET_INVALID;
822 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
823 !pa_tagstruct_eof(t)) {
828 if (!c->authorized) {
829 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
833 if (command == PA_COMMAND_LOOKUP_SINK) {
835 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
839 assert(command == PA_COMMAND_LOOKUP_SOURCE);
840 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
844 if (idx == PA_IDXSET_INVALID)
845 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
848 reply = pa_tagstruct_new(NULL, 0);
850 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
851 pa_tagstruct_putu32(reply, tag);
852 pa_tagstruct_putu32(reply, idx);
853 pa_pstream_send_tagstruct(c->pstream, reply);
857 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) {
858 struct connection *c = userdata;
860 struct playback_stream *s;
863 if (pa_tagstruct_getu32(t, &idx) < 0 ||
864 !pa_tagstruct_eof(t)) {
869 if (!c->authorized) {
870 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
874 if (!(s = pa_idxset_get_by_index(c->output_streams, idx)) || s->type != PLAYBACK_STREAM) {
875 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
879 s->drain_request = 0;
881 pa_memblockq_prebuf_disable(s->memblockq);
883 if (!pa_memblockq_is_readable(s->memblockq)) {
884 /* pa_log("immediate drain: %u\n", pa_memblockq_get_length(s->memblockq)); */
885 pa_pstream_send_simple_ack(c->pstream, tag);
887 /* pa_log("slow drain triggered\n"); */
888 s->drain_request = 1;
891 pa_sink_notify(s->sink_input->sink);
895 static void command_stat(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
896 struct connection *c = userdata;
900 if (!pa_tagstruct_eof(t)) {
905 if (!c->authorized) {
906 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
910 reply = pa_tagstruct_new(NULL, 0);
912 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
913 pa_tagstruct_putu32(reply, tag);
914 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total);
915 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total_size);
916 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated);
917 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated_size);
918 pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
919 pa_pstream_send_tagstruct(c->pstream, reply);
922 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) {
923 struct connection *c = userdata;
925 struct playback_stream *s;
926 struct timeval tv, now;
931 if (pa_tagstruct_getu32(t, &idx) < 0 ||
932 pa_tagstruct_get_timeval(t, &tv) < 0 ||
933 pa_tagstruct_getu64(t, &counter) < 0 ||
934 !pa_tagstruct_eof(t)) {
939 if (!c->authorized) {
940 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
944 if (!(s = pa_idxset_get_by_index(c->output_streams, idx)) || s->type != PLAYBACK_STREAM) {
945 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
949 reply = pa_tagstruct_new(NULL, 0);
951 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
952 pa_tagstruct_putu32(reply, tag);
953 pa_tagstruct_put_usec(reply, pa_sink_input_get_latency(s->sink_input));
954 pa_tagstruct_put_usec(reply, pa_sink_get_latency(s->sink_input->sink));
955 pa_tagstruct_put_usec(reply, 0);
956 pa_tagstruct_put_boolean(reply, pa_memblockq_is_readable(s->memblockq));
957 pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
958 pa_tagstruct_put_timeval(reply, &tv);
959 pa_gettimeofday(&now);
960 pa_tagstruct_put_timeval(reply, &now);
961 pa_tagstruct_putu64(reply, counter);
962 pa_pstream_send_tagstruct(c->pstream, reply);
965 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) {
966 struct connection *c = userdata;
968 struct record_stream *s;
969 struct timeval tv, now;
974 if (pa_tagstruct_getu32(t, &idx) < 0 ||
975 pa_tagstruct_get_timeval(t, &tv) < 0 ||
976 pa_tagstruct_getu64(t, &counter) < 0 ||
977 !pa_tagstruct_eof(t)) {
982 if (!c->authorized) {
983 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
987 if (!(s = pa_idxset_get_by_index(c->record_streams, idx))) {
988 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
992 reply = pa_tagstruct_new(NULL, 0);
994 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
995 pa_tagstruct_putu32(reply, tag);
996 pa_tagstruct_put_usec(reply, pa_source_output_get_latency(s->source_output));
997 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
998 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
999 pa_tagstruct_put_boolean(reply, 0);
1000 pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
1001 pa_tagstruct_put_timeval(reply, &tv);
1002 pa_gettimeofday(&now);
1003 pa_tagstruct_put_timeval(reply, &now);
1004 pa_tagstruct_putu64(reply, counter);
1005 pa_pstream_send_tagstruct(c->pstream, reply);
1009 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) {
1010 struct connection *c = userdata;
1011 struct upload_stream *s;
1016 pa_tagstruct *reply;
1017 assert(c && t && c->protocol && c->protocol->core);
1019 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1020 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
1021 pa_tagstruct_get_channel_map(t, &map) < 0 ||
1022 pa_tagstruct_getu32(t, &length) < 0 ||
1023 !pa_tagstruct_eof(t)) {
1028 if (!c->authorized) {
1029 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1033 if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
1034 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
1038 if (!(s = upload_stream_new(c, &ss, &map, name, length))) {
1039 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
1043 reply = pa_tagstruct_new(NULL, 0);
1045 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1046 pa_tagstruct_putu32(reply, tag);
1047 pa_tagstruct_putu32(reply, s->index);
1048 pa_tagstruct_putu32(reply, length);
1049 pa_pstream_send_tagstruct(c->pstream, reply);
1052 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) {
1053 struct connection *c = userdata;
1055 struct upload_stream *s;
1059 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1060 !pa_tagstruct_eof(t)) {
1065 if (!c->authorized) {
1066 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1070 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
1071 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
1075 pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->channel_map, &s->memchunk, &idx);
1076 pa_pstream_send_simple_ack(c->pstream, tag);
1077 upload_stream_free(s);
1080 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) {
1081 struct connection *c = userdata;
1082 uint32_t sink_index;
1085 const char *name, *sink_name;
1088 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
1089 pa_tagstruct_gets(t, &sink_name) < 0 ||
1090 pa_tagstruct_get_cvolume(t, &volume) < 0 ||
1091 pa_tagstruct_gets(t, &name) < 0 || !name ||
1092 !pa_tagstruct_eof(t)) {
1097 if (!c->authorized) {
1098 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1102 if (sink_index != (uint32_t) -1)
1103 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1105 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1108 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1112 if (pa_scache_play_item(c->protocol->core, name, sink, &volume) < 0) {
1113 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1117 pa_pstream_send_simple_ack(c->pstream, tag);
1120 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) {
1121 struct connection *c = userdata;
1125 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1126 !pa_tagstruct_eof(t)) {
1131 if (!c->authorized) {
1132 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1136 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
1137 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1141 pa_pstream_send_simple_ack(c->pstream, tag);
1144 static void sink_fill_tagstruct(pa_tagstruct *t, pa_sink *sink) {
1148 PA_TAG_U32, sink->index,
1149 PA_TAG_STRING, sink->name,
1150 PA_TAG_STRING, sink->description,
1151 PA_TAG_SAMPLE_SPEC, &sink->sample_spec,
1152 PA_TAG_CHANNEL_MAP, &sink->channel_map,
1153 PA_TAG_U32, sink->owner ? sink->owner->index : PA_INVALID_INDEX,
1154 PA_TAG_CVOLUME, pa_sink_get_volume(sink, PA_MIXER_HARDWARE),
1155 PA_TAG_U32, sink->monitor_source->index,
1156 PA_TAG_STRING, sink->monitor_source->name,
1157 PA_TAG_USEC, pa_sink_get_latency(sink),
1158 PA_TAG_STRING, sink->driver,
1162 static void source_fill_tagstruct(pa_tagstruct *t, pa_source *source) {
1163 assert(t && source);
1164 pa_tagstruct_putu32(t, source->index);
1165 pa_tagstruct_puts(t, source->name);
1166 pa_tagstruct_puts(t, source->description);
1167 pa_tagstruct_put_sample_spec(t, &source->sample_spec);
1168 pa_tagstruct_put_channel_map(t, &source->channel_map);
1169 pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
1170 pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
1171 pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : NULL);
1172 pa_tagstruct_put_usec(t, pa_source_get_latency(source));
1173 pa_tagstruct_puts(t, source->driver);
1176 static void client_fill_tagstruct(pa_tagstruct *t, pa_client *client) {
1177 assert(t && client);
1178 pa_tagstruct_putu32(t, client->index);
1179 pa_tagstruct_puts(t, client->name);
1180 pa_tagstruct_putu32(t, client->owner ? client->owner->index : (uint32_t) -1);
1181 pa_tagstruct_puts(t, client->driver);
1184 static void module_fill_tagstruct(pa_tagstruct *t, pa_module *module) {
1185 assert(t && module);
1186 pa_tagstruct_putu32(t, module->index);
1187 pa_tagstruct_puts(t, module->name);
1188 pa_tagstruct_puts(t, module->argument);
1189 pa_tagstruct_putu32(t, module->n_used);
1190 pa_tagstruct_put_boolean(t, module->auto_unload);
1193 static void sink_input_fill_tagstruct(pa_tagstruct *t, pa_sink_input *s) {
1195 pa_tagstruct_putu32(t, s->index);
1196 pa_tagstruct_puts(t, s->name);
1197 pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1198 pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1199 pa_tagstruct_putu32(t, s->sink->index);
1200 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1201 pa_tagstruct_put_channel_map(t, &s->channel_map);
1202 pa_tagstruct_put_cvolume(t, &s->volume);
1203 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
1204 pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
1205 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
1206 pa_tagstruct_puts(t, s->driver);
1209 static void source_output_fill_tagstruct(pa_tagstruct *t, pa_source_output *s) {
1211 pa_tagstruct_putu32(t, s->index);
1212 pa_tagstruct_puts(t, s->name);
1213 pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1214 pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1215 pa_tagstruct_putu32(t, s->source->index);
1216 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1217 pa_tagstruct_put_channel_map(t, &s->channel_map);
1218 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
1219 pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
1220 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
1221 pa_tagstruct_puts(t, s->driver);
1224 static void scache_fill_tagstruct(pa_tagstruct *t, pa_scache_entry *e) {
1226 pa_tagstruct_putu32(t, e->index);
1227 pa_tagstruct_puts(t, e->name);
1228 pa_tagstruct_put_cvolume(t, &e->volume);
1229 pa_tagstruct_put_usec(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1230 pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1231 pa_tagstruct_put_channel_map(t, &e->channel_map);
1232 pa_tagstruct_putu32(t, e->memchunk.length);
1233 pa_tagstruct_put_boolean(t, e->lazy);
1234 pa_tagstruct_puts(t, e->filename);
1237 static void command_get_info(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1238 struct connection *c = userdata;
1240 pa_sink *sink = NULL;
1241 pa_source *source = NULL;
1242 pa_client *client = NULL;
1243 pa_module *module = NULL;
1244 pa_sink_input *si = NULL;
1245 pa_source_output *so = NULL;
1246 pa_scache_entry *sce = NULL;
1248 pa_tagstruct *reply;
1252 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1253 (command != PA_COMMAND_GET_CLIENT_INFO &&
1254 command != PA_COMMAND_GET_MODULE_INFO &&
1255 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1256 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1257 pa_tagstruct_gets(t, &name) < 0) ||
1258 !pa_tagstruct_eof(t)) {
1263 if (!c->authorized) {
1264 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1268 if (command == PA_COMMAND_GET_SINK_INFO) {
1269 if (idx != (uint32_t) -1)
1270 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1272 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1273 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1274 if (idx != (uint32_t) -1)
1275 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1277 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1278 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1279 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
1280 else if (command == PA_COMMAND_GET_MODULE_INFO)
1281 module = pa_idxset_get_by_index(c->protocol->core->modules, idx);
1282 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1283 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1284 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1285 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
1287 assert(command == PA_COMMAND_GET_SAMPLE_INFO);
1288 if (idx != (uint32_t) -1)
1289 sce = pa_idxset_get_by_index(c->protocol->core->scache, idx);
1291 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1294 if (!sink && !source && !client && !module && !si && !so && !sce) {
1295 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1299 reply = pa_tagstruct_new(NULL, 0);
1301 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1302 pa_tagstruct_putu32(reply, tag);
1304 sink_fill_tagstruct(reply, sink);
1306 source_fill_tagstruct(reply, source);
1308 client_fill_tagstruct(reply, client);
1310 module_fill_tagstruct(reply, module);
1312 sink_input_fill_tagstruct(reply, si);
1314 source_output_fill_tagstruct(reply, so);
1316 scache_fill_tagstruct(reply, sce);
1317 pa_pstream_send_tagstruct(c->pstream, reply);
1320 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1321 struct connection *c = userdata;
1325 pa_tagstruct *reply;
1328 if (!pa_tagstruct_eof(t)) {
1333 if (!c->authorized) {
1334 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1338 reply = pa_tagstruct_new(NULL, 0);
1340 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1341 pa_tagstruct_putu32(reply, tag);
1343 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1344 i = c->protocol->core->sinks;
1345 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1346 i = c->protocol->core->sources;
1347 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1348 i = c->protocol->core->clients;
1349 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1350 i = c->protocol->core->modules;
1351 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1352 i = c->protocol->core->sink_inputs;
1353 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1354 i = c->protocol->core->source_outputs;
1356 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1357 i = c->protocol->core->scache;
1361 for (p = pa_idxset_first(i, &idx); p; p = pa_idxset_next(i, &idx)) {
1362 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1363 sink_fill_tagstruct(reply, p);
1364 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1365 source_fill_tagstruct(reply, p);
1366 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1367 client_fill_tagstruct(reply, p);
1368 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1369 module_fill_tagstruct(reply, p);
1370 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1371 sink_input_fill_tagstruct(reply, p);
1372 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1373 source_output_fill_tagstruct(reply, p);
1375 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1376 scache_fill_tagstruct(reply, p);
1381 pa_pstream_send_tagstruct(c->pstream, reply);
1384 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) {
1385 struct connection *c = userdata;
1386 pa_tagstruct *reply;
1391 if (!pa_tagstruct_eof(t)) {
1396 if (!c->authorized) {
1397 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1401 reply = pa_tagstruct_new(NULL, 0);
1403 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1404 pa_tagstruct_putu32(reply, tag);
1405 pa_tagstruct_puts(reply, PACKAGE_NAME);
1406 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1407 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1408 pa_tagstruct_puts(reply, pa_get_fqdn(txt, sizeof(txt)));
1409 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1411 n = pa_namereg_get_default_sink_name(c->protocol->core);
1412 pa_tagstruct_puts(reply, n);
1413 n = pa_namereg_get_default_source_name(c->protocol->core);
1414 pa_tagstruct_puts(reply, n);
1416 pa_tagstruct_putu32(reply, c->protocol->core->cookie);
1418 pa_pstream_send_tagstruct(c->pstream, reply);
1421 static void subscription_cb(pa_core *core, pa_subscription_event_type_t e, uint32_t idx, void *userdata) {
1423 struct connection *c = userdata;
1426 t = pa_tagstruct_new(NULL, 0);
1428 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1429 pa_tagstruct_putu32(t, (uint32_t) -1);
1430 pa_tagstruct_putu32(t, e);
1431 pa_tagstruct_putu32(t, idx);
1432 pa_pstream_send_tagstruct(c->pstream, t);
1435 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1436 struct connection *c = userdata;
1437 pa_subscription_mask_t m;
1440 if (pa_tagstruct_getu32(t, &m) < 0 ||
1441 !pa_tagstruct_eof(t)) {
1446 if (!c->authorized) {
1447 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1451 if (c->subscription)
1452 pa_subscription_free(c->subscription);
1455 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1456 assert(c->subscription);
1458 c->subscription = NULL;
1460 pa_pstream_send_simple_ack(c->pstream, tag);
1463 static void command_set_volume(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1464 struct connection *c = userdata;
1467 pa_sink *sink = NULL;
1468 pa_sink_input *si = NULL;
1469 const char *name = NULL;
1472 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1473 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1474 pa_tagstruct_get_cvolume(t, &volume) ||
1475 !pa_tagstruct_eof(t)) {
1480 if (!c->authorized) {
1481 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1485 if (command == PA_COMMAND_SET_SINK_VOLUME) {
1486 if (idx != (uint32_t) -1)
1487 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1489 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1491 assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1492 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1496 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1501 pa_sink_set_volume(sink, PA_MIXER_HARDWARE, &volume);
1503 pa_sink_input_set_volume(si, &volume);
1505 pa_pstream_send_simple_ack(c->pstream, tag);
1508 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) {
1509 struct connection *c = userdata;
1512 struct playback_stream *s;
1515 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1516 pa_tagstruct_get_boolean(t, &b) < 0 ||
1517 !pa_tagstruct_eof(t)) {
1522 if (!c->authorized) {
1523 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1527 if (!(s = pa_idxset_get_by_index(c->output_streams, idx)) || s->type != PLAYBACK_STREAM) {
1528 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1532 pa_sink_input_cork(s->sink_input, b);
1533 pa_pstream_send_simple_ack(c->pstream, tag);
1536 static void command_flush_or_trigger_playback_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1537 struct connection *c = userdata;
1539 struct playback_stream *s;
1542 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1543 !pa_tagstruct_eof(t)) {
1548 if (!c->authorized) {
1549 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1553 if (!(s = pa_idxset_get_by_index(c->output_streams, idx)) || s->type != PLAYBACK_STREAM) {
1554 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1558 if (command == PA_COMMAND_PREBUF_PLAYBACK_STREAM)
1559 pa_memblockq_prebuf_reenable(s->memblockq);
1560 else if (command == PA_COMMAND_TRIGGER_PLAYBACK_STREAM)
1561 pa_memblockq_prebuf_disable(s->memblockq);
1563 assert(command == PA_COMMAND_FLUSH_PLAYBACK_STREAM);
1564 pa_memblockq_flush(s->memblockq);
1565 /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1568 pa_sink_notify(s->sink_input->sink);
1569 pa_pstream_send_simple_ack(c->pstream, tag);
1573 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) {
1574 struct connection *c = userdata;
1576 struct record_stream *s;
1580 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1581 pa_tagstruct_get_boolean(t, &b) < 0 ||
1582 !pa_tagstruct_eof(t)) {
1587 if (!c->authorized) {
1588 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1592 if (!(s = pa_idxset_get_by_index(c->record_streams, idx))) {
1593 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1597 pa_source_output_cork(s->source_output, b);
1598 pa_pstream_send_simple_ack(c->pstream, tag);
1601 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) {
1602 struct connection *c = userdata;
1604 struct record_stream *s;
1607 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1608 !pa_tagstruct_eof(t)) {
1613 if (!c->authorized) {
1614 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1618 if (!(s = pa_idxset_get_by_index(c->record_streams, idx))) {
1619 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1623 pa_memblockq_flush(s->memblockq);
1624 pa_pstream_send_simple_ack(c->pstream, tag);
1627 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) {
1628 struct connection *c = userdata;
1633 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1634 pa_tagstruct_gets(t, &s) < 0 || !s ||
1635 !pa_tagstruct_eof(t)) {
1640 if (!c->authorized) {
1641 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1645 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
1646 pa_pstream_send_simple_ack(c->pstream, tag);
1649 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1650 struct connection *c = userdata;
1655 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1656 pa_tagstruct_gets(t, &name) < 0 || !name ||
1657 !pa_tagstruct_eof(t)) {
1662 if (!c->authorized) {
1663 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1667 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
1668 struct playback_stream *s;
1670 if (!(s = pa_idxset_get_by_index(c->output_streams, idx)) || s->type != PLAYBACK_STREAM) {
1671 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1675 pa_sink_input_set_name(s->sink_input, name);
1678 struct record_stream *s;
1680 if (!(s = pa_idxset_get_by_index(c->record_streams, idx))) {
1681 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1685 pa_source_output_set_name(s->source_output, name);
1688 pa_pstream_send_simple_ack(c->pstream, tag);
1691 static void command_kill(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1692 struct connection *c = userdata;
1696 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1697 !pa_tagstruct_eof(t)) {
1702 if (!c->authorized) {
1703 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1707 if (command == PA_COMMAND_KILL_CLIENT) {
1710 if (!(client = pa_idxset_get_by_index(c->protocol->core->clients, idx))) {
1711 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1715 pa_client_kill(client);
1716 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
1719 if (!(s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx))) {
1720 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1724 pa_sink_input_kill(s);
1726 pa_source_output *s;
1728 assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
1730 if (!(s = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx))) {
1731 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1735 pa_source_output_kill(s);
1738 pa_pstream_send_simple_ack(c->pstream, tag);
1741 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) {
1742 struct connection *c = userdata;
1744 const char *name, *argument;
1745 pa_tagstruct *reply;
1748 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1749 pa_tagstruct_gets(t, &argument) < 0 ||
1750 !pa_tagstruct_eof(t)) {
1755 if (!c->authorized) {
1756 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1760 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
1761 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INITFAILED);
1765 reply = pa_tagstruct_new(NULL, 0);
1766 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1767 pa_tagstruct_putu32(reply, tag);
1768 pa_tagstruct_putu32(reply, m->index);
1769 pa_pstream_send_tagstruct(c->pstream, reply);
1772 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) {
1773 struct connection *c = userdata;
1778 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1779 !pa_tagstruct_eof(t)) {
1784 if (!c->authorized) {
1785 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1789 if (!(m = pa_idxset_get_by_index(c->protocol->core->modules, idx))) {
1790 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1794 pa_module_unload_request(m);
1795 pa_pstream_send_simple_ack(c->pstream, tag);
1798 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) {
1799 struct connection *c = userdata;
1800 const char *name, *module, *argument;
1803 pa_tagstruct *reply;
1806 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1807 pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1808 pa_tagstruct_gets(t, &module) < 0 || !module ||
1809 pa_tagstruct_gets(t, &argument) < 0 ||
1810 !pa_tagstruct_eof(t)) {
1815 if (!c->authorized) {
1816 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1820 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument, &idx) < 0) {
1821 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
1825 reply = pa_tagstruct_new(NULL, 0);
1826 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1827 pa_tagstruct_putu32(reply, tag);
1828 pa_tagstruct_putu32(reply, idx);
1829 pa_pstream_send_tagstruct(c->pstream, reply);
1832 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) {
1833 struct connection *c = userdata;
1834 const char *name = NULL;
1835 uint32_t type, idx = PA_IDXSET_INVALID;
1839 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
1840 (pa_tagstruct_gets(t, &name) < 0 ||
1841 pa_tagstruct_getu32(t, &type) < 0)) ||
1842 (!name && idx == PA_IDXSET_INVALID) ||
1843 (name && type > 1) ||
1844 !pa_tagstruct_eof(t)) {
1849 if (!c->authorized) {
1850 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1855 r = pa_autoload_remove_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
1857 r = pa_autoload_remove_by_index(c->protocol->core, idx);
1860 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1864 pa_pstream_send_simple_ack(c->pstream, tag);
1867 static void autoload_fill_tagstruct(pa_tagstruct *t, const pa_autoload_entry *e) {
1870 pa_tagstruct_putu32(t, e->index);
1871 pa_tagstruct_puts(t, e->name);
1872 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
1873 pa_tagstruct_puts(t, e->module);
1874 pa_tagstruct_puts(t, e->argument);
1877 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) {
1878 struct connection *c = userdata;
1879 const pa_autoload_entry *a = NULL;
1882 pa_tagstruct *reply;
1885 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
1886 (pa_tagstruct_gets(t, &name) < 0 ||
1887 pa_tagstruct_getu32(t, &type) < 0)) ||
1888 (!name && idx == PA_IDXSET_INVALID) ||
1889 (name && type > 1) ||
1890 !pa_tagstruct_eof(t)) {
1895 if (!c->authorized) {
1896 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1902 a = pa_autoload_get_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
1904 a = pa_autoload_get_by_index(c->protocol->core, idx);
1907 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1911 reply = pa_tagstruct_new(NULL, 0);
1913 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1914 pa_tagstruct_putu32(reply, tag);
1915 autoload_fill_tagstruct(reply, a);
1916 pa_pstream_send_tagstruct(c->pstream, reply);
1919 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) {
1920 struct connection *c = userdata;
1921 pa_tagstruct *reply;
1924 if (!pa_tagstruct_eof(t)) {
1929 if (!c->authorized) {
1930 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1934 reply = pa_tagstruct_new(NULL, 0);
1935 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1936 pa_tagstruct_putu32(reply, tag);
1938 if (c->protocol->core->autoload_hashmap) {
1939 pa_autoload_entry *a;
1942 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state, NULL)))
1943 autoload_fill_tagstruct(reply, a);
1946 pa_pstream_send_tagstruct(c->pstream, reply);
1949 /*** pstream callbacks ***/
1951 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, void *userdata) {
1952 struct connection *c = userdata;
1953 assert(p && packet && packet->data && c);
1955 if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1956 pa_log(__FILE__": invalid packet.\n");
1961 static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, uint32_t delta, const pa_memchunk *chunk, void *userdata) {
1962 struct connection *c = userdata;
1963 struct output_stream *stream;
1964 assert(p && chunk && userdata);
1966 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1967 pa_log(__FILE__": client sent block for invalid stream.\n");
1972 if (stream->type == PLAYBACK_STREAM) {
1973 struct playback_stream *ps = (struct playback_stream*) stream;
1974 if (chunk->length >= ps->requested_bytes)
1975 ps->requested_bytes = 0;
1977 ps->requested_bytes -= chunk->length;
1979 pa_memblockq_push_align(ps->memblockq, chunk, delta);
1980 assert(ps->sink_input);
1981 /* pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq)); */
1983 pa_sink_notify(ps->sink_input->sink);
1984 /* pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1987 struct upload_stream *u = (struct upload_stream*) stream;
1989 assert(u->type == UPLOAD_STREAM);
1991 if (!u->memchunk.memblock) {
1992 if (u->length == chunk->length) {
1993 u->memchunk = *chunk;
1994 pa_memblock_ref(u->memchunk.memblock);
1997 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
1998 u->memchunk.index = u->memchunk.length = 0;
2002 assert(u->memchunk.memblock);
2005 if (l > chunk->length)
2009 memcpy((uint8_t*) u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length,
2010 (uint8_t*) chunk->memblock->data+chunk->index, l);
2011 u->memchunk.length += l;
2017 static void pstream_die_callback(pa_pstream *p, void *userdata) {
2018 struct connection *c = userdata;
2022 /* pa_log(__FILE__": connection died.\n");*/
2026 static void pstream_drain_callback(pa_pstream *p, void *userdata) {
2027 struct connection *c = userdata;
2033 /*** client callbacks ***/
2035 static void client_kill_cb(pa_client *c) {
2036 assert(c && c->userdata);
2037 connection_free(c->userdata);
2040 /*** socket server callbacks ***/
2042 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
2043 struct connection *c = userdata;
2044 assert(m && tv && c && c->auth_timeout_event == e);
2050 static void on_connection(PA_GCC_UNUSED pa_socket_server*s, pa_iochannel *io, void *userdata) {
2051 pa_protocol_native *p = userdata;
2052 struct connection *c;
2055 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
2056 pa_log_warn(__FILE__": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS);
2057 pa_iochannel_free(io);
2061 c = pa_xmalloc(sizeof(struct connection));
2063 c->authorized =!! p->public;
2065 if (!c->authorized) {
2067 pa_gettimeofday(&tv);
2068 tv.tv_sec += AUTH_TIMEOUT;
2069 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
2071 c->auth_timeout_event = NULL;
2075 c->client = pa_client_new(p->core, __FILE__, "Client");
2077 c->client->kill = client_kill_cb;
2078 c->client->userdata = c;
2079 c->client->owner = p->module;
2081 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
2084 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
2085 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
2086 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
2087 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
2089 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
2090 assert(c->pdispatch);
2092 c->record_streams = pa_idxset_new(NULL, NULL);
2093 c->output_streams = pa_idxset_new(NULL, NULL);
2094 assert(c->record_streams && c->output_streams);
2096 c->rrobin_index = PA_IDXSET_INVALID;
2097 c->subscription = NULL;
2099 pa_idxset_put(p->connections, c, NULL);
2102 /*** module entry points ***/
2104 static int load_key(pa_protocol_native*p, const char*fn) {
2107 p->auth_cookie_in_property = 0;
2109 if (!fn && pa_authkey_prop_get(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0) {
2110 pa_log_info(__FILE__": using already loaded auth cookie.\n");
2111 pa_authkey_prop_ref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2112 p->auth_cookie_in_property = 1;
2117 fn = PA_NATIVE_COOKIE_FILE;
2119 if (pa_authkey_load_auto(fn, p->auth_cookie, sizeof(p->auth_cookie)) < 0)
2122 pa_log_info(__FILE__": loading cookie from disk.\n");
2124 if (pa_authkey_prop_put(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0)
2125 p->auth_cookie_in_property = 1;
2130 static pa_protocol_native* protocol_new_internal(pa_core *c, pa_module *m, pa_modargs *ma) {
2131 pa_protocol_native *p;
2135 if (pa_modargs_get_value_boolean(ma, "public", &public) < 0) {
2136 pa_log(__FILE__": public= expects a boolean argument.\n");
2140 p = pa_xmalloc(sizeof(pa_protocol_native));
2146 if (load_key(p, pa_modargs_get_value(ma, "cookie", NULL)) < 0) {
2151 p->connections = pa_idxset_new(NULL, NULL);
2152 assert(p->connections);
2157 pa_protocol_native* pa_protocol_native_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
2159 pa_protocol_native *p;
2161 if (!(p = protocol_new_internal(core, m, ma)))
2165 pa_socket_server_set_callback(p->server, on_connection, p);
2167 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
2169 l = pa_property_get(core, PA_NATIVE_SERVER_PROPERTY_NAME);
2170 l = pa_strlist_prepend(l, t);
2171 pa_property_replace(core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
2177 void pa_protocol_native_free(pa_protocol_native *p) {
2178 struct connection *c;
2181 while ((c = pa_idxset_first(p->connections, NULL)))
2183 pa_idxset_free(p->connections, NULL, NULL);
2188 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
2190 l = pa_property_get(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
2191 l = pa_strlist_remove(l, t);
2194 pa_property_replace(p->core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
2196 pa_property_remove(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
2199 pa_socket_server_unref(p->server);
2202 if (p->auth_cookie_in_property)
2203 pa_authkey_prop_unref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2208 pa_protocol_native* pa_protocol_native_new_iochannel(pa_core*core, pa_iochannel *io, pa_module *m, pa_modargs *ma) {
2209 pa_protocol_native *p;
2211 if (!(p = protocol_new_internal(core, m, ma)))
2214 on_connection(NULL, io, p);