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