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