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