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