]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-native.c
fe73865a0b5f362cd7b18a2cff500fc690b905a8
[pulseaudio] / src / pulsecore / protocol-native.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include <pulse/timeval.h>
35 #include <pulse/version.h>
36 #include <pulse/utf8.h>
37 #include <pulse/util.h>
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/native-common.h>
41 #include <pulsecore/packet.h>
42 #include <pulsecore/client.h>
43 #include <pulsecore/source-output.h>
44 #include <pulsecore/sink-input.h>
45 #include <pulsecore/pstream.h>
46 #include <pulsecore/tagstruct.h>
47 #include <pulsecore/pdispatch.h>
48 #include <pulsecore/pstream-util.h>
49 #include <pulsecore/authkey.h>
50 #include <pulsecore/namereg.h>
51 #include <pulsecore/core-scache.h>
52 #include <pulsecore/core-subscribe.h>
53 #include <pulsecore/log.h>
54 #include <pulsecore/autoload.h>
55 #include <pulsecore/authkey-prop.h>
56 #include <pulsecore/strlist.h>
57 #include <pulsecore/props.h>
58 #include <pulsecore/sample-util.h>
59 #include <pulsecore/llist.h>
60 #include <pulsecore/creds.h>
61 #include <pulsecore/core-util.h>
62 #include <pulsecore/ipacl.h>
63 #include <pulsecore/thread-mq.h>
64
65 #include "protocol-native.h"
66
67 /* Kick a client if it doesn't authenticate within this time */
68 #define AUTH_TIMEOUT 60
69
70 /* Don't accept more connection than this */
71 #define MAX_CONNECTIONS 64
72
73 #define MAX_MEMBLOCKQ_LENGTH (4*1024*1024) /* 4MB */
74
75 typedef struct connection connection;
76 struct pa_protocol_native;
77
78 typedef struct record_stream {
79 pa_msgobject parent;
80
81 connection *connection;
82 uint32_t index;
83
84 pa_source_output *source_output;
85 pa_memblockq *memblockq;
86 size_t fragment_size;
87 } record_stream;
88
89 typedef struct output_stream {
90 pa_msgobject parent;
91 } output_stream;
92
93 typedef struct playback_stream {
94 output_stream parent;
95
96 connection *connection;
97 uint32_t index;
98
99 pa_sink_input *sink_input;
100 pa_memblockq *memblockq;
101 int drain_request;
102 uint32_t drain_tag;
103 uint32_t syncid;
104 int underrun;
105
106 pa_atomic_t missing;
107 size_t last_missing;
108
109 /* Only updated after SINK_INPUT_MESSAGE_UPDATE_LATENCY */
110 int64_t read_index, write_index;
111 size_t resampled_chunk_length;
112 } playback_stream;
113
114 typedef struct upload_stream {
115 output_stream parent;
116
117 connection *connection;
118 uint32_t index;
119
120 pa_memchunk memchunk;
121 size_t length;
122 char *name;
123 pa_sample_spec sample_spec;
124 pa_channel_map channel_map;
125 } upload_stream;
126
127 struct connection {
128 pa_msgobject parent;
129
130 int authorized;
131 uint32_t version;
132 pa_protocol_native *protocol;
133 pa_client *client;
134 pa_pstream *pstream;
135 pa_pdispatch *pdispatch;
136 pa_idxset *record_streams, *output_streams;
137 uint32_t rrobin_index;
138 pa_subscription *subscription;
139 pa_time_event *auth_timeout_event;
140 };
141
142 PA_DECLARE_CLASS(record_stream);
143 #define RECORD_STREAM(o) (record_stream_cast(o))
144 static PA_DEFINE_CHECK_TYPE(record_stream, pa_msgobject);
145
146 PA_DECLARE_CLASS(output_stream);
147 #define OUTPUT_STREAM(o) (output_stream_cast(o))
148 static PA_DEFINE_CHECK_TYPE(output_stream, pa_msgobject);
149
150 PA_DECLARE_CLASS(playback_stream);
151 #define PLAYBACK_STREAM(o) (playback_stream_cast(o))
152 static PA_DEFINE_CHECK_TYPE(playback_stream, output_stream);
153
154 PA_DECLARE_CLASS(upload_stream);
155 #define UPLOAD_STREAM(o) (upload_stream_cast(o))
156 static PA_DEFINE_CHECK_TYPE(upload_stream, output_stream);
157
158 PA_DECLARE_CLASS(connection);
159 #define CONNECTION(o) (connection_cast(o))
160 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
161
162 struct pa_protocol_native {
163 pa_module *module;
164 pa_core *core;
165 int public;
166 pa_socket_server *server;
167 pa_idxset *connections;
168 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
169 int auth_cookie_in_property;
170 #ifdef HAVE_CREDS
171 char *auth_group;
172 #endif
173 pa_ip_acl *auth_ip_acl;
174 };
175
176 enum {
177 SINK_INPUT_MESSAGE_POST_DATA = PA_SINK_INPUT_MESSAGE_MAX, /* data from main loop to sink input */
178 SINK_INPUT_MESSAGE_DRAIN, /* disabled prebuf, get playback started. */
179 SINK_INPUT_MESSAGE_FLUSH,
180 SINK_INPUT_MESSAGE_TRIGGER,
181 SINK_INPUT_MESSAGE_SEEK,
182 SINK_INPUT_MESSAGE_PREBUF_FORCE,
183 SINK_INPUT_MESSAGE_UPDATE_LATENCY
184 };
185
186 enum {
187 PLAYBACK_STREAM_MESSAGE_REQUEST_DATA, /* data requested from sink input from the main loop */
188 PLAYBACK_STREAM_MESSAGE_UNDERFLOW,
189 PLAYBACK_STREAM_MESSAGE_OVERFLOW,
190 PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
191 };
192
193 enum {
194 RECORD_STREAM_MESSAGE_POST_DATA /* data from source output to main loop */
195 };
196
197 enum {
198 CONNECTION_MESSAGE_RELEASE,
199 CONNECTION_MESSAGE_REVOKE
200 };
201
202 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk);
203 static void sink_input_drop_cb(pa_sink_input *i, size_t length);
204 static void sink_input_kill_cb(pa_sink_input *i);
205
206 static void send_memblock(connection *c);
207 static void request_bytes(struct playback_stream*s);
208
209 static void source_output_kill_cb(pa_source_output *o);
210 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
211 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
212
213 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk);
214
215 static void command_exit(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
216 static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
217 static void command_drain_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
218 static void command_create_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
219 static void command_delete_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
220 static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
221 static void command_set_client_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
222 static void command_lookup(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
223 static void command_stat(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
224 static void command_get_playback_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
225 static void command_get_record_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
226 static void command_create_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
227 static void command_finish_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
228 static void command_play_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
229 static void command_remove_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
230 static void command_get_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
231 static void command_get_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
232 static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
233 static void command_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
234 static void command_set_volume(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
235 static void command_set_mute(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
236 static void command_cork_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
237 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
238 static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
239 static void command_set_stream_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
240 static void command_kill(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
241 static void command_load_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
242 static void command_unload_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
243 static void command_add_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
244 static void command_remove_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
245 static void command_get_autoload_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
246 static void command_get_autoload_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
247 static void command_cork_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
248 static void command_flush_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
249 static void command_move_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
250 static void command_suspend(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
251
252 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
253 [PA_COMMAND_ERROR] = NULL,
254 [PA_COMMAND_TIMEOUT] = NULL,
255 [PA_COMMAND_REPLY] = NULL,
256 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = command_create_playback_stream,
257 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = command_delete_stream,
258 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = command_drain_playback_stream,
259 [PA_COMMAND_CREATE_RECORD_STREAM] = command_create_record_stream,
260 [PA_COMMAND_DELETE_RECORD_STREAM] = command_delete_stream,
261 [PA_COMMAND_AUTH] = command_auth,
262 [PA_COMMAND_REQUEST] = NULL,
263 [PA_COMMAND_EXIT] = command_exit,
264 [PA_COMMAND_SET_CLIENT_NAME] = command_set_client_name,
265 [PA_COMMAND_LOOKUP_SINK] = command_lookup,
266 [PA_COMMAND_LOOKUP_SOURCE] = command_lookup,
267 [PA_COMMAND_STAT] = command_stat,
268 [PA_COMMAND_GET_PLAYBACK_LATENCY] = command_get_playback_latency,
269 [PA_COMMAND_GET_RECORD_LATENCY] = command_get_record_latency,
270 [PA_COMMAND_CREATE_UPLOAD_STREAM] = command_create_upload_stream,
271 [PA_COMMAND_DELETE_UPLOAD_STREAM] = command_delete_stream,
272 [PA_COMMAND_FINISH_UPLOAD_STREAM] = command_finish_upload_stream,
273 [PA_COMMAND_PLAY_SAMPLE] = command_play_sample,
274 [PA_COMMAND_REMOVE_SAMPLE] = command_remove_sample,
275 [PA_COMMAND_GET_SINK_INFO] = command_get_info,
276 [PA_COMMAND_GET_SOURCE_INFO] = command_get_info,
277 [PA_COMMAND_GET_CLIENT_INFO] = command_get_info,
278 [PA_COMMAND_GET_MODULE_INFO] = command_get_info,
279 [PA_COMMAND_GET_SINK_INPUT_INFO] = command_get_info,
280 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = command_get_info,
281 [PA_COMMAND_GET_SAMPLE_INFO] = command_get_info,
282 [PA_COMMAND_GET_SINK_INFO_LIST] = command_get_info_list,
283 [PA_COMMAND_GET_SOURCE_INFO_LIST] = command_get_info_list,
284 [PA_COMMAND_GET_MODULE_INFO_LIST] = command_get_info_list,
285 [PA_COMMAND_GET_CLIENT_INFO_LIST] = command_get_info_list,
286 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = command_get_info_list,
287 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = command_get_info_list,
288 [PA_COMMAND_GET_SAMPLE_INFO_LIST] = command_get_info_list,
289 [PA_COMMAND_GET_SERVER_INFO] = command_get_server_info,
290 [PA_COMMAND_SUBSCRIBE] = command_subscribe,
291
292 [PA_COMMAND_SET_SINK_VOLUME] = command_set_volume,
293 [PA_COMMAND_SET_SINK_INPUT_VOLUME] = command_set_volume,
294 [PA_COMMAND_SET_SOURCE_VOLUME] = command_set_volume,
295
296 [PA_COMMAND_SET_SINK_MUTE] = command_set_mute,
297 [PA_COMMAND_SET_SINK_INPUT_MUTE] = command_set_mute,
298 [PA_COMMAND_SET_SOURCE_MUTE] = command_set_mute,
299
300 [PA_COMMAND_SUSPEND_SINK] = command_suspend,
301 [PA_COMMAND_SUSPEND_SOURCE] = command_suspend,
302
303 [PA_COMMAND_CORK_PLAYBACK_STREAM] = command_cork_playback_stream,
304 [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = command_trigger_or_flush_or_prebuf_playback_stream,
305 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = command_trigger_or_flush_or_prebuf_playback_stream,
306 [PA_COMMAND_PREBUF_PLAYBACK_STREAM] = command_trigger_or_flush_or_prebuf_playback_stream,
307
308 [PA_COMMAND_CORK_RECORD_STREAM] = command_cork_record_stream,
309 [PA_COMMAND_FLUSH_RECORD_STREAM] = command_flush_record_stream,
310
311 [PA_COMMAND_SET_DEFAULT_SINK] = command_set_default_sink_or_source,
312 [PA_COMMAND_SET_DEFAULT_SOURCE] = command_set_default_sink_or_source,
313 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME] = command_set_stream_name,
314 [PA_COMMAND_SET_RECORD_STREAM_NAME] = command_set_stream_name,
315 [PA_COMMAND_KILL_CLIENT] = command_kill,
316 [PA_COMMAND_KILL_SINK_INPUT] = command_kill,
317 [PA_COMMAND_KILL_SOURCE_OUTPUT] = command_kill,
318 [PA_COMMAND_LOAD_MODULE] = command_load_module,
319 [PA_COMMAND_UNLOAD_MODULE] = command_unload_module,
320 [PA_COMMAND_GET_AUTOLOAD_INFO] = command_get_autoload_info,
321 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST] = command_get_autoload_info_list,
322 [PA_COMMAND_ADD_AUTOLOAD] = command_add_autoload,
323 [PA_COMMAND_REMOVE_AUTOLOAD] = command_remove_autoload,
324
325 [PA_COMMAND_MOVE_SINK_INPUT] = command_move_stream,
326 [PA_COMMAND_MOVE_SOURCE_OUTPUT] = command_move_stream
327 };
328
329 /* structure management */
330
331 static void upload_stream_unlink(upload_stream *s) {
332 pa_assert(s);
333
334 if (!s->connection)
335 return;
336
337 pa_assert_se(pa_idxset_remove_by_data(s->connection->output_streams, s, NULL) == s);
338 s->connection = NULL;
339 upload_stream_unref(s);
340 }
341
342 static void upload_stream_free(pa_object *o) {
343 upload_stream *s = UPLOAD_STREAM(o);
344 pa_assert(s);
345
346 upload_stream_unlink(s);
347
348 pa_xfree(s->name);
349
350 if (s->memchunk.memblock)
351 pa_memblock_unref(s->memchunk.memblock);
352
353 pa_xfree(s);
354 }
355
356 static upload_stream* upload_stream_new(
357 connection *c,
358 const pa_sample_spec *ss,
359 const pa_channel_map *map,
360 const char *name, size_t length) {
361
362 upload_stream *s;
363
364 pa_assert(c);
365 pa_assert(ss);
366 pa_assert(name);
367 pa_assert(length > 0);
368
369 s = pa_msgobject_new(upload_stream);
370 s->parent.parent.parent.free = upload_stream_free;
371 s->connection = c;
372 s->sample_spec = *ss;
373 s->channel_map = *map;
374 s->name = pa_xstrdup(name);
375 pa_memchunk_reset(&s->memchunk);
376 s->length = length;
377
378 pa_idxset_put(c->output_streams, s, &s->index);
379
380 return s;
381 }
382
383 static void record_stream_unlink(record_stream *s) {
384 pa_assert(s);
385
386 if (!s->connection)
387 return;
388
389 if (s->source_output) {
390 pa_source_output_disconnect(s->source_output);
391 pa_source_output_unref(s->source_output);
392 s->source_output = NULL;
393 }
394
395 pa_assert_se(pa_idxset_remove_by_data(s->connection->record_streams, s, NULL) == s);
396 s->connection = NULL;
397 record_stream_unref(s);
398 }
399
400 static void record_stream_free(pa_object *o) {
401 record_stream *s = RECORD_STREAM(o);
402 pa_assert(s);
403
404 record_stream_unlink(s);
405
406 pa_memblockq_free(s->memblockq);
407 pa_xfree(s);
408 }
409
410 static int record_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
411 record_stream *s = RECORD_STREAM(o);
412 record_stream_assert_ref(s);
413
414 if (!s->connection)
415 return -1;
416
417 switch (code) {
418
419 case RECORD_STREAM_MESSAGE_POST_DATA:
420
421 if (pa_memblockq_push_align(s->memblockq, chunk) < 0) {
422 /* pa_log_warn("Failed to push data into output queue."); */
423 return -1;
424 }
425
426 if (!pa_pstream_is_pending(s->connection->pstream))
427 send_memblock(s->connection);
428
429 pa_pstream_send_memblock(s->connection->pstream, s->index, 0, PA_SEEK_RELATIVE, chunk);
430 break;
431 }
432
433 return 0;
434 }
435
436 static record_stream* record_stream_new(
437 connection *c,
438 pa_source *source,
439 const pa_sample_spec *ss,
440 const pa_channel_map *map,
441 const char *name,
442 size_t *maxlength,
443 size_t fragment_size,
444 int corked) {
445
446 record_stream *s;
447 pa_source_output *source_output;
448 size_t base;
449 pa_source_output_new_data data;
450
451 pa_assert(c);
452 pa_assert(ss);
453 pa_assert(name);
454 pa_assert(maxlength);
455 pa_assert(*maxlength > 0);
456
457 pa_source_output_new_data_init(&data);
458 data.module = c->protocol->module;
459 data.client = c->client;
460 data.source = source;
461 data.driver = __FILE__;
462 data.name = name;
463 data.start_corked = corked;
464 pa_source_output_new_data_set_sample_spec(&data, ss);
465 pa_source_output_new_data_set_channel_map(&data, map);
466
467 if (!(source_output = pa_source_output_new(c->protocol->core, &data, 0)))
468 return NULL;
469
470 s = pa_msgobject_new(record_stream);
471 s->parent.parent.free = record_stream_free;
472 s->parent.process_msg = record_stream_process_msg;
473 s->connection = c;
474 s->source_output = source_output;
475 s->source_output->push = source_output_push_cb;
476 s->source_output->kill = source_output_kill_cb;
477 s->source_output->get_latency = source_output_get_latency_cb;
478 s->source_output->userdata = s;
479
480 s->memblockq = pa_memblockq_new(
481 0,
482 *maxlength,
483 0,
484 base = pa_frame_size(ss),
485 1,
486 0,
487 NULL);
488
489 s->fragment_size = (fragment_size/base)*base;
490 if (s->fragment_size <= 0)
491 s->fragment_size = base;
492 *maxlength = pa_memblockq_get_maxlength(s->memblockq);
493
494 pa_idxset_put(c->record_streams, s, &s->index);
495
496 pa_source_output_put(s->source_output);
497 return s;
498 }
499
500 static void playback_stream_unlink(playback_stream *s) {
501 pa_assert(s);
502
503 if (!s->connection)
504 return;
505
506 if (s->sink_input) {
507 pa_sink_input_disconnect(s->sink_input);
508 pa_sink_input_unref(s->sink_input);
509 s->sink_input = NULL;
510 }
511
512 if (s->drain_request)
513 pa_pstream_send_error(s->connection->pstream, s->drain_tag, PA_ERR_NOENTITY);
514
515 pa_assert_se(pa_idxset_remove_by_data(s->connection->output_streams, s, NULL) == s);
516 s->connection = NULL;
517 playback_stream_unref(s);
518 }
519
520 static void playback_stream_free(pa_object* o) {
521 playback_stream *s = PLAYBACK_STREAM(o);
522 pa_assert(s);
523
524 playback_stream_unlink(s);
525
526 pa_memblockq_free(s->memblockq);
527 pa_xfree(s);
528 }
529
530 static int playback_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
531 playback_stream *s = PLAYBACK_STREAM(o);
532 playback_stream_assert_ref(s);
533
534 if (!s->connection)
535 return -1;
536
537 switch (code) {
538 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA: {
539 pa_tagstruct *t;
540 int32_t l;
541
542 if ((l = pa_atomic_load(&s->missing)) <= 0)
543 break;
544
545 pa_assert_se(pa_atomic_sub(&s->missing, l) >= l);
546
547 t = pa_tagstruct_new(NULL, 0);
548 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
549 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
550 pa_tagstruct_putu32(t, s->index);
551 pa_tagstruct_putu32(t, l);
552 pa_pstream_send_tagstruct(s->connection->pstream, t);
553
554 /* pa_log("Requesting %u bytes", l); */
555 break;
556 }
557
558 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW: {
559 pa_tagstruct *t;
560
561 /* Report that we're empty */
562 t = pa_tagstruct_new(NULL, 0);
563 pa_tagstruct_putu32(t, PA_COMMAND_UNDERFLOW);
564 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
565 pa_tagstruct_putu32(t, s->index);
566 pa_pstream_send_tagstruct(s->connection->pstream, t);
567 break;
568 }
569
570 case PLAYBACK_STREAM_MESSAGE_OVERFLOW: {
571 pa_tagstruct *t;
572
573 /* Notify the user we're overflowed*/
574 t = pa_tagstruct_new(NULL, 0);
575 pa_tagstruct_putu32(t, PA_COMMAND_OVERFLOW);
576 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
577 pa_tagstruct_putu32(t, s->index);
578 pa_pstream_send_tagstruct(s->connection->pstream, t);
579 break;
580 }
581
582 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK:
583 pa_pstream_send_simple_ack(s->connection->pstream, PA_PTR_TO_UINT(userdata));
584 break;
585
586 }
587
588 return 0;
589 }
590
591 static playback_stream* playback_stream_new(
592 connection *c,
593 pa_sink *sink,
594 const pa_sample_spec *ss,
595 const pa_channel_map *map,
596 const char *name,
597 uint32_t *maxlength,
598 uint32_t *tlength,
599 uint32_t *prebuf,
600 uint32_t *minreq,
601 pa_cvolume *volume,
602 uint32_t syncid,
603 int corked,
604 uint32_t *missing) {
605
606 playback_stream *s, *ssync;
607 pa_sink_input *sink_input;
608 pa_memblock *silence;
609 uint32_t idx;
610 int64_t start_index;
611 pa_sink_input_new_data data;
612
613 pa_assert(c);
614 pa_assert(ss);
615 pa_assert(name);
616 pa_assert(maxlength);
617
618 /* Find syncid group */
619 for (ssync = pa_idxset_first(c->output_streams, &idx); ssync; ssync = pa_idxset_next(c->output_streams, &idx)) {
620
621 if (!playback_stream_isinstance(ssync))
622 continue;
623
624 if (ssync->syncid == syncid)
625 break;
626 }
627
628 /* Synced streams must connect to the same sink */
629 if (ssync) {
630
631 if (!sink)
632 sink = ssync->sink_input->sink;
633 else if (sink != ssync->sink_input->sink)
634 return NULL;
635 }
636
637 pa_sink_input_new_data_init(&data);
638 data.sink = sink;
639 data.driver = __FILE__;
640 data.name = name;
641 pa_sink_input_new_data_set_sample_spec(&data, ss);
642 pa_sink_input_new_data_set_channel_map(&data, map);
643 pa_sink_input_new_data_set_volume(&data, volume);
644 data.module = c->protocol->module;
645 data.client = c->client;
646 data.start_corked = corked;
647 data.sync_base = ssync ? ssync->sink_input : NULL;
648
649 if (!(sink_input = pa_sink_input_new(c->protocol->core, &data, 0)))
650 return NULL;
651
652 s = pa_msgobject_new(playback_stream);
653 s->parent.parent.parent.free = playback_stream_free;
654 s->parent.parent.process_msg = playback_stream_process_msg;
655 s->connection = c;
656 s->syncid = syncid;
657 s->sink_input = sink_input;
658 s->underrun = 1;
659
660 s->sink_input->parent.process_msg = sink_input_process_msg;
661 s->sink_input->peek = sink_input_peek_cb;
662 s->sink_input->drop = sink_input_drop_cb;
663 s->sink_input->kill = sink_input_kill_cb;
664 s->sink_input->userdata = s;
665
666 start_index = ssync ? pa_memblockq_get_read_index(ssync->memblockq) : 0;
667
668 silence = pa_silence_memblock_new(c->protocol->core->mempool, ss, 0);
669
670 s->memblockq = pa_memblockq_new(
671 start_index,
672 *maxlength,
673 *tlength,
674 pa_frame_size(ss),
675 *prebuf,
676 *minreq,
677 silence);
678
679 pa_memblock_unref(silence);
680
681 *maxlength = (uint32_t) pa_memblockq_get_maxlength(s->memblockq);
682 *tlength = (uint32_t) pa_memblockq_get_tlength(s->memblockq);
683 *prebuf = (uint32_t) pa_memblockq_get_prebuf(s->memblockq);
684 *minreq = (uint32_t) pa_memblockq_get_minreq(s->memblockq);
685 *missing = (uint32_t) pa_memblockq_missing(s->memblockq);
686
687 pa_atomic_store(&s->missing, 0);
688 s->last_missing = *missing;
689 s->drain_request = 0;
690
691 pa_idxset_put(c->output_streams, s, &s->index);
692
693 pa_sink_input_put(s->sink_input);
694
695 return s;
696 }
697
698 static int connection_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
699 connection *c = CONNECTION(o);
700 connection_assert_ref(c);
701
702 if (!c->protocol)
703 return -1;
704
705 switch (code) {
706
707 case CONNECTION_MESSAGE_REVOKE:
708 pa_pstream_send_revoke(c->pstream, PA_PTR_TO_UINT(userdata));
709 break;
710
711 case CONNECTION_MESSAGE_RELEASE:
712 pa_pstream_send_release(c->pstream, PA_PTR_TO_UINT(userdata));
713 break;
714 }
715
716 return 0;
717 }
718
719 static void connection_unlink(connection *c) {
720 record_stream *r;
721 output_stream *o;
722
723 pa_assert(c);
724
725 if (!c->protocol)
726 return;
727
728 while ((r = pa_idxset_first(c->record_streams, NULL)))
729 record_stream_unlink(r);
730
731 while ((o = pa_idxset_first(c->output_streams, NULL)))
732 if (playback_stream_isinstance(o))
733 playback_stream_unlink(PLAYBACK_STREAM(o));
734 else
735 upload_stream_unlink(UPLOAD_STREAM(o));
736
737 if (c->subscription)
738 pa_subscription_free(c->subscription);
739
740 if (c->pstream)
741 pa_pstream_unlink(c->pstream);
742
743 if (c->auth_timeout_event) {
744 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
745 c->auth_timeout_event = NULL;
746 }
747
748 pa_assert_se(pa_idxset_remove_by_data(c->protocol->connections, c, NULL) == c);
749 c->protocol = NULL;
750 connection_unref(c);
751 }
752
753 static void connection_free(pa_object *o) {
754 connection *c = CONNECTION(o);
755
756 pa_assert(c);
757
758 connection_unlink(c);
759
760 pa_idxset_free(c->record_streams, NULL, NULL);
761 pa_idxset_free(c->output_streams, NULL, NULL);
762
763 pa_pdispatch_unref(c->pdispatch);
764 pa_pstream_unref(c->pstream);
765 pa_client_free(c->client);
766
767 pa_xfree(c);
768 }
769
770 /* Called from thread context */
771 static void request_bytes(playback_stream *s) {
772 size_t new_missing, delta, previous_missing;
773
774 /* pa_log("request_bytes()"); */
775 playback_stream_assert_ref(s);
776
777 new_missing = pa_memblockq_missing(s->memblockq);
778
779 if (new_missing <= s->last_missing) {
780 s->last_missing = new_missing;
781 return;
782 }
783
784 delta = new_missing - s->last_missing;
785 s->last_missing = new_missing;
786
787 /* pa_log("request_bytes(%u)", delta); */
788
789 previous_missing = pa_atomic_add(&s->missing, delta);
790 if (previous_missing < pa_memblockq_get_minreq(s->memblockq) && previous_missing+delta >= pa_memblockq_get_minreq(s->memblockq))
791 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
792 }
793
794 static void send_memblock(connection *c) {
795 uint32_t start;
796 record_stream *r;
797
798 start = PA_IDXSET_INVALID;
799 for (;;) {
800 pa_memchunk chunk;
801
802 if (!(r = RECORD_STREAM(pa_idxset_rrobin(c->record_streams, &c->rrobin_index))))
803 return;
804
805 if (start == PA_IDXSET_INVALID)
806 start = c->rrobin_index;
807 else if (start == c->rrobin_index)
808 return;
809
810 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
811 pa_memchunk schunk = chunk;
812
813 if (schunk.length > r->fragment_size)
814 schunk.length = r->fragment_size;
815
816 pa_pstream_send_memblock(c->pstream, r->index, 0, PA_SEEK_RELATIVE, &schunk);
817
818 pa_memblockq_drop(r->memblockq, schunk.length);
819 pa_memblock_unref(schunk.memblock);
820
821 return;
822 }
823 }
824 }
825
826 static void send_playback_stream_killed(playback_stream *p) {
827 pa_tagstruct *t;
828 playback_stream_assert_ref(p);
829
830 t = pa_tagstruct_new(NULL, 0);
831 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
832 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
833 pa_tagstruct_putu32(t, p->index);
834 pa_pstream_send_tagstruct(p->connection->pstream, t);
835 }
836
837 static void send_record_stream_killed(record_stream *r) {
838 pa_tagstruct *t;
839 record_stream_assert_ref(r);
840
841 t = pa_tagstruct_new(NULL, 0);
842 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
843 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
844 pa_tagstruct_putu32(t, r->index);
845 pa_pstream_send_tagstruct(r->connection->pstream, t);
846 }
847
848 /*** sink input callbacks ***/
849
850 /* Called from thread context */
851 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
852 pa_sink_input *i = PA_SINK_INPUT(o);
853 playback_stream *s;
854
855 pa_sink_input_assert_ref(i);
856 s = PLAYBACK_STREAM(i->userdata);
857 playback_stream_assert_ref(s);
858
859 switch (code) {
860
861 case SINK_INPUT_MESSAGE_SEEK:
862 pa_memblockq_seek(s->memblockq, offset, PA_PTR_TO_UINT(userdata));
863 request_bytes(s);
864 return 0;
865
866 case SINK_INPUT_MESSAGE_POST_DATA: {
867 pa_assert(chunk);
868
869 if (pa_memblockq_push_align(s->memblockq, chunk) < 0) {
870
871 pa_log_warn("Failed to push data into queue");
872 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_OVERFLOW, NULL, 0, NULL, NULL);
873 pa_memblockq_seek(s->memblockq, chunk->length, PA_SEEK_RELATIVE);
874 }
875
876 request_bytes(s);
877
878 s->underrun = 0;
879 return 0;
880 }
881
882 case SINK_INPUT_MESSAGE_DRAIN: {
883
884 pa_memblockq_prebuf_disable(s->memblockq);
885
886 if (!pa_memblockq_is_readable(s->memblockq))
887 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK, userdata, 0, NULL, NULL);
888 else {
889 s->drain_tag = PA_PTR_TO_UINT(userdata);
890 s->drain_request = 1;
891 }
892 request_bytes(s);
893
894 return 0;
895 }
896
897 case SINK_INPUT_MESSAGE_FLUSH:
898 case SINK_INPUT_MESSAGE_PREBUF_FORCE:
899 case SINK_INPUT_MESSAGE_TRIGGER: {
900
901 pa_sink_input *isync;
902 void (*func)(pa_memblockq *bq);
903
904 switch (code) {
905 case SINK_INPUT_MESSAGE_FLUSH:
906 func = pa_memblockq_flush;
907 break;
908
909 case SINK_INPUT_MESSAGE_PREBUF_FORCE:
910 func = pa_memblockq_prebuf_force;
911 break;
912
913 case SINK_INPUT_MESSAGE_TRIGGER:
914 func = pa_memblockq_prebuf_disable;
915 break;
916
917 default:
918 pa_assert_not_reached();
919 }
920
921 func(s->memblockq);
922 s->underrun = 0;
923 request_bytes(s);
924
925 /* Do the same for all other members in the sync group */
926 for (isync = i->sync_prev; isync; isync = isync->sync_prev) {
927 playback_stream *ssync = PLAYBACK_STREAM(isync->userdata);
928 func(ssync->memblockq);
929 ssync->underrun = 0;
930 request_bytes(ssync);
931 }
932
933 for (isync = i->sync_next; isync; isync = isync->sync_next) {
934 playback_stream *ssync = PLAYBACK_STREAM(isync->userdata);
935 func(ssync->memblockq);
936 ssync->underrun = 0;
937 request_bytes(ssync);
938 }
939
940 return 0;
941 }
942
943 case SINK_INPUT_MESSAGE_UPDATE_LATENCY:
944
945 s->read_index = pa_memblockq_get_read_index(s->memblockq);
946 s->write_index = pa_memblockq_get_write_index(s->memblockq);
947 s->resampled_chunk_length = s->sink_input->thread_info.resampled_chunk.memblock ? s->sink_input->thread_info.resampled_chunk.length : 0;
948 return 0;
949
950 case PA_SINK_INPUT_MESSAGE_SET_STATE:
951
952 pa_memblockq_prebuf_force(s->memblockq);
953 request_bytes(s);
954 break;
955
956 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
957 pa_usec_t *r = userdata;
958
959 *r = pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &i->sample_spec);
960
961 /* Fall through, the default handler will add in the extra
962 * latency added by the resampler */
963 break;
964 }
965 }
966
967 return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
968 }
969
970 /* Called from thread context */
971 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
972 playback_stream *s;
973
974 pa_sink_input_assert_ref(i);
975 s = PLAYBACK_STREAM(i->userdata);
976 playback_stream_assert_ref(s);
977 pa_assert(chunk);
978
979 if (pa_memblockq_get_length(s->memblockq) <= 0 && !s->underrun) {
980 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_UNDERFLOW, NULL, 0, NULL, NULL);
981 s->underrun = 1;
982 }
983
984 if (pa_memblockq_peek(s->memblockq, chunk) < 0) {
985 /* pa_log("peek: failure"); */
986 return -1;
987 }
988
989 /* pa_log("peek: %u", chunk->length); */
990
991 request_bytes(s);
992
993 return 0;
994 }
995
996 /* Called from thread context */
997 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
998 playback_stream *s;
999
1000 pa_sink_input_assert_ref(i);
1001 s = PLAYBACK_STREAM(i->userdata);
1002 playback_stream_assert_ref(s);
1003 pa_assert(length > 0);
1004
1005 pa_memblockq_drop(s->memblockq, length);
1006
1007 if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
1008 s->drain_request = 0;
1009 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK, PA_UINT_TO_PTR(s->drain_tag), 0, NULL, NULL);
1010 }
1011
1012 request_bytes(s);
1013
1014 /* pa_log("after_drop: %u %u", pa_memblockq_get_length(s->memblockq), pa_memblockq_is_readable(s->memblockq)); */
1015 }
1016
1017 static void sink_input_kill_cb(pa_sink_input *i) {
1018 playback_stream *s;
1019
1020 pa_sink_input_assert_ref(i);
1021 s = PLAYBACK_STREAM(i->userdata);
1022 playback_stream_assert_ref(s);
1023
1024 send_playback_stream_killed(s);
1025 playback_stream_unlink(s);
1026 }
1027
1028 /*** source_output callbacks ***/
1029
1030 /* Called from thread context */
1031 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
1032 record_stream *s;
1033
1034 pa_source_output_assert_ref(o);
1035 s = RECORD_STREAM(o->userdata);
1036 record_stream_assert_ref(s);
1037 pa_assert(chunk);
1038
1039 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), RECORD_STREAM_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
1040 }
1041
1042 static void source_output_kill_cb(pa_source_output *o) {
1043 record_stream *s;
1044
1045 pa_source_output_assert_ref(o);
1046 s = RECORD_STREAM(o->userdata);
1047 record_stream_assert_ref(s);
1048
1049 send_record_stream_killed(s);
1050 record_stream_unlink(s);
1051 }
1052
1053 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
1054 record_stream *s;
1055
1056 pa_source_output_assert_ref(o);
1057 s = RECORD_STREAM(o->userdata);
1058 record_stream_assert_ref(s);
1059
1060 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1061
1062 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &o->sample_spec);
1063 }
1064
1065 /*** pdispatch callbacks ***/
1066
1067 static void protocol_error(connection *c) {
1068 pa_log("protocol error, kicking client");
1069 connection_unlink(c);
1070 }
1071
1072 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1073 if (!(expression)) { \
1074 pa_pstream_send_error((pstream), (tag), (error)); \
1075 return; \
1076 } \
1077 } while(0);
1078
1079 static pa_tagstruct *reply_new(uint32_t tag) {
1080 pa_tagstruct *reply;
1081
1082 reply = pa_tagstruct_new(NULL, 0);
1083 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1084 pa_tagstruct_putu32(reply, tag);
1085 return reply;
1086 }
1087
1088 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) {
1089 connection *c = CONNECTION(userdata);
1090 playback_stream *s;
1091 uint32_t maxlength, tlength, prebuf, minreq, sink_index, syncid, missing;
1092 const char *name, *sink_name;
1093 pa_sample_spec ss;
1094 pa_channel_map map;
1095 pa_tagstruct *reply;
1096 pa_sink *sink = NULL;
1097 pa_cvolume volume;
1098 int corked;
1099
1100 connection_assert_ref(c);
1101 pa_assert(t);
1102
1103 if (pa_tagstruct_get(
1104 t,
1105 PA_TAG_STRING, &name,
1106 PA_TAG_SAMPLE_SPEC, &ss,
1107 PA_TAG_CHANNEL_MAP, &map,
1108 PA_TAG_U32, &sink_index,
1109 PA_TAG_STRING, &sink_name,
1110 PA_TAG_U32, &maxlength,
1111 PA_TAG_BOOLEAN, &corked,
1112 PA_TAG_U32, &tlength,
1113 PA_TAG_U32, &prebuf,
1114 PA_TAG_U32, &minreq,
1115 PA_TAG_U32, &syncid,
1116 PA_TAG_CVOLUME, &volume,
1117 PA_TAG_INVALID) < 0 ||
1118 !pa_tagstruct_eof(t) ||
1119 !name) {
1120 protocol_error(c);
1121 return;
1122 }
1123
1124 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1125 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1126 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1127 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1128 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1129 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
1130 CHECK_VALIDITY(c->pstream, map.channels == ss.channels && volume.channels == ss.channels, tag, PA_ERR_INVALID);
1131 CHECK_VALIDITY(c->pstream, maxlength > 0 && maxlength <= MAX_MEMBLOCKQ_LENGTH, tag, PA_ERR_INVALID);
1132 CHECK_VALIDITY(c->pstream, maxlength >= pa_frame_size(&ss), tag, PA_ERR_INVALID);
1133
1134 if (sink_index != PA_INVALID_INDEX) {
1135 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1136 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
1137 } else if (sink_name) {
1138 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1139 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
1140 }
1141
1142 s = playback_stream_new(c, sink, &ss, &map, name, &maxlength, &tlength, &prebuf, &minreq, &volume, syncid, corked, &missing);
1143 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1144
1145 reply = reply_new(tag);
1146 pa_tagstruct_putu32(reply, s->index);
1147 pa_assert(s->sink_input);
1148 pa_tagstruct_putu32(reply, s->sink_input->index);
1149 pa_tagstruct_putu32(reply, missing);
1150
1151 /* pa_log("initial request is %u", missing); */
1152
1153 if (c->version >= 9) {
1154 /* Since 0.9 we support sending the buffer metrics back to the client */
1155
1156 pa_tagstruct_putu32(reply, (uint32_t) maxlength);
1157 pa_tagstruct_putu32(reply, (uint32_t) tlength);
1158 pa_tagstruct_putu32(reply, (uint32_t) prebuf);
1159 pa_tagstruct_putu32(reply, (uint32_t) minreq);
1160 }
1161
1162 pa_pstream_send_tagstruct(c->pstream, reply);
1163 }
1164
1165 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1166 connection *c = CONNECTION(userdata);
1167 uint32_t channel;
1168
1169 connection_assert_ref(c);
1170 pa_assert(t);
1171
1172 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1173 !pa_tagstruct_eof(t)) {
1174 protocol_error(c);
1175 return;
1176 }
1177
1178 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1179
1180 switch (command) {
1181
1182 case PA_COMMAND_DELETE_PLAYBACK_STREAM: {
1183 playback_stream *s;
1184 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || !playback_stream_isinstance(s)) {
1185 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1186 return;
1187 }
1188
1189 playback_stream_unlink(s);
1190 break;
1191 }
1192
1193 case PA_COMMAND_DELETE_RECORD_STREAM: {
1194 record_stream *s;
1195 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
1196 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1197 return;
1198 }
1199
1200 record_stream_unlink(s);
1201 break;
1202 }
1203
1204 case PA_COMMAND_DELETE_UPLOAD_STREAM: {
1205 upload_stream *s;
1206
1207 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || !upload_stream_isinstance(s)) {
1208 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1209 return;
1210 }
1211
1212 upload_stream_unlink(s);
1213 break;
1214 }
1215
1216 default:
1217 pa_assert_not_reached();
1218 }
1219
1220 pa_pstream_send_simple_ack(c->pstream, tag);
1221 }
1222
1223 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) {
1224 connection *c = CONNECTION(userdata);
1225 record_stream *s;
1226 uint32_t maxlength, fragment_size;
1227 uint32_t source_index;
1228 const char *name, *source_name;
1229 pa_sample_spec ss;
1230 pa_channel_map map;
1231 pa_tagstruct *reply;
1232 pa_source *source = NULL;
1233 int corked;
1234
1235 connection_assert_ref(c);
1236 pa_assert(t);
1237
1238 if (pa_tagstruct_gets(t, &name) < 0 ||
1239 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
1240 pa_tagstruct_get_channel_map(t, &map) < 0 ||
1241 pa_tagstruct_getu32(t, &source_index) < 0 ||
1242 pa_tagstruct_gets(t, &source_name) < 0 ||
1243 pa_tagstruct_getu32(t, &maxlength) < 0 ||
1244 pa_tagstruct_get_boolean(t, &corked) < 0 ||
1245 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
1246 !pa_tagstruct_eof(t)) {
1247 protocol_error(c);
1248 return;
1249 }
1250
1251 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1252 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1253 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1254 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1255 CHECK_VALIDITY(c->pstream, source_index != PA_INVALID_INDEX || !source_name || (*source_name && pa_utf8_valid(source_name)), tag, PA_ERR_INVALID);
1256 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
1257 CHECK_VALIDITY(c->pstream, maxlength <= MAX_MEMBLOCKQ_LENGTH, tag, PA_ERR_INVALID);
1258
1259 if (source_index != PA_INVALID_INDEX) {
1260 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
1261 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
1262 } else if (source_name) {
1263 source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1);
1264 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
1265 }
1266
1267 s = record_stream_new(c, source, &ss, &map, name, &maxlength, fragment_size, corked);
1268 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1269
1270 reply = reply_new(tag);
1271 pa_tagstruct_putu32(reply, s->index);
1272 pa_assert(s->source_output);
1273 pa_tagstruct_putu32(reply, s->source_output->index);
1274
1275 if (c->version >= 9) {
1276 /* Since 0.9 we support sending the buffer metrics back to the client */
1277
1278 pa_tagstruct_putu32(reply, (uint32_t) maxlength);
1279 pa_tagstruct_putu32(reply, (uint32_t) s->fragment_size);
1280 }
1281
1282 pa_pstream_send_tagstruct(c->pstream, reply);
1283 }
1284
1285 static void command_exit(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1286 connection *c = CONNECTION(userdata);
1287
1288 connection_assert_ref(c);
1289 pa_assert(t);
1290
1291 if (!pa_tagstruct_eof(t)) {
1292 protocol_error(c);
1293 return;
1294 }
1295
1296 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1297
1298 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
1299 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
1300 }
1301
1302 static void command_auth(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1303 connection *c = CONNECTION(userdata);
1304 const void*cookie;
1305 pa_tagstruct *reply;
1306
1307 connection_assert_ref(c);
1308 pa_assert(t);
1309
1310 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
1311 pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
1312 !pa_tagstruct_eof(t)) {
1313 protocol_error(c);
1314 return;
1315 }
1316
1317 /* Minimum supported version */
1318 if (c->version < 8) {
1319 pa_pstream_send_error(c->pstream, tag, PA_ERR_VERSION);
1320 return;
1321 }
1322
1323 if (!c->authorized) {
1324 int success = 0;
1325
1326 #ifdef HAVE_CREDS
1327 const pa_creds *creds;
1328
1329 if ((creds = pa_pdispatch_creds(pd))) {
1330 if (creds->uid == getuid())
1331 success = 1;
1332 else if (c->protocol->auth_group) {
1333 int r;
1334 gid_t gid;
1335
1336 if ((gid = pa_get_gid_of_group(c->protocol->auth_group)) == (gid_t) -1)
1337 pa_log_warn("failed to get GID of group '%s'", c->protocol->auth_group);
1338 else if (gid == creds->gid)
1339 success = 1;
1340
1341 if (!success) {
1342 if ((r = pa_uid_in_group(creds->uid, c->protocol->auth_group)) < 0)
1343 pa_log_warn("failed to check group membership.");
1344 else if (r > 0)
1345 success = 1;
1346 }
1347 }
1348
1349 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
1350 (unsigned long) creds->uid,
1351 (unsigned long) creds->gid,
1352 success);
1353
1354 if (c->version >= 10 &&
1355 pa_mempool_is_shared(c->protocol->core->mempool) &&
1356 creds->uid == getuid()) {
1357
1358 pa_pstream_use_shm(c->pstream, 1);
1359 pa_log_info("Enabled SHM for new connection");
1360 }
1361
1362 }
1363 #endif
1364
1365 if (!success && memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) == 0)
1366 success = 1;
1367
1368 if (!success) {
1369 pa_log_warn("Denied access to client with invalid authorization data.");
1370 pa_pstream_send_error(c->pstream, tag, PA_ERR_ACCESS);
1371 return;
1372 }
1373
1374 c->authorized = 1;
1375 if (c->auth_timeout_event) {
1376 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
1377 c->auth_timeout_event = NULL;
1378 }
1379 }
1380
1381 reply = reply_new(tag);
1382 pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION);
1383
1384 #ifdef HAVE_CREDS
1385 {
1386 /* SHM support is only enabled after both sides made sure they are the same user. */
1387
1388 pa_creds ucred;
1389
1390 ucred.uid = getuid();
1391 ucred.gid = getgid();
1392
1393 pa_pstream_send_tagstruct_with_creds(c->pstream, reply, &ucred);
1394 }
1395 #else
1396 pa_pstream_send_tagstruct(c->pstream, reply);
1397 #endif
1398 }
1399
1400 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) {
1401 connection *c = CONNECTION(userdata);
1402 const char *name;
1403
1404 connection_assert_ref(c);
1405 pa_assert(t);
1406
1407 if (pa_tagstruct_gets(t, &name) < 0 ||
1408 !pa_tagstruct_eof(t)) {
1409 protocol_error(c);
1410 return;
1411 }
1412
1413 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1414
1415 pa_client_set_name(c->client, name);
1416 pa_pstream_send_simple_ack(c->pstream, tag);
1417 }
1418
1419 static void command_lookup(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1420 connection *c = CONNECTION(userdata);
1421 const char *name;
1422 uint32_t idx = PA_IDXSET_INVALID;
1423
1424 connection_assert_ref(c);
1425 pa_assert(t);
1426
1427 if (pa_tagstruct_gets(t, &name) < 0 ||
1428 !pa_tagstruct_eof(t)) {
1429 protocol_error(c);
1430 return;
1431 }
1432
1433 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1434 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1435
1436 if (command == PA_COMMAND_LOOKUP_SINK) {
1437 pa_sink *sink;
1438 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
1439 idx = sink->index;
1440 } else {
1441 pa_source *source;
1442 pa_assert(command == PA_COMMAND_LOOKUP_SOURCE);
1443 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
1444 idx = source->index;
1445 }
1446
1447 if (idx == PA_IDXSET_INVALID)
1448 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1449 else {
1450 pa_tagstruct *reply;
1451 reply = reply_new(tag);
1452 pa_tagstruct_putu32(reply, idx);
1453 pa_pstream_send_tagstruct(c->pstream, reply);
1454 }
1455 }
1456
1457 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) {
1458 connection *c = CONNECTION(userdata);
1459 uint32_t idx;
1460 playback_stream *s;
1461
1462 connection_assert_ref(c);
1463 pa_assert(t);
1464
1465 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1466 !pa_tagstruct_eof(t)) {
1467 protocol_error(c);
1468 return;
1469 }
1470
1471 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1472 s = pa_idxset_get_by_index(c->output_streams, idx);
1473 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1474 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
1475
1476 pa_asyncmsgq_post(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_DRAIN, PA_UINT_TO_PTR(tag), 0, NULL, NULL);
1477 }
1478
1479 static void command_stat(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1480 connection *c = CONNECTION(userdata);
1481 pa_tagstruct *reply;
1482 const pa_mempool_stat *stat;
1483
1484 connection_assert_ref(c);
1485 pa_assert(t);
1486
1487 if (!pa_tagstruct_eof(t)) {
1488 protocol_error(c);
1489 return;
1490 }
1491
1492 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1493
1494 stat = pa_mempool_get_stat(c->protocol->core->mempool);
1495
1496 reply = reply_new(tag);
1497 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->n_allocated));
1498 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->allocated_size));
1499 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->n_accumulated));
1500 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->accumulated_size));
1501 pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
1502 pa_pstream_send_tagstruct(c->pstream, reply);
1503 }
1504
1505 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) {
1506 connection *c = CONNECTION(userdata);
1507 pa_tagstruct *reply;
1508 playback_stream *s;
1509 struct timeval tv, now;
1510 uint32_t idx;
1511 pa_usec_t latency;
1512
1513 connection_assert_ref(c);
1514 pa_assert(t);
1515
1516 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1517 pa_tagstruct_get_timeval(t, &tv) < 0 ||
1518 !pa_tagstruct_eof(t)) {
1519 protocol_error(c);
1520 return;
1521 }
1522
1523 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1524 s = pa_idxset_get_by_index(c->output_streams, idx);
1525 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1526 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
1527 CHECK_VALIDITY(c->pstream, pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_UPDATE_LATENCY, s, 0, NULL) == 0, tag, PA_ERR_NOENTITY)
1528
1529 reply = reply_new(tag);
1530
1531 latency = pa_sink_get_latency(s->sink_input->sink);
1532 latency += pa_bytes_to_usec(s->resampled_chunk_length, &s->sink_input->sample_spec);
1533
1534 pa_tagstruct_put_usec(reply, latency);
1535
1536 pa_tagstruct_put_usec(reply, 0);
1537 pa_tagstruct_put_boolean(reply, pa_sink_input_get_state(s->sink_input) == PA_SINK_INPUT_RUNNING);
1538 pa_tagstruct_put_timeval(reply, &tv);
1539 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
1540 pa_tagstruct_puts64(reply, s->write_index);
1541 pa_tagstruct_puts64(reply, s->read_index);
1542 pa_pstream_send_tagstruct(c->pstream, reply);
1543 }
1544
1545 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) {
1546 connection *c = CONNECTION(userdata);
1547 pa_tagstruct *reply;
1548 record_stream *s;
1549 struct timeval tv, now;
1550 uint32_t idx;
1551
1552 connection_assert_ref(c);
1553 pa_assert(t);
1554
1555 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1556 pa_tagstruct_get_timeval(t, &tv) < 0 ||
1557 !pa_tagstruct_eof(t)) {
1558 protocol_error(c);
1559 return;
1560 }
1561
1562 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1563 s = pa_idxset_get_by_index(c->record_streams, idx);
1564 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1565
1566 reply = reply_new(tag);
1567 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
1568 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
1569 pa_tagstruct_put_boolean(reply, 0);
1570 pa_tagstruct_put_timeval(reply, &tv);
1571 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
1572 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
1573 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
1574 pa_pstream_send_tagstruct(c->pstream, reply);
1575 }
1576
1577 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) {
1578 connection *c = CONNECTION(userdata);
1579 upload_stream *s;
1580 uint32_t length;
1581 const char *name;
1582 pa_sample_spec ss;
1583 pa_channel_map map;
1584 pa_tagstruct *reply;
1585
1586 connection_assert_ref(c);
1587 pa_assert(t);
1588
1589 if (pa_tagstruct_gets(t, &name) < 0 ||
1590 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
1591 pa_tagstruct_get_channel_map(t, &map) < 0 ||
1592 pa_tagstruct_getu32(t, &length) < 0 ||
1593 !pa_tagstruct_eof(t)) {
1594 protocol_error(c);
1595 return;
1596 }
1597
1598 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1599 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1600 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1601 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
1602 CHECK_VALIDITY(c->pstream, (length % pa_frame_size(&ss)) == 0 && length > 0, tag, PA_ERR_INVALID);
1603 CHECK_VALIDITY(c->pstream, length <= PA_SCACHE_ENTRY_SIZE_MAX, tag, PA_ERR_TOOLARGE);
1604 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1605
1606 s = upload_stream_new(c, &ss, &map, name, length);
1607 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1608
1609 reply = reply_new(tag);
1610 pa_tagstruct_putu32(reply, s->index);
1611 pa_tagstruct_putu32(reply, length);
1612 pa_pstream_send_tagstruct(c->pstream, reply);
1613 }
1614
1615 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) {
1616 connection *c = CONNECTION(userdata);
1617 uint32_t channel;
1618 upload_stream *s;
1619 uint32_t idx;
1620
1621 connection_assert_ref(c);
1622 pa_assert(t);
1623
1624 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1625 !pa_tagstruct_eof(t)) {
1626 protocol_error(c);
1627 return;
1628 }
1629
1630 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1631
1632 s = pa_idxset_get_by_index(c->output_streams, channel);
1633 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
1634 CHECK_VALIDITY(c->pstream, upload_stream_isinstance(s), tag, PA_ERR_NOENTITY);
1635
1636 if (pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->channel_map, &s->memchunk, &idx) < 0)
1637 pa_pstream_send_error(c->pstream, tag, PA_ERR_INTERNAL);
1638 else
1639 pa_pstream_send_simple_ack(c->pstream, tag);
1640
1641 upload_stream_unlink(s);
1642 }
1643
1644 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) {
1645 connection *c = CONNECTION(userdata);
1646 uint32_t sink_index;
1647 pa_volume_t volume;
1648 pa_sink *sink;
1649 const char *name, *sink_name;
1650
1651 connection_assert_ref(c);
1652 pa_assert(t);
1653
1654 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
1655 pa_tagstruct_gets(t, &sink_name) < 0 ||
1656 pa_tagstruct_getu32(t, &volume) < 0 ||
1657 pa_tagstruct_gets(t, &name) < 0 ||
1658 !pa_tagstruct_eof(t)) {
1659 protocol_error(c);
1660 return;
1661 }
1662
1663 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1664 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1665 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1666
1667 if (sink_index != PA_INVALID_INDEX)
1668 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1669 else
1670 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1671
1672 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
1673
1674 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
1675 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1676 return;
1677 }
1678
1679 pa_pstream_send_simple_ack(c->pstream, tag);
1680 }
1681
1682 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) {
1683 connection *c = CONNECTION(userdata);
1684 const char *name;
1685
1686 connection_assert_ref(c);
1687 pa_assert(t);
1688
1689 if (pa_tagstruct_gets(t, &name) < 0 ||
1690 !pa_tagstruct_eof(t)) {
1691 protocol_error(c);
1692 return;
1693 }
1694
1695 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1696 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
1697
1698 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
1699 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1700 return;
1701 }
1702
1703 pa_pstream_send_simple_ack(c->pstream, tag);
1704 }
1705
1706 static void sink_fill_tagstruct(pa_tagstruct *t, pa_sink *sink) {
1707 pa_assert(t);
1708 pa_sink_assert_ref(sink);
1709
1710 pa_tagstruct_put(
1711 t,
1712 PA_TAG_U32, sink->index,
1713 PA_TAG_STRING, sink->name,
1714 PA_TAG_STRING, sink->description,
1715 PA_TAG_SAMPLE_SPEC, &sink->sample_spec,
1716 PA_TAG_CHANNEL_MAP, &sink->channel_map,
1717 PA_TAG_U32, sink->module ? sink->module->index : PA_INVALID_INDEX,
1718 PA_TAG_CVOLUME, pa_sink_get_volume(sink),
1719 PA_TAG_BOOLEAN, pa_sink_get_mute(sink),
1720 PA_TAG_U32, sink->monitor_source ? sink->monitor_source->index : PA_INVALID_INDEX,
1721 PA_TAG_STRING, sink->monitor_source ? sink->monitor_source->name : NULL,
1722 PA_TAG_USEC, pa_sink_get_latency(sink),
1723 PA_TAG_STRING, sink->driver,
1724 PA_TAG_U32,
1725 (sink->get_volume ? PA_SINK_HW_VOLUME_CTRL : 0) | /* FIXME */
1726 (sink->get_latency ? PA_SINK_LATENCY : 0) | /* FIXME */
1727 (sink->is_hardware ? PA_SINK_HARDWARE : 0),
1728 PA_TAG_INVALID);
1729 }
1730
1731 static void source_fill_tagstruct(pa_tagstruct *t, pa_source *source) {
1732 pa_assert(t);
1733 pa_source_assert_ref(source);
1734
1735 pa_tagstruct_put(
1736 t,
1737 PA_TAG_U32, source->index,
1738 PA_TAG_STRING, source->name,
1739 PA_TAG_STRING, source->description,
1740 PA_TAG_SAMPLE_SPEC, &source->sample_spec,
1741 PA_TAG_CHANNEL_MAP, &source->channel_map,
1742 PA_TAG_U32, source->module ? source->module->index : PA_INVALID_INDEX,
1743 PA_TAG_CVOLUME, pa_source_get_volume(source),
1744 PA_TAG_BOOLEAN, pa_source_get_mute(source),
1745 PA_TAG_U32, source->monitor_of ? source->monitor_of->index : PA_INVALID_INDEX,
1746 PA_TAG_STRING, source->monitor_of ? source->monitor_of->name : NULL,
1747 PA_TAG_USEC, pa_source_get_latency(source),
1748 PA_TAG_STRING, source->driver,
1749 PA_TAG_U32,
1750 (source->get_volume ? PA_SOURCE_HW_VOLUME_CTRL : 0) | /* FIXME */
1751 (source->get_latency ? PA_SOURCE_LATENCY : 0) | /* FIXME */
1752 (source->is_hardware ? PA_SOURCE_HARDWARE : 0),
1753 PA_TAG_INVALID);
1754 }
1755
1756 static void client_fill_tagstruct(pa_tagstruct *t, pa_client *client) {
1757 pa_assert(t);
1758 pa_assert(client);
1759
1760 pa_tagstruct_putu32(t, client->index);
1761 pa_tagstruct_puts(t, client->name);
1762 pa_tagstruct_putu32(t, client->owner ? client->owner->index : PA_INVALID_INDEX);
1763 pa_tagstruct_puts(t, client->driver);
1764 }
1765
1766 static void module_fill_tagstruct(pa_tagstruct *t, pa_module *module) {
1767 pa_assert(t);
1768 pa_assert(module);
1769
1770 pa_tagstruct_putu32(t, module->index);
1771 pa_tagstruct_puts(t, module->name);
1772 pa_tagstruct_puts(t, module->argument);
1773 pa_tagstruct_putu32(t, module->n_used);
1774 pa_tagstruct_put_boolean(t, module->auto_unload);
1775 }
1776
1777 static void sink_input_fill_tagstruct(connection *c, pa_tagstruct *t, pa_sink_input *s) {
1778 pa_assert(t);
1779 pa_sink_input_assert_ref(s);
1780
1781 pa_tagstruct_putu32(t, s->index);
1782 pa_tagstruct_puts(t, s->name);
1783 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
1784 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
1785 pa_tagstruct_putu32(t, s->sink->index);
1786 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1787 pa_tagstruct_put_channel_map(t, &s->channel_map);
1788 pa_tagstruct_put_cvolume(t, &s->volume);
1789 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
1790 pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
1791 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
1792 pa_tagstruct_puts(t, s->driver);
1793 if (c->version >= 11)
1794 pa_tagstruct_put_boolean(t, pa_sink_input_get_mute(s));
1795 }
1796
1797 static void source_output_fill_tagstruct(pa_tagstruct *t, pa_source_output *s) {
1798 pa_assert(t);
1799 pa_source_output_assert_ref(s);
1800
1801 pa_tagstruct_putu32(t, s->index);
1802 pa_tagstruct_puts(t, s->name);
1803 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
1804 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
1805 pa_tagstruct_putu32(t, s->source->index);
1806 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1807 pa_tagstruct_put_channel_map(t, &s->channel_map);
1808 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
1809 pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
1810 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
1811 pa_tagstruct_puts(t, s->driver);
1812 }
1813
1814 static void scache_fill_tagstruct(pa_tagstruct *t, pa_scache_entry *e) {
1815 pa_assert(t);
1816 pa_assert(e);
1817
1818 pa_tagstruct_putu32(t, e->index);
1819 pa_tagstruct_puts(t, e->name);
1820 pa_tagstruct_put_cvolume(t, &e->volume);
1821 pa_tagstruct_put_usec(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1822 pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1823 pa_tagstruct_put_channel_map(t, &e->channel_map);
1824 pa_tagstruct_putu32(t, e->memchunk.length);
1825 pa_tagstruct_put_boolean(t, e->lazy);
1826 pa_tagstruct_puts(t, e->filename);
1827 }
1828
1829 static void command_get_info(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1830 connection *c = CONNECTION(userdata);
1831 uint32_t idx;
1832 pa_sink *sink = NULL;
1833 pa_source *source = NULL;
1834 pa_client *client = NULL;
1835 pa_module *module = NULL;
1836 pa_sink_input *si = NULL;
1837 pa_source_output *so = NULL;
1838 pa_scache_entry *sce = NULL;
1839 const char *name;
1840 pa_tagstruct *reply;
1841
1842 connection_assert_ref(c);
1843 pa_assert(t);
1844
1845 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1846 (command != PA_COMMAND_GET_CLIENT_INFO &&
1847 command != PA_COMMAND_GET_MODULE_INFO &&
1848 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1849 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1850 pa_tagstruct_gets(t, &name) < 0) ||
1851 !pa_tagstruct_eof(t)) {
1852 protocol_error(c);
1853 return;
1854 }
1855
1856 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1857 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
1858
1859 if (command == PA_COMMAND_GET_SINK_INFO) {
1860 if (idx != PA_INVALID_INDEX)
1861 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
1862 else
1863 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1864 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1865 if (idx != PA_INVALID_INDEX)
1866 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
1867 else
1868 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1869 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1870 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
1871 else if (command == PA_COMMAND_GET_MODULE_INFO)
1872 module = pa_idxset_get_by_index(c->protocol->core->modules, idx);
1873 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1874 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
1875 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1876 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
1877 else {
1878 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO);
1879 if (idx != PA_INVALID_INDEX)
1880 sce = pa_idxset_get_by_index(c->protocol->core->scache, idx);
1881 else
1882 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1883 }
1884
1885 if (!sink && !source && !client && !module && !si && !so && !sce) {
1886 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1887 return;
1888 }
1889
1890 reply = reply_new(tag);
1891 if (sink)
1892 sink_fill_tagstruct(reply, sink);
1893 else if (source)
1894 source_fill_tagstruct(reply, source);
1895 else if (client)
1896 client_fill_tagstruct(reply, client);
1897 else if (module)
1898 module_fill_tagstruct(reply, module);
1899 else if (si)
1900 sink_input_fill_tagstruct(c, reply, si);
1901 else if (so)
1902 source_output_fill_tagstruct(reply, so);
1903 else
1904 scache_fill_tagstruct(reply, sce);
1905 pa_pstream_send_tagstruct(c->pstream, reply);
1906 }
1907
1908 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1909 connection *c = CONNECTION(userdata);
1910 pa_idxset *i;
1911 uint32_t idx;
1912 void *p;
1913 pa_tagstruct *reply;
1914
1915 connection_assert_ref(c);
1916 pa_assert(t);
1917
1918 if (!pa_tagstruct_eof(t)) {
1919 protocol_error(c);
1920 return;
1921 }
1922
1923 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1924
1925 reply = reply_new(tag);
1926
1927 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1928 i = c->protocol->core->sinks;
1929 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1930 i = c->protocol->core->sources;
1931 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1932 i = c->protocol->core->clients;
1933 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1934 i = c->protocol->core->modules;
1935 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1936 i = c->protocol->core->sink_inputs;
1937 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1938 i = c->protocol->core->source_outputs;
1939 else {
1940 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1941 i = c->protocol->core->scache;
1942 }
1943
1944 if (i) {
1945 for (p = pa_idxset_first(i, &idx); p; p = pa_idxset_next(i, &idx)) {
1946 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1947 sink_fill_tagstruct(reply, p);
1948 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1949 source_fill_tagstruct(reply, p);
1950 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1951 client_fill_tagstruct(reply, p);
1952 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1953 module_fill_tagstruct(reply, p);
1954 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1955 sink_input_fill_tagstruct(c, reply, p);
1956 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1957 source_output_fill_tagstruct(reply, p);
1958 else {
1959 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1960 scache_fill_tagstruct(reply, p);
1961 }
1962 }
1963 }
1964
1965 pa_pstream_send_tagstruct(c->pstream, reply);
1966 }
1967
1968 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) {
1969 connection *c = CONNECTION(userdata);
1970 pa_tagstruct *reply;
1971 char txt[256];
1972 const char *n;
1973
1974 connection_assert_ref(c);
1975 pa_assert(t);
1976
1977 if (!pa_tagstruct_eof(t)) {
1978 protocol_error(c);
1979 return;
1980 }
1981
1982 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1983
1984 reply = reply_new(tag);
1985 pa_tagstruct_puts(reply, PACKAGE_NAME);
1986 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1987 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1988 pa_tagstruct_puts(reply, pa_get_fqdn(txt, sizeof(txt)));
1989 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1990
1991 n = pa_namereg_get_default_sink_name(c->protocol->core);
1992 pa_tagstruct_puts(reply, n);
1993 n = pa_namereg_get_default_source_name(c->protocol->core);
1994 pa_tagstruct_puts(reply, n);
1995
1996 pa_tagstruct_putu32(reply, c->protocol->core->cookie);
1997
1998 pa_pstream_send_tagstruct(c->pstream, reply);
1999 }
2000
2001 static void subscription_cb(pa_core *core, pa_subscription_event_type_t e, uint32_t idx, void *userdata) {
2002 pa_tagstruct *t;
2003 connection *c = CONNECTION(userdata);
2004
2005 connection_assert_ref(c);
2006
2007 t = pa_tagstruct_new(NULL, 0);
2008 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
2009 pa_tagstruct_putu32(t, (uint32_t) -1);
2010 pa_tagstruct_putu32(t, e);
2011 pa_tagstruct_putu32(t, idx);
2012 pa_pstream_send_tagstruct(c->pstream, t);
2013 }
2014
2015 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2016 connection *c = CONNECTION(userdata);
2017 pa_subscription_mask_t m;
2018
2019 connection_assert_ref(c);
2020 pa_assert(t);
2021
2022 if (pa_tagstruct_getu32(t, &m) < 0 ||
2023 !pa_tagstruct_eof(t)) {
2024 protocol_error(c);
2025 return;
2026 }
2027
2028 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2029 CHECK_VALIDITY(c->pstream, (m & ~PA_SUBSCRIPTION_MASK_ALL) == 0, tag, PA_ERR_INVALID);
2030
2031 if (c->subscription)
2032 pa_subscription_free(c->subscription);
2033
2034 if (m != 0) {
2035 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
2036 pa_assert(c->subscription);
2037 } else
2038 c->subscription = NULL;
2039
2040 pa_pstream_send_simple_ack(c->pstream, tag);
2041 }
2042
2043 static void command_set_volume(
2044 PA_GCC_UNUSED pa_pdispatch *pd,
2045 uint32_t command,
2046 uint32_t tag,
2047 pa_tagstruct *t,
2048 void *userdata) {
2049
2050 connection *c = CONNECTION(userdata);
2051 uint32_t idx;
2052 pa_cvolume volume;
2053 pa_sink *sink = NULL;
2054 pa_source *source = NULL;
2055 pa_sink_input *si = NULL;
2056 const char *name = NULL;
2057
2058 connection_assert_ref(c);
2059 pa_assert(t);
2060
2061 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2062 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
2063 (command == PA_COMMAND_SET_SOURCE_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
2064 pa_tagstruct_get_cvolume(t, &volume) ||
2065 !pa_tagstruct_eof(t)) {
2066 protocol_error(c);
2067 return;
2068 }
2069
2070 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2071 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2072 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
2073
2074 switch (command) {
2075
2076 case PA_COMMAND_SET_SINK_VOLUME:
2077 if (idx != PA_INVALID_INDEX)
2078 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2079 else
2080 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2081 break;
2082
2083 case PA_COMMAND_SET_SOURCE_VOLUME:
2084 if (idx != PA_INVALID_INDEX)
2085 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2086 else
2087 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2088 break;
2089
2090 case PA_COMMAND_SET_SINK_INPUT_VOLUME:
2091 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2092 break;
2093
2094 default:
2095 pa_assert_not_reached();
2096 }
2097
2098 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
2099
2100 if (sink)
2101 pa_sink_set_volume(sink, &volume);
2102 else if (source)
2103 pa_source_set_volume(source, &volume);
2104 else if (si)
2105 pa_sink_input_set_volume(si, &volume);
2106
2107 pa_pstream_send_simple_ack(c->pstream, tag);
2108 }
2109
2110 static void command_set_mute(
2111 PA_GCC_UNUSED pa_pdispatch *pd,
2112 uint32_t command,
2113 uint32_t tag,
2114 pa_tagstruct *t,
2115 void *userdata) {
2116
2117 connection *c = CONNECTION(userdata);
2118 uint32_t idx;
2119 int mute;
2120 pa_sink *sink = NULL;
2121 pa_source *source = NULL;
2122 pa_sink_input *si = NULL;
2123 const char *name = NULL;
2124
2125 connection_assert_ref(c);
2126 pa_assert(t);
2127
2128 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2129 (command == PA_COMMAND_SET_SINK_MUTE && pa_tagstruct_gets(t, &name) < 0) ||
2130 (command == PA_COMMAND_SET_SOURCE_MUTE && pa_tagstruct_gets(t, &name) < 0) ||
2131 pa_tagstruct_get_boolean(t, &mute) ||
2132 !pa_tagstruct_eof(t)) {
2133 protocol_error(c);
2134 return;
2135 }
2136
2137 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2138 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2139
2140 switch (command) {
2141
2142 case PA_COMMAND_SET_SINK_MUTE:
2143
2144 if (idx != PA_INVALID_INDEX)
2145 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2146 else
2147 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2148
2149 break;
2150
2151 case PA_COMMAND_SET_SOURCE_MUTE:
2152 if (idx != PA_INVALID_INDEX)
2153 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2154 else
2155 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2156
2157 break;
2158
2159 case PA_COMMAND_SET_SINK_INPUT_MUTE:
2160 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2161 break;
2162
2163 default:
2164 pa_assert_not_reached();
2165 }
2166
2167 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
2168
2169 if (sink)
2170 pa_sink_set_mute(sink, mute);
2171 else if (source)
2172 pa_source_set_mute(source, mute);
2173 else if (si)
2174 pa_sink_input_set_mute(si, mute);
2175
2176 pa_pstream_send_simple_ack(c->pstream, tag);
2177 }
2178
2179 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) {
2180 connection *c = CONNECTION(userdata);
2181 uint32_t idx;
2182 int b;
2183 playback_stream *s;
2184
2185 connection_assert_ref(c);
2186 pa_assert(t);
2187
2188 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2189 pa_tagstruct_get_boolean(t, &b) < 0 ||
2190 !pa_tagstruct_eof(t)) {
2191 protocol_error(c);
2192 return;
2193 }
2194
2195 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2196 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2197 s = pa_idxset_get_by_index(c->output_streams, idx);
2198 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2199 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2200
2201 pa_sink_input_cork(s->sink_input, b);
2202 pa_pstream_send_simple_ack(c->pstream, tag);
2203 }
2204
2205 static void command_trigger_or_flush_or_prebuf_playback_stream(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2206 connection *c = CONNECTION(userdata);
2207 uint32_t idx;
2208 playback_stream *s;
2209
2210 connection_assert_ref(c);
2211 pa_assert(t);
2212
2213 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2214 !pa_tagstruct_eof(t)) {
2215 protocol_error(c);
2216 return;
2217 }
2218
2219 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2220 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2221 s = pa_idxset_get_by_index(c->output_streams, idx);
2222 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2223 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2224
2225 switch (command) {
2226 case PA_COMMAND_FLUSH_PLAYBACK_STREAM:
2227 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_FLUSH, NULL, 0, NULL);
2228 break;
2229
2230 case PA_COMMAND_PREBUF_PLAYBACK_STREAM:
2231 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_PREBUF_FORCE, NULL, 0, NULL);
2232 break;
2233
2234 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM:
2235 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_TRIGGER, NULL, 0, NULL);
2236 break;
2237
2238 default:
2239 pa_assert_not_reached();
2240 }
2241
2242 pa_pstream_send_simple_ack(c->pstream, tag);
2243 }
2244
2245 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) {
2246 connection *c = CONNECTION(userdata);
2247 uint32_t idx;
2248 record_stream *s;
2249 int b;
2250
2251 connection_assert_ref(c);
2252 pa_assert(t);
2253
2254 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2255 pa_tagstruct_get_boolean(t, &b) < 0 ||
2256 !pa_tagstruct_eof(t)) {
2257 protocol_error(c);
2258 return;
2259 }
2260
2261 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2262 s = pa_idxset_get_by_index(c->record_streams, idx);
2263 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2264
2265 pa_source_output_cork(s->source_output, b);
2266 pa_memblockq_prebuf_force(s->memblockq);
2267 pa_pstream_send_simple_ack(c->pstream, tag);
2268 }
2269
2270 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) {
2271 connection *c = CONNECTION(userdata);
2272 uint32_t idx;
2273 record_stream *s;
2274
2275 connection_assert_ref(c);
2276 pa_assert(t);
2277
2278 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2279 !pa_tagstruct_eof(t)) {
2280 protocol_error(c);
2281 return;
2282 }
2283
2284 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2285 s = pa_idxset_get_by_index(c->record_streams, idx);
2286 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2287
2288 pa_memblockq_flush(s->memblockq);
2289 pa_pstream_send_simple_ack(c->pstream, tag);
2290 }
2291
2292 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) {
2293 connection *c = CONNECTION(userdata);
2294 const char *s;
2295
2296 connection_assert_ref(c);
2297 pa_assert(t);
2298
2299 if (pa_tagstruct_gets(t, &s) < 0 ||
2300 !pa_tagstruct_eof(t)) {
2301 protocol_error(c);
2302 return;
2303 }
2304
2305 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2306 CHECK_VALIDITY(c->pstream, !s || (*s && pa_utf8_valid(s)), tag, PA_ERR_INVALID);
2307
2308 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
2309 pa_pstream_send_simple_ack(c->pstream, tag);
2310 }
2311
2312 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2313 connection *c = CONNECTION(userdata);
2314 uint32_t idx;
2315 const char *name;
2316
2317 connection_assert_ref(c);
2318 pa_assert(t);
2319
2320 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2321 pa_tagstruct_gets(t, &name) < 0 ||
2322 !pa_tagstruct_eof(t)) {
2323 protocol_error(c);
2324 return;
2325 }
2326
2327 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2328 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
2329
2330 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
2331 playback_stream *s;
2332
2333 s = pa_idxset_get_by_index(c->output_streams, idx);
2334 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2335 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2336
2337 pa_sink_input_set_name(s->sink_input, name);
2338
2339 } else {
2340 record_stream *s;
2341
2342 s = pa_idxset_get_by_index(c->record_streams, idx);
2343 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2344
2345 pa_source_output_set_name(s->source_output, name);
2346 }
2347
2348 pa_pstream_send_simple_ack(c->pstream, tag);
2349 }
2350
2351 static void command_kill(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2352 connection *c = CONNECTION(userdata);
2353 uint32_t idx;
2354
2355 connection_assert_ref(c);
2356 pa_assert(t);
2357
2358 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2359 !pa_tagstruct_eof(t)) {
2360 protocol_error(c);
2361 return;
2362 }
2363
2364 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2365
2366 if (command == PA_COMMAND_KILL_CLIENT) {
2367 pa_client *client;
2368
2369 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
2370 CHECK_VALIDITY(c->pstream, client, tag, PA_ERR_NOENTITY);
2371
2372 connection_ref(c);
2373 pa_client_kill(client);
2374
2375 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
2376 pa_sink_input *s;
2377
2378 s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2379 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2380
2381 connection_ref(c);
2382 pa_sink_input_kill(s);
2383 } else {
2384 pa_source_output *s;
2385
2386 pa_assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
2387
2388 s = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
2389 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2390
2391 connection_ref(c);
2392 pa_source_output_kill(s);
2393 }
2394
2395 pa_pstream_send_simple_ack(c->pstream, tag);
2396 connection_unref(c);
2397 }
2398
2399 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) {
2400 connection *c = CONNECTION(userdata);
2401 pa_module *m;
2402 const char *name, *argument;
2403 pa_tagstruct *reply;
2404
2405 connection_assert_ref(c);
2406 pa_assert(t);
2407
2408 if (pa_tagstruct_gets(t, &name) < 0 ||
2409 pa_tagstruct_gets(t, &argument) < 0 ||
2410 !pa_tagstruct_eof(t)) {
2411 protocol_error(c);
2412 return;
2413 }
2414
2415 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2416 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name) && !strchr(name, '/'), tag, PA_ERR_INVALID);
2417 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
2418
2419 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
2420 pa_pstream_send_error(c->pstream, tag, PA_ERR_MODINITFAILED);
2421 return;
2422 }
2423
2424 reply = reply_new(tag);
2425 pa_tagstruct_putu32(reply, m->index);
2426 pa_pstream_send_tagstruct(c->pstream, reply);
2427 }
2428
2429 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) {
2430 connection *c = CONNECTION(userdata);
2431 uint32_t idx;
2432 pa_module *m;
2433
2434 connection_assert_ref(c);
2435 pa_assert(t);
2436
2437 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2438 !pa_tagstruct_eof(t)) {
2439 protocol_error(c);
2440 return;
2441 }
2442
2443 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2444 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
2445 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOENTITY);
2446
2447 pa_module_unload_request(m);
2448 pa_pstream_send_simple_ack(c->pstream, tag);
2449 }
2450
2451 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) {
2452 connection *c = CONNECTION(userdata);
2453 const char *name, *module, *argument;
2454 uint32_t type;
2455 uint32_t idx;
2456 pa_tagstruct *reply;
2457
2458 connection_assert_ref(c);
2459 pa_assert(t);
2460
2461 if (pa_tagstruct_gets(t, &name) < 0 ||
2462 pa_tagstruct_getu32(t, &type) < 0 ||
2463 pa_tagstruct_gets(t, &module) < 0 ||
2464 pa_tagstruct_gets(t, &argument) < 0 ||
2465 !pa_tagstruct_eof(t)) {
2466 protocol_error(c);
2467 return;
2468 }
2469
2470 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2471 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
2472 CHECK_VALIDITY(c->pstream, type == 0 || type == 1, tag, PA_ERR_INVALID);
2473 CHECK_VALIDITY(c->pstream, module && *module && pa_utf8_valid(module), tag, PA_ERR_INVALID);
2474 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
2475
2476 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument, &idx) < 0) {
2477 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
2478 return;
2479 }
2480
2481 reply = reply_new(tag);
2482 pa_tagstruct_putu32(reply, idx);
2483 pa_pstream_send_tagstruct(c->pstream, reply);
2484 }
2485
2486 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) {
2487 connection *c = CONNECTION(userdata);
2488 const char *name = NULL;
2489 uint32_t type, idx = PA_IDXSET_INVALID;
2490 int r;
2491
2492 connection_assert_ref(c);
2493 pa_assert(t);
2494
2495 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
2496 (pa_tagstruct_gets(t, &name) < 0 ||
2497 pa_tagstruct_getu32(t, &type) < 0)) ||
2498 !pa_tagstruct_eof(t)) {
2499 protocol_error(c);
2500 return;
2501 }
2502
2503 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2504 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
2505 CHECK_VALIDITY(c->pstream, !name || (*name && pa_utf8_valid(name) && (type == 0 || type == 1)), tag, PA_ERR_INVALID);
2506
2507 if (name)
2508 r = pa_autoload_remove_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
2509 else
2510 r = pa_autoload_remove_by_index(c->protocol->core, idx);
2511
2512 CHECK_VALIDITY(c->pstream, r >= 0, tag, PA_ERR_NOENTITY);
2513
2514 pa_pstream_send_simple_ack(c->pstream, tag);
2515 }
2516
2517 static void autoload_fill_tagstruct(pa_tagstruct *t, const pa_autoload_entry *e) {
2518 pa_assert(t && e);
2519
2520 pa_tagstruct_putu32(t, e->index);
2521 pa_tagstruct_puts(t, e->name);
2522 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
2523 pa_tagstruct_puts(t, e->module);
2524 pa_tagstruct_puts(t, e->argument);
2525 }
2526
2527 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) {
2528 connection *c = CONNECTION(userdata);
2529 const pa_autoload_entry *a = NULL;
2530 uint32_t type, idx;
2531 const char *name;
2532 pa_tagstruct *reply;
2533
2534 connection_assert_ref(c);
2535 pa_assert(t);
2536
2537 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
2538 (pa_tagstruct_gets(t, &name) < 0 ||
2539 pa_tagstruct_getu32(t, &type) < 0)) ||
2540 !pa_tagstruct_eof(t)) {
2541 protocol_error(c);
2542 return;
2543 }
2544
2545 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2546 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
2547 CHECK_VALIDITY(c->pstream, !name || (*name && (type == 0 || type == 1) && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2548
2549 if (name)
2550 a = pa_autoload_get_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
2551 else
2552 a = pa_autoload_get_by_index(c->protocol->core, idx);
2553
2554 CHECK_VALIDITY(c->pstream, a, tag, PA_ERR_NOENTITY);
2555
2556 reply = reply_new(tag);
2557 autoload_fill_tagstruct(reply, a);
2558 pa_pstream_send_tagstruct(c->pstream, reply);
2559 }
2560
2561 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) {
2562 connection *c = CONNECTION(userdata);
2563 pa_tagstruct *reply;
2564
2565 connection_assert_ref(c);
2566 pa_assert(t);
2567
2568 if (!pa_tagstruct_eof(t)) {
2569 protocol_error(c);
2570 return;
2571 }
2572
2573 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2574
2575 reply = reply_new(tag);
2576
2577 if (c->protocol->core->autoload_hashmap) {
2578 pa_autoload_entry *a;
2579 void *state = NULL;
2580
2581 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state, NULL)))
2582 autoload_fill_tagstruct(reply, a);
2583 }
2584
2585 pa_pstream_send_tagstruct(c->pstream, reply);
2586 }
2587
2588 static void command_move_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2589 connection *c = CONNECTION(userdata);
2590 uint32_t idx = PA_INVALID_INDEX, idx_device = PA_INVALID_INDEX;
2591 const char *name = NULL;
2592
2593 connection_assert_ref(c);
2594 pa_assert(t);
2595
2596 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2597 pa_tagstruct_getu32(t, &idx_device) < 0 ||
2598 pa_tagstruct_gets(t, &name) < 0 ||
2599 !pa_tagstruct_eof(t)) {
2600 protocol_error(c);
2601 return;
2602 }
2603
2604 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2605 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2606 CHECK_VALIDITY(c->pstream, idx_device != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2607
2608 if (command == PA_COMMAND_MOVE_SINK_INPUT) {
2609 pa_sink_input *si = NULL;
2610 pa_sink *sink = NULL;
2611
2612 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2613
2614 if (idx_device != PA_INVALID_INDEX)
2615 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx_device);
2616 else
2617 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2618
2619 CHECK_VALIDITY(c->pstream, si && sink, tag, PA_ERR_NOENTITY);
2620
2621 if (pa_sink_input_move_to(si, sink, 0) < 0) {
2622 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
2623 return;
2624 }
2625 } else {
2626 pa_source_output *so = NULL;
2627 pa_source *source;
2628
2629 pa_assert(command == PA_COMMAND_MOVE_SOURCE_OUTPUT);
2630
2631 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
2632
2633 if (idx_device != PA_INVALID_INDEX)
2634 source = pa_idxset_get_by_index(c->protocol->core->sources, idx_device);
2635 else
2636 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2637
2638 CHECK_VALIDITY(c->pstream, so && source, tag, PA_ERR_NOENTITY);
2639
2640 if (pa_source_output_move_to(so, source) < 0) {
2641 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
2642 return;
2643 }
2644 }
2645
2646 pa_pstream_send_simple_ack(c->pstream, tag);
2647 }
2648
2649 static void command_suspend(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2650 connection *c = CONNECTION(userdata);
2651 uint32_t idx = PA_INVALID_INDEX;
2652 const char *name = NULL;
2653 int b;
2654
2655 connection_assert_ref(c);
2656 pa_assert(t);
2657
2658 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2659 pa_tagstruct_gets(t, &name) < 0 ||
2660 pa_tagstruct_get_boolean(t, &b) < 0 ||
2661 !pa_tagstruct_eof(t)) {
2662 protocol_error(c);
2663 return;
2664 }
2665
2666 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2667 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || !*name || pa_utf8_valid(name), tag, PA_ERR_INVALID);
2668
2669 if (command == PA_COMMAND_SUSPEND_SINK) {
2670
2671 if (idx == PA_INVALID_INDEX && name && !*name) {
2672
2673 if (pa_sink_suspend_all(c->protocol->core, b) < 0) {
2674 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
2675 return;
2676 }
2677 } else {
2678 pa_sink *sink = NULL;
2679
2680 if (idx != PA_INVALID_INDEX)
2681 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2682 else
2683 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2684
2685 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
2686
2687 if (pa_sink_suspend(sink, b) < 0) {
2688 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
2689 return;
2690 }
2691 }
2692 } else {
2693
2694 pa_assert(command == PA_COMMAND_SUSPEND_SOURCE);
2695
2696 if (idx == PA_INVALID_INDEX && name && !*name) {
2697
2698 if (pa_source_suspend_all(c->protocol->core, b) < 0) {
2699 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
2700 return;
2701 }
2702
2703 } else {
2704 pa_source *source;
2705
2706 if (idx != PA_INVALID_INDEX)
2707 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2708 else
2709 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2710
2711 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
2712
2713 if (pa_source_suspend(source, b) < 0) {
2714 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
2715 return;
2716 }
2717 }
2718 }
2719
2720 pa_pstream_send_simple_ack(c->pstream, tag);
2721 }
2722
2723 /*** pstream callbacks ***/
2724
2725 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
2726 connection *c = CONNECTION(userdata);
2727
2728 pa_assert(p);
2729 pa_assert(packet);
2730 connection_assert_ref(c);
2731
2732 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0) {
2733 pa_log("invalid packet.");
2734 connection_unlink(c);
2735 }
2736 }
2737
2738 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) {
2739 connection *c = CONNECTION(userdata);
2740 output_stream *stream;
2741
2742 pa_assert(p);
2743 pa_assert(chunk);
2744 connection_assert_ref(c);
2745
2746 if (!(stream = OUTPUT_STREAM(pa_idxset_get_by_index(c->output_streams, channel)))) {
2747 pa_log("client sent block for invalid stream.");
2748 /* Ignoring */
2749 return;
2750 }
2751
2752 if (playback_stream_isinstance(stream)) {
2753 playback_stream *ps = PLAYBACK_STREAM(stream);
2754
2755 if (seek != PA_SEEK_RELATIVE || offset != 0)
2756 pa_asyncmsgq_post(ps->sink_input->sink->asyncmsgq, PA_MSGOBJECT(ps->sink_input), SINK_INPUT_MESSAGE_SEEK, PA_UINT_TO_PTR(seek), offset, NULL, NULL);
2757
2758 pa_asyncmsgq_post(ps->sink_input->sink->asyncmsgq, PA_MSGOBJECT(ps->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
2759
2760 } else {
2761 upload_stream *u = UPLOAD_STREAM(stream);
2762 size_t l;
2763
2764 if (!u->memchunk.memblock) {
2765 if (u->length == chunk->length) {
2766 u->memchunk = *chunk;
2767 pa_memblock_ref(u->memchunk.memblock);
2768 u->length = 0;
2769 } else {
2770 u->memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, u->length);
2771 u->memchunk.index = u->memchunk.length = 0;
2772 }
2773 }
2774
2775 pa_assert(u->memchunk.memblock);
2776
2777 l = u->length;
2778 if (l > chunk->length)
2779 l = chunk->length;
2780
2781
2782 if (l > 0) {
2783 void *src, *dst;
2784 dst = pa_memblock_acquire(u->memchunk.memblock);
2785 src = pa_memblock_acquire(chunk->memblock);
2786
2787 memcpy((uint8_t*) dst + u->memchunk.index + u->memchunk.length,
2788 (uint8_t*) src+chunk->index, l);
2789
2790 pa_memblock_release(u->memchunk.memblock);
2791 pa_memblock_release(chunk->memblock);
2792
2793 u->memchunk.length += l;
2794 u->length -= l;
2795 }
2796 }
2797 }
2798
2799 static void pstream_die_callback(pa_pstream *p, void *userdata) {
2800 connection *c = CONNECTION(userdata);
2801
2802 pa_assert(p);
2803 connection_assert_ref(c);
2804
2805 connection_unlink(c);
2806 pa_log_info("connection died.");
2807 }
2808
2809 static void pstream_drain_callback(pa_pstream *p, void *userdata) {
2810 connection *c = CONNECTION(userdata);
2811
2812 pa_assert(p);
2813 connection_assert_ref(c);
2814
2815 send_memblock(c);
2816 }
2817
2818 static void pstream_revoke_callback(pa_pstream *p, uint32_t block_id, void *userdata) {
2819 pa_thread_mq *q;
2820
2821 if (!(q = pa_thread_mq_get()))
2822 pa_pstream_send_revoke(p, block_id);
2823 else
2824 pa_asyncmsgq_post(q->outq, PA_MSGOBJECT(userdata), CONNECTION_MESSAGE_REVOKE, PA_UINT_TO_PTR(block_id), 0, NULL, NULL);
2825 }
2826
2827 static void pstream_release_callback(pa_pstream *p, uint32_t block_id, void *userdata) {
2828 pa_thread_mq *q;
2829
2830 if (!(q = pa_thread_mq_get()))
2831 pa_pstream_send_release(p, block_id);
2832 else
2833 pa_asyncmsgq_post(q->outq, PA_MSGOBJECT(userdata), CONNECTION_MESSAGE_RELEASE, PA_UINT_TO_PTR(block_id), 0, NULL, NULL);
2834 }
2835
2836 /*** client callbacks ***/
2837
2838 static void client_kill_cb(pa_client *c) {
2839 pa_assert(c);
2840
2841 connection_unlink(CONNECTION(c->userdata));
2842 }
2843
2844 /*** socket server callbacks ***/
2845
2846 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
2847 connection *c = CONNECTION(userdata);
2848
2849 pa_assert(m);
2850 pa_assert(tv);
2851 connection_assert_ref(c);
2852 pa_assert(c->auth_timeout_event == e);
2853
2854 if (!c->authorized)
2855 connection_unlink(c);
2856 }
2857
2858 static void on_connection(PA_GCC_UNUSED pa_socket_server*s, pa_iochannel *io, void *userdata) {
2859 pa_protocol_native *p = userdata;
2860 connection *c;
2861 char cname[256], pname[128];
2862
2863 pa_assert(s);
2864 pa_assert(io);
2865 pa_assert(p);
2866
2867 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
2868 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
2869 pa_iochannel_free(io);
2870 return;
2871 }
2872
2873 c = pa_msgobject_new(connection);
2874 c->parent.parent.free = connection_free;
2875 c->parent.process_msg = connection_process_msg;
2876
2877 c->authorized = !!p->public;
2878
2879 if (!c->authorized && p->auth_ip_acl && pa_ip_acl_check(p->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
2880 pa_log_info("Client authenticated by IP ACL.");
2881 c->authorized = 1;
2882 }
2883
2884 if (!c->authorized) {
2885 struct timeval tv;
2886 pa_gettimeofday(&tv);
2887 tv.tv_sec += AUTH_TIMEOUT;
2888 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
2889 } else
2890 c->auth_timeout_event = NULL;
2891
2892 c->version = 8;
2893 c->protocol = p;
2894 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
2895 pa_snprintf(cname, sizeof(cname), "Native client (%s)", pname);
2896 c->client = pa_client_new(p->core, __FILE__, cname);
2897 c->client->kill = client_kill_cb;
2898 c->client->userdata = c;
2899 c->client->owner = p->module;
2900
2901 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->mempool);
2902
2903 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
2904 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
2905 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
2906 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
2907 pa_pstream_set_revoke_callback(c->pstream, pstream_revoke_callback, c);
2908 pa_pstream_set_release_callback(c->pstream, pstream_release_callback, c);
2909
2910 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
2911
2912 c->record_streams = pa_idxset_new(NULL, NULL);
2913 c->output_streams = pa_idxset_new(NULL, NULL);
2914
2915 c->rrobin_index = PA_IDXSET_INVALID;
2916 c->subscription = NULL;
2917
2918 pa_idxset_put(p->connections, c, NULL);
2919
2920 #ifdef HAVE_CREDS
2921 if (pa_iochannel_creds_supported(io))
2922 pa_iochannel_creds_enable(io);
2923
2924 #endif
2925 }
2926
2927 /*** module entry points ***/
2928
2929 static int load_key(pa_protocol_native*p, const char*fn) {
2930 pa_assert(p);
2931
2932 p->auth_cookie_in_property = 0;
2933
2934 if (!fn && pa_authkey_prop_get(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0) {
2935 pa_log_info("using already loaded auth cookie.");
2936 pa_authkey_prop_ref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
2937 p->auth_cookie_in_property = 1;
2938 return 0;
2939 }
2940
2941 if (!fn)
2942 fn = PA_NATIVE_COOKIE_FILE;
2943
2944 if (pa_authkey_load_auto(fn, p->auth_cookie, sizeof(p->auth_cookie)) < 0)
2945 return -1;
2946
2947 pa_log_info("loading cookie from disk.");
2948
2949 if (pa_authkey_prop_put(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0)
2950 p->auth_cookie_in_property = 1;
2951
2952 return 0;
2953 }
2954
2955 static pa_protocol_native* protocol_new_internal(pa_core *c, pa_module *m, pa_modargs *ma) {
2956 pa_protocol_native *p;
2957 int public = 0;
2958 const char *acl;
2959
2960 pa_assert(c);
2961 pa_assert(ma);
2962
2963 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
2964 pa_log("auth-anonymous= expects a boolean argument.");
2965 return NULL;
2966 }
2967
2968 p = pa_xnew(pa_protocol_native, 1);
2969 p->core = c;
2970 p->module = m;
2971 p->public = public;
2972 p->server = NULL;
2973 p->auth_ip_acl = NULL;
2974
2975 #ifdef HAVE_CREDS
2976 {
2977 int a = 1;
2978 if (pa_modargs_get_value_boolean(ma, "auth-group-enabled", &a) < 0) {
2979 pa_log("auth-group-enabled= expects a boolean argument.");
2980 return NULL;
2981 }
2982 p->auth_group = a ? pa_xstrdup(pa_modargs_get_value(ma, "auth-group", c->is_system_instance ? PA_ACCESS_GROUP : NULL)) : NULL;
2983
2984 if (p->auth_group)
2985 pa_log_info("Allowing access to group '%s'.", p->auth_group);
2986 }
2987 #endif
2988
2989
2990 if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
2991
2992 if (!(p->auth_ip_acl = pa_ip_acl_new(acl))) {
2993 pa_log("Failed to parse IP ACL '%s'", acl);
2994 goto fail;
2995 }
2996 }
2997
2998 if (load_key(p, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
2999 goto fail;
3000
3001 p->connections = pa_idxset_new(NULL, NULL);
3002
3003 return p;
3004
3005 fail:
3006 #ifdef HAVE_CREDS
3007 pa_xfree(p->auth_group);
3008 #endif
3009 if (p->auth_ip_acl)
3010 pa_ip_acl_free(p->auth_ip_acl);
3011 pa_xfree(p);
3012 return NULL;
3013 }
3014
3015 pa_protocol_native* pa_protocol_native_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
3016 char t[256];
3017 pa_protocol_native *p;
3018
3019 if (!(p = protocol_new_internal(core, m, ma)))
3020 return NULL;
3021
3022 p->server = server;
3023 pa_socket_server_set_callback(p->server, on_connection, p);
3024
3025 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
3026 pa_strlist *l;
3027 l = pa_property_get(core, PA_NATIVE_SERVER_PROPERTY_NAME);
3028 l = pa_strlist_prepend(l, t);
3029 pa_property_replace(core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
3030 }
3031
3032 return p;
3033 }
3034
3035 void pa_protocol_native_free(pa_protocol_native *p) {
3036 connection *c;
3037 pa_assert(p);
3038
3039 while ((c = pa_idxset_first(p->connections, NULL)))
3040 connection_unlink(c);
3041 pa_idxset_free(p->connections, NULL, NULL);
3042
3043 if (p->server) {
3044 char t[256];
3045
3046 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
3047 pa_strlist *l;
3048 l = pa_property_get(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
3049 l = pa_strlist_remove(l, t);
3050
3051 if (l)
3052 pa_property_replace(p->core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
3053 else
3054 pa_property_remove(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
3055 }
3056
3057 pa_socket_server_unref(p->server);
3058 }
3059
3060 if (p->auth_cookie_in_property)
3061 pa_authkey_prop_unref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
3062
3063 if (p->auth_ip_acl)
3064 pa_ip_acl_free(p->auth_ip_acl);
3065
3066 #ifdef HAVE_CREDS
3067 pa_xfree(p->auth_group);
3068 #endif
3069 pa_xfree(p);
3070 }
3071
3072 pa_protocol_native* pa_protocol_native_new_iochannel(
3073 pa_core*core,
3074 pa_iochannel *io,
3075 pa_module *m,
3076 pa_modargs *ma) {
3077
3078 pa_protocol_native *p;
3079
3080 if (!(p = protocol_new_internal(core, m, ma)))
3081 return NULL;
3082
3083 on_connection(NULL, io, p);
3084
3085 return p;
3086 }