]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-native.c
14f880d7f302d1999373d2eaf82b8132737e8cda
[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
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 64
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 uint32_t maxlength, tlength, prebuf, minreq, sink_index, syncid;
700 const char *name, *sink_name;
701 pa_sample_spec ss;
702 pa_channel_map map;
703 pa_tagstruct *reply;
704 pa_sink *sink;
705 pa_cvolume volume;
706 int corked;
707
708 assert(c && t && c->protocol && c->protocol->core);
709
710 if (pa_tagstruct_get(
711 t,
712 PA_TAG_STRING, &name,
713 PA_TAG_SAMPLE_SPEC, &ss,
714 PA_TAG_CHANNEL_MAP, &map,
715 PA_TAG_U32, &sink_index,
716 PA_TAG_STRING, &sink_name,
717 PA_TAG_U32, &maxlength,
718 PA_TAG_BOOLEAN, &corked,
719 PA_TAG_U32, &tlength,
720 PA_TAG_U32, &prebuf,
721 PA_TAG_U32, &minreq,
722 PA_TAG_U32, &syncid,
723 PA_TAG_CVOLUME, &volume,
724 PA_TAG_INVALID) < 0 ||
725 !pa_tagstruct_eof(t) ||
726 !name) {
727 protocol_error(c);
728 return;
729 }
730
731 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
732 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
733 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
734 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
735 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
736 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
737 CHECK_VALIDITY(c->pstream, map.channels == ss.channels && volume.channels == ss.channels, tag, PA_ERR_INVALID);
738 CHECK_VALIDITY(c->pstream, maxlength > 0 && maxlength <= MAX_MEMBLOCKQ_LENGTH, tag, PA_ERR_INVALID);
739
740 if (sink_index != PA_INVALID_INDEX)
741 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
742 else
743 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
744
745 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
746
747 s = playback_stream_new(c, sink, &ss, &map, name, maxlength, tlength, prebuf, minreq, &volume, syncid);
748 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
749
750 pa_sink_input_cork(s->sink_input, corked);
751
752 reply = reply_new(tag);
753 pa_tagstruct_putu32(reply, s->index);
754 assert(s->sink_input);
755 pa_tagstruct_putu32(reply, s->sink_input->index);
756 pa_tagstruct_putu32(reply, s->requested_bytes = pa_memblockq_missing(s->memblockq));
757
758 if (c->version >= 9) {
759 /* Since 0.9 we support sending the buffer metrics back to the client */
760
761 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_maxlength(s->memblockq));
762 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_tlength(s->memblockq));
763 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_prebuf(s->memblockq));
764 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_minreq(s->memblockq));
765 }
766
767 pa_pstream_send_tagstruct(c->pstream, reply);
768 request_bytes(s);
769 }
770
771 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
772 struct connection *c = userdata;
773 uint32_t channel;
774 assert(c && t);
775
776 if (pa_tagstruct_getu32(t, &channel) < 0 ||
777 !pa_tagstruct_eof(t)) {
778 protocol_error(c);
779 return;
780 }
781
782 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
783
784 if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
785 struct playback_stream *s;
786 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
787 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
788 return;
789 }
790
791 playback_stream_free(s);
792 } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
793 struct record_stream *s;
794 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
795 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
796 return;
797 }
798
799 record_stream_free(s);
800 } else {
801 struct upload_stream *s;
802 assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
803 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
804 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
805 return;
806 }
807
808 upload_stream_free(s);
809 }
810
811 pa_pstream_send_simple_ack(c->pstream, tag);
812 }
813
814 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) {
815 struct connection *c = userdata;
816 struct record_stream *s;
817 uint32_t maxlength, fragment_size;
818 uint32_t source_index;
819 const char *name, *source_name;
820 pa_sample_spec ss;
821 pa_channel_map map;
822 pa_tagstruct *reply;
823 pa_source *source;
824 int corked;
825 assert(c && t && c->protocol && c->protocol->core);
826
827 if (pa_tagstruct_gets(t, &name) < 0 ||
828 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
829 pa_tagstruct_get_channel_map(t, &map) < 0 ||
830 pa_tagstruct_getu32(t, &source_index) < 0 ||
831 pa_tagstruct_gets(t, &source_name) < 0 ||
832 pa_tagstruct_getu32(t, &maxlength) < 0 ||
833 pa_tagstruct_get_boolean(t, &corked) < 0 ||
834 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
835 !pa_tagstruct_eof(t)) {
836 protocol_error(c);
837 return;
838 }
839
840 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
841 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
842 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
843 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
844 CHECK_VALIDITY(c->pstream, source_index != PA_INVALID_INDEX || !source_name || (*source_name && pa_utf8_valid(source_name)), tag, PA_ERR_INVALID);
845 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
846 CHECK_VALIDITY(c->pstream, maxlength <= MAX_MEMBLOCKQ_LENGTH, tag, PA_ERR_INVALID);
847
848 if (source_index != PA_INVALID_INDEX)
849 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
850 else
851 source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1);
852
853 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
854
855 s = record_stream_new(c, source, &ss, &map, name, maxlength, fragment_size);
856 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
857
858 pa_source_output_cork(s->source_output, corked);
859
860 reply = reply_new(tag);
861 pa_tagstruct_putu32(reply, s->index);
862 assert(s->source_output);
863 pa_tagstruct_putu32(reply, s->source_output->index);
864
865 if (c->version >= 9) {
866 /* Since 0.9 we support sending the buffer metrics back to the client */
867
868 pa_tagstruct_putu32(reply, (uint32_t) pa_memblockq_get_maxlength(s->memblockq));
869 pa_tagstruct_putu32(reply, (uint32_t) s->fragment_size);
870 }
871
872 pa_pstream_send_tagstruct(c->pstream, reply);
873 }
874
875 static void command_exit(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
876 struct connection *c = userdata;
877 assert(c && t);
878
879 if (!pa_tagstruct_eof(t)) {
880 protocol_error(c);
881 return;
882 }
883
884 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
885
886 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
887 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
888 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
889 }
890
891 static void command_auth(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
892 struct connection *c = userdata;
893 const void*cookie;
894 pa_tagstruct *reply;
895 assert(c && t);
896
897 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
898 pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
899 !pa_tagstruct_eof(t)) {
900 protocol_error(c);
901 return;
902 }
903
904 /* Minimum supported version */
905 if (c->version < 8) {
906 pa_pstream_send_error(c->pstream, tag, PA_ERR_VERSION);
907 return;
908 }
909
910 if (!c->authorized) {
911 int success = 0;
912
913 #ifdef SCM_CREDENTIALS
914 const struct ucred *ucred = pa_pdispatch_creds(pd);
915
916 if (ucred) {
917 if (ucred->uid == getuid())
918 success = 1;
919 else if (c->protocol->auth_group) {
920 int r;
921
922 if ((r = pa_uid_in_group(ucred->uid, c->protocol->auth_group)) < 0)
923 pa_log_warn(__FILE__": failed to check group membership.");
924 else if (r > 0)
925 success = 1;
926 }
927
928 pa_log_info(__FILE__": Got credentials: pid=%lu uid=%lu gid=%lu auth=%i",
929 (unsigned long) ucred->pid,
930 (unsigned long) ucred->uid,
931 (unsigned long) ucred->gid,
932 success);
933 }
934 #endif
935
936 if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) == 0)
937 success = 1;
938
939 if (!success) {
940 pa_log_warn(__FILE__": Denied access to client with invalid authorization data.");
941 pa_pstream_send_error(c->pstream, tag, PA_ERR_ACCESS);
942 return;
943 }
944
945 c->authorized = 1;
946 if (c->auth_timeout_event) {
947 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
948 c->auth_timeout_event = NULL;
949 }
950 }
951
952 reply = reply_new(tag);
953 pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION);
954 pa_pstream_send_tagstruct(c->pstream, reply);
955 }
956
957 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) {
958 struct connection *c = userdata;
959 const char *name;
960 assert(c && t);
961
962 if (pa_tagstruct_gets(t, &name) < 0 ||
963 !pa_tagstruct_eof(t)) {
964 protocol_error(c);
965 return;
966 }
967
968 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
969
970 pa_client_set_name(c->client, name);
971 pa_pstream_send_simple_ack(c->pstream, tag);
972 }
973
974 static void command_lookup(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
975 struct connection *c = userdata;
976 const char *name;
977 uint32_t idx = PA_IDXSET_INVALID;
978 assert(c && t);
979
980 if (pa_tagstruct_gets(t, &name) < 0 ||
981 !pa_tagstruct_eof(t)) {
982 protocol_error(c);
983 return;
984 }
985
986 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
987 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
988
989 if (command == PA_COMMAND_LOOKUP_SINK) {
990 pa_sink *sink;
991 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
992 idx = sink->index;
993 } else {
994 pa_source *source;
995 assert(command == PA_COMMAND_LOOKUP_SOURCE);
996 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
997 idx = source->index;
998 }
999
1000 if (idx == PA_IDXSET_INVALID)
1001 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1002 else {
1003 pa_tagstruct *reply;
1004 reply = reply_new(tag);
1005 pa_tagstruct_putu32(reply, idx);
1006 pa_pstream_send_tagstruct(c->pstream, reply);
1007 }
1008 }
1009
1010 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) {
1011 struct connection *c = userdata;
1012 uint32_t idx;
1013 struct playback_stream *s;
1014 assert(c && t);
1015
1016 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1017 !pa_tagstruct_eof(t)) {
1018 protocol_error(c);
1019 return;
1020 }
1021
1022 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1023 s = pa_idxset_get_by_index(c->output_streams, idx);
1024 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1025 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1026
1027 s->drain_request = 0;
1028
1029 pa_memblockq_prebuf_disable(s->memblockq);
1030
1031 if (!pa_memblockq_is_readable(s->memblockq)) {
1032 /* pa_log("immediate drain: %u", pa_memblockq_get_length(s->memblockq)); */
1033 pa_pstream_send_simple_ack(c->pstream, tag);
1034 } else {
1035 /* pa_log("slow drain triggered"); */
1036 s->drain_request = 1;
1037 s->drain_tag = tag;
1038
1039 pa_sink_notify(s->sink_input->sink);
1040 }
1041 }
1042
1043 static void command_stat(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1044 struct connection *c = userdata;
1045 pa_tagstruct *reply;
1046 assert(c && t);
1047
1048 if (!pa_tagstruct_eof(t)) {
1049 protocol_error(c);
1050 return;
1051 }
1052
1053 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1054
1055 reply = reply_new(tag);
1056 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total);
1057 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total_size);
1058 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated);
1059 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated_size);
1060 pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
1061 pa_pstream_send_tagstruct(c->pstream, reply);
1062 }
1063
1064 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) {
1065 struct connection *c = userdata;
1066 pa_tagstruct *reply;
1067 struct playback_stream *s;
1068 struct timeval tv, now;
1069 uint32_t idx;
1070 pa_usec_t latency;
1071 assert(c && t);
1072
1073 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1074 pa_tagstruct_get_timeval(t, &tv) < 0 ||
1075 !pa_tagstruct_eof(t)) {
1076 protocol_error(c);
1077 return;
1078 }
1079
1080 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1081 s = pa_idxset_get_by_index(c->output_streams, idx);
1082 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1083 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1084
1085 reply = reply_new(tag);
1086
1087 latency = pa_sink_get_latency(s->sink_input->sink);
1088 if (s->sink_input->resampled_chunk.memblock)
1089 latency += pa_bytes_to_usec(s->sink_input->resampled_chunk.length, &s->sink_input->sample_spec);
1090 pa_tagstruct_put_usec(reply, latency);
1091
1092 pa_tagstruct_put_usec(reply, 0);
1093 pa_tagstruct_put_boolean(reply, pa_memblockq_is_readable(s->memblockq));
1094 pa_tagstruct_put_timeval(reply, &tv);
1095 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
1096 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
1097 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
1098 pa_pstream_send_tagstruct(c->pstream, reply);
1099 }
1100
1101 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) {
1102 struct connection *c = userdata;
1103 pa_tagstruct *reply;
1104 struct record_stream *s;
1105 struct timeval tv, now;
1106 uint32_t idx;
1107 assert(c && t);
1108
1109 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1110 pa_tagstruct_get_timeval(t, &tv) < 0 ||
1111 !pa_tagstruct_eof(t)) {
1112 protocol_error(c);
1113 return;
1114 }
1115
1116 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1117 s = pa_idxset_get_by_index(c->record_streams, idx);
1118 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1119
1120 reply = reply_new(tag);
1121 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
1122 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
1123 pa_tagstruct_put_boolean(reply, 0);
1124 pa_tagstruct_put_timeval(reply, &tv);
1125 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
1126 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
1127 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
1128 pa_pstream_send_tagstruct(c->pstream, reply);
1129 }
1130
1131 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) {
1132 struct connection *c = userdata;
1133 struct upload_stream *s;
1134 uint32_t length;
1135 const char *name;
1136 pa_sample_spec ss;
1137 pa_channel_map map;
1138 pa_tagstruct *reply;
1139 assert(c && t && c->protocol && c->protocol->core);
1140
1141 if (pa_tagstruct_gets(t, &name) < 0 ||
1142 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
1143 pa_tagstruct_get_channel_map(t, &map) < 0 ||
1144 pa_tagstruct_getu32(t, &length) < 0 ||
1145 !pa_tagstruct_eof(t)) {
1146 protocol_error(c);
1147 return;
1148 }
1149
1150 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1151 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1152 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1153 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
1154 CHECK_VALIDITY(c->pstream, (length % pa_frame_size(&ss)) == 0 && length > 0, tag, PA_ERR_INVALID);
1155 CHECK_VALIDITY(c->pstream, length <= PA_SCACHE_ENTRY_SIZE_MAX, tag, PA_ERR_TOOLARGE);
1156 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1157
1158 s = upload_stream_new(c, &ss, &map, name, length);
1159 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1160
1161 reply = reply_new(tag);
1162 pa_tagstruct_putu32(reply, s->index);
1163 pa_tagstruct_putu32(reply, length);
1164 pa_pstream_send_tagstruct(c->pstream, reply);
1165 }
1166
1167 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) {
1168 struct connection *c = userdata;
1169 uint32_t channel;
1170 struct upload_stream *s;
1171 uint32_t idx;
1172 assert(c && t);
1173
1174 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1175 !pa_tagstruct_eof(t)) {
1176 protocol_error(c);
1177 return;
1178 }
1179
1180 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1181
1182 s = pa_idxset_get_by_index(c->output_streams, channel);
1183 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1184 CHECK_VALIDITY(c->pstream, s->type == UPLOAD_STREAM, tag, PA_ERR_NOENTITY);
1185
1186 if (pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->channel_map, &s->memchunk, &idx) < 0)
1187 pa_pstream_send_error(c->pstream, tag, PA_ERR_INTERNAL);
1188 else
1189 pa_pstream_send_simple_ack(c->pstream, tag);
1190
1191 upload_stream_free(s);
1192 }
1193
1194 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) {
1195 struct connection *c = userdata;
1196 uint32_t sink_index;
1197 pa_volume_t volume;
1198 pa_sink *sink;
1199 const char *name, *sink_name;
1200 assert(c && t);
1201
1202 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
1203 pa_tagstruct_gets(t, &sink_name) < 0 ||
1204 pa_tagstruct_getu32(t, &volume) < 0 ||
1205 pa_tagstruct_gets(t, &name) < 0 ||
1206 !pa_tagstruct_eof(t)) {
1207 protocol_error(c);
1208 return;
1209 }
1210
1211 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1212 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1213 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1214
1215 if (sink_index != PA_INVALID_INDEX)
1216 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1217 else
1218 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1219
1220 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
1221
1222 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
1223 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1224 return;
1225 }
1226
1227 pa_pstream_send_simple_ack(c->pstream, tag);
1228 }
1229
1230 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) {
1231 struct connection *c = userdata;
1232 const char *name;
1233 assert(c && t);
1234
1235 if (pa_tagstruct_gets(t, &name) < 0 ||
1236 !pa_tagstruct_eof(t)) {
1237 protocol_error(c);
1238 return;
1239 }
1240
1241 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1242 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1243
1244 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
1245 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1246 return;
1247 }
1248
1249 pa_pstream_send_simple_ack(c->pstream, tag);
1250 }
1251
1252 static void sink_fill_tagstruct(pa_tagstruct *t, pa_sink *sink) {
1253 assert(t && sink);
1254 pa_tagstruct_put(
1255 t,
1256 PA_TAG_U32, sink->index,
1257 PA_TAG_STRING, sink->name,
1258 PA_TAG_STRING, sink->description,
1259 PA_TAG_SAMPLE_SPEC, &sink->sample_spec,
1260 PA_TAG_CHANNEL_MAP, &sink->channel_map,
1261 PA_TAG_U32, sink->owner ? sink->owner->index : PA_INVALID_INDEX,
1262 PA_TAG_CVOLUME, pa_sink_get_volume(sink, PA_MIXER_HARDWARE),
1263 PA_TAG_BOOLEAN, pa_sink_get_mute(sink, PA_MIXER_HARDWARE),
1264 PA_TAG_U32, sink->monitor_source->index,
1265 PA_TAG_STRING, sink->monitor_source->name,
1266 PA_TAG_USEC, pa_sink_get_latency(sink),
1267 PA_TAG_STRING, sink->driver,
1268 PA_TAG_U32,
1269 (sink->get_hw_volume ? PA_SINK_HW_VOLUME_CTRL : 0) |
1270 (sink->get_latency ? PA_SINK_LATENCY : 0) |
1271 (sink->is_hardware ? PA_SINK_HARDWARE : 0),
1272 PA_TAG_INVALID);
1273 }
1274
1275 static void source_fill_tagstruct(pa_tagstruct *t, pa_source *source) {
1276 assert(t && source);
1277 pa_tagstruct_put(
1278 t,
1279 PA_TAG_U32, source->index,
1280 PA_TAG_STRING, source->name,
1281 PA_TAG_STRING, source->description,
1282 PA_TAG_SAMPLE_SPEC, &source->sample_spec,
1283 PA_TAG_CHANNEL_MAP, &source->channel_map,
1284 PA_TAG_U32, source->owner ? source->owner->index : PA_INVALID_INDEX,
1285 PA_TAG_CVOLUME, pa_source_get_volume(source, PA_MIXER_HARDWARE),
1286 PA_TAG_BOOLEAN, pa_source_get_mute(source, PA_MIXER_HARDWARE),
1287 PA_TAG_U32, source->monitor_of ? source->monitor_of->index : PA_INVALID_INDEX,
1288 PA_TAG_STRING, source->monitor_of ? source->monitor_of->name : NULL,
1289 PA_TAG_USEC, pa_source_get_latency(source),
1290 PA_TAG_STRING, source->driver,
1291 PA_TAG_U32,
1292 (source->get_hw_volume ? PA_SOURCE_HW_VOLUME_CTRL : 0) |
1293 (source->get_latency ? PA_SOURCE_LATENCY : 0) |
1294 (source->is_hardware ? PA_SOURCE_HARDWARE : 0),
1295 PA_TAG_INVALID);
1296 }
1297
1298 static void client_fill_tagstruct(pa_tagstruct *t, pa_client *client) {
1299 assert(t && client);
1300 pa_tagstruct_putu32(t, client->index);
1301 pa_tagstruct_puts(t, client->name);
1302 pa_tagstruct_putu32(t, client->owner ? client->owner->index : PA_INVALID_INDEX);
1303 pa_tagstruct_puts(t, client->driver);
1304 }
1305
1306 static void module_fill_tagstruct(pa_tagstruct *t, pa_module *module) {
1307 assert(t && module);
1308 pa_tagstruct_putu32(t, module->index);
1309 pa_tagstruct_puts(t, module->name);
1310 pa_tagstruct_puts(t, module->argument);
1311 pa_tagstruct_putu32(t, module->n_used);
1312 pa_tagstruct_put_boolean(t, module->auto_unload);
1313 }
1314
1315 static void sink_input_fill_tagstruct(pa_tagstruct *t, pa_sink_input *s) {
1316 assert(t && s);
1317 pa_tagstruct_putu32(t, s->index);
1318 pa_tagstruct_puts(t, s->name);
1319 pa_tagstruct_putu32(t, s->owner ? s->owner->index : PA_INVALID_INDEX);
1320 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
1321 pa_tagstruct_putu32(t, s->sink->index);
1322 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1323 pa_tagstruct_put_channel_map(t, &s->channel_map);
1324 pa_tagstruct_put_cvolume(t, &s->volume);
1325 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
1326 pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
1327 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
1328 pa_tagstruct_puts(t, s->driver);
1329 }
1330
1331 static void source_output_fill_tagstruct(pa_tagstruct *t, pa_source_output *s) {
1332 assert(t && s);
1333 pa_tagstruct_putu32(t, s->index);
1334 pa_tagstruct_puts(t, s->name);
1335 pa_tagstruct_putu32(t, s->owner ? s->owner->index : PA_INVALID_INDEX);
1336 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
1337 pa_tagstruct_putu32(t, s->source->index);
1338 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1339 pa_tagstruct_put_channel_map(t, &s->channel_map);
1340 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
1341 pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
1342 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
1343 pa_tagstruct_puts(t, s->driver);
1344 }
1345
1346 static void scache_fill_tagstruct(pa_tagstruct *t, pa_scache_entry *e) {
1347 assert(t && e);
1348 pa_tagstruct_putu32(t, e->index);
1349 pa_tagstruct_puts(t, e->name);
1350 pa_tagstruct_put_cvolume(t, &e->volume);
1351 pa_tagstruct_put_usec(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1352 pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1353 pa_tagstruct_put_channel_map(t, &e->channel_map);
1354 pa_tagstruct_putu32(t, e->memchunk.length);
1355 pa_tagstruct_put_boolean(t, e->lazy);
1356 pa_tagstruct_puts(t, e->filename);
1357 }
1358
1359 static void command_get_info(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1360 struct connection *c = userdata;
1361 uint32_t idx;
1362 pa_sink *sink = NULL;
1363 pa_source *source = NULL;
1364 pa_client *client = NULL;
1365 pa_module *module = NULL;
1366 pa_sink_input *si = NULL;
1367 pa_source_output *so = NULL;
1368 pa_scache_entry *sce = NULL;
1369 const char *name;
1370 pa_tagstruct *reply;
1371 assert(c && t);
1372
1373 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1374 (command != PA_COMMAND_GET_CLIENT_INFO &&
1375 command != PA_COMMAND_GET_MODULE_INFO &&
1376 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1377 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1378 pa_tagstruct_gets(t, &name) < 0) ||
1379 !pa_tagstruct_eof(t)) {
1380 protocol_error(c);
1381 return;
1382 }
1383
1384 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1385 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1386
1387 if (command == PA_COMMAND_GET_SINK_INFO) {
1388 if (idx != PA_INVALID_INDEX)
1389 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1390 else
1391 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1392 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1393 if (idx != PA_INVALID_INDEX)
1394 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1395 else
1396 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1397 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1398 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
1399 else if (command == PA_COMMAND_GET_MODULE_INFO)
1400 module = pa_idxset_get_by_index(c->protocol->core->modules, idx);
1401 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1402 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1403 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1404 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
1405 else {
1406 assert(command == PA_COMMAND_GET_SAMPLE_INFO);
1407 if (idx != PA_INVALID_INDEX)
1408 sce = pa_idxset_get_by_index(c->protocol->core->scache, idx);
1409 else
1410 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1411 }
1412
1413 if (!sink && !source && !client && !module && !si && !so && !sce) {
1414 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1415 return;
1416 }
1417
1418 reply = reply_new(tag);
1419 if (sink)
1420 sink_fill_tagstruct(reply, sink);
1421 else if (source)
1422 source_fill_tagstruct(reply, source);
1423 else if (client)
1424 client_fill_tagstruct(reply, client);
1425 else if (module)
1426 module_fill_tagstruct(reply, module);
1427 else if (si)
1428 sink_input_fill_tagstruct(reply, si);
1429 else if (so)
1430 source_output_fill_tagstruct(reply, so);
1431 else
1432 scache_fill_tagstruct(reply, sce);
1433 pa_pstream_send_tagstruct(c->pstream, reply);
1434 }
1435
1436 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1437 struct connection *c = userdata;
1438 pa_idxset *i;
1439 uint32_t idx;
1440 void *p;
1441 pa_tagstruct *reply;
1442 assert(c && t);
1443
1444 if (!pa_tagstruct_eof(t)) {
1445 protocol_error(c);
1446 return;
1447 }
1448
1449 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1450
1451 reply = reply_new(tag);
1452
1453 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1454 i = c->protocol->core->sinks;
1455 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1456 i = c->protocol->core->sources;
1457 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1458 i = c->protocol->core->clients;
1459 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1460 i = c->protocol->core->modules;
1461 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1462 i = c->protocol->core->sink_inputs;
1463 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1464 i = c->protocol->core->source_outputs;
1465 else {
1466 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1467 i = c->protocol->core->scache;
1468 }
1469
1470 if (i) {
1471 for (p = pa_idxset_first(i, &idx); p; p = pa_idxset_next(i, &idx)) {
1472 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1473 sink_fill_tagstruct(reply, p);
1474 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1475 source_fill_tagstruct(reply, p);
1476 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1477 client_fill_tagstruct(reply, p);
1478 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1479 module_fill_tagstruct(reply, p);
1480 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1481 sink_input_fill_tagstruct(reply, p);
1482 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1483 source_output_fill_tagstruct(reply, p);
1484 else {
1485 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1486 scache_fill_tagstruct(reply, p);
1487 }
1488 }
1489 }
1490
1491 pa_pstream_send_tagstruct(c->pstream, reply);
1492 }
1493
1494 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) {
1495 struct connection *c = userdata;
1496 pa_tagstruct *reply;
1497 char txt[256];
1498 const char *n;
1499 assert(c && t);
1500
1501 if (!pa_tagstruct_eof(t)) {
1502 protocol_error(c);
1503 return;
1504 }
1505
1506 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1507
1508 reply = reply_new(tag);
1509 pa_tagstruct_puts(reply, PACKAGE_NAME);
1510 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1511 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1512 pa_tagstruct_puts(reply, pa_get_fqdn(txt, sizeof(txt)));
1513 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1514
1515 n = pa_namereg_get_default_sink_name(c->protocol->core);
1516 pa_tagstruct_puts(reply, n);
1517 n = pa_namereg_get_default_source_name(c->protocol->core);
1518 pa_tagstruct_puts(reply, n);
1519
1520 pa_tagstruct_putu32(reply, c->protocol->core->cookie);
1521
1522 pa_pstream_send_tagstruct(c->pstream, reply);
1523 }
1524
1525 static void subscription_cb(pa_core *core, pa_subscription_event_type_t e, uint32_t idx, void *userdata) {
1526 pa_tagstruct *t;
1527 struct connection *c = userdata;
1528 assert(c && core);
1529
1530 t = pa_tagstruct_new(NULL, 0);
1531 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1532 pa_tagstruct_putu32(t, (uint32_t) -1);
1533 pa_tagstruct_putu32(t, e);
1534 pa_tagstruct_putu32(t, idx);
1535 pa_pstream_send_tagstruct(c->pstream, t);
1536 }
1537
1538 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1539 struct connection *c = userdata;
1540 pa_subscription_mask_t m;
1541 assert(c && t);
1542
1543 if (pa_tagstruct_getu32(t, &m) < 0 ||
1544 !pa_tagstruct_eof(t)) {
1545 protocol_error(c);
1546 return;
1547 }
1548
1549 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1550 CHECK_VALIDITY(c->pstream, (m & ~PA_SUBSCRIPTION_MASK_ALL) == 0, tag, PA_ERR_INVALID);
1551
1552 if (c->subscription)
1553 pa_subscription_free(c->subscription);
1554
1555 if (m != 0) {
1556 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1557 assert(c->subscription);
1558 } else
1559 c->subscription = NULL;
1560
1561 pa_pstream_send_simple_ack(c->pstream, tag);
1562 }
1563
1564 static void command_set_volume(
1565 PA_GCC_UNUSED pa_pdispatch *pd,
1566 uint32_t command,
1567 uint32_t tag,
1568 pa_tagstruct *t,
1569 void *userdata) {
1570
1571 struct connection *c = userdata;
1572 uint32_t idx;
1573 pa_cvolume volume;
1574 pa_sink *sink = NULL;
1575 pa_source *source = NULL;
1576 pa_sink_input *si = NULL;
1577 const char *name = NULL;
1578 assert(c && t);
1579
1580 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1581 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1582 (command == PA_COMMAND_SET_SOURCE_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1583 pa_tagstruct_get_cvolume(t, &volume) ||
1584 !pa_tagstruct_eof(t)) {
1585 protocol_error(c);
1586 return;
1587 }
1588
1589 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1590 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1591 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
1592
1593 if (command == PA_COMMAND_SET_SINK_VOLUME) {
1594 if (idx != PA_INVALID_INDEX)
1595 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1596 else
1597 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1598 } else if (command == PA_COMMAND_SET_SOURCE_VOLUME) {
1599 if (idx != (uint32_t) -1)
1600 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1601 else
1602 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1603 } else {
1604 assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1605 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1606 }
1607
1608 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
1609
1610 if (sink)
1611 pa_sink_set_volume(sink, PA_MIXER_HARDWARE, &volume);
1612 else if (source)
1613 pa_source_set_volume(source, PA_MIXER_HARDWARE, &volume);
1614 else if (si)
1615 pa_sink_input_set_volume(si, &volume);
1616
1617 pa_pstream_send_simple_ack(c->pstream, tag);
1618 }
1619
1620 static void command_set_mute(
1621 PA_GCC_UNUSED pa_pdispatch *pd,
1622 uint32_t command,
1623 uint32_t tag,
1624 pa_tagstruct *t,
1625 void *userdata) {
1626
1627 struct connection *c = userdata;
1628 uint32_t idx;
1629 int mute;
1630 pa_sink *sink = NULL;
1631 pa_source *source = NULL;
1632 const char *name = NULL;
1633 assert(c && t);
1634
1635 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1636 pa_tagstruct_gets(t, &name) < 0 ||
1637 pa_tagstruct_get_boolean(t, &mute) ||
1638 !pa_tagstruct_eof(t)) {
1639 protocol_error(c);
1640 return;
1641 }
1642
1643 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1644 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1645
1646 if (command == PA_COMMAND_SET_SINK_MUTE) {
1647 if (idx != PA_INVALID_INDEX)
1648 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1649 else
1650 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1651 } else {
1652 assert(command == PA_COMMAND_SET_SOURCE_MUTE);
1653 if (idx != (uint32_t) -1)
1654 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1655 else
1656 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1657 }
1658
1659 CHECK_VALIDITY(c->pstream, sink || source, tag, PA_ERR_NOENTITY);
1660
1661 if (sink)
1662 pa_sink_set_mute(sink, PA_MIXER_HARDWARE, mute);
1663 else if (source)
1664 pa_source_set_mute(source, PA_MIXER_HARDWARE, mute);
1665
1666 pa_pstream_send_simple_ack(c->pstream, tag);
1667 }
1668
1669 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) {
1670 struct connection *c = userdata;
1671 uint32_t idx;
1672 int b;
1673 struct playback_stream *s, *ssync;
1674 assert(c && t);
1675
1676 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1677 pa_tagstruct_get_boolean(t, &b) < 0 ||
1678 !pa_tagstruct_eof(t)) {
1679 protocol_error(c);
1680 return;
1681 }
1682
1683 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1684 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1685 s = pa_idxset_get_by_index(c->output_streams, idx);
1686 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1687 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1688
1689 pa_sink_input_cork(s->sink_input, b);
1690 pa_memblockq_prebuf_force(s->memblockq);
1691
1692 /* Do the same for all other members in the sync group */
1693 for (ssync = s->prev; ssync; ssync = ssync->prev) {
1694 pa_sink_input_cork(ssync->sink_input, b);
1695 pa_memblockq_prebuf_force(ssync->memblockq);
1696 }
1697
1698 for (ssync = s->next; ssync; ssync = ssync->next) {
1699 pa_sink_input_cork(ssync->sink_input, b);
1700 pa_memblockq_prebuf_force(ssync->memblockq);
1701 }
1702
1703 pa_pstream_send_simple_ack(c->pstream, tag);
1704 }
1705
1706 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) {
1707 struct connection *c = userdata;
1708 uint32_t idx;
1709 struct playback_stream *s, *ssync;
1710 assert(c && t);
1711
1712 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1713 !pa_tagstruct_eof(t)) {
1714 protocol_error(c);
1715 return;
1716 }
1717
1718 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1719 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1720 s = pa_idxset_get_by_index(c->output_streams, idx);
1721 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1722 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1723
1724 pa_memblockq_flush(s->memblockq);
1725 s->underrun = 0;
1726
1727 /* Do the same for all other members in the sync group */
1728 for (ssync = s->prev; ssync; ssync = ssync->prev) {
1729 pa_memblockq_flush(ssync->memblockq);
1730 ssync->underrun = 0;
1731 }
1732
1733 for (ssync = s->next; ssync; ssync = ssync->next) {
1734 pa_memblockq_flush(ssync->memblockq);
1735 ssync->underrun = 0;
1736 }
1737
1738 pa_pstream_send_simple_ack(c->pstream, tag);
1739 pa_sink_notify(s->sink_input->sink);
1740 request_bytes(s);
1741
1742 for (ssync = s->prev; ssync; ssync = ssync->prev)
1743 request_bytes(ssync);
1744
1745 for (ssync = s->next; ssync; ssync = ssync->next)
1746 request_bytes(ssync);
1747 }
1748
1749 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) {
1750 struct connection *c = userdata;
1751 uint32_t idx;
1752 struct playback_stream *s;
1753 assert(c && t);
1754
1755 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1756 !pa_tagstruct_eof(t)) {
1757 protocol_error(c);
1758 return;
1759 }
1760
1761 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1762 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1763 s = pa_idxset_get_by_index(c->output_streams, idx);
1764 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1765 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1766
1767 switch (command) {
1768 case PA_COMMAND_PREBUF_PLAYBACK_STREAM:
1769 pa_memblockq_prebuf_force(s->memblockq);
1770 break;
1771
1772 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM:
1773 pa_memblockq_prebuf_disable(s->memblockq);
1774 break;
1775
1776 default:
1777 abort();
1778 }
1779
1780 pa_sink_notify(s->sink_input->sink);
1781 pa_pstream_send_simple_ack(c->pstream, tag);
1782 request_bytes(s);
1783 }
1784
1785 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) {
1786 struct connection *c = userdata;
1787 uint32_t idx;
1788 struct record_stream *s;
1789 int b;
1790 assert(c && t);
1791
1792 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1793 pa_tagstruct_get_boolean(t, &b) < 0 ||
1794 !pa_tagstruct_eof(t)) {
1795 protocol_error(c);
1796 return;
1797 }
1798
1799 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1800 s = pa_idxset_get_by_index(c->record_streams, idx);
1801 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1802
1803 pa_source_output_cork(s->source_output, b);
1804 pa_memblockq_prebuf_force(s->memblockq);
1805 pa_pstream_send_simple_ack(c->pstream, tag);
1806 }
1807
1808 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) {
1809 struct connection *c = userdata;
1810 uint32_t idx;
1811 struct record_stream *s;
1812 assert(c && t);
1813
1814 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1815 !pa_tagstruct_eof(t)) {
1816 protocol_error(c);
1817 return;
1818 }
1819
1820 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1821 s = pa_idxset_get_by_index(c->record_streams, idx);
1822 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1823
1824 pa_memblockq_flush(s->memblockq);
1825 pa_pstream_send_simple_ack(c->pstream, tag);
1826 }
1827
1828 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) {
1829 struct connection *c = userdata;
1830 const char *s;
1831 assert(c && t);
1832
1833 if (pa_tagstruct_gets(t, &s) < 0 ||
1834 !pa_tagstruct_eof(t)) {
1835 protocol_error(c);
1836 return;
1837 }
1838
1839 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1840 CHECK_VALIDITY(c->pstream, !s || (*s && pa_utf8_valid(s)), tag, PA_ERR_INVALID);
1841
1842 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
1843 pa_pstream_send_simple_ack(c->pstream, tag);
1844 }
1845
1846 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1847 struct connection *c = userdata;
1848 uint32_t idx;
1849 const char *name;
1850 assert(c && t);
1851
1852 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1853 pa_tagstruct_gets(t, &name) < 0 ||
1854 !pa_tagstruct_eof(t)) {
1855 protocol_error(c);
1856 return;
1857 }
1858
1859 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1860 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1861
1862 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
1863 struct playback_stream *s;
1864
1865 s = pa_idxset_get_by_index(c->output_streams, idx);
1866 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1867 CHECK_VALIDITY(c->pstream, s->type == PLAYBACK_STREAM, tag, PA_ERR_NOENTITY);
1868
1869 pa_sink_input_set_name(s->sink_input, name);
1870
1871 } else {
1872 struct record_stream *s;
1873
1874 s = pa_idxset_get_by_index(c->record_streams, idx);
1875 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1876
1877 pa_source_output_set_name(s->source_output, name);
1878 }
1879
1880 pa_pstream_send_simple_ack(c->pstream, tag);
1881 }
1882
1883 static void command_kill(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1884 struct connection *c = userdata;
1885 uint32_t idx;
1886 assert(c && t);
1887
1888 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1889 !pa_tagstruct_eof(t)) {
1890 protocol_error(c);
1891 return;
1892 }
1893
1894 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1895
1896 if (command == PA_COMMAND_KILL_CLIENT) {
1897 pa_client *client;
1898
1899 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
1900 CHECK_VALIDITY(c->pstream, client, tag, PA_ERR_NOENTITY);
1901 pa_client_kill(client);
1902
1903 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
1904 pa_sink_input *s;
1905
1906 s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1907 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1908
1909 pa_sink_input_kill(s);
1910 } else {
1911 pa_source_output *s;
1912
1913 assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
1914
1915 s = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
1916 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1917
1918 pa_source_output_kill(s);
1919 }
1920
1921 pa_pstream_send_simple_ack(c->pstream, tag);
1922 }
1923
1924 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) {
1925 struct connection *c = userdata;
1926 pa_module *m;
1927 const char *name, *argument;
1928 pa_tagstruct *reply;
1929 assert(c && t);
1930
1931 if (pa_tagstruct_gets(t, &name) < 0 ||
1932 pa_tagstruct_gets(t, &argument) < 0 ||
1933 !pa_tagstruct_eof(t)) {
1934 protocol_error(c);
1935 return;
1936 }
1937
1938 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1939 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name) && !strchr(name, '/'), tag, PA_ERR_INVALID);
1940 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
1941
1942 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
1943 pa_pstream_send_error(c->pstream, tag, PA_ERR_MODINITFAILED);
1944 return;
1945 }
1946
1947 reply = reply_new(tag);
1948 pa_tagstruct_putu32(reply, m->index);
1949 pa_pstream_send_tagstruct(c->pstream, reply);
1950 }
1951
1952 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) {
1953 struct connection *c = userdata;
1954 uint32_t idx;
1955 pa_module *m;
1956 assert(c && t);
1957
1958 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1959 !pa_tagstruct_eof(t)) {
1960 protocol_error(c);
1961 return;
1962 }
1963
1964 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1965 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
1966 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOENTITY);
1967
1968 pa_module_unload_request(m);
1969 pa_pstream_send_simple_ack(c->pstream, tag);
1970 }
1971
1972 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) {
1973 struct connection *c = userdata;
1974 const char *name, *module, *argument;
1975 uint32_t type;
1976 uint32_t idx;
1977 pa_tagstruct *reply;
1978 assert(c && t);
1979
1980 if (pa_tagstruct_gets(t, &name) < 0 ||
1981 pa_tagstruct_getu32(t, &type) < 0 ||
1982 pa_tagstruct_gets(t, &module) < 0 ||
1983 pa_tagstruct_gets(t, &argument) < 0 ||
1984 !pa_tagstruct_eof(t)) {
1985 protocol_error(c);
1986 return;
1987 }
1988
1989 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1990 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1991 CHECK_VALIDITY(c->pstream, type == 0 || type == 1, tag, PA_ERR_INVALID);
1992 CHECK_VALIDITY(c->pstream, module && *module && pa_utf8_valid(module), tag, PA_ERR_INVALID);
1993 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
1994
1995 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument, &idx) < 0) {
1996 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1997 return;
1998 }
1999
2000 reply = reply_new(tag);
2001 pa_tagstruct_putu32(reply, idx);
2002 pa_pstream_send_tagstruct(c->pstream, reply);
2003 }
2004
2005 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) {
2006 struct connection *c = userdata;
2007 const char *name = NULL;
2008 uint32_t type, idx = PA_IDXSET_INVALID;
2009 int r;
2010 assert(c && t);
2011
2012 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
2013 (pa_tagstruct_gets(t, &name) < 0 ||
2014 pa_tagstruct_getu32(t, &type) < 0)) ||
2015 !pa_tagstruct_eof(t)) {
2016 protocol_error(c);
2017 return;
2018 }
2019
2020 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2021 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
2022 CHECK_VALIDITY(c->pstream, !name || (*name && pa_utf8_valid(name) && (type == 0 || type == 1)), tag, PA_ERR_INVALID);
2023
2024 if (name)
2025 r = pa_autoload_remove_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
2026 else
2027 r = pa_autoload_remove_by_index(c->protocol->core, idx);
2028
2029 CHECK_VALIDITY(c->pstream, r >= 0, tag, PA_ERR_NOENTITY);
2030
2031 pa_pstream_send_simple_ack(c->pstream, tag);
2032 }
2033
2034 static void autoload_fill_tagstruct(pa_tagstruct *t, const pa_autoload_entry *e) {
2035 assert(t && e);
2036
2037 pa_tagstruct_putu32(t, e->index);
2038 pa_tagstruct_puts(t, e->name);
2039 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
2040 pa_tagstruct_puts(t, e->module);
2041 pa_tagstruct_puts(t, e->argument);
2042 }
2043
2044 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) {
2045 struct connection *c = userdata;
2046 const pa_autoload_entry *a = NULL;
2047 uint32_t type, idx;
2048 const char *name;
2049 pa_tagstruct *reply;
2050 assert(c && t);
2051
2052 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
2053 (pa_tagstruct_gets(t, &name) < 0 ||
2054 pa_tagstruct_getu32(t, &type) < 0)) ||
2055 !pa_tagstruct_eof(t)) {
2056 protocol_error(c);
2057 return;
2058 }
2059
2060 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2061 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
2062 CHECK_VALIDITY(c->pstream, !name || (*name && (type == 0 || type == 1) && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2063
2064 if (name)
2065 a = pa_autoload_get_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
2066 else
2067 a = pa_autoload_get_by_index(c->protocol->core, idx);
2068
2069 CHECK_VALIDITY(c->pstream, a, tag, PA_ERR_NOENTITY);
2070
2071 reply = reply_new(tag);
2072 autoload_fill_tagstruct(reply, a);
2073 pa_pstream_send_tagstruct(c->pstream, reply);
2074 }
2075
2076 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) {
2077 struct connection *c = userdata;
2078 pa_tagstruct *reply;
2079 assert(c && t);
2080
2081 if (!pa_tagstruct_eof(t)) {
2082 protocol_error(c);
2083 return;
2084 }
2085
2086 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2087
2088 reply = reply_new(tag);
2089
2090 if (c->protocol->core->autoload_hashmap) {
2091 pa_autoload_entry *a;
2092 void *state = NULL;
2093
2094 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state, NULL)))
2095 autoload_fill_tagstruct(reply, a);
2096 }
2097
2098 pa_pstream_send_tagstruct(c->pstream, reply);
2099 }
2100
2101 /*** pstream callbacks ***/
2102
2103 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const struct ucred *creds, void *userdata) {
2104 struct connection *c = userdata;
2105 assert(p && packet && packet->data && c);
2106
2107 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0) {
2108 pa_log(__FILE__": invalid packet.");
2109 connection_free(c);
2110 }
2111 }
2112
2113 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) {
2114 struct connection *c = userdata;
2115 struct output_stream *stream;
2116 assert(p && chunk && userdata);
2117
2118 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
2119 pa_log(__FILE__": client sent block for invalid stream.");
2120 connection_free(c);
2121 return;
2122 }
2123
2124 if (stream->type == PLAYBACK_STREAM) {
2125 struct playback_stream *ps = (struct playback_stream*) stream;
2126 if (chunk->length >= ps->requested_bytes)
2127 ps->requested_bytes = 0;
2128 else
2129 ps->requested_bytes -= chunk->length;
2130
2131 pa_memblockq_seek(ps->memblockq, offset, seek);
2132
2133 if (pa_memblockq_push_align(ps->memblockq, chunk) < 0) {
2134 pa_tagstruct *t;
2135
2136 pa_log_warn(__FILE__": failed to push data into queue");
2137
2138 /* Pushing this block into the queue failed, so we simulate
2139 * it by skipping ahead */
2140
2141 pa_memblockq_seek(ps->memblockq, chunk->length, PA_SEEK_RELATIVE);
2142
2143 /* Notify the user */
2144 t = pa_tagstruct_new(NULL, 0);
2145 pa_tagstruct_putu32(t, PA_COMMAND_OVERFLOW);
2146 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
2147 pa_tagstruct_putu32(t, ps->index);
2148 pa_pstream_send_tagstruct(p, t);
2149 }
2150
2151 ps->underrun = 0;
2152
2153 pa_sink_notify(ps->sink_input->sink);
2154
2155 } else {
2156 struct upload_stream *u = (struct upload_stream*) stream;
2157 size_t l;
2158 assert(u->type == UPLOAD_STREAM);
2159
2160 if (!u->memchunk.memblock) {
2161 if (u->length == chunk->length) {
2162 u->memchunk = *chunk;
2163 pa_memblock_ref(u->memchunk.memblock);
2164 u->length = 0;
2165 } else {
2166 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
2167 u->memchunk.index = u->memchunk.length = 0;
2168 }
2169 }
2170
2171 assert(u->memchunk.memblock);
2172
2173 l = u->length;
2174 if (l > chunk->length)
2175 l = chunk->length;
2176
2177 if (l > 0) {
2178 memcpy((uint8_t*) u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length,
2179 (uint8_t*) chunk->memblock->data+chunk->index, l);
2180 u->memchunk.length += l;
2181 u->length -= l;
2182 }
2183 }
2184 }
2185
2186 static void pstream_die_callback(pa_pstream *p, void *userdata) {
2187 struct connection *c = userdata;
2188 assert(p && c);
2189 connection_free(c);
2190
2191 /* pa_log(__FILE__": connection died.");*/
2192 }
2193
2194
2195 static void pstream_drain_callback(pa_pstream *p, void *userdata) {
2196 struct connection *c = userdata;
2197 assert(p && c);
2198
2199 send_memblock(c);
2200 }
2201
2202 /*** client callbacks ***/
2203
2204 static void client_kill_cb(pa_client *c) {
2205 assert(c && c->userdata);
2206 connection_free(c->userdata);
2207 }
2208
2209 /*** socket server callbacks ***/
2210
2211 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
2212 struct connection *c = userdata;
2213 assert(m && tv && c && c->auth_timeout_event == e);
2214
2215 if (!c->authorized)
2216 connection_free(c);
2217 }
2218
2219 static void on_connection(PA_GCC_UNUSED pa_socket_server*s, pa_iochannel *io, void *userdata) {
2220 pa_protocol_native *p = userdata;
2221 struct connection *c;
2222 char cname[256], pname[128];
2223 assert(io && p);
2224
2225 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
2226 pa_log_warn(__FILE__": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
2227 pa_iochannel_free(io);
2228 return;
2229 }
2230
2231 c = pa_xmalloc(sizeof(struct connection));
2232
2233 c->authorized =!! p->public;
2234
2235 if (!c->authorized) {
2236 struct timeval tv;
2237 pa_gettimeofday(&tv);
2238 tv.tv_sec += AUTH_TIMEOUT;
2239 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
2240 } else
2241 c->auth_timeout_event = NULL;
2242
2243 c->version = 8;
2244 c->protocol = p;
2245 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
2246 snprintf(cname, sizeof(cname), "Native client (%s)", pname);
2247 assert(p->core);
2248 c->client = pa_client_new(p->core, __FILE__, cname);
2249 assert(c->client);
2250 c->client->kill = client_kill_cb;
2251 c->client->userdata = c;
2252 c->client->owner = p->module;
2253
2254 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
2255 assert(c->pstream);
2256
2257 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
2258 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
2259 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
2260 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
2261
2262 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
2263 assert(c->pdispatch);
2264
2265 c->record_streams = pa_idxset_new(NULL, NULL);
2266 c->output_streams = pa_idxset_new(NULL, NULL);
2267 assert(c->record_streams && c->output_streams);
2268
2269 c->rrobin_index = PA_IDXSET_INVALID;
2270 c->subscription = NULL;
2271
2272 pa_idxset_put(p->connections, c, NULL);
2273
2274
2275 #ifdef SCM_CREDENTIALS
2276 if (pa_iochannel_creds_supported(io))
2277 pa_iochannel_creds_enable(io);
2278
2279 #endif
2280 }
2281
2282 /*** module entry points ***/
2283
2284 static int load_key(pa_protocol_native*p, const char*fn) {
2285 assert(p);
2286
2287 p->auth_cookie_in_property = 0;
2288
2289 if (!fn && pa_authkey_prop_get(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0) {
2290 pa_log_info(__FILE__": using already loaded auth cookie.");
2291 pa_authkey_prop_ref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2292 p->auth_cookie_in_property = 1;
2293 return 0;
2294 }
2295
2296 if (!fn)
2297 fn = PA_NATIVE_COOKIE_FILE;
2298
2299 if (pa_authkey_load_auto(fn, p->auth_cookie, sizeof(p->auth_cookie)) < 0)
2300 return -1;
2301
2302 pa_log_info(__FILE__": loading cookie from disk.");
2303
2304 if (pa_authkey_prop_put(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0)
2305 p->auth_cookie_in_property = 1;
2306
2307 return 0;
2308 }
2309
2310 static pa_protocol_native* protocol_new_internal(pa_core *c, pa_module *m, pa_modargs *ma) {
2311 pa_protocol_native *p;
2312 int public = 0;
2313 assert(c && ma);
2314
2315 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
2316 pa_log(__FILE__": auth-anonymous= expects a boolean argument.");
2317 return NULL;
2318 }
2319
2320 p = pa_xnew(pa_protocol_native, 1);
2321 p->core = c;
2322 p->module = m;
2323 p->public = public;
2324 p->server = NULL;
2325
2326 #ifdef SCM_CREDENTIALS
2327 p->auth_group = pa_xstrdup(pa_modargs_get_value(ma, "auth-group", NULL));
2328 #endif
2329
2330 if (load_key(p, pa_modargs_get_value(ma, "cookie", NULL)) < 0) {
2331 pa_xfree(p);
2332 return NULL;
2333 }
2334
2335 p->connections = pa_idxset_new(NULL, NULL);
2336 assert(p->connections);
2337
2338 return p;
2339 }
2340
2341 pa_protocol_native* pa_protocol_native_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
2342 char t[256];
2343 pa_protocol_native *p;
2344
2345 if (!(p = protocol_new_internal(core, m, ma)))
2346 return NULL;
2347
2348 p->server = server;
2349 pa_socket_server_set_callback(p->server, on_connection, p);
2350
2351 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
2352 pa_strlist *l;
2353 l = pa_property_get(core, PA_NATIVE_SERVER_PROPERTY_NAME);
2354 l = pa_strlist_prepend(l, t);
2355 pa_property_replace(core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
2356 }
2357
2358 return p;
2359 }
2360
2361 void pa_protocol_native_free(pa_protocol_native *p) {
2362 struct connection *c;
2363 assert(p);
2364
2365 while ((c = pa_idxset_first(p->connections, NULL)))
2366 connection_free(c);
2367 pa_idxset_free(p->connections, NULL, NULL);
2368
2369 if (p->server) {
2370 char t[256];
2371
2372 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
2373 pa_strlist *l;
2374 l = pa_property_get(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
2375 l = pa_strlist_remove(l, t);
2376
2377 if (l)
2378 pa_property_replace(p->core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
2379 else
2380 pa_property_remove(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
2381 }
2382
2383 pa_socket_server_unref(p->server);
2384 }
2385
2386 if (p->auth_cookie_in_property)
2387 pa_authkey_prop_unref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2388
2389 #ifdef SCM_CREDENTIALS
2390 pa_xfree(p->auth_group);
2391 #endif
2392 pa_xfree(p);
2393 }
2394
2395 pa_protocol_native* pa_protocol_native_new_iochannel(pa_core*core, pa_iochannel *io, pa_module *m, pa_modargs *ma) {
2396 pa_protocol_native *p;
2397
2398 if (!(p = protocol_new_internal(core, m, ma)))
2399 return NULL;
2400
2401 on_connection(NULL, io, p);
2402
2403 return p;
2404 }