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