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