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