]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-native.c
* add new function pa_check_in_group()
[pulseaudio] / src / pulsecore / protocol-native.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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.
10
11 PulseAudio 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.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31
32 #include <pulse/timeval.h>
33 #include <pulse/version.h>
34 #include <pulse/utf8.h>
35 #include <pulse/util.h>
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/native-common.h>
39 #include <pulsecore/packet.h>
40 #include <pulsecore/client.h>
41 #include <pulsecore/source-output.h>
42 #include <pulsecore/sink-input.h>
43 #include <pulsecore/pstream.h>
44 #include <pulsecore/tagstruct.h>
45 #include <pulsecore/pdispatch.h>
46 #include <pulsecore/pstream-util.h>
47 #include <pulsecore/authkey.h>
48 #include <pulsecore/namereg.h>
49 #include <pulsecore/core-scache.h>
50 #include <pulsecore/core-subscribe.h>
51 #include <pulsecore/log.h>
52 #include <pulsecore/autoload.h>
53 #include <pulsecore/authkey-prop.h>
54 #include <pulsecore/strlist.h>
55 #include <pulsecore/props.h>
56 #include <pulsecore/sample-util.h>
57 #include <pulsecore/llist.h>
58 #include <pulsecore/creds.h>
59 #include <pulsecore/core-util.h>
60
61 #include "protocol-native.h"
62
63 /* Kick a client if it doesn't authenticate within this time */
64 #define AUTH_TIMEOUT 60
65
66 /* Don't accept more connection than this */
67 #define MAX_CONNECTIONS 64
68
69 #define MAX_MEMBLOCKQ_LENGTH (4*1024*1024) /* 4MB */
70
71 struct connection;
72 struct pa_protocol_native;
73
74 struct record_stream {
75 struct connection *connection;
76 uint32_t index;
77 pa_source_output *source_output;
78 pa_memblockq *memblockq;
79 size_t fragment_size;
80 };
81
82 struct playback_stream {
83 int type;
84 struct connection *connection;
85 uint32_t index;
86 pa_sink_input *sink_input;
87 pa_memblockq *memblockq;
88 size_t requested_bytes;
89 int drain_request;
90 uint32_t drain_tag;
91 uint32_t syncid;
92 int underrun;
93
94 /* Sync group members */
95 PA_LLIST_FIELDS(struct playback_stream);
96 };
97
98 struct upload_stream {
99 int type;
100 struct connection *connection;
101 uint32_t index;
102 pa_memchunk memchunk;
103 size_t length;
104 char *name;
105 pa_sample_spec sample_spec;
106 pa_channel_map channel_map;
107 };
108
109 struct output_stream {
110 int type;
111 };
112
113 enum {
114 UPLOAD_STREAM,
115 PLAYBACK_STREAM
116 };
117
118 struct connection {
119 int authorized;
120 uint32_t version;
121 pa_protocol_native *protocol;
122 pa_client *client;
123 pa_pstream *pstream;
124 pa_pdispatch *pdispatch;
125 pa_idxset *record_streams, *output_streams;
126 uint32_t rrobin_index;
127 pa_subscription *subscription;
128 pa_time_event *auth_timeout_event;
129 };
130
131 struct pa_protocol_native {
132 pa_module *module;
133 int public;
134 pa_core *core;
135 pa_socket_server *server;
136 pa_idxset *connections;
137 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
138 int auth_cookie_in_property;
139 #ifdef HAVE_CREDS
140 char *auth_group;
141 #endif
142 };
143
144 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk);
145 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length);
146 static void sink_input_kill_cb(pa_sink_input *i);
147 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i);
148
149 static void request_bytes(struct playback_stream*s);
150
151 static void source_output_kill_cb(pa_source_output *o);
152 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
153 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
154
155 static void command_exit(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
156 static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
157 static void command_drain_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
158 static void command_create_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
159 static void command_delete_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
160 static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
161 static void command_set_client_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
162 static void command_lookup(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
163 static void command_stat(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
164 static void command_get_playback_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
165 static void command_get_record_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
166 static void command_create_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
167 static void command_finish_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
168 static void command_play_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
169 static void command_remove_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
170 static void command_get_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
171 static void command_get_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
172 static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
173 static void command_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
174 static void command_set_volume(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
175 static void command_set_mute(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
176 static void command_cork_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
177 static void command_flush_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
178 static void command_trigger_or_prebuf_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
179 static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
180 static void command_set_stream_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
181 static void command_kill(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
182 static void command_load_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
183 static void command_unload_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
184 static void command_add_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
185 static void command_remove_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
186 static void command_get_autoload_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
187 static void command_get_autoload_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
188 static void command_cork_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
189 static void command_flush_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
190
191 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
192 [PA_COMMAND_ERROR] = NULL,
193 [PA_COMMAND_TIMEOUT] = NULL,
194 [PA_COMMAND_REPLY] = NULL,
195 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = command_create_playback_stream,
196 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = command_delete_stream,
197 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = command_drain_playback_stream,
198 [PA_COMMAND_CREATE_RECORD_STREAM] = command_create_record_stream,
199 [PA_COMMAND_DELETE_RECORD_STREAM] = command_delete_stream,
200 [PA_COMMAND_AUTH] = command_auth,
201 [PA_COMMAND_REQUEST] = NULL,
202 [PA_COMMAND_EXIT] = command_exit,
203 [PA_COMMAND_SET_CLIENT_NAME] = command_set_client_name,
204 [PA_COMMAND_LOOKUP_SINK] = command_lookup,
205 [PA_COMMAND_LOOKUP_SOURCE] = command_lookup,
206 [PA_COMMAND_STAT] = command_stat,
207 [PA_COMMAND_GET_PLAYBACK_LATENCY] = command_get_playback_latency,
208 [PA_COMMAND_GET_RECORD_LATENCY] = command_get_record_latency,
209 [PA_COMMAND_CREATE_UPLOAD_STREAM] = command_create_upload_stream,
210 [PA_COMMAND_DELETE_UPLOAD_STREAM] = command_delete_stream,
211 [PA_COMMAND_FINISH_UPLOAD_STREAM] = command_finish_upload_stream,
212 [PA_COMMAND_PLAY_SAMPLE] = command_play_sample,
213 [PA_COMMAND_REMOVE_SAMPLE] = command_remove_sample,
214 [PA_COMMAND_GET_SINK_INFO] = command_get_info,
215 [PA_COMMAND_GET_SOURCE_INFO] = command_get_info,
216 [PA_COMMAND_GET_CLIENT_INFO] = command_get_info,
217 [PA_COMMAND_GET_MODULE_INFO] = command_get_info,
218 [PA_COMMAND_GET_SINK_INPUT_INFO] = command_get_info,
219 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = command_get_info,
220 [PA_COMMAND_GET_SAMPLE_INFO] = command_get_info,
221 [PA_COMMAND_GET_SINK_INFO_LIST] = command_get_info_list,
222 [PA_COMMAND_GET_SOURCE_INFO_LIST] = command_get_info_list,
223 [PA_COMMAND_GET_MODULE_INFO_LIST] = command_get_info_list,
224 [PA_COMMAND_GET_CLIENT_INFO_LIST] = command_get_info_list,
225 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = command_get_info_list,
226 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = command_get_info_list,
227 [PA_COMMAND_GET_SAMPLE_INFO_LIST] = command_get_info_list,
228 [PA_COMMAND_GET_SERVER_INFO] = command_get_server_info,
229 [PA_COMMAND_SUBSCRIBE] = command_subscribe,
230
231 [PA_COMMAND_SET_SINK_VOLUME] = command_set_volume,
232 [PA_COMMAND_SET_SINK_INPUT_VOLUME] = command_set_volume,
233 [PA_COMMAND_SET_SOURCE_VOLUME] = command_set_volume,
234
235 [PA_COMMAND_SET_SINK_MUTE] = command_set_mute,
236 [PA_COMMAND_SET_SOURCE_MUTE] = command_set_mute,
237
238 [PA_COMMAND_CORK_PLAYBACK_STREAM] = command_cork_playback_stream,
239 [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = command_flush_playback_stream,
240 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = command_trigger_or_prebuf_playback_stream,
241 [PA_COMMAND_PREBUF_PLAYBACK_STREAM] = command_trigger_or_prebuf_playback_stream,
242
243 [PA_COMMAND_CORK_RECORD_STREAM] = command_cork_record_stream,
244 [PA_COMMAND_FLUSH_RECORD_STREAM] = command_flush_record_stream,
245
246 [PA_COMMAND_SET_DEFAULT_SINK] = command_set_default_sink_or_source,
247 [PA_COMMAND_SET_DEFAULT_SOURCE] = command_set_default_sink_or_source,
248 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME] = command_set_stream_name,
249 [PA_COMMAND_SET_RECORD_STREAM_NAME] = command_set_stream_name,
250 [PA_COMMAND_KILL_CLIENT] = command_kill,
251 [PA_COMMAND_KILL_SINK_INPUT] = command_kill,
252 [PA_COMMAND_KILL_SOURCE_OUTPUT] = command_kill,
253 [PA_COMMAND_LOAD_MODULE] = command_load_module,
254 [PA_COMMAND_UNLOAD_MODULE] = command_unload_module,
255 [PA_COMMAND_GET_AUTOLOAD_INFO] = command_get_autoload_info,
256 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST] = command_get_autoload_info_list,
257 [PA_COMMAND_ADD_AUTOLOAD] = command_add_autoload,
258 [PA_COMMAND_REMOVE_AUTOLOAD] = command_remove_autoload
259 };
260
261 /* structure management */
262
263 static struct upload_stream* upload_stream_new(
264 struct connection *c,
265 const pa_sample_spec *ss,
266 const pa_channel_map *map,
267 const char *name, size_t length) {
268
269 struct upload_stream *s;
270 assert(c && ss && name && length);
271
272 s = pa_xnew(struct upload_stream, 1);
273 s->type = UPLOAD_STREAM;
274 s->connection = c;
275 s->sample_spec = *ss;
276 s->channel_map = *map;
277 s->name = pa_xstrdup(name);
278
279 s->memchunk.memblock = NULL;
280 s->memchunk.index = 0;
281 s->memchunk.length = 0;
282
283 s->length = length;
284
285 pa_idxset_put(c->output_streams, s, &s->index);
286 return s;
287 }
288
289 static void upload_stream_free(struct upload_stream *o) {
290 assert(o && o->connection);
291
292 pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
293
294 pa_xfree(o->name);
295
296 if (o->memchunk.memblock)
297 pa_memblock_unref(o->memchunk.memblock);
298
299 pa_xfree(o);
300 }
301
302 static struct record_stream* record_stream_new(
303 struct connection *c,
304 pa_source *source,
305 const pa_sample_spec *ss,
306 const pa_channel_map *map,
307 const char *name,
308 size_t maxlength,
309 size_t fragment_size) {
310
311 struct record_stream *s;
312 pa_source_output *source_output;
313 size_t base;
314 assert(c && source && ss && name && maxlength);
315
316 if (!(source_output = pa_source_output_new(source, __FILE__, name, ss, map, -1)))
317 return NULL;
318
319 s = pa_xnew(struct record_stream, 1);
320 s->connection = c;
321 s->source_output = source_output;
322 s->source_output->push = source_output_push_cb;
323 s->source_output->kill = source_output_kill_cb;
324 s->source_output->get_latency = source_output_get_latency_cb;
325 s->source_output->userdata = s;
326 s->source_output->owner = c->protocol->module;
327 s->source_output->client = c->client;
328
329 s->memblockq = pa_memblockq_new(
330 0,
331 maxlength,
332 0,
333 base = pa_frame_size(ss),
334 1,
335 0,
336 NULL,
337 c->protocol->core->memblock_stat);
338 assert(s->memblockq);
339
340 s->fragment_size = (fragment_size/base)*base;
341 if (!s->fragment_size)
342 s->fragment_size = base;
343
344 pa_idxset_put(c->record_streams, s, &s->index);
345 return s;
346 }
347
348 static void record_stream_free(struct record_stream* r) {
349 assert(r && r->connection);
350
351 pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
352 pa_source_output_disconnect(r->source_output);
353 pa_source_output_unref(r->source_output);
354 pa_memblockq_free(r->memblockq);
355 pa_xfree(r);
356 }
357
358 static struct playback_stream* playback_stream_new(
359 struct connection *c,
360 pa_sink *sink,
361 const pa_sample_spec *ss,
362 const pa_channel_map *map,
363 const char *name,
364 size_t maxlength,
365 size_t tlength,
366 size_t prebuf,
367 size_t minreq,
368 pa_cvolume *volume,
369 uint32_t syncid) {
370
371 struct playback_stream *s, *ssync;
372 pa_sink_input *sink_input;
373 pa_memblock *silence;
374 uint32_t idx;
375 int64_t start_index;
376
377 assert(c && sink && ss && name && maxlength);
378
379 /* Find syncid group */
380 for (ssync = pa_idxset_first(c->output_streams, &idx); ssync; ssync = pa_idxset_next(c->output_streams, &idx)) {
381
382 if (ssync->type != PLAYBACK_STREAM)
383 continue;
384
385 if (ssync->syncid == syncid)
386 break;
387 }
388
389 /* Synced streams must connect to the same sink */
390 if (ssync && ssync->sink_input->sink != sink)
391 return NULL;
392
393 if (!(sink_input = pa_sink_input_new(sink, __FILE__, name, ss, map, volume, 0, -1)))
394 return NULL;
395
396 s = pa_xnew(struct playback_stream, 1);
397 s->type = PLAYBACK_STREAM;
398 s->connection = c;
399 s->syncid = syncid;
400 s->sink_input = sink_input;
401 s->underrun = 1;
402
403 s->sink_input->peek = sink_input_peek_cb;
404 s->sink_input->drop = sink_input_drop_cb;
405 s->sink_input->kill = sink_input_kill_cb;
406 s->sink_input->get_latency = sink_input_get_latency_cb;
407 s->sink_input->userdata = s;
408 s->sink_input->owner = c->protocol->module;
409 s->sink_input->client = c->client;
410
411 if (ssync) {
412 /* Sync id found, now find head of list */
413 PA_LLIST_FIND_HEAD(struct playback_stream, ssync, &ssync);
414
415 /* Prepend ourselves */
416 PA_LLIST_PREPEND(struct playback_stream, ssync, s);
417
418 /* Set our start index to the current read index of the other grozp member(s) */
419 assert(ssync->next);
420 start_index = pa_memblockq_get_read_index(ssync->next->memblockq);
421 } else {
422 /* This ia a new sync group */
423 PA_LLIST_INIT(struct playback_stream, s);
424 start_index = 0;
425 }
426
427 silence = pa_silence_memblock_new(ss, 0, c->protocol->core->memblock_stat);
428
429 s->memblockq = pa_memblockq_new(
430 start_index,
431 maxlength,
432 tlength,
433 pa_frame_size(ss),
434 prebuf,
435 minreq,
436 silence,
437 c->protocol->core->memblock_stat);
438
439 pa_memblock_unref(silence);
440
441 s->requested_bytes = 0;
442 s->drain_request = 0;
443
444 pa_idxset_put(c->output_streams, s, &s->index);
445
446 return s;
447 }
448
449 static void playback_stream_free(struct playback_stream* p) {
450 struct playback_stream *head;
451 assert(p && p->connection);
452
453 if (p->drain_request)
454 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERR_NOENTITY);
455
456 PA_LLIST_FIND_HEAD(struct playback_stream, p, &head);
457 PA_LLIST_REMOVE(struct playback_stream, head, p);
458
459 pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
460 pa_sink_input_disconnect(p->sink_input);
461 pa_sink_input_unref(p->sink_input);
462 pa_memblockq_free(p->memblockq);
463 pa_xfree(p);
464 }
465
466 static void connection_free(struct connection *c) {
467 struct record_stream *r;
468 struct output_stream *o;
469 assert(c && c->protocol);
470
471 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
472 while ((r = pa_idxset_first(c->record_streams, NULL)))
473 record_stream_free(r);
474 pa_idxset_free(c->record_streams, NULL, NULL);
475
476 while ((o = pa_idxset_first(c->output_streams, NULL)))
477 if (o->type == PLAYBACK_STREAM)
478 playback_stream_free((struct playback_stream*) o);
479 else
480 upload_stream_free((struct upload_stream*) o);
481 pa_idxset_free(c->output_streams, NULL, NULL);
482
483 pa_pdispatch_unref(c->pdispatch);
484 pa_pstream_close(c->pstream);
485 pa_pstream_unref(c->pstream);
486 pa_client_free(c->client);
487
488 if (c->subscription)
489 pa_subscription_free(c->subscription);
490
491 if (c->auth_timeout_event)
492 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
493
494 pa_xfree(c);
495 }
496
497 static void request_bytes(struct playback_stream *s) {
498 pa_tagstruct *t;
499 size_t l;
500 assert(s);
501
502 if (!(l = pa_memblockq_missing(s->memblockq)))
503 return;
504
505 if (l <= s->requested_bytes)
506 return;
507
508 l -= s->requested_bytes;
509
510 if (l < pa_memblockq_get_minreq(s->memblockq))
511 return;
512
513 s->requested_bytes += l;
514
515 t = pa_tagstruct_new(NULL, 0);
516 assert(t);
517 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
518 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
519 pa_tagstruct_putu32(t, s->index);
520 pa_tagstruct_putu32(t, l);
521 pa_pstream_send_tagstruct(s->connection->pstream, t);
522
523 /* pa_log(__FILE__": Requesting %u bytes", l); */
524 }
525
526 static void send_memblock(struct connection *c) {
527 uint32_t start;
528 struct record_stream *r;
529
530 start = PA_IDXSET_INVALID;
531 for (;;) {
532 pa_memchunk chunk;
533
534 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
535 return;
536
537 if (start == PA_IDXSET_INVALID)
538 start = c->rrobin_index;
539 else if (start == c->rrobin_index)
540 return;
541
542 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
543 pa_memchunk schunk = chunk;
544
545 if (schunk.length > r->fragment_size)
546 schunk.length = r->fragment_size;
547
548 pa_pstream_send_memblock(c->pstream, r->index, 0, PA_SEEK_RELATIVE, &schunk);
549 pa_memblockq_drop(r->memblockq, &chunk, schunk.length);
550 pa_memblock_unref(schunk.memblock);
551
552 return;
553 }
554 }
555 }
556
557 static void send_playback_stream_killed(struct playback_stream *p) {
558 pa_tagstruct *t;
559 assert(p);
560
561 t = pa_tagstruct_new(NULL, 0);
562 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
563 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
564 pa_tagstruct_putu32(t, p->index);
565 pa_pstream_send_tagstruct(p->connection->pstream, t);
566 }
567
568 static void send_record_stream_killed(struct record_stream *r) {
569 pa_tagstruct *t;
570 assert(r);
571
572 t = pa_tagstruct_new(NULL, 0);
573 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
574 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
575 pa_tagstruct_putu32(t, r->index);
576 pa_pstream_send_tagstruct(r->connection->pstream, t);
577 }
578
579 /*** sinkinput callbacks ***/
580
581 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
582 struct playback_stream *s;
583 assert(i && i->userdata && chunk);
584 s = i->userdata;
585
586 if (pa_memblockq_get_length(s->memblockq) <= 0 && !s->underrun) {
587 pa_tagstruct *t;
588
589 /* Report that we're empty */
590
591 t = pa_tagstruct_new(NULL, 0);
592 pa_tagstruct_putu32(t, PA_COMMAND_UNDERFLOW);
593 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
594 pa_tagstruct_putu32(t, s->index);
595 pa_pstream_send_tagstruct(s->connection->pstream, t);
596
597 s->underrun = 1;
598 }
599
600 if (pa_memblockq_peek(s->memblockq, chunk) < 0) {
601 /* pa_log(__FILE__": peek: failure"); */
602 return -1;
603 }
604
605 /* pa_log(__FILE__": peek: %u", chunk->length); */
606
607 return 0;
608 }
609
610 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
611 struct playback_stream *s;
612 assert(i && i->userdata && length);
613 s = i->userdata;
614
615 pa_memblockq_drop(s->memblockq, chunk, length);
616
617 request_bytes(s);
618
619 if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
620 pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
621 s->drain_request = 0;
622 }
623
624 /* pa_log(__FILE__": after_drop: %u %u", pa_memblockq_get_length(s->memblockq), pa_memblockq_is_readable(s->memblockq)); */
625 }
626
627 static void sink_input_kill_cb(pa_sink_input *i) {
628 assert(i && i->userdata);
629 send_playback_stream_killed((struct playback_stream *) i->userdata);
630 playback_stream_free((struct playback_stream *) i->userdata);
631 }
632
633 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
634 struct playback_stream *s;
635 assert(i && i->userdata);
636 s = i->userdata;
637
638 /*pa_log(__FILE__": get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
639
640 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
641 }
642
643 /*** source_output callbacks ***/
644
645 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
646 struct record_stream *s;
647 assert(o && o->userdata && chunk);
648 s = o->userdata;
649
650 if (pa_memblockq_push_align(s->memblockq, chunk) < 0) {
651 pa_log_warn(__FILE__": Failed to push data into output queue.");
652 return;
653 }
654
655 if (!pa_pstream_is_pending(s->connection->pstream))
656 send_memblock(s->connection);
657 }
658
659 static void source_output_kill_cb(pa_source_output *o) {
660 assert(o && o->userdata);
661 send_record_stream_killed((struct record_stream *) o->userdata);
662 record_stream_free((struct record_stream *) o->userdata);
663 }
664
665 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
666 struct record_stream *s;
667 assert(o && o->userdata);
668 s = o->userdata;
669
670 /*pa_log(__FILE__": get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
671
672 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &o->sample_spec);
673 }
674
675 /*** pdispatch callbacks ***/
676
677 static void protocol_error(struct connection *c) {
678 pa_log(__FILE__": protocol error, kicking client");
679 connection_free(c);
680 }
681
682 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
683 if (!(expression)) { \
684 pa_pstream_send_error((pstream), (tag), (error)); \
685 return; \
686 } \
687 } while(0);
688
689 static pa_tagstruct *reply_new(uint32_t tag) {
690 pa_tagstruct *reply;
691
692 reply = pa_tagstruct_new(NULL, 0);
693 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
694 pa_tagstruct_putu32(reply, tag);
695 return reply;
696 }
697
698 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) {
699 struct connection *c = userdata;
700 struct playback_stream *s;
701 uint32_t maxlength, tlength, prebuf, minreq, sink_index, syncid;
702 const char *name, *sink_name;
703 pa_sample_spec ss;
704 pa_channel_map map;
705 pa_tagstruct *reply;
706 pa_sink *sink;
707 pa_cvolume volume;
708 int corked;
709
710 assert(c && t && c->protocol && c->protocol->core);
711
712 if (pa_tagstruct_get(
713 t,
714 PA_TAG_STRING, &name,
715 PA_TAG_SAMPLE_SPEC, &ss,
716 PA_TAG_CHANNEL_MAP, &map,
717 PA_TAG_U32, &sink_index,
718 PA_TAG_STRING, &sink_name,
719 PA_TAG_U32, &maxlength,
720 PA_TAG_BOOLEAN, &corked,
721 PA_TAG_U32, &tlength,
722 PA_TAG_U32, &prebuf,
723 PA_TAG_U32, &minreq,
724 PA_TAG_U32, &syncid,
725 PA_TAG_CVOLUME, &volume,
726 PA_TAG_INVALID) < 0 ||
727 !pa_tagstruct_eof(t) ||
728 !name) {
729 protocol_error(c);
730 return;
731 }
732
733 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
734 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
735 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
736 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
737 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
738 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
739 CHECK_VALIDITY(c->pstream, map.channels == ss.channels && volume.channels == ss.channels, tag, PA_ERR_INVALID);
740 CHECK_VALIDITY(c->pstream, maxlength > 0 && maxlength <= MAX_MEMBLOCKQ_LENGTH, tag, PA_ERR_INVALID);
741
742 if (sink_index != PA_INVALID_INDEX)
743 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
744 else
745 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
746
747 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
748
749 s = playback_stream_new(c, sink, &ss, &map, name, maxlength, tlength, prebuf, minreq, &volume, syncid);
750 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
751
752 pa_sink_input_cork(s->sink_input, corked);
753
754 reply = reply_new(tag);
755 pa_tagstruct_putu32(reply, s->index);
756 assert(s->sink_input);
757 pa_tagstruct_putu32(reply, s->sink_input->index);
758 pa_tagstruct_putu32(reply, s->requested_bytes = pa_memblockq_missing(s->memblockq));
759
760 if (c->version >= 9) {
761 /* Since 0.9 we support sending the buffer metrics back to the client */
762
763 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_maxlength(s->memblockq));
764 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_tlength(s->memblockq));
765 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_prebuf(s->memblockq));
766 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_minreq(s->memblockq));
767 }
768
769 pa_pstream_send_tagstruct(c->pstream, reply);
770 request_bytes(s);
771 }
772
773 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
774 struct connection *c = userdata;
775 uint32_t channel;
776 assert(c && t);
777
778 if (pa_tagstruct_getu32(t, &channel) < 0 ||
779 !pa_tagstruct_eof(t)) {
780 protocol_error(c);
781 return;
782 }
783
784 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
785
786 if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
787 struct playback_stream *s;
788 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
789 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
790 return;
791 }
792
793 playback_stream_free(s);
794 } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
795 struct record_stream *s;
796 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
797 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
798 return;
799 }
800
801 record_stream_free(s);
802 } else {
803 struct upload_stream *s;
804 assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
805 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
806 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
807 return;
808 }
809
810 upload_stream_free(s);
811 }
812
813 pa_pstream_send_simple_ack(c->pstream, tag);
814 }
815
816 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) {
817 struct connection *c = userdata;
818 struct record_stream *s;
819 uint32_t maxlength, fragment_size;
820 uint32_t source_index;
821 const char *name, *source_name;
822 pa_sample_spec ss;
823 pa_channel_map map;
824 pa_tagstruct *reply;
825 pa_source *source;
826 int corked;
827 assert(c && t && c->protocol && c->protocol->core);
828
829 if (pa_tagstruct_gets(t, &name) < 0 ||
830 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
831 pa_tagstruct_get_channel_map(t, &map) < 0 ||
832 pa_tagstruct_getu32(t, &source_index) < 0 ||
833 pa_tagstruct_gets(t, &source_name) < 0 ||
834 pa_tagstruct_getu32(t, &maxlength) < 0 ||
835 pa_tagstruct_get_boolean(t, &corked) < 0 ||
836 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
837 !pa_tagstruct_eof(t)) {
838 protocol_error(c);
839 return;
840 }
841
842 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
843 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
844 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
845 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
846 CHECK_VALIDITY(c->pstream, source_index != PA_INVALID_INDEX || !source_name || (*source_name && pa_utf8_valid(source_name)), tag, PA_ERR_INVALID);
847 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
848 CHECK_VALIDITY(c->pstream, maxlength <= MAX_MEMBLOCKQ_LENGTH, tag, PA_ERR_INVALID);
849
850 if (source_index != PA_INVALID_INDEX)
851 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
852 else
853 source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1);
854
855 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
856
857 s = record_stream_new(c, source, &ss, &map, name, maxlength, fragment_size);
858 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
859
860 pa_source_output_cork(s->source_output, corked);
861
862 reply = reply_new(tag);
863 pa_tagstruct_putu32(reply, s->index);
864 assert(s->source_output);
865 pa_tagstruct_putu32(reply, s->source_output->index);
866
867 if (c->version >= 9) {
868 /* Since 0.9 we support sending the buffer metrics back to the client */
869
870 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_maxlength(s->memblockq));
871 pa_tagstruct_putu32(reply, (uint32_t) s->fragment_size);
872 }
873
874 pa_pstream_send_tagstruct(c->pstream, reply);
875 }
876
877 static void command_exit(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
878 struct connection *c = userdata;
879 assert(c && t);
880
881 if (!pa_tagstruct_eof(t)) {
882 protocol_error(c);
883 return;
884 }
885
886 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
887
888 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
889 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
890 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
891 }
892
893 static void command_auth(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
894 struct connection *c = userdata;
895 const void*cookie;
896 pa_tagstruct *reply;
897 assert(c && t);
898
899 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
900 pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
901 !pa_tagstruct_eof(t)) {
902 protocol_error(c);
903 return;
904 }
905
906 /* Minimum supported version */
907 if (c->version < 8) {
908 pa_pstream_send_error(c->pstream, tag, PA_ERR_VERSION);
909 return;
910 }
911
912 if (!c->authorized) {
913 int success = 0;
914
915 #ifdef HAVE_CREDS
916 const pa_creds *creds;
917
918 if ((creds = pa_pdispatch_creds(pd))) {
919 if (creds->uid == getuid())
920 success = 1;
921 else if (c->protocol->auth_group) {
922 int r;
923 gid_t gid;
924
925 if ((gid = pa_get_gid_of_group(c->protocol->auth_group)) == (gid_t) -1)
926 pa_log_warn(__FILE__": failed to get GID of group '%s'", c->protocol->auth_group);
927 else if (gid == creds->gid)
928 success = 1;
929
930 if (!success) {
931 if ((r = pa_uid_in_group(creds->uid, c->protocol->auth_group)) < 0)
932 pa_log_warn(__FILE__": failed to check group membership.");
933 else if (r > 0)
934 success = 1;
935 }
936 }
937
938 pa_log_info(__FILE__": Got credentials: uid=%lu gid=%lu success=%i",
939 (unsigned long) creds->uid,
940 (unsigned long) creds->gid,
941 success);
942 }
943 #endif
944
945 if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) == 0)
946 success = 1;
947
948 if (!success) {
949 pa_log_warn(__FILE__": Denied access to client with invalid authorization data.");
950 pa_pstream_send_error(c->pstream, tag, PA_ERR_ACCESS);
951 return;
952 }
953
954 c->authorized = 1;
955 if (c->auth_timeout_event) {
956 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
957 c->auth_timeout_event = NULL;
958 }
959 }
960
961 reply = reply_new(tag);
962 pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION);
963 pa_pstream_send_tagstruct(c->pstream, reply);
964 }
965
966 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) {
967 struct connection *c = userdata;
968 const char *name;
969 assert(c && t);
970
971 if (pa_tagstruct_gets(t, &name) < 0 ||
972 !pa_tagstruct_eof(t)) {
973 protocol_error(c);
974 return;
975 }
976
977 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
978
979 pa_client_set_name(c->client, name);
980 pa_pstream_send_simple_ack(c->pstream, tag);
981 }
982
983 static void command_lookup(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
984 struct connection *c = userdata;
985 const char *name;
986 uint32_t idx = PA_IDXSET_INVALID;
987 assert(c && t);
988
989 if (pa_tagstruct_gets(t, &name) < 0 ||
990 !pa_tagstruct_eof(t)) {
991 protocol_error(c);
992 return;
993 }
994
995 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
996 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
997
998 if (command == PA_COMMAND_LOOKUP_SINK) {
999 pa_sink *sink;
1000 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
1001 idx = sink->index;
1002 } else {
1003 pa_source *source;
1004 assert(command == PA_COMMAND_LOOKUP_SOURCE);
1005 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
1006 idx = source->index;
1007 }
1008
1009 if (idx == PA_IDXSET_INVALID)
1010 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1011 else {
1012 pa_tagstruct *reply;
1013 reply = reply_new(tag);
1014 pa_tagstruct_putu32(reply, idx);
1015 pa_pstream_send_tagstruct(c->pstream, reply);
1016 }
1017 }
1018
1019 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) {
1020 struct connection *c = userdata;
1021 uint32_t idx;
1022 struct playback_stream *s;
1023 assert(c && t);
1024
1025 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1026 !pa_tagstruct_eof(t)) {
1027 protocol_error(c);
1028 return;
1029 }
1030
1031 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1032 s = pa_idxset_get_by_index(c->output_streams, idx);
1033 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1034 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1035
1036 s->drain_request = 0;
1037
1038 pa_memblockq_prebuf_disable(s->memblockq);
1039
1040 if (!pa_memblockq_is_readable(s->memblockq)) {
1041 /* pa_log("immediate drain: %u", pa_memblockq_get_length(s->memblockq)); */
1042 pa_pstream_send_simple_ack(c->pstream, tag);
1043 } else {
1044 /* pa_log("slow drain triggered"); */
1045 s->drain_request = 1;
1046 s->drain_tag = tag;
1047
1048 pa_sink_notify(s->sink_input->sink);
1049 }
1050 }
1051
1052 static void command_stat(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;
1054 pa_tagstruct *reply;
1055 assert(c && t);
1056
1057 if (!pa_tagstruct_eof(t)) {
1058 protocol_error(c);
1059 return;
1060 }
1061
1062 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1063
1064 reply = reply_new(tag);
1065 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total);
1066 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total_size);
1067 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated);
1068 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated_size);
1069 pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
1070 pa_pstream_send_tagstruct(c->pstream, reply);
1071 }
1072
1073 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) {
1074 struct connection *c = userdata;
1075 pa_tagstruct *reply;
1076 struct playback_stream *s;
1077 struct timeval tv, now;
1078 uint32_t idx;
1079 pa_usec_t latency;
1080 assert(c && t);
1081
1082 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1083 pa_tagstruct_get_timeval(t, &tv) < 0 ||
1084 !pa_tagstruct_eof(t)) {
1085 protocol_error(c);
1086 return;
1087 }
1088
1089 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1090 s = pa_idxset_get_by_index(c->output_streams, idx);
1091 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1092 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1093
1094 reply = reply_new(tag);
1095
1096 latency = pa_sink_get_latency(s->sink_input->sink);
1097 if (s->sink_input->resampled_chunk.memblock)
1098 latency += pa_bytes_to_usec(s->sink_input->resampled_chunk.length, &s->sink_input->sample_spec);
1099 pa_tagstruct_put_usec(reply, latency);
1100
1101 pa_tagstruct_put_usec(reply, 0);
1102 pa_tagstruct_put_boolean(reply, pa_memblockq_is_readable(s->memblockq));
1103 pa_tagstruct_put_timeval(reply, &tv);
1104 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
1105 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
1106 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
1107 pa_pstream_send_tagstruct(c->pstream, reply);
1108 }
1109
1110 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) {
1111 struct connection *c = userdata;
1112 pa_tagstruct *reply;
1113 struct record_stream *s;
1114 struct timeval tv, now;
1115 uint32_t idx;
1116 assert(c && t);
1117
1118 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1119 pa_tagstruct_get_timeval(t, &tv) < 0 ||
1120 !pa_tagstruct_eof(t)) {
1121 protocol_error(c);
1122 return;
1123 }
1124
1125 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1126 s = pa_idxset_get_by_index(c->record_streams, idx);
1127 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1128
1129 reply = reply_new(tag);
1130 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
1131 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
1132 pa_tagstruct_put_boolean(reply, 0);
1133 pa_tagstruct_put_timeval(reply, &tv);
1134 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
1135 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
1136 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
1137 pa_pstream_send_tagstruct(c->pstream, reply);
1138 }
1139
1140 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) {
1141 struct connection *c = userdata;
1142 struct upload_stream *s;
1143 uint32_t length;
1144 const char *name;
1145 pa_sample_spec ss;
1146 pa_channel_map map;
1147 pa_tagstruct *reply;
1148 assert(c && t && c->protocol && c->protocol->core);
1149
1150 if (pa_tagstruct_gets(t, &name) < 0 ||
1151 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
1152 pa_tagstruct_get_channel_map(t, &map) < 0 ||
1153 pa_tagstruct_getu32(t, &length) < 0 ||
1154 !pa_tagstruct_eof(t)) {
1155 protocol_error(c);
1156 return;
1157 }
1158
1159 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1160 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1161 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1162 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
1163 CHECK_VALIDITY(c->pstream, (length % pa_frame_size(&ss)) == 0 && length > 0, tag, PA_ERR_INVALID);
1164 CHECK_VALIDITY(c->pstream, length <= PA_SCACHE_ENTRY_SIZE_MAX, tag, PA_ERR_TOOLARGE);
1165 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1166
1167 s = upload_stream_new(c, &ss, &map, name, length);
1168 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1169
1170 reply = reply_new(tag);
1171 pa_tagstruct_putu32(reply, s->index);
1172 pa_tagstruct_putu32(reply, length);
1173 pa_pstream_send_tagstruct(c->pstream, reply);
1174 }
1175
1176 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) {
1177 struct connection *c = userdata;
1178 uint32_t channel;
1179 struct upload_stream *s;
1180 uint32_t idx;
1181 assert(c && t);
1182
1183 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1184 !pa_tagstruct_eof(t)) {
1185 protocol_error(c);
1186 return;
1187 }
1188
1189 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1190
1191 s = pa_idxset_get_by_index(c->output_streams, channel);
1192 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1193 CHECK_VALIDITY(c->pstream, s->type == UPLOAD_STREAM, tag, PA_ERR_NOENTITY);
1194
1195 if (pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->channel_map, &s->memchunk, &idx) < 0)
1196 pa_pstream_send_error(c->pstream, tag, PA_ERR_INTERNAL);
1197 else
1198 pa_pstream_send_simple_ack(c->pstream, tag);
1199
1200 upload_stream_free(s);
1201 }
1202
1203 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) {
1204 struct connection *c = userdata;
1205 uint32_t sink_index;
1206 pa_volume_t volume;
1207 pa_sink *sink;
1208 const char *name, *sink_name;
1209 assert(c && t);
1210
1211 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
1212 pa_tagstruct_gets(t, &sink_name) < 0 ||
1213 pa_tagstruct_getu32(t, &volume) < 0 ||
1214 pa_tagstruct_gets(t, &name) < 0 ||
1215 !pa_tagstruct_eof(t)) {
1216 protocol_error(c);
1217 return;
1218 }
1219
1220 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1221 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1222 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1223
1224 if (sink_index != PA_INVALID_INDEX)
1225 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1226 else
1227 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1228
1229 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
1230
1231 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
1232 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1233 return;
1234 }
1235
1236 pa_pstream_send_simple_ack(c->pstream, tag);
1237 }
1238
1239 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) {
1240 struct connection *c = userdata;
1241 const char *name;
1242 assert(c && t);
1243
1244 if (pa_tagstruct_gets(t, &name) < 0 ||
1245 !pa_tagstruct_eof(t)) {
1246 protocol_error(c);
1247 return;
1248 }
1249
1250 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1251 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1252
1253 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
1254 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1255 return;
1256 }
1257
1258 pa_pstream_send_simple_ack(c->pstream, tag);
1259 }
1260
1261 static void sink_fill_tagstruct(pa_tagstruct *t, pa_sink *sink) {
1262 assert(t && sink);
1263 pa_tagstruct_put(
1264 t,
1265 PA_TAG_U32, sink->index,
1266 PA_TAG_STRING, sink->name,
1267 PA_TAG_STRING, sink->description,
1268 PA_TAG_SAMPLE_SPEC, &sink->sample_spec,
1269 PA_TAG_CHANNEL_MAP, &sink->channel_map,
1270 PA_TAG_U32, sink->owner ? sink->owner->index : PA_INVALID_INDEX,
1271 PA_TAG_CVOLUME, pa_sink_get_volume(sink, PA_MIXER_HARDWARE),
1272 PA_TAG_BOOLEAN, pa_sink_get_mute(sink, PA_MIXER_HARDWARE),
1273 PA_TAG_U32, sink->monitor_source->index,
1274 PA_TAG_STRING, sink->monitor_source->name,
1275 PA_TAG_USEC, pa_sink_get_latency(sink),
1276 PA_TAG_STRING, sink->driver,
1277 PA_TAG_U32,
1278 (sink->get_hw_volume ? PA_SINK_HW_VOLUME_CTRL : 0) |
1279 (sink->get_latency ? PA_SINK_LATENCY : 0) |
1280 (sink->is_hardware ? PA_SINK_HARDWARE : 0),
1281 PA_TAG_INVALID);
1282 }
1283
1284 static void source_fill_tagstruct(pa_tagstruct *t, pa_source *source) {
1285 assert(t && source);
1286 pa_tagstruct_put(
1287 t,
1288 PA_TAG_U32, source->index,
1289 PA_TAG_STRING, source->name,
1290 PA_TAG_STRING, source->description,
1291 PA_TAG_SAMPLE_SPEC, &source->sample_spec,
1292 PA_TAG_CHANNEL_MAP, &source->channel_map,
1293 PA_TAG_U32, source->owner ? source->owner->index : PA_INVALID_INDEX,
1294 PA_TAG_CVOLUME, pa_source_get_volume(source, PA_MIXER_HARDWARE),
1295 PA_TAG_BOOLEAN, pa_source_get_mute(source, PA_MIXER_HARDWARE),
1296 PA_TAG_U32, source->monitor_of ? source->monitor_of->index : PA_INVALID_INDEX,
1297 PA_TAG_STRING, source->monitor_of ? source->monitor_of->name : NULL,
1298 PA_TAG_USEC, pa_source_get_latency(source),
1299 PA_TAG_STRING, source->driver,
1300 PA_TAG_U32,
1301 (source->get_hw_volume ? PA_SOURCE_HW_VOLUME_CTRL : 0) |
1302 (source->get_latency ? PA_SOURCE_LATENCY : 0) |
1303 (source->is_hardware ? PA_SOURCE_HARDWARE : 0),
1304 PA_TAG_INVALID);
1305 }
1306
1307 static void client_fill_tagstruct(pa_tagstruct *t, pa_client *client) {
1308 assert(t && client);
1309 pa_tagstruct_putu32(t, client->index);
1310 pa_tagstruct_puts(t, client->name);
1311 pa_tagstruct_putu32(t, client->owner ? client->owner->index : PA_INVALID_INDEX);
1312 pa_tagstruct_puts(t, client->driver);
1313 }
1314
1315 static void module_fill_tagstruct(pa_tagstruct *t, pa_module *module) {
1316 assert(t && module);
1317 pa_tagstruct_putu32(t, module->index);
1318 pa_tagstruct_puts(t, module->name);
1319 pa_tagstruct_puts(t, module->argument);
1320 pa_tagstruct_putu32(t, module->n_used);
1321 pa_tagstruct_put_boolean(t, module->auto_unload);
1322 }
1323
1324 static void sink_input_fill_tagstruct(pa_tagstruct *t, pa_sink_input *s) {
1325 assert(t && s);
1326 pa_tagstruct_putu32(t, s->index);
1327 pa_tagstruct_puts(t, s->name);
1328 pa_tagstruct_putu32(t, s->owner ? s->owner->index : PA_INVALID_INDEX);
1329 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
1330 pa_tagstruct_putu32(t, s->sink->index);
1331 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1332 pa_tagstruct_put_channel_map(t, &s->channel_map);
1333 pa_tagstruct_put_cvolume(t, &s->volume);
1334 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
1335 pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
1336 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
1337 pa_tagstruct_puts(t, s->driver);
1338 }
1339
1340 static void source_output_fill_tagstruct(pa_tagstruct *t, pa_source_output *s) {
1341 assert(t && s);
1342 pa_tagstruct_putu32(t, s->index);
1343 pa_tagstruct_puts(t, s->name);
1344 pa_tagstruct_putu32(t, s->owner ? s->owner->index : PA_INVALID_INDEX);
1345 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
1346 pa_tagstruct_putu32(t, s->source->index);
1347 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1348 pa_tagstruct_put_channel_map(t, &s->channel_map);
1349 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
1350 pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
1351 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
1352 pa_tagstruct_puts(t, s->driver);
1353 }
1354
1355 static void scache_fill_tagstruct(pa_tagstruct *t, pa_scache_entry *e) {
1356 assert(t && e);
1357 pa_tagstruct_putu32(t, e->index);
1358 pa_tagstruct_puts(t, e->name);
1359 pa_tagstruct_put_cvolume(t, &e->volume);
1360 pa_tagstruct_put_usec(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1361 pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1362 pa_tagstruct_put_channel_map(t, &e->channel_map);
1363 pa_tagstruct_putu32(t, e->memchunk.length);
1364 pa_tagstruct_put_boolean(t, e->lazy);
1365 pa_tagstruct_puts(t, e->filename);
1366 }
1367
1368 static void command_get_info(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1369 struct connection *c = userdata;
1370 uint32_t idx;
1371 pa_sink *sink = NULL;
1372 pa_source *source = NULL;
1373 pa_client *client = NULL;
1374 pa_module *module = NULL;
1375 pa_sink_input *si = NULL;
1376 pa_source_output *so = NULL;
1377 pa_scache_entry *sce = NULL;
1378 const char *name;
1379 pa_tagstruct *reply;
1380 assert(c && t);
1381
1382 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1383 (command != PA_COMMAND_GET_CLIENT_INFO &&
1384 command != PA_COMMAND_GET_MODULE_INFO &&
1385 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1386 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1387 pa_tagstruct_gets(t, &name) < 0) ||
1388 !pa_tagstruct_eof(t)) {
1389 protocol_error(c);
1390 return;
1391 }
1392
1393 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1394 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1395
1396 if (command == PA_COMMAND_GET_SINK_INFO) {
1397 if (idx != PA_INVALID_INDEX)
1398 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1399 else
1400 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1401 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1402 if (idx != PA_INVALID_INDEX)
1403 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1404 else
1405 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1406 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1407 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
1408 else if (command == PA_COMMAND_GET_MODULE_INFO)
1409 module = pa_idxset_get_by_index(c->protocol->core->modules, idx);
1410 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1411 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1412 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1413 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
1414 else {
1415 assert(command == PA_COMMAND_GET_SAMPLE_INFO);
1416 if (idx != PA_INVALID_INDEX)
1417 sce = pa_idxset_get_by_index(c->protocol->core->scache, idx);
1418 else
1419 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1420 }
1421
1422 if (!sink && !source && !client && !module && !si && !so && !sce) {
1423 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1424 return;
1425 }
1426
1427 reply = reply_new(tag);
1428 if (sink)
1429 sink_fill_tagstruct(reply, sink);
1430 else if (source)
1431 source_fill_tagstruct(reply, source);
1432 else if (client)
1433 client_fill_tagstruct(reply, client);
1434 else if (module)
1435 module_fill_tagstruct(reply, module);
1436 else if (si)
1437 sink_input_fill_tagstruct(reply, si);
1438 else if (so)
1439 source_output_fill_tagstruct(reply, so);
1440 else
1441 scache_fill_tagstruct(reply, sce);
1442 pa_pstream_send_tagstruct(c->pstream, reply);
1443 }
1444
1445 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1446 struct connection *c = userdata;
1447 pa_idxset *i;
1448 uint32_t idx;
1449 void *p;
1450 pa_tagstruct *reply;
1451 assert(c && t);
1452
1453 if (!pa_tagstruct_eof(t)) {
1454 protocol_error(c);
1455 return;
1456 }
1457
1458 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1459
1460 reply = reply_new(tag);
1461
1462 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1463 i = c->protocol->core->sinks;
1464 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1465 i = c->protocol->core->sources;
1466 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1467 i = c->protocol->core->clients;
1468 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1469 i = c->protocol->core->modules;
1470 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1471 i = c->protocol->core->sink_inputs;
1472 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1473 i = c->protocol->core->source_outputs;
1474 else {
1475 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1476 i = c->protocol->core->scache;
1477 }
1478
1479 if (i) {
1480 for (p = pa_idxset_first(i, &idx); p; p = pa_idxset_next(i, &idx)) {
1481 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1482 sink_fill_tagstruct(reply, p);
1483 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1484 source_fill_tagstruct(reply, p);
1485 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1486 client_fill_tagstruct(reply, p);
1487 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1488 module_fill_tagstruct(reply, p);
1489 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1490 sink_input_fill_tagstruct(reply, p);
1491 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1492 source_output_fill_tagstruct(reply, p);
1493 else {
1494 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1495 scache_fill_tagstruct(reply, p);
1496 }
1497 }
1498 }
1499
1500 pa_pstream_send_tagstruct(c->pstream, reply);
1501 }
1502
1503 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) {
1504 struct connection *c = userdata;
1505 pa_tagstruct *reply;
1506 char txt[256];
1507 const char *n;
1508 assert(c && t);
1509
1510 if (!pa_tagstruct_eof(t)) {
1511 protocol_error(c);
1512 return;
1513 }
1514
1515 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1516
1517 reply = reply_new(tag);
1518 pa_tagstruct_puts(reply, PACKAGE_NAME);
1519 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1520 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1521 pa_tagstruct_puts(reply, pa_get_fqdn(txt, sizeof(txt)));
1522 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1523
1524 n = pa_namereg_get_default_sink_name(c->protocol->core);
1525 pa_tagstruct_puts(reply, n);
1526 n = pa_namereg_get_default_source_name(c->protocol->core);
1527 pa_tagstruct_puts(reply, n);
1528
1529 pa_tagstruct_putu32(reply, c->protocol->core->cookie);
1530
1531 pa_pstream_send_tagstruct(c->pstream, reply);
1532 }
1533
1534 static void subscription_cb(pa_core *core, pa_subscription_event_type_t e, uint32_t idx, void *userdata) {
1535 pa_tagstruct *t;
1536 struct connection *c = userdata;
1537 assert(c && core);
1538
1539 t = pa_tagstruct_new(NULL, 0);
1540 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1541 pa_tagstruct_putu32(t, (uint32_t) -1);
1542 pa_tagstruct_putu32(t, e);
1543 pa_tagstruct_putu32(t, idx);
1544 pa_pstream_send_tagstruct(c->pstream, t);
1545 }
1546
1547 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1548 struct connection *c = userdata;
1549 pa_subscription_mask_t m;
1550 assert(c && t);
1551
1552 if (pa_tagstruct_getu32(t, &m) < 0 ||
1553 !pa_tagstruct_eof(t)) {
1554 protocol_error(c);
1555 return;
1556 }
1557
1558 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1559 CHECK_VALIDITY(c->pstream, (m & ~PA_SUBSCRIPTION_MASK_ALL) == 0, tag, PA_ERR_INVALID);
1560
1561 if (c->subscription)
1562 pa_subscription_free(c->subscription);
1563
1564 if (m != 0) {
1565 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1566 assert(c->subscription);
1567 } else
1568 c->subscription = NULL;
1569
1570 pa_pstream_send_simple_ack(c->pstream, tag);
1571 }
1572
1573 static void command_set_volume(
1574 PA_GCC_UNUSED pa_pdispatch *pd,
1575 uint32_t command,
1576 uint32_t tag,
1577 pa_tagstruct *t,
1578 void *userdata) {
1579
1580 struct connection *c = userdata;
1581 uint32_t idx;
1582 pa_cvolume volume;
1583 pa_sink *sink = NULL;
1584 pa_source *source = NULL;
1585 pa_sink_input *si = NULL;
1586 const char *name = NULL;
1587 assert(c && t);
1588
1589 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1590 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1591 (command == PA_COMMAND_SET_SOURCE_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1592 pa_tagstruct_get_cvolume(t, &volume) ||
1593 !pa_tagstruct_eof(t)) {
1594 protocol_error(c);
1595 return;
1596 }
1597
1598 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1599 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1600 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
1601
1602 if (command == PA_COMMAND_SET_SINK_VOLUME) {
1603 if (idx != PA_INVALID_INDEX)
1604 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1605 else
1606 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1607 } else if (command == PA_COMMAND_SET_SOURCE_VOLUME) {
1608 if (idx != (uint32_t) -1)
1609 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1610 else
1611 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1612 } else {
1613 assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1614 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1615 }
1616
1617 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
1618
1619 if (sink)
1620 pa_sink_set_volume(sink, PA_MIXER_HARDWARE, &volume);
1621 else if (source)
1622 pa_source_set_volume(source, PA_MIXER_HARDWARE, &volume);
1623 else if (si)
1624 pa_sink_input_set_volume(si, &volume);
1625
1626 pa_pstream_send_simple_ack(c->pstream, tag);
1627 }
1628
1629 static void command_set_mute(
1630 PA_GCC_UNUSED pa_pdispatch *pd,
1631 uint32_t command,
1632 uint32_t tag,
1633 pa_tagstruct *t,
1634 void *userdata) {
1635
1636 struct connection *c = userdata;
1637 uint32_t idx;
1638 int mute;
1639 pa_sink *sink = NULL;
1640 pa_source *source = NULL;
1641 const char *name = NULL;
1642 assert(c && t);
1643
1644 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1645 pa_tagstruct_gets(t, &name) < 0 ||
1646 pa_tagstruct_get_boolean(t, &mute) ||
1647 !pa_tagstruct_eof(t)) {
1648 protocol_error(c);
1649 return;
1650 }
1651
1652 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1653 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1654
1655 if (command == PA_COMMAND_SET_SINK_MUTE) {
1656 if (idx != PA_INVALID_INDEX)
1657 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1658 else
1659 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1660 } else {
1661 assert(command == PA_COMMAND_SET_SOURCE_MUTE);
1662 if (idx != (uint32_t) -1)
1663 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1664 else
1665 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1666 }
1667
1668 CHECK_VALIDITY(c->pstream, sink || source, tag, PA_ERR_NOENTITY);
1669
1670 if (sink)
1671 pa_sink_set_mute(sink, PA_MIXER_HARDWARE, mute);
1672 else if (source)
1673 pa_source_set_mute(source, PA_MIXER_HARDWARE, mute);
1674
1675 pa_pstream_send_simple_ack(c->pstream, tag);
1676 }
1677
1678 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) {
1679 struct connection *c = userdata;
1680 uint32_t idx;
1681 int b;
1682 struct playback_stream *s, *ssync;
1683 assert(c && t);
1684
1685 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1686 pa_tagstruct_get_boolean(t, &b) < 0 ||
1687 !pa_tagstruct_eof(t)) {
1688 protocol_error(c);
1689 return;
1690 }
1691
1692 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1693 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1694 s = pa_idxset_get_by_index(c->output_streams, idx);
1695 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1696 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1697
1698 pa_sink_input_cork(s->sink_input, b);
1699 pa_memblockq_prebuf_force(s->memblockq);
1700
1701 /* Do the same for all other members in the sync group */
1702 for (ssync = s->prev; ssync; ssync = ssync->prev) {
1703 pa_sink_input_cork(ssync->sink_input, b);
1704 pa_memblockq_prebuf_force(ssync->memblockq);
1705 }
1706
1707 for (ssync = s->next; ssync; ssync = ssync->next) {
1708 pa_sink_input_cork(ssync->sink_input, b);
1709 pa_memblockq_prebuf_force(ssync->memblockq);
1710 }
1711
1712 pa_pstream_send_simple_ack(c->pstream, tag);
1713 }
1714
1715 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) {
1716 struct connection *c = userdata;
1717 uint32_t idx;
1718 struct playback_stream *s, *ssync;
1719 assert(c && t);
1720
1721 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1722 !pa_tagstruct_eof(t)) {
1723 protocol_error(c);
1724 return;
1725 }
1726
1727 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1728 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1729 s = pa_idxset_get_by_index(c->output_streams, idx);
1730 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1731 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1732
1733 pa_memblockq_flush(s->memblockq);
1734 s->underrun = 0;
1735
1736 /* Do the same for all other members in the sync group */
1737 for (ssync = s->prev; ssync; ssync = ssync->prev) {
1738 pa_memblockq_flush(ssync->memblockq);
1739 ssync->underrun = 0;
1740 }
1741
1742 for (ssync = s->next; ssync; ssync = ssync->next) {
1743 pa_memblockq_flush(ssync->memblockq);
1744 ssync->underrun = 0;
1745 }
1746
1747 pa_pstream_send_simple_ack(c->pstream, tag);
1748 pa_sink_notify(s->sink_input->sink);
1749 request_bytes(s);
1750
1751 for (ssync = s->prev; ssync; ssync = ssync->prev)
1752 request_bytes(ssync);
1753
1754 for (ssync = s->next; ssync; ssync = ssync->next)
1755 request_bytes(ssync);
1756 }
1757
1758 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) {
1759 struct connection *c = userdata;
1760 uint32_t idx;
1761 struct playback_stream *s;
1762 assert(c && t);
1763
1764 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1765 !pa_tagstruct_eof(t)) {
1766 protocol_error(c);
1767 return;
1768 }
1769
1770 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1771 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1772 s = pa_idxset_get_by_index(c->output_streams, idx);
1773 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1774 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1775
1776 switch (command) {
1777 case PA_COMMAND_PREBUF_PLAYBACK_STREAM:
1778 pa_memblockq_prebuf_force(s->memblockq);
1779 break;
1780
1781 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM:
1782 pa_memblockq_prebuf_disable(s->memblockq);
1783 break;
1784
1785 default:
1786 abort();
1787 }
1788
1789 pa_sink_notify(s->sink_input->sink);
1790 pa_pstream_send_simple_ack(c->pstream, tag);
1791 request_bytes(s);
1792 }
1793
1794 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) {
1795 struct connection *c = userdata;
1796 uint32_t idx;
1797 struct record_stream *s;
1798 int b;
1799 assert(c && t);
1800
1801 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1802 pa_tagstruct_get_boolean(t, &b) < 0 ||
1803 !pa_tagstruct_eof(t)) {
1804 protocol_error(c);
1805 return;
1806 }
1807
1808 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1809 s = pa_idxset_get_by_index(c->record_streams, idx);
1810 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1811
1812 pa_source_output_cork(s->source_output, b);
1813 pa_memblockq_prebuf_force(s->memblockq);
1814 pa_pstream_send_simple_ack(c->pstream, tag);
1815 }
1816
1817 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) {
1818 struct connection *c = userdata;
1819 uint32_t idx;
1820 struct record_stream *s;
1821 assert(c && t);
1822
1823 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1824 !pa_tagstruct_eof(t)) {
1825 protocol_error(c);
1826 return;
1827 }
1828
1829 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1830 s = pa_idxset_get_by_index(c->record_streams, idx);
1831 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1832
1833 pa_memblockq_flush(s->memblockq);
1834 pa_pstream_send_simple_ack(c->pstream, tag);
1835 }
1836
1837 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) {
1838 struct connection *c = userdata;
1839 const char *s;
1840 assert(c && t);
1841
1842 if (pa_tagstruct_gets(t, &s) < 0 ||
1843 !pa_tagstruct_eof(t)) {
1844 protocol_error(c);
1845 return;
1846 }
1847
1848 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1849 CHECK_VALIDITY(c->pstream, !s || (*s && pa_utf8_valid(s)), tag, PA_ERR_INVALID);
1850
1851 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
1852 pa_pstream_send_simple_ack(c->pstream, tag);
1853 }
1854
1855 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1856 struct connection *c = userdata;
1857 uint32_t idx;
1858 const char *name;
1859 assert(c && t);
1860
1861 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1862 pa_tagstruct_gets(t, &name) < 0 ||
1863 !pa_tagstruct_eof(t)) {
1864 protocol_error(c);
1865 return;
1866 }
1867
1868 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1869 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1870
1871 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
1872 struct playback_stream *s;
1873
1874 s = pa_idxset_get_by_index(c->output_streams, idx);
1875 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1876 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1877
1878 pa_sink_input_set_name(s->sink_input, name);
1879
1880 } else {
1881 struct record_stream *s;
1882
1883 s = pa_idxset_get_by_index(c->record_streams, idx);
1884 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1885
1886 pa_source_output_set_name(s->source_output, name);
1887 }
1888
1889 pa_pstream_send_simple_ack(c->pstream, tag);
1890 }
1891
1892 static void command_kill(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1893 struct connection *c = userdata;
1894 uint32_t idx;
1895 assert(c && t);
1896
1897 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1898 !pa_tagstruct_eof(t)) {
1899 protocol_error(c);
1900 return;
1901 }
1902
1903 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1904
1905 if (command == PA_COMMAND_KILL_CLIENT) {
1906 pa_client *client;
1907
1908 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
1909 CHECK_VALIDITY(c->pstream, client, tag, PA_ERR_NOENTITY);
1910 pa_client_kill(client);
1911
1912 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
1913 pa_sink_input *s;
1914
1915 s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1916 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1917
1918 pa_sink_input_kill(s);
1919 } else {
1920 pa_source_output *s;
1921
1922 assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
1923
1924 s = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
1925 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1926
1927 pa_source_output_kill(s);
1928 }
1929
1930 pa_pstream_send_simple_ack(c->pstream, tag);
1931 }
1932
1933 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) {
1934 struct connection *c = userdata;
1935 pa_module *m;
1936 const char *name, *argument;
1937 pa_tagstruct *reply;
1938 assert(c && t);
1939
1940 if (pa_tagstruct_gets(t, &name) < 0 ||
1941 pa_tagstruct_gets(t, &argument) < 0 ||
1942 !pa_tagstruct_eof(t)) {
1943 protocol_error(c);
1944 return;
1945 }
1946
1947 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1948 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name) && !strchr(name, '/'), tag, PA_ERR_INVALID);
1949 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
1950
1951 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
1952 pa_pstream_send_error(c->pstream, tag, PA_ERR_MODINITFAILED);
1953 return;
1954 }
1955
1956 reply = reply_new(tag);
1957 pa_tagstruct_putu32(reply, m->index);
1958 pa_pstream_send_tagstruct(c->pstream, reply);
1959 }
1960
1961 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) {
1962 struct connection *c = userdata;
1963 uint32_t idx;
1964 pa_module *m;
1965 assert(c && t);
1966
1967 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1968 !pa_tagstruct_eof(t)) {
1969 protocol_error(c);
1970 return;
1971 }
1972
1973 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1974 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
1975 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOENTITY);
1976
1977 pa_module_unload_request(m);
1978 pa_pstream_send_simple_ack(c->pstream, tag);
1979 }
1980
1981 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) {
1982 struct connection *c = userdata;
1983 const char *name, *module, *argument;
1984 uint32_t type;
1985 uint32_t idx;
1986 pa_tagstruct *reply;
1987 assert(c && t);
1988
1989 if (pa_tagstruct_gets(t, &name) < 0 ||
1990 pa_tagstruct_getu32(t, &type) < 0 ||
1991 pa_tagstruct_gets(t, &module) < 0 ||
1992 pa_tagstruct_gets(t, &argument) < 0 ||
1993 !pa_tagstruct_eof(t)) {
1994 protocol_error(c);
1995 return;
1996 }
1997
1998 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1999 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
2000 CHECK_VALIDITY(c->pstream, type == 0 || type == 1, tag, PA_ERR_INVALID);
2001 CHECK_VALIDITY(c->pstream, module && *module && pa_utf8_valid(module), tag, PA_ERR_INVALID);
2002 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
2003
2004 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument, &idx) < 0) {
2005 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
2006 return;
2007 }
2008
2009 reply = reply_new(tag);
2010 pa_tagstruct_putu32(reply, idx);
2011 pa_pstream_send_tagstruct(c->pstream, reply);
2012 }
2013
2014 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) {
2015 struct connection *c = userdata;
2016 const char *name = NULL;
2017 uint32_t type, idx = PA_IDXSET_INVALID;
2018 int r;
2019 assert(c && t);
2020
2021 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
2022 (pa_tagstruct_gets(t, &name) < 0 ||
2023 pa_tagstruct_getu32(t, &type) < 0)) ||
2024 !pa_tagstruct_eof(t)) {
2025 protocol_error(c);
2026 return;
2027 }
2028
2029 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2030 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
2031 CHECK_VALIDITY(c->pstream, !name || (*name && pa_utf8_valid(name) && (type == 0 || type == 1)), tag, PA_ERR_INVALID);
2032
2033 if (name)
2034 r = pa_autoload_remove_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
2035 else
2036 r = pa_autoload_remove_by_index(c->protocol->core, idx);
2037
2038 CHECK_VALIDITY(c->pstream, r >= 0, tag, PA_ERR_NOENTITY);
2039
2040 pa_pstream_send_simple_ack(c->pstream, tag);
2041 }
2042
2043 static void autoload_fill_tagstruct(pa_tagstruct *t, const pa_autoload_entry *e) {
2044 assert(t && e);
2045
2046 pa_tagstruct_putu32(t, e->index);
2047 pa_tagstruct_puts(t, e->name);
2048 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
2049 pa_tagstruct_puts(t, e->module);
2050 pa_tagstruct_puts(t, e->argument);
2051 }
2052
2053 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) {
2054 struct connection *c = userdata;
2055 const pa_autoload_entry *a = NULL;
2056 uint32_t type, idx;
2057 const char *name;
2058 pa_tagstruct *reply;
2059 assert(c && t);
2060
2061 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
2062 (pa_tagstruct_gets(t, &name) < 0 ||
2063 pa_tagstruct_getu32(t, &type) < 0)) ||
2064 !pa_tagstruct_eof(t)) {
2065 protocol_error(c);
2066 return;
2067 }
2068
2069 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2070 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
2071 CHECK_VALIDITY(c->pstream, !name || (*name && (type == 0 || type == 1) && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2072
2073 if (name)
2074 a = pa_autoload_get_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
2075 else
2076 a = pa_autoload_get_by_index(c->protocol->core, idx);
2077
2078 CHECK_VALIDITY(c->pstream, a, tag, PA_ERR_NOENTITY);
2079
2080 reply = reply_new(tag);
2081 autoload_fill_tagstruct(reply, a);
2082 pa_pstream_send_tagstruct(c->pstream, reply);
2083 }
2084
2085 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) {
2086 struct connection *c = userdata;
2087 pa_tagstruct *reply;
2088 assert(c && t);
2089
2090 if (!pa_tagstruct_eof(t)) {
2091 protocol_error(c);
2092 return;
2093 }
2094
2095 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2096
2097 reply = reply_new(tag);
2098
2099 if (c->protocol->core->autoload_hashmap) {
2100 pa_autoload_entry *a;
2101 void *state = NULL;
2102
2103 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state, NULL)))
2104 autoload_fill_tagstruct(reply, a);
2105 }
2106
2107 pa_pstream_send_tagstruct(c->pstream, reply);
2108 }
2109
2110 /*** pstream callbacks ***/
2111
2112 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
2113 struct connection *c = userdata;
2114 assert(p && packet && packet->data && c);
2115
2116 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0) {
2117 pa_log(__FILE__": invalid packet.");
2118 connection_free(c);
2119 }
2120 }
2121
2122 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) {
2123 struct connection *c = userdata;
2124 struct output_stream *stream;
2125 assert(p && chunk && userdata);
2126
2127 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
2128 pa_log(__FILE__": client sent block for invalid stream.");
2129 connection_free(c);
2130 return;
2131 }
2132
2133 if (stream->type == PLAYBACK_STREAM) {
2134 struct playback_stream *ps = (struct playback_stream*) stream;
2135 if (chunk->length >= ps->requested_bytes)
2136 ps->requested_bytes = 0;
2137 else
2138 ps->requested_bytes -= chunk->length;
2139
2140 pa_memblockq_seek(ps->memblockq, offset, seek);
2141
2142 if (pa_memblockq_push_align(ps->memblockq, chunk) < 0) {
2143 pa_tagstruct *t;
2144
2145 pa_log_warn(__FILE__": failed to push data into queue");
2146
2147 /* Pushing this block into the queue failed, so we simulate
2148 * it by skipping ahead */
2149
2150 pa_memblockq_seek(ps->memblockq, chunk->length, PA_SEEK_RELATIVE);
2151
2152 /* Notify the user */
2153 t = pa_tagstruct_new(NULL, 0);
2154 pa_tagstruct_putu32(t, PA_COMMAND_OVERFLOW);
2155 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
2156 pa_tagstruct_putu32(t, ps->index);
2157 pa_pstream_send_tagstruct(p, t);
2158 }
2159
2160 ps->underrun = 0;
2161
2162 pa_sink_notify(ps->sink_input->sink);
2163
2164 } else {
2165 struct upload_stream *u = (struct upload_stream*) stream;
2166 size_t l;
2167 assert(u->type == UPLOAD_STREAM);
2168
2169 if (!u->memchunk.memblock) {
2170 if (u->length == chunk->length) {
2171 u->memchunk = *chunk;
2172 pa_memblock_ref(u->memchunk.memblock);
2173 u->length = 0;
2174 } else {
2175 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
2176 u->memchunk.index = u->memchunk.length = 0;
2177 }
2178 }
2179
2180 assert(u->memchunk.memblock);
2181
2182 l = u->length;
2183 if (l > chunk->length)
2184 l = chunk->length;
2185
2186 if (l > 0) {
2187 memcpy((uint8_t*) u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length,
2188 (uint8_t*) chunk->memblock->data+chunk->index, l);
2189 u->memchunk.length += l;
2190 u->length -= l;
2191 }
2192 }
2193 }
2194
2195 static void pstream_die_callback(pa_pstream *p, void *userdata) {
2196 struct connection *c = userdata;
2197 assert(p && c);
2198 connection_free(c);
2199
2200 /* pa_log(__FILE__": connection died.");*/
2201 }
2202
2203
2204 static void pstream_drain_callback(pa_pstream *p, void *userdata) {
2205 struct connection *c = userdata;
2206 assert(p && c);
2207
2208 send_memblock(c);
2209 }
2210
2211 /*** client callbacks ***/
2212
2213 static void client_kill_cb(pa_client *c) {
2214 assert(c && c->userdata);
2215 connection_free(c->userdata);
2216 }
2217
2218 /*** socket server callbacks ***/
2219
2220 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
2221 struct connection *c = userdata;
2222 assert(m && tv && c && c->auth_timeout_event == e);
2223
2224 if (!c->authorized)
2225 connection_free(c);
2226 }
2227
2228 static void on_connection(PA_GCC_UNUSED pa_socket_server*s, pa_iochannel *io, void *userdata) {
2229 pa_protocol_native *p = userdata;
2230 struct connection *c;
2231 char cname[256], pname[128];
2232 assert(io && p);
2233
2234 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
2235 pa_log_warn(__FILE__": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
2236 pa_iochannel_free(io);
2237 return;
2238 }
2239
2240 c = pa_xmalloc(sizeof(struct connection));
2241
2242 c->authorized =!! p->public;
2243
2244 if (!c->authorized) {
2245 struct timeval tv;
2246 pa_gettimeofday(&tv);
2247 tv.tv_sec += AUTH_TIMEOUT;
2248 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
2249 } else
2250 c->auth_timeout_event = NULL;
2251
2252 c->version = 8;
2253 c->protocol = p;
2254 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
2255 snprintf(cname, sizeof(cname), "Native client (%s)", pname);
2256 assert(p->core);
2257 c->client = pa_client_new(p->core, __FILE__, cname);
2258 assert(c->client);
2259 c->client->kill = client_kill_cb;
2260 c->client->userdata = c;
2261 c->client->owner = p->module;
2262
2263 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
2264 assert(c->pstream);
2265
2266 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
2267 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
2268 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
2269 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
2270
2271 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
2272 assert(c->pdispatch);
2273
2274 c->record_streams = pa_idxset_new(NULL, NULL);
2275 c->output_streams = pa_idxset_new(NULL, NULL);
2276 assert(c->record_streams && c->output_streams);
2277
2278 c->rrobin_index = PA_IDXSET_INVALID;
2279 c->subscription = NULL;
2280
2281 pa_idxset_put(p->connections, c, NULL);
2282
2283
2284 #ifdef HAVE_CREDS
2285 if (pa_iochannel_creds_supported(io))
2286 pa_iochannel_creds_enable(io);
2287
2288 #endif
2289 }
2290
2291 /*** module entry points ***/
2292
2293 static int load_key(pa_protocol_native*p, const char*fn) {
2294 assert(p);
2295
2296 p->auth_cookie_in_property = 0;
2297
2298 if (!fn && pa_authkey_prop_get(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0) {
2299 pa_log_info(__FILE__": using already loaded auth cookie.");
2300 pa_authkey_prop_ref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2301 p->auth_cookie_in_property = 1;
2302 return 0;
2303 }
2304
2305 if (!fn)
2306 fn = PA_NATIVE_COOKIE_FILE;
2307
2308 if (pa_authkey_load_auto(fn, p->auth_cookie, sizeof(p->auth_cookie)) < 0)
2309 return -1;
2310
2311 pa_log_info(__FILE__": loading cookie from disk.");
2312
2313 if (pa_authkey_prop_put(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0)
2314 p->auth_cookie_in_property = 1;
2315
2316 return 0;
2317 }
2318
2319 static pa_protocol_native* protocol_new_internal(pa_core *c, pa_module *m, pa_modargs *ma) {
2320 pa_protocol_native *p;
2321 int public = 0;
2322 assert(c && ma);
2323
2324 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
2325 pa_log(__FILE__": auth-anonymous= expects a boolean argument.");
2326 return NULL;
2327 }
2328
2329 p = pa_xnew(pa_protocol_native, 1);
2330 p->core = c;
2331 p->module = m;
2332 p->public = public;
2333 p->server = NULL;
2334
2335 #ifdef HAVE_CREDS
2336 {
2337 int a = 1;
2338 if (pa_modargs_get_value_boolean(ma, "auth-group-enabled", &a) < 0) {
2339 pa_log(__FILE__": auth-group-enabled= expects a boolean argument.");
2340 return NULL;
2341 }
2342 p->auth_group = a ? pa_xstrdup(pa_modargs_get_value(ma, "auth-group", c->is_system_instance ? PA_ACCESS_GROUP : NULL)) : NULL;
2343
2344 if (p->auth_group)
2345 pa_log_info(__FILE__": Allowing access to group '%s'.", p->auth_group);
2346 }
2347 #endif
2348
2349 if (load_key(p, pa_modargs_get_value(ma, "cookie", NULL)) < 0) {
2350 pa_xfree(p);
2351 return NULL;
2352 }
2353
2354 p->connections = pa_idxset_new(NULL, NULL);
2355 assert(p->connections);
2356
2357 return p;
2358 }
2359
2360 pa_protocol_native* pa_protocol_native_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
2361 char t[256];
2362 pa_protocol_native *p;
2363
2364 if (!(p = protocol_new_internal(core, m, ma)))
2365 return NULL;
2366
2367 p->server = server;
2368 pa_socket_server_set_callback(p->server, on_connection, p);
2369
2370 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
2371 pa_strlist *l;
2372 l = pa_property_get(core, PA_NATIVE_SERVER_PROPERTY_NAME);
2373 l = pa_strlist_prepend(l, t);
2374 pa_property_replace(core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
2375 }
2376
2377 return p;
2378 }
2379
2380 void pa_protocol_native_free(pa_protocol_native *p) {
2381 struct connection *c;
2382 assert(p);
2383
2384 while ((c = pa_idxset_first(p->connections, NULL)))
2385 connection_free(c);
2386 pa_idxset_free(p->connections, NULL, NULL);
2387
2388 if (p->server) {
2389 char t[256];
2390
2391 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
2392 pa_strlist *l;
2393 l = pa_property_get(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
2394 l = pa_strlist_remove(l, t);
2395
2396 if (l)
2397 pa_property_replace(p->core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
2398 else
2399 pa_property_remove(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
2400 }
2401
2402 pa_socket_server_unref(p->server);
2403 }
2404
2405 if (p->auth_cookie_in_property)
2406 pa_authkey_prop_unref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2407
2408 #ifdef HAVE_CREDS
2409 pa_xfree(p->auth_group);
2410 #endif
2411 pa_xfree(p);
2412 }
2413
2414 pa_protocol_native* pa_protocol_native_new_iochannel(pa_core*core, pa_iochannel *io, pa_module *m, pa_modargs *ma) {
2415 pa_protocol_native *p;
2416
2417 if (!(p = protocol_new_internal(core, m, ma)))
2418 return NULL;
2419
2420 on_connection(NULL, io, p);
2421
2422 return p;
2423 }