]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-native.c
Merge dead branch 'ossman'
[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 pa_usec_t sink_latency;
2539
2540 pa_assert(t);
2541 pa_sink_input_assert_ref(s);
2542
2543 fixup_sample_spec(c, &fixed_ss, &s->sample_spec);
2544
2545 pa_tagstruct_putu32(t, s->index);
2546 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME)));
2547 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
2548 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
2549 pa_tagstruct_putu32(t, s->sink->index);
2550 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2551 pa_tagstruct_put_channel_map(t, &s->channel_map);
2552 pa_tagstruct_put_cvolume(t, &s->volume);
2553 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s, &sink_latency));
2554 pa_tagstruct_put_usec(t, sink_latency);
2555 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
2556 pa_tagstruct_puts(t, s->driver);
2557 if (c->version >= 11)
2558 pa_tagstruct_put_boolean(t, pa_sink_input_get_mute(s));
2559 if (c->version >= 13)
2560 pa_tagstruct_put_proplist(t, s->proplist);
2561 }
2562
2563 static void source_output_fill_tagstruct(connection *c, pa_tagstruct *t, pa_source_output *s) {
2564 pa_sample_spec fixed_ss;
2565 pa_usec_t source_latency;
2566
2567 pa_assert(t);
2568 pa_source_output_assert_ref(s);
2569
2570 fixup_sample_spec(c, &fixed_ss, &s->sample_spec);
2571
2572 pa_tagstruct_putu32(t, s->index);
2573 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME)));
2574 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
2575 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
2576 pa_tagstruct_putu32(t, s->source->index);
2577 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2578 pa_tagstruct_put_channel_map(t, &s->channel_map);
2579 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s, &source_latency));
2580 pa_tagstruct_put_usec(t, source_latency);
2581 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
2582 pa_tagstruct_puts(t, s->driver);
2583
2584 if (c->version >= 13)
2585 pa_tagstruct_put_proplist(t, s->proplist);
2586 }
2587
2588 static void scache_fill_tagstruct(connection *c, pa_tagstruct *t, pa_scache_entry *e) {
2589 pa_sample_spec fixed_ss;
2590
2591 pa_assert(t);
2592 pa_assert(e);
2593
2594 if (e->memchunk.memblock)
2595 fixup_sample_spec(c, &fixed_ss, &e->sample_spec);
2596 else
2597 memset(&fixed_ss, 0, sizeof(fixed_ss));
2598
2599 pa_tagstruct_putu32(t, e->index);
2600 pa_tagstruct_puts(t, e->name);
2601 pa_tagstruct_put_cvolume(t, &e->volume);
2602 pa_tagstruct_put_usec(t, e->memchunk.memblock ? pa_bytes_to_usec(e->memchunk.length, &e->sample_spec) : 0);
2603 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2604 pa_tagstruct_put_channel_map(t, &e->channel_map);
2605 pa_tagstruct_putu32(t, e->memchunk.length);
2606 pa_tagstruct_put_boolean(t, e->lazy);
2607 pa_tagstruct_puts(t, e->filename);
2608
2609 if (c->version >= 13)
2610 pa_tagstruct_put_proplist(t, e->proplist);
2611 }
2612
2613 static void command_get_info(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2614 connection *c = CONNECTION(userdata);
2615 uint32_t idx;
2616 pa_sink *sink = NULL;
2617 pa_source *source = NULL;
2618 pa_client *client = NULL;
2619 pa_module *module = NULL;
2620 pa_sink_input *si = NULL;
2621 pa_source_output *so = NULL;
2622 pa_scache_entry *sce = NULL;
2623 const char *name;
2624 pa_tagstruct *reply;
2625
2626 connection_assert_ref(c);
2627 pa_assert(t);
2628
2629 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2630 (command != PA_COMMAND_GET_CLIENT_INFO &&
2631 command != PA_COMMAND_GET_MODULE_INFO &&
2632 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
2633 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
2634 pa_tagstruct_gets(t, &name) < 0) ||
2635 !pa_tagstruct_eof(t)) {
2636 protocol_error(c);
2637 return;
2638 }
2639
2640 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2641 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2642
2643 if (command == PA_COMMAND_GET_SINK_INFO) {
2644 if (idx != PA_INVALID_INDEX)
2645 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2646 else
2647 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2648 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
2649 if (idx != PA_INVALID_INDEX)
2650 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2651 else
2652 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2653 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
2654 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
2655 else if (command == PA_COMMAND_GET_MODULE_INFO)
2656 module = pa_idxset_get_by_index(c->protocol->core->modules, idx);
2657 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
2658 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2659 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
2660 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
2661 else {
2662 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO);
2663 if (idx != PA_INVALID_INDEX)
2664 sce = pa_idxset_get_by_index(c->protocol->core->scache, idx);
2665 else
2666 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
2667 }
2668
2669 if (!sink && !source && !client && !module && !si && !so && !sce) {
2670 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2671 return;
2672 }
2673
2674 reply = reply_new(tag);
2675 if (sink)
2676 sink_fill_tagstruct(c, reply, sink);
2677 else if (source)
2678 source_fill_tagstruct(c, reply, source);
2679 else if (client)
2680 client_fill_tagstruct(c, reply, client);
2681 else if (module)
2682 module_fill_tagstruct(reply, module);
2683 else if (si)
2684 sink_input_fill_tagstruct(c, reply, si);
2685 else if (so)
2686 source_output_fill_tagstruct(c, reply, so);
2687 else
2688 scache_fill_tagstruct(c, reply, sce);
2689 pa_pstream_send_tagstruct(c->pstream, reply);
2690 }
2691
2692 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2693 connection *c = CONNECTION(userdata);
2694 pa_idxset *i;
2695 uint32_t idx;
2696 void *p;
2697 pa_tagstruct *reply;
2698
2699 connection_assert_ref(c);
2700 pa_assert(t);
2701
2702 if (!pa_tagstruct_eof(t)) {
2703 protocol_error(c);
2704 return;
2705 }
2706
2707 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2708
2709 reply = reply_new(tag);
2710
2711 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
2712 i = c->protocol->core->sinks;
2713 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
2714 i = c->protocol->core->sources;
2715 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
2716 i = c->protocol->core->clients;
2717 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
2718 i = c->protocol->core->modules;
2719 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
2720 i = c->protocol->core->sink_inputs;
2721 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
2722 i = c->protocol->core->source_outputs;
2723 else {
2724 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
2725 i = c->protocol->core->scache;
2726 }
2727
2728 if (i) {
2729 for (p = pa_idxset_first(i, &idx); p; p = pa_idxset_next(i, &idx)) {
2730 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
2731 sink_fill_tagstruct(c, reply, p);
2732 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
2733 source_fill_tagstruct(c, reply, p);
2734 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
2735 client_fill_tagstruct(c, reply, p);
2736 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
2737 module_fill_tagstruct(reply, p);
2738 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
2739 sink_input_fill_tagstruct(c, reply, p);
2740 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
2741 source_output_fill_tagstruct(c, reply, p);
2742 else {
2743 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
2744 scache_fill_tagstruct(c, reply, p);
2745 }
2746 }
2747 }
2748
2749 pa_pstream_send_tagstruct(c->pstream, reply);
2750 }
2751
2752 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) {
2753 connection *c = CONNECTION(userdata);
2754 pa_tagstruct *reply;
2755 char txt[256];
2756 const char *n;
2757 pa_sample_spec fixed_ss;
2758
2759 connection_assert_ref(c);
2760 pa_assert(t);
2761
2762 if (!pa_tagstruct_eof(t)) {
2763 protocol_error(c);
2764 return;
2765 }
2766
2767 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2768
2769 reply = reply_new(tag);
2770 pa_tagstruct_puts(reply, PACKAGE_NAME);
2771 pa_tagstruct_puts(reply, PACKAGE_VERSION);
2772 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
2773 pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
2774
2775 fixup_sample_spec(c, &fixed_ss, &c->protocol->core->default_sample_spec);
2776 pa_tagstruct_put_sample_spec(reply, &fixed_ss);
2777
2778 n = pa_namereg_get_default_sink_name(c->protocol->core);
2779 pa_tagstruct_puts(reply, n);
2780 n = pa_namereg_get_default_source_name(c->protocol->core);
2781 pa_tagstruct_puts(reply, n);
2782
2783 pa_tagstruct_putu32(reply, c->protocol->core->cookie);
2784
2785 pa_pstream_send_tagstruct(c->pstream, reply);
2786 }
2787
2788 static void subscription_cb(pa_core *core, pa_subscription_event_type_t e, uint32_t idx, void *userdata) {
2789 pa_tagstruct *t;
2790 connection *c = CONNECTION(userdata);
2791
2792 connection_assert_ref(c);
2793
2794 t = pa_tagstruct_new(NULL, 0);
2795 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
2796 pa_tagstruct_putu32(t, (uint32_t) -1);
2797 pa_tagstruct_putu32(t, e);
2798 pa_tagstruct_putu32(t, idx);
2799 pa_pstream_send_tagstruct(c->pstream, t);
2800 }
2801
2802 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2803 connection *c = CONNECTION(userdata);
2804 pa_subscription_mask_t m;
2805
2806 connection_assert_ref(c);
2807 pa_assert(t);
2808
2809 if (pa_tagstruct_getu32(t, &m) < 0 ||
2810 !pa_tagstruct_eof(t)) {
2811 protocol_error(c);
2812 return;
2813 }
2814
2815 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2816 CHECK_VALIDITY(c->pstream, (m & ~PA_SUBSCRIPTION_MASK_ALL) == 0, tag, PA_ERR_INVALID);
2817
2818 if (c->subscription)
2819 pa_subscription_free(c->subscription);
2820
2821 if (m != 0) {
2822 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
2823 pa_assert(c->subscription);
2824 } else
2825 c->subscription = NULL;
2826
2827 pa_pstream_send_simple_ack(c->pstream, tag);
2828 }
2829
2830 static void command_set_volume(
2831 PA_GCC_UNUSED pa_pdispatch *pd,
2832 uint32_t command,
2833 uint32_t tag,
2834 pa_tagstruct *t,
2835 void *userdata) {
2836
2837 connection *c = CONNECTION(userdata);
2838 uint32_t idx;
2839 pa_cvolume volume;
2840 pa_sink *sink = NULL;
2841 pa_source *source = NULL;
2842 pa_sink_input *si = NULL;
2843 const char *name = NULL;
2844
2845 connection_assert_ref(c);
2846 pa_assert(t);
2847
2848 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2849 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
2850 (command == PA_COMMAND_SET_SOURCE_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
2851 pa_tagstruct_get_cvolume(t, &volume) ||
2852 !pa_tagstruct_eof(t)) {
2853 protocol_error(c);
2854 return;
2855 }
2856
2857 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2858 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2859 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
2860
2861 switch (command) {
2862
2863 case PA_COMMAND_SET_SINK_VOLUME:
2864 if (idx != PA_INVALID_INDEX)
2865 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2866 else
2867 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2868 break;
2869
2870 case PA_COMMAND_SET_SOURCE_VOLUME:
2871 if (idx != PA_INVALID_INDEX)
2872 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2873 else
2874 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2875 break;
2876
2877 case PA_COMMAND_SET_SINK_INPUT_VOLUME:
2878 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2879 break;
2880
2881 default:
2882 pa_assert_not_reached();
2883 }
2884
2885 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
2886
2887 if (sink)
2888 pa_sink_set_volume(sink, &volume);
2889 else if (source)
2890 pa_source_set_volume(source, &volume);
2891 else if (si)
2892 pa_sink_input_set_volume(si, &volume);
2893
2894 pa_pstream_send_simple_ack(c->pstream, tag);
2895 }
2896
2897 static void command_set_mute(
2898 PA_GCC_UNUSED pa_pdispatch *pd,
2899 uint32_t command,
2900 uint32_t tag,
2901 pa_tagstruct *t,
2902 void *userdata) {
2903
2904 connection *c = CONNECTION(userdata);
2905 uint32_t idx;
2906 pa_bool_t mute;
2907 pa_sink *sink = NULL;
2908 pa_source *source = NULL;
2909 pa_sink_input *si = NULL;
2910 const char *name = NULL;
2911
2912 connection_assert_ref(c);
2913 pa_assert(t);
2914
2915 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2916 (command == PA_COMMAND_SET_SINK_MUTE && pa_tagstruct_gets(t, &name) < 0) ||
2917 (command == PA_COMMAND_SET_SOURCE_MUTE && pa_tagstruct_gets(t, &name) < 0) ||
2918 pa_tagstruct_get_boolean(t, &mute) ||
2919 !pa_tagstruct_eof(t)) {
2920 protocol_error(c);
2921 return;
2922 }
2923
2924 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2925 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2926
2927 switch (command) {
2928
2929 case PA_COMMAND_SET_SINK_MUTE:
2930
2931 if (idx != PA_INVALID_INDEX)
2932 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2933 else
2934 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2935
2936 break;
2937
2938 case PA_COMMAND_SET_SOURCE_MUTE:
2939 if (idx != PA_INVALID_INDEX)
2940 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2941 else
2942 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2943
2944 break;
2945
2946 case PA_COMMAND_SET_SINK_INPUT_MUTE:
2947 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2948 break;
2949
2950 default:
2951 pa_assert_not_reached();
2952 }
2953
2954 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
2955
2956 if (sink)
2957 pa_sink_set_mute(sink, mute);
2958 else if (source)
2959 pa_source_set_mute(source, mute);
2960 else if (si)
2961 pa_sink_input_set_mute(si, mute);
2962
2963 pa_pstream_send_simple_ack(c->pstream, tag);
2964 }
2965
2966 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) {
2967 connection *c = CONNECTION(userdata);
2968 uint32_t idx;
2969 pa_bool_t b;
2970 playback_stream *s;
2971
2972 connection_assert_ref(c);
2973 pa_assert(t);
2974
2975 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2976 pa_tagstruct_get_boolean(t, &b) < 0 ||
2977 !pa_tagstruct_eof(t)) {
2978 protocol_error(c);
2979 return;
2980 }
2981
2982 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2983 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2984 s = pa_idxset_get_by_index(c->output_streams, idx);
2985 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2986 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2987
2988 pa_sink_input_cork(s->sink_input, b);
2989 pa_pstream_send_simple_ack(c->pstream, tag);
2990 }
2991
2992 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) {
2993 connection *c = CONNECTION(userdata);
2994 uint32_t idx;
2995 playback_stream *s;
2996
2997 connection_assert_ref(c);
2998 pa_assert(t);
2999
3000 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3001 !pa_tagstruct_eof(t)) {
3002 protocol_error(c);
3003 return;
3004 }
3005
3006 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3007 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3008 s = pa_idxset_get_by_index(c->output_streams, idx);
3009 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3010 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3011
3012 switch (command) {
3013 case PA_COMMAND_FLUSH_PLAYBACK_STREAM:
3014 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_FLUSH, NULL, 0, NULL);
3015 break;
3016
3017 case PA_COMMAND_PREBUF_PLAYBACK_STREAM:
3018 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_PREBUF_FORCE, NULL, 0, NULL);
3019 break;
3020
3021 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM:
3022 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_TRIGGER, NULL, 0, NULL);
3023 break;
3024
3025 default:
3026 pa_assert_not_reached();
3027 }
3028
3029 pa_pstream_send_simple_ack(c->pstream, tag);
3030 }
3031
3032 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) {
3033 connection *c = CONNECTION(userdata);
3034 uint32_t idx;
3035 record_stream *s;
3036 pa_bool_t b;
3037
3038 connection_assert_ref(c);
3039 pa_assert(t);
3040
3041 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3042 pa_tagstruct_get_boolean(t, &b) < 0 ||
3043 !pa_tagstruct_eof(t)) {
3044 protocol_error(c);
3045 return;
3046 }
3047
3048 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3049 s = pa_idxset_get_by_index(c->record_streams, idx);
3050 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3051
3052 pa_source_output_cork(s->source_output, b);
3053 pa_memblockq_prebuf_force(s->memblockq);
3054 pa_pstream_send_simple_ack(c->pstream, tag);
3055 }
3056
3057 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) {
3058 connection *c = CONNECTION(userdata);
3059 uint32_t idx;
3060 record_stream *s;
3061
3062 connection_assert_ref(c);
3063 pa_assert(t);
3064
3065 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3066 !pa_tagstruct_eof(t)) {
3067 protocol_error(c);
3068 return;
3069 }
3070
3071 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3072 s = pa_idxset_get_by_index(c->record_streams, idx);
3073 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3074
3075 pa_memblockq_flush(s->memblockq);
3076 pa_pstream_send_simple_ack(c->pstream, tag);
3077 }
3078
3079 static void command_set_stream_buffer_attr(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3080 connection *c = CONNECTION(userdata);
3081 uint32_t idx;
3082 uint32_t maxlength, tlength, prebuf, minreq, fragsize;
3083 pa_tagstruct *reply;
3084
3085 connection_assert_ref(c);
3086 pa_assert(t);
3087
3088 if (pa_tagstruct_getu32(t, &idx) < 0) {
3089 protocol_error(c);
3090 return;
3091 }
3092
3093 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3094
3095 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR) {
3096 playback_stream *s;
3097 pa_bool_t adjust_latency = FALSE;
3098
3099 s = pa_idxset_get_by_index(c->output_streams, idx);
3100 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3101 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3102
3103 if (pa_tagstruct_get(
3104 t,
3105 PA_TAG_U32, &maxlength,
3106 PA_TAG_U32, &tlength,
3107 PA_TAG_U32, &prebuf,
3108 PA_TAG_U32, &minreq,
3109 PA_TAG_INVALID) < 0 ||
3110 (c->version >= 13 && pa_tagstruct_get_boolean(t, &adjust_latency) < 0) ||
3111 !pa_tagstruct_eof(t)) {
3112 protocol_error(c);
3113 return;
3114 }
3115
3116 fix_playback_buffer_attr_pre(s, adjust_latency, &maxlength, &tlength, &prebuf, &minreq);
3117 pa_memblockq_set_maxlength(s->memblockq, maxlength);
3118 pa_memblockq_set_tlength(s->memblockq, tlength);
3119 pa_memblockq_set_prebuf(s->memblockq, prebuf);
3120 pa_memblockq_set_minreq(s->memblockq, minreq);
3121 fix_playback_buffer_attr_post(s, &maxlength, &tlength, &prebuf, &minreq);
3122
3123 reply = reply_new(tag);
3124 pa_tagstruct_putu32(reply, maxlength);
3125 pa_tagstruct_putu32(reply, tlength);
3126 pa_tagstruct_putu32(reply, prebuf);
3127 pa_tagstruct_putu32(reply, minreq);
3128
3129 if (c->version >= 13)
3130 pa_tagstruct_put_usec(reply, s->sink_latency);
3131
3132 } else {
3133 record_stream *s;
3134 pa_bool_t adjust_latency = FALSE;
3135 pa_assert(command == PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR);
3136
3137 s = pa_idxset_get_by_index(c->record_streams, idx);
3138 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3139
3140 if (pa_tagstruct_get(
3141 t,
3142 PA_TAG_U32, &maxlength,
3143 PA_TAG_U32, &fragsize,
3144 PA_TAG_INVALID) < 0 ||
3145 (c->version >= 13 && pa_tagstruct_get_boolean(t, &adjust_latency) < 0) ||
3146 !pa_tagstruct_eof(t)) {
3147 protocol_error(c);
3148 return;
3149 }
3150
3151 fix_record_buffer_attr_pre(s, adjust_latency, &maxlength, &fragsize);
3152 pa_memblockq_set_maxlength(s->memblockq, maxlength);
3153 fix_record_buffer_attr_post(s, &maxlength, &fragsize);
3154
3155 reply = reply_new(tag);
3156 pa_tagstruct_putu32(reply, maxlength);
3157 pa_tagstruct_putu32(reply, fragsize);
3158
3159 if (c->version >= 13)
3160 pa_tagstruct_put_usec(reply, s->source_latency);
3161 }
3162
3163 pa_pstream_send_tagstruct(c->pstream, reply);
3164 }
3165
3166 static void command_update_stream_sample_rate(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3167 connection *c = CONNECTION(userdata);
3168 uint32_t idx;
3169 uint32_t rate;
3170
3171 connection_assert_ref(c);
3172 pa_assert(t);
3173
3174 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3175 pa_tagstruct_getu32(t, &rate) < 0 ||
3176 !pa_tagstruct_eof(t)) {
3177 protocol_error(c);
3178 return;
3179 }
3180
3181 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3182 CHECK_VALIDITY(c->pstream, rate > 0 && rate <= PA_RATE_MAX, tag, PA_ERR_INVALID);
3183
3184 if (command == PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE) {
3185 playback_stream *s;
3186
3187 s = pa_idxset_get_by_index(c->output_streams, idx);
3188 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3189 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3190
3191 pa_sink_input_set_rate(s->sink_input, rate);
3192
3193 } else {
3194 record_stream *s;
3195 pa_assert(command == PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE);
3196
3197 s = pa_idxset_get_by_index(c->record_streams, idx);
3198 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3199
3200 pa_source_output_set_rate(s->source_output, rate);
3201 }
3202
3203 pa_pstream_send_simple_ack(c->pstream, tag);
3204 }
3205
3206 static void command_update_proplist(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3207 connection *c = CONNECTION(userdata);
3208 uint32_t idx;
3209 uint32_t mode;
3210 pa_proplist *p;
3211
3212 connection_assert_ref(c);
3213 pa_assert(t);
3214
3215 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3216
3217 p = pa_proplist_new();
3218
3219 if (command == PA_COMMAND_UPDATE_CLIENT_PROPLIST) {
3220
3221 if (pa_tagstruct_getu32(t, &mode) < 0 ||
3222 pa_tagstruct_get_proplist(t, p) < 0 ||
3223 !pa_tagstruct_eof(t)) {
3224 protocol_error(c);
3225 pa_proplist_free(p);
3226 return;
3227 }
3228
3229 } else {
3230
3231 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3232 pa_tagstruct_getu32(t, &mode) < 0 ||
3233 pa_tagstruct_get_proplist(t, p) < 0 ||
3234 !pa_tagstruct_eof(t)) {
3235 protocol_error(c);
3236 pa_proplist_free(p);
3237 return;
3238 }
3239 }
3240
3241 CHECK_VALIDITY(c->pstream, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, tag, PA_ERR_INVALID);
3242
3243 if (command == PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST) {
3244 playback_stream *s;
3245
3246 s = pa_idxset_get_by_index(c->output_streams, idx);
3247 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3248 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3249
3250 pa_proplist_update(s->sink_input->proplist, mode, p);
3251 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->sink_input->index);
3252
3253 } else if (command == PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST) {
3254 record_stream *s;
3255
3256 s = pa_idxset_get_by_index(c->record_streams, idx);
3257 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3258
3259 pa_proplist_update(s->source_output->proplist, mode, p);
3260 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->source_output->index);
3261 } else {
3262 pa_assert(command == PA_COMMAND_UPDATE_CLIENT_PROPLIST);
3263
3264 pa_proplist_update(c->client->proplist, mode, p);
3265 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
3266 }
3267
3268 pa_pstream_send_simple_ack(c->pstream, tag);
3269 }
3270
3271 static void command_remove_proplist(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3272 connection *c = CONNECTION(userdata);
3273 uint32_t idx;
3274 unsigned changed = 0;
3275 pa_proplist *p;
3276 pa_strlist *l = NULL;
3277
3278 connection_assert_ref(c);
3279 pa_assert(t);
3280
3281 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3282
3283 if (command != PA_COMMAND_REMOVE_CLIENT_PROPLIST) {
3284
3285 if (pa_tagstruct_getu32(t, &idx) < 0) {
3286 protocol_error(c);
3287 return;
3288 }
3289 }
3290
3291 if (command == PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST) {
3292 playback_stream *s;
3293
3294 s = pa_idxset_get_by_index(c->output_streams, idx);
3295 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3296 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3297
3298 p = s->sink_input->proplist;
3299
3300 } else if (command == PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST) {
3301 record_stream *s;
3302
3303 s = pa_idxset_get_by_index(c->record_streams, idx);
3304 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3305
3306 p = s->source_output->proplist;
3307 } else {
3308 pa_assert(command == PA_COMMAND_REMOVE_CLIENT_PROPLIST);
3309
3310 p = c->client->proplist;
3311 }
3312
3313 for (;;) {
3314 const char *k;
3315
3316 if (pa_tagstruct_gets(t, &k) < 0) {
3317 protocol_error(c);
3318 pa_strlist_free(l);
3319 return;
3320 }
3321
3322 if (!k)
3323 break;
3324
3325 l = pa_strlist_prepend(l, k);
3326 }
3327
3328 if (!pa_tagstruct_eof(t)) {
3329 protocol_error(c);
3330 pa_strlist_free(l);
3331 return;
3332 }
3333
3334 for (;;) {
3335 char *z;
3336
3337 l = pa_strlist_pop(l, &z);
3338
3339 if (!z)
3340 break;
3341
3342 changed += pa_proplist_unset(p, z) >= 0;
3343 pa_xfree(z);
3344 }
3345
3346 pa_pstream_send_simple_ack(c->pstream, tag);
3347
3348 if (changed) {
3349 if (command == PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST) {
3350 playback_stream *s;
3351
3352 s = pa_idxset_get_by_index(c->output_streams, idx);
3353 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->sink_input->index);
3354
3355 } else if (command == PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST) {
3356 record_stream *s;
3357
3358 s = pa_idxset_get_by_index(c->record_streams, idx);
3359 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->source_output->index);
3360
3361 } else {
3362 pa_assert(command == PA_COMMAND_REMOVE_CLIENT_PROPLIST);
3363 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
3364 }
3365 }
3366 }
3367
3368 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) {
3369 connection *c = CONNECTION(userdata);
3370 const char *s;
3371
3372 connection_assert_ref(c);
3373 pa_assert(t);
3374
3375 if (pa_tagstruct_gets(t, &s) < 0 ||
3376 !pa_tagstruct_eof(t)) {
3377 protocol_error(c);
3378 return;
3379 }
3380
3381 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3382 CHECK_VALIDITY(c->pstream, !s || (*s && pa_utf8_valid(s)), tag, PA_ERR_INVALID);
3383
3384 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
3385 pa_pstream_send_simple_ack(c->pstream, tag);
3386 }
3387
3388 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3389 connection *c = CONNECTION(userdata);
3390 uint32_t idx;
3391 const char *name;
3392
3393 connection_assert_ref(c);
3394 pa_assert(t);
3395
3396 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3397 pa_tagstruct_gets(t, &name) < 0 ||
3398 !pa_tagstruct_eof(t)) {
3399 protocol_error(c);
3400 return;
3401 }
3402
3403 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3404 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
3405
3406 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
3407 playback_stream *s;
3408
3409 s = pa_idxset_get_by_index(c->output_streams, idx);
3410 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3411 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3412
3413 pa_sink_input_set_name(s->sink_input, name);
3414
3415 } else {
3416 record_stream *s;
3417 pa_assert(command == PA_COMMAND_SET_RECORD_STREAM_NAME);
3418
3419 s = pa_idxset_get_by_index(c->record_streams, idx);
3420 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3421
3422 pa_source_output_set_name(s->source_output, name);
3423 }
3424
3425 pa_pstream_send_simple_ack(c->pstream, tag);
3426 }
3427
3428 static void command_kill(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3429 connection *c = CONNECTION(userdata);
3430 uint32_t idx;
3431
3432 connection_assert_ref(c);
3433 pa_assert(t);
3434
3435 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3436 !pa_tagstruct_eof(t)) {
3437 protocol_error(c);
3438 return;
3439 }
3440
3441 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3442
3443 if (command == PA_COMMAND_KILL_CLIENT) {
3444 pa_client *client;
3445
3446 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
3447 CHECK_VALIDITY(c->pstream, client, tag, PA_ERR_NOENTITY);
3448
3449 connection_ref(c);
3450 pa_client_kill(client);
3451
3452 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
3453 pa_sink_input *s;
3454
3455 s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3456 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3457
3458 connection_ref(c);
3459 pa_sink_input_kill(s);
3460 } else {
3461 pa_source_output *s;
3462
3463 pa_assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
3464
3465 s = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
3466 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3467
3468 connection_ref(c);
3469 pa_source_output_kill(s);
3470 }
3471
3472 pa_pstream_send_simple_ack(c->pstream, tag);
3473 connection_unref(c);
3474 }
3475
3476 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) {
3477 connection *c = CONNECTION(userdata);
3478 pa_module *m;
3479 const char *name, *argument;
3480 pa_tagstruct *reply;
3481
3482 connection_assert_ref(c);
3483 pa_assert(t);
3484
3485 if (pa_tagstruct_gets(t, &name) < 0 ||
3486 pa_tagstruct_gets(t, &argument) < 0 ||
3487 !pa_tagstruct_eof(t)) {
3488 protocol_error(c);
3489 return;
3490 }
3491
3492 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3493 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name) && !strchr(name, '/'), tag, PA_ERR_INVALID);
3494 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
3495
3496 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
3497 pa_pstream_send_error(c->pstream, tag, PA_ERR_MODINITFAILED);
3498 return;
3499 }
3500
3501 reply = reply_new(tag);
3502 pa_tagstruct_putu32(reply, m->index);
3503 pa_pstream_send_tagstruct(c->pstream, reply);
3504 }
3505
3506 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) {
3507 connection *c = CONNECTION(userdata);
3508 uint32_t idx;
3509 pa_module *m;
3510
3511 connection_assert_ref(c);
3512 pa_assert(t);
3513
3514 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3515 !pa_tagstruct_eof(t)) {
3516 protocol_error(c);
3517 return;
3518 }
3519
3520 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3521 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
3522 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOENTITY);
3523
3524 pa_module_unload_request(m);
3525 pa_pstream_send_simple_ack(c->pstream, tag);
3526 }
3527
3528 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) {
3529 connection *c = CONNECTION(userdata);
3530 const char *name, *module, *argument;
3531 uint32_t type;
3532 uint32_t idx;
3533 pa_tagstruct *reply;
3534
3535 connection_assert_ref(c);
3536 pa_assert(t);
3537
3538 if (pa_tagstruct_gets(t, &name) < 0 ||
3539 pa_tagstruct_getu32(t, &type) < 0 ||
3540 pa_tagstruct_gets(t, &module) < 0 ||
3541 pa_tagstruct_gets(t, &argument) < 0 ||
3542 !pa_tagstruct_eof(t)) {
3543 protocol_error(c);
3544 return;
3545 }
3546
3547 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3548 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
3549 CHECK_VALIDITY(c->pstream, type == 0 || type == 1, tag, PA_ERR_INVALID);
3550 CHECK_VALIDITY(c->pstream, module && *module && pa_utf8_valid(module), tag, PA_ERR_INVALID);
3551 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
3552
3553 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument, &idx) < 0) {
3554 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
3555 return;
3556 }
3557
3558 reply = reply_new(tag);
3559 pa_tagstruct_putu32(reply, idx);
3560 pa_pstream_send_tagstruct(c->pstream, reply);
3561 }
3562
3563 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) {
3564 connection *c = CONNECTION(userdata);
3565 const char *name = NULL;
3566 uint32_t type, idx = PA_IDXSET_INVALID;
3567 int r;
3568
3569 connection_assert_ref(c);
3570 pa_assert(t);
3571
3572 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
3573 (pa_tagstruct_gets(t, &name) < 0 ||
3574 pa_tagstruct_getu32(t, &type) < 0)) ||
3575 !pa_tagstruct_eof(t)) {
3576 protocol_error(c);
3577 return;
3578 }
3579
3580 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3581 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
3582 CHECK_VALIDITY(c->pstream, !name || (*name && pa_utf8_valid(name) && (type == 0 || type == 1)), tag, PA_ERR_INVALID);
3583
3584 if (name)
3585 r = pa_autoload_remove_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
3586 else
3587 r = pa_autoload_remove_by_index(c->protocol->core, idx);
3588
3589 CHECK_VALIDITY(c->pstream, r >= 0, tag, PA_ERR_NOENTITY);
3590
3591 pa_pstream_send_simple_ack(c->pstream, tag);
3592 }
3593
3594 static void autoload_fill_tagstruct(pa_tagstruct *t, const pa_autoload_entry *e) {
3595 pa_assert(t && e);
3596
3597 pa_tagstruct_putu32(t, e->index);
3598 pa_tagstruct_puts(t, e->name);
3599 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
3600 pa_tagstruct_puts(t, e->module);
3601 pa_tagstruct_puts(t, e->argument);
3602 }
3603
3604 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) {
3605 connection *c = CONNECTION(userdata);
3606 const pa_autoload_entry *a = NULL;
3607 uint32_t type, idx;
3608 const char *name;
3609 pa_tagstruct *reply;
3610
3611 connection_assert_ref(c);
3612 pa_assert(t);
3613
3614 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
3615 (pa_tagstruct_gets(t, &name) < 0 ||
3616 pa_tagstruct_getu32(t, &type) < 0)) ||
3617 !pa_tagstruct_eof(t)) {
3618 protocol_error(c);
3619 return;
3620 }
3621
3622 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3623 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
3624 CHECK_VALIDITY(c->pstream, !name || (*name && (type == 0 || type == 1) && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
3625
3626 if (name)
3627 a = pa_autoload_get_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
3628 else
3629 a = pa_autoload_get_by_index(c->protocol->core, idx);
3630
3631 CHECK_VALIDITY(c->pstream, a, tag, PA_ERR_NOENTITY);
3632
3633 reply = reply_new(tag);
3634 autoload_fill_tagstruct(reply, a);
3635 pa_pstream_send_tagstruct(c->pstream, reply);
3636 }
3637
3638 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) {
3639 connection *c = CONNECTION(userdata);
3640 pa_tagstruct *reply;
3641
3642 connection_assert_ref(c);
3643 pa_assert(t);
3644
3645 if (!pa_tagstruct_eof(t)) {
3646 protocol_error(c);
3647 return;
3648 }
3649
3650 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3651
3652 reply = reply_new(tag);
3653
3654 if (c->protocol->core->autoload_hashmap) {
3655 pa_autoload_entry *a;
3656 void *state = NULL;
3657
3658 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state, NULL)))
3659 autoload_fill_tagstruct(reply, a);
3660 }
3661
3662 pa_pstream_send_tagstruct(c->pstream, reply);
3663 }
3664
3665 static void command_move_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3666 connection *c = CONNECTION(userdata);
3667 uint32_t idx = PA_INVALID_INDEX, idx_device = PA_INVALID_INDEX;
3668 const char *name = NULL;
3669
3670 connection_assert_ref(c);
3671 pa_assert(t);
3672
3673 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3674 pa_tagstruct_getu32(t, &idx_device) < 0 ||
3675 pa_tagstruct_gets(t, &name) < 0 ||
3676 !pa_tagstruct_eof(t)) {
3677 protocol_error(c);
3678 return;
3679 }
3680
3681 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3682 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3683 CHECK_VALIDITY(c->pstream, idx_device != PA_INVALID_INDEX || !name || (*name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
3684
3685 if (command == PA_COMMAND_MOVE_SINK_INPUT) {
3686 pa_sink_input *si = NULL;
3687 pa_sink *sink = NULL;
3688
3689 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3690
3691 if (idx_device != PA_INVALID_INDEX)
3692 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx_device);
3693 else
3694 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
3695
3696 CHECK_VALIDITY(c->pstream, si && sink, tag, PA_ERR_NOENTITY);
3697
3698 if (pa_sink_input_move_to(si, sink) < 0) {
3699 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3700 return;
3701 }
3702 } else {
3703 pa_source_output *so = NULL;
3704 pa_source *source;
3705
3706 pa_assert(command == PA_COMMAND_MOVE_SOURCE_OUTPUT);
3707
3708 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
3709
3710 if (idx_device != PA_INVALID_INDEX)
3711 source = pa_idxset_get_by_index(c->protocol->core->sources, idx_device);
3712 else
3713 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
3714
3715 CHECK_VALIDITY(c->pstream, so && source, tag, PA_ERR_NOENTITY);
3716
3717 if (pa_source_output_move_to(so, source) < 0) {
3718 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3719 return;
3720 }
3721 }
3722
3723 pa_pstream_send_simple_ack(c->pstream, tag);
3724 }
3725
3726 static void command_suspend(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3727 connection *c = CONNECTION(userdata);
3728 uint32_t idx = PA_INVALID_INDEX;
3729 const char *name = NULL;
3730 pa_bool_t b;
3731
3732 connection_assert_ref(c);
3733 pa_assert(t);
3734
3735 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3736 pa_tagstruct_gets(t, &name) < 0 ||
3737 pa_tagstruct_get_boolean(t, &b) < 0 ||
3738 !pa_tagstruct_eof(t)) {
3739 protocol_error(c);
3740 return;
3741 }
3742
3743 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3744 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || !name || !*name || pa_utf8_valid(name), tag, PA_ERR_INVALID);
3745
3746 if (command == PA_COMMAND_SUSPEND_SINK) {
3747
3748 if (idx == PA_INVALID_INDEX && name && !*name) {
3749
3750 if (pa_sink_suspend_all(c->protocol->core, b) < 0) {
3751 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3752 return;
3753 }
3754 } else {
3755 pa_sink *sink = NULL;
3756
3757 if (idx != PA_INVALID_INDEX)
3758 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
3759 else
3760 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
3761
3762 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
3763
3764 if (pa_sink_suspend(sink, b) < 0) {
3765 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3766 return;
3767 }
3768 }
3769 } else {
3770
3771 pa_assert(command == PA_COMMAND_SUSPEND_SOURCE);
3772
3773 if (idx == PA_INVALID_INDEX && name && !*name) {
3774
3775 if (pa_source_suspend_all(c->protocol->core, b) < 0) {
3776 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3777 return;
3778 }
3779
3780 } else {
3781 pa_source *source;
3782
3783 if (idx != PA_INVALID_INDEX)
3784 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
3785 else
3786 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
3787
3788 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
3789
3790 if (pa_source_suspend(source, b) < 0) {
3791 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3792 return;
3793 }
3794 }
3795 }
3796
3797 pa_pstream_send_simple_ack(c->pstream, tag);
3798 }
3799
3800 /*** pstream callbacks ***/
3801
3802 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
3803 connection *c = CONNECTION(userdata);
3804
3805 pa_assert(p);
3806 pa_assert(packet);
3807 connection_assert_ref(c);
3808
3809 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0) {
3810 pa_log("invalid packet.");
3811 connection_unlink(c);
3812 }
3813 }
3814
3815 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) {
3816 connection *c = CONNECTION(userdata);
3817 output_stream *stream;
3818
3819 pa_assert(p);
3820 pa_assert(chunk);
3821 connection_assert_ref(c);
3822
3823 if (!(stream = OUTPUT_STREAM(pa_idxset_get_by_index(c->output_streams, channel)))) {
3824 pa_log("client sent block for invalid stream.");
3825 /* Ignoring */
3826 return;
3827 }
3828
3829 if (playback_stream_isinstance(stream)) {
3830 playback_stream *ps = PLAYBACK_STREAM(stream);
3831
3832 if (seek != PA_SEEK_RELATIVE || offset != 0)
3833 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);
3834
3835 pa_asyncmsgq_post(ps->sink_input->sink->asyncmsgq, PA_MSGOBJECT(ps->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
3836
3837 } else {
3838 upload_stream *u = UPLOAD_STREAM(stream);
3839 size_t l;
3840
3841 if (!u->memchunk.memblock) {
3842 if (u->length == chunk->length) {
3843 u->memchunk = *chunk;
3844 pa_memblock_ref(u->memchunk.memblock);
3845 u->length = 0;
3846 } else {
3847 u->memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, u->length);
3848 u->memchunk.index = u->memchunk.length = 0;
3849 }
3850 }
3851
3852 pa_assert(u->memchunk.memblock);
3853
3854 l = u->length;
3855 if (l > chunk->length)
3856 l = chunk->length;
3857
3858
3859 if (l > 0) {
3860 void *src, *dst;
3861 dst = pa_memblock_acquire(u->memchunk.memblock);
3862 src = pa_memblock_acquire(chunk->memblock);
3863
3864 memcpy((uint8_t*) dst + u->memchunk.index + u->memchunk.length,
3865 (uint8_t*) src+chunk->index, l);
3866
3867 pa_memblock_release(u->memchunk.memblock);
3868 pa_memblock_release(chunk->memblock);
3869
3870 u->memchunk.length += l;
3871 u->length -= l;
3872 }
3873 }
3874 }
3875
3876 static void pstream_die_callback(pa_pstream *p, void *userdata) {
3877 connection *c = CONNECTION(userdata);
3878
3879 pa_assert(p);
3880 connection_assert_ref(c);
3881
3882 connection_unlink(c);
3883 pa_log_info("connection died.");
3884 }
3885
3886 static void pstream_drain_callback(pa_pstream *p, void *userdata) {
3887 connection *c = CONNECTION(userdata);
3888
3889 pa_assert(p);
3890 connection_assert_ref(c);
3891
3892 send_memblock(c);
3893 }
3894
3895 static void pstream_revoke_callback(pa_pstream *p, uint32_t block_id, void *userdata) {
3896 pa_thread_mq *q;
3897
3898 if (!(q = pa_thread_mq_get()))
3899 pa_pstream_send_revoke(p, block_id);
3900 else
3901 pa_asyncmsgq_post(q->outq, PA_MSGOBJECT(userdata), CONNECTION_MESSAGE_REVOKE, PA_UINT_TO_PTR(block_id), 0, NULL, NULL);
3902 }
3903
3904 static void pstream_release_callback(pa_pstream *p, uint32_t block_id, void *userdata) {
3905 pa_thread_mq *q;
3906
3907 if (!(q = pa_thread_mq_get()))
3908 pa_pstream_send_release(p, block_id);
3909 else
3910 pa_asyncmsgq_post(q->outq, PA_MSGOBJECT(userdata), CONNECTION_MESSAGE_RELEASE, PA_UINT_TO_PTR(block_id), 0, NULL, NULL);
3911 }
3912
3913 /*** client callbacks ***/
3914
3915 static void client_kill_cb(pa_client *c) {
3916 pa_assert(c);
3917
3918 connection_unlink(CONNECTION(c->userdata));
3919 }
3920
3921 /*** socket server callbacks ***/
3922
3923 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
3924 connection *c = CONNECTION(userdata);
3925
3926 pa_assert(m);
3927 pa_assert(tv);
3928 connection_assert_ref(c);
3929 pa_assert(c->auth_timeout_event == e);
3930
3931 if (!c->authorized)
3932 connection_unlink(c);
3933 }
3934
3935 static void on_connection(PA_GCC_UNUSED pa_socket_server*s, pa_iochannel *io, void *userdata) {
3936 pa_protocol_native *p = userdata;
3937 connection *c;
3938 char cname[256], pname[128];
3939
3940 pa_assert(io);
3941 pa_assert(p);
3942
3943 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
3944 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
3945 pa_iochannel_free(io);
3946 return;
3947 }
3948
3949 c = pa_msgobject_new(connection);
3950 c->parent.parent.free = connection_free;
3951 c->parent.process_msg = connection_process_msg;
3952
3953 c->authorized = p->public;
3954
3955 if (!c->authorized && p->auth_ip_acl && pa_ip_acl_check(p->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
3956 pa_log_info("Client authenticated by IP ACL.");
3957 c->authorized = TRUE;
3958 }
3959
3960 if (!c->authorized) {
3961 struct timeval tv;
3962 pa_gettimeofday(&tv);
3963 tv.tv_sec += AUTH_TIMEOUT;
3964 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
3965 } else
3966 c->auth_timeout_event = NULL;
3967
3968 c->is_local = pa_iochannel_socket_is_local(io);
3969 c->version = 8;
3970 c->protocol = p;
3971 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
3972 pa_snprintf(cname, sizeof(cname), "Native client (%s)", pname);
3973 c->client = pa_client_new(p->core, __FILE__, cname);
3974 pa_proplist_sets(c->client->proplist, "native-protocol.peer", pname);
3975 c->client->kill = client_kill_cb;
3976 c->client->userdata = c;
3977 c->client->module = p->module;
3978
3979 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->mempool);
3980
3981 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
3982 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
3983 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
3984 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
3985 pa_pstream_set_revoke_callback(c->pstream, pstream_revoke_callback, c);
3986 pa_pstream_set_release_callback(c->pstream, pstream_release_callback, c);
3987
3988 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
3989
3990 c->record_streams = pa_idxset_new(NULL, NULL);
3991 c->output_streams = pa_idxset_new(NULL, NULL);
3992
3993 c->rrobin_index = PA_IDXSET_INVALID;
3994 c->subscription = NULL;
3995
3996 pa_idxset_put(p->connections, c, NULL);
3997
3998 #ifdef HAVE_CREDS
3999 if (pa_iochannel_creds_supported(io))
4000 pa_iochannel_creds_enable(io);
4001 #endif
4002 }
4003
4004 /*** module entry points ***/
4005
4006 static int load_key(pa_protocol_native*p, const char*fn) {
4007 pa_assert(p);
4008
4009 p->auth_cookie_in_property = FALSE;
4010
4011 if (!fn && pa_authkey_prop_get(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0) {
4012 pa_log_info("using already loaded auth cookie.");
4013 pa_authkey_prop_ref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
4014 p->auth_cookie_in_property = TRUE;
4015 return 0;
4016 }
4017
4018 if (!fn)
4019 fn = PA_NATIVE_COOKIE_FILE;
4020
4021 if (pa_authkey_load_auto(fn, p->auth_cookie, sizeof(p->auth_cookie)) < 0)
4022 return -1;
4023
4024 pa_log_info("loading cookie from disk.");
4025
4026 if (pa_authkey_prop_put(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME, p->auth_cookie, sizeof(p->auth_cookie)) >= 0)
4027 p->auth_cookie_in_property = TRUE;
4028
4029 return 0;
4030 }
4031
4032 static pa_protocol_native* protocol_new_internal(pa_core *c, pa_module *m, pa_modargs *ma) {
4033 pa_protocol_native *p;
4034 pa_bool_t public = FALSE;
4035 const char *acl;
4036
4037 pa_assert(c);
4038 pa_assert(ma);
4039
4040 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
4041 pa_log("auth-anonymous= expects a boolean argument.");
4042 return NULL;
4043 }
4044
4045 p = pa_xnew(pa_protocol_native, 1);
4046 p->core = c;
4047 p->module = m;
4048 p->public = public;
4049 p->server = NULL;
4050 p->auth_ip_acl = NULL;
4051
4052 #ifdef HAVE_CREDS
4053 {
4054 pa_bool_t a = TRUE;
4055 if (pa_modargs_get_value_boolean(ma, "auth-group-enabled", &a) < 0) {
4056 pa_log("auth-group-enabled= expects a boolean argument.");
4057 return NULL;
4058 }
4059 p->auth_group = a ? pa_xstrdup(pa_modargs_get_value(ma, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP : NULL)) : NULL;
4060
4061 if (p->auth_group)
4062 pa_log_info("Allowing access to group '%s'.", p->auth_group);
4063 }
4064 #endif
4065
4066
4067 if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
4068
4069 if (!(p->auth_ip_acl = pa_ip_acl_new(acl))) {
4070 pa_log("Failed to parse IP ACL '%s'", acl);
4071 goto fail;
4072 }
4073 }
4074
4075 if (load_key(p, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
4076 goto fail;
4077
4078 p->connections = pa_idxset_new(NULL, NULL);
4079
4080 return p;
4081
4082 fail:
4083 #ifdef HAVE_CREDS
4084 pa_xfree(p->auth_group);
4085 #endif
4086 if (p->auth_ip_acl)
4087 pa_ip_acl_free(p->auth_ip_acl);
4088 pa_xfree(p);
4089 return NULL;
4090 }
4091
4092 pa_protocol_native* pa_protocol_native_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
4093 char t[256];
4094 pa_protocol_native *p;
4095
4096 if (!(p = protocol_new_internal(core, m, ma)))
4097 return NULL;
4098
4099 p->server = pa_socket_server_ref(server);
4100 pa_socket_server_set_callback(p->server, on_connection, p);
4101
4102 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
4103 pa_strlist *l;
4104 l = pa_property_get(core, PA_NATIVE_SERVER_PROPERTY_NAME);
4105 l = pa_strlist_prepend(l, t);
4106 pa_property_replace(core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
4107 }
4108
4109 return p;
4110 }
4111
4112 void pa_protocol_native_free(pa_protocol_native *p) {
4113 connection *c;
4114 pa_assert(p);
4115
4116 while ((c = pa_idxset_first(p->connections, NULL)))
4117 connection_unlink(c);
4118 pa_idxset_free(p->connections, NULL, NULL);
4119
4120 if (p->server) {
4121 char t[256];
4122
4123 if (pa_socket_server_get_address(p->server, t, sizeof(t))) {
4124 pa_strlist *l;
4125 l = pa_property_get(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
4126 l = pa_strlist_remove(l, t);
4127
4128 if (l)
4129 pa_property_replace(p->core, PA_NATIVE_SERVER_PROPERTY_NAME, l);
4130 else
4131 pa_property_remove(p->core, PA_NATIVE_SERVER_PROPERTY_NAME);
4132 }
4133
4134 pa_socket_server_unref(p->server);
4135 }
4136
4137 if (p->auth_cookie_in_property)
4138 pa_authkey_prop_unref(p->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
4139
4140 if (p->auth_ip_acl)
4141 pa_ip_acl_free(p->auth_ip_acl);
4142
4143 #ifdef HAVE_CREDS
4144 pa_xfree(p->auth_group);
4145 #endif
4146 pa_xfree(p);
4147 }
4148
4149 pa_protocol_native* pa_protocol_native_new_iochannel(
4150 pa_core*core,
4151 pa_iochannel *io,
4152 pa_module *m,
4153 pa_modargs *ma) {
4154
4155 pa_protocol_native *p;
4156
4157 if (!(p = protocol_new_internal(core, m, ma)))
4158 return NULL;
4159
4160 on_connection(NULL, io, p);
4161
4162 return p;
4163 }