]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-native.c
reset prebuf if it is too large
[pulseaudio] / src / pulsecore / protocol-native.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include <pulse/timeval.h>
35 #include <pulse/version.h>
36 #include <pulse/utf8.h>
37 #include <pulse/util.h>
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/native-common.h>
41 #include <pulsecore/packet.h>
42 #include <pulsecore/client.h>
43 #include <pulsecore/source-output.h>
44 #include <pulsecore/sink-input.h>
45 #include <pulsecore/pstream.h>
46 #include <pulsecore/tagstruct.h>
47 #include <pulsecore/pdispatch.h>
48 #include <pulsecore/pstream-util.h>
49 #include <pulsecore/authkey.h>
50 #include <pulsecore/namereg.h>
51 #include <pulsecore/core-scache.h>
52 #include <pulsecore/core-subscribe.h>
53 #include <pulsecore/log.h>
54 #include <pulsecore/autoload.h>
55 #include <pulsecore/authkey-prop.h>
56 #include <pulsecore/strlist.h>
57 #include <pulsecore/props.h>
58 #include <pulsecore/sample-util.h>
59 #include <pulsecore/llist.h>
60 #include <pulsecore/creds.h>
61 #include <pulsecore/core-util.h>
62 #include <pulsecore/ipacl.h>
63 #include <pulsecore/thread-mq.h>
64
65 #include "protocol-native.h"
66
67 /* Kick a client if it doesn't authenticate within this time */
68 #define AUTH_TIMEOUT 60
69
70 /* Don't accept more connection than this */
71 #define MAX_CONNECTIONS 64
72
73 #define MAX_MEMBLOCKQ_LENGTH (4*1024*1024) /* 4MB */
74 #define DEFAULT_TLENGTH_MSEC 2000 /* 2s */
75 #define DEFAULT_PROCESS_MSEC 20 /* 20ms */
76 #define DEFAULT_FRAGSIZE_MSEC DEFAULT_TLENGTH_MSEC
77
78 typedef struct connection connection;
79 struct pa_protocol_native;
80
81 typedef struct record_stream {
82 pa_msgobject parent;
83
84 connection *connection;
85 uint32_t index;
86
87 pa_source_output *source_output;
88 pa_memblockq *memblockq;
89 size_t fragment_size;
90 pa_usec_t source_latency;
91 } record_stream;
92
93 typedef struct output_stream {
94 pa_msgobject parent;
95 } output_stream;
96
97 typedef struct playback_stream {
98 output_stream parent;
99
100 connection *connection;
101 uint32_t index;
102
103 pa_sink_input *sink_input;
104 pa_memblockq *memblockq;
105 pa_bool_t drain_request;
106 uint32_t drain_tag;
107 uint32_t syncid;
108
109 pa_atomic_t missing;
110 size_t minreq;
111 pa_usec_t sink_latency;
112
113 /* Only updated after SINK_INPUT_MESSAGE_UPDATE_LATENCY */
114 int64_t read_index, write_index;
115 size_t render_memblockq_length;
116 } playback_stream;
117
118 typedef struct upload_stream {
119 output_stream parent;
120
121 connection *connection;
122 uint32_t index;
123
124 pa_memchunk memchunk;
125 size_t length;
126 char *name;
127 pa_sample_spec sample_spec;
128 pa_channel_map channel_map;
129 pa_proplist *proplist;
130 } upload_stream;
131
132 struct connection {
133 pa_msgobject parent;
134
135 pa_bool_t authorized:1;
136 pa_bool_t is_local:1;
137 uint32_t version;
138 pa_protocol_native *protocol;
139 pa_client *client;
140 pa_pstream *pstream;
141 pa_pdispatch *pdispatch;
142 pa_idxset *record_streams, *output_streams;
143 uint32_t rrobin_index;
144 pa_subscription *subscription;
145 pa_time_event *auth_timeout_event;
146 };
147
148 PA_DECLARE_CLASS(record_stream);
149 #define RECORD_STREAM(o) (record_stream_cast(o))
150 static PA_DEFINE_CHECK_TYPE(record_stream, pa_msgobject);
151
152 PA_DECLARE_CLASS(output_stream);
153 #define OUTPUT_STREAM(o) (output_stream_cast(o))
154 static PA_DEFINE_CHECK_TYPE(output_stream, pa_msgobject);
155
156 PA_DECLARE_CLASS(playback_stream);
157 #define PLAYBACK_STREAM(o) (playback_stream_cast(o))
158 static PA_DEFINE_CHECK_TYPE(playback_stream, output_stream);
159
160 PA_DECLARE_CLASS(upload_stream);
161 #define UPLOAD_STREAM(o) (upload_stream_cast(o))
162 static PA_DEFINE_CHECK_TYPE(upload_stream, output_stream);
163
164 PA_DECLARE_CLASS(connection);
165 #define CONNECTION(o) (connection_cast(o))
166 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
167
168 struct pa_protocol_native {
169 pa_module *module;
170 pa_core *core;
171 pa_bool_t public;
172 pa_socket_server *server;
173 pa_idxset *connections;
174 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
175 pa_bool_t auth_cookie_in_property;
176 #ifdef HAVE_CREDS
177 char *auth_group;
178 #endif
179 pa_ip_acl *auth_ip_acl;
180 };
181
182 enum {
183 SINK_INPUT_MESSAGE_POST_DATA = PA_SINK_INPUT_MESSAGE_MAX, /* data from main loop to sink input */
184 SINK_INPUT_MESSAGE_DRAIN, /* disabled prebuf, get playback started. */
185 SINK_INPUT_MESSAGE_FLUSH,
186 SINK_INPUT_MESSAGE_TRIGGER,
187 SINK_INPUT_MESSAGE_SEEK,
188 SINK_INPUT_MESSAGE_PREBUF_FORCE,
189 SINK_INPUT_MESSAGE_UPDATE_LATENCY
190 };
191
192 enum {
193 PLAYBACK_STREAM_MESSAGE_REQUEST_DATA, /* data requested from sink input from the main loop */
194 PLAYBACK_STREAM_MESSAGE_UNDERFLOW,
195 PLAYBACK_STREAM_MESSAGE_OVERFLOW,
196 PLAYBACK_STREAM_MESSAGE_DRAIN_ACK,
197 PLAYBACK_STREAM_MESSAGE_STARTED
198 };
199
200 enum {
201 RECORD_STREAM_MESSAGE_POST_DATA /* data from source output to main loop */
202 };
203
204 enum {
205 CONNECTION_MESSAGE_RELEASE,
206 CONNECTION_MESSAGE_REVOKE
207 };
208
209 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk);
210 static void sink_input_kill_cb(pa_sink_input *i);
211 static void sink_input_suspend_cb(pa_sink_input *i, pa_bool_t suspend);
212 static void sink_input_moved_cb(pa_sink_input *i);
213 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes);
214 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes);
215
216
217 static void send_memblock(connection *c);
218 static void request_bytes(struct playback_stream*s);
219
220 static void source_output_kill_cb(pa_source_output *o);
221 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
222 static void source_output_suspend_cb(pa_source_output *o, pa_bool_t suspend);
223 static void source_output_moved_cb(pa_source_output *o);
224 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
225
226 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk);
227
228 static void command_exit(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
229 static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
230 static void command_drain_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
231 static void command_create_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
232 static void command_delete_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
233 static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
234 static void command_set_client_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
235 static void command_lookup(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
236 static void command_stat(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
237 static void command_get_playback_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
238 static void command_get_record_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
239 static void command_create_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
240 static void command_finish_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
241 static void command_play_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
242 static void command_remove_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
243 static void command_get_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
244 static void command_get_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
245 static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
246 static void command_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
247 static void command_set_volume(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
248 static void command_set_mute(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
249 static void command_cork_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
250 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
251 static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
252 static void command_set_stream_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
253 static void command_kill(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
254 static void command_load_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
255 static void command_unload_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
256 static void command_add_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
257 static void command_remove_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
258 static void command_get_autoload_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
259 static void command_get_autoload_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
260 static void command_cork_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
261 static void command_flush_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
262 static void command_move_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
263 static void command_suspend(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
264 static void command_set_stream_buffer_attr(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
265 static void command_update_stream_sample_rate(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
266 static void command_update_proplist(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
267 static void command_remove_proplist(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
268
269 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
270 [PA_COMMAND_ERROR] = NULL,
271 [PA_COMMAND_TIMEOUT] = NULL,
272 [PA_COMMAND_REPLY] = NULL,
273 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = command_create_playback_stream,
274 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = command_delete_stream,
275 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = command_drain_playback_stream,
276 [PA_COMMAND_CREATE_RECORD_STREAM] = command_create_record_stream,
277 [PA_COMMAND_DELETE_RECORD_STREAM] = command_delete_stream,
278 [PA_COMMAND_AUTH] = command_auth,
279 [PA_COMMAND_REQUEST] = NULL,
280 [PA_COMMAND_EXIT] = command_exit,
281 [PA_COMMAND_SET_CLIENT_NAME] = command_set_client_name,
282 [PA_COMMAND_LOOKUP_SINK] = command_lookup,
283 [PA_COMMAND_LOOKUP_SOURCE] = command_lookup,
284 [PA_COMMAND_STAT] = command_stat,
285 [PA_COMMAND_GET_PLAYBACK_LATENCY] = command_get_playback_latency,
286 [PA_COMMAND_GET_RECORD_LATENCY] = command_get_record_latency,
287 [PA_COMMAND_CREATE_UPLOAD_STREAM] = command_create_upload_stream,
288 [PA_COMMAND_DELETE_UPLOAD_STREAM] = command_delete_stream,
289 [PA_COMMAND_FINISH_UPLOAD_STREAM] = command_finish_upload_stream,
290 [PA_COMMAND_PLAY_SAMPLE] = command_play_sample,
291 [PA_COMMAND_REMOVE_SAMPLE] = command_remove_sample,
292 [PA_COMMAND_GET_SINK_INFO] = command_get_info,
293 [PA_COMMAND_GET_SOURCE_INFO] = command_get_info,
294 [PA_COMMAND_GET_CLIENT_INFO] = command_get_info,
295 [PA_COMMAND_GET_MODULE_INFO] = command_get_info,
296 [PA_COMMAND_GET_SINK_INPUT_INFO] = command_get_info,
297 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = command_get_info,
298 [PA_COMMAND_GET_SAMPLE_INFO] = command_get_info,
299 [PA_COMMAND_GET_SINK_INFO_LIST] = command_get_info_list,
300 [PA_COMMAND_GET_SOURCE_INFO_LIST] = command_get_info_list,
301 [PA_COMMAND_GET_MODULE_INFO_LIST] = command_get_info_list,
302 [PA_COMMAND_GET_CLIENT_INFO_LIST] = command_get_info_list,
303 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = command_get_info_list,
304 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = command_get_info_list,
305 [PA_COMMAND_GET_SAMPLE_INFO_LIST] = command_get_info_list,
306 [PA_COMMAND_GET_SERVER_INFO] = command_get_server_info,
307 [PA_COMMAND_SUBSCRIBE] = command_subscribe,
308
309 [PA_COMMAND_SET_SINK_VOLUME] = command_set_volume,
310 [PA_COMMAND_SET_SINK_INPUT_VOLUME] = command_set_volume,
311 [PA_COMMAND_SET_SOURCE_VOLUME] = command_set_volume,
312
313 [PA_COMMAND_SET_SINK_MUTE] = command_set_mute,
314 [PA_COMMAND_SET_SINK_INPUT_MUTE] = command_set_mute,
315 [PA_COMMAND_SET_SOURCE_MUTE] = command_set_mute,
316
317 [PA_COMMAND_SUSPEND_SINK] = command_suspend,
318 [PA_COMMAND_SUSPEND_SOURCE] = command_suspend,
319
320 [PA_COMMAND_CORK_PLAYBACK_STREAM] = command_cork_playback_stream,
321 [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = command_trigger_or_flush_or_prebuf_playback_stream,
322 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = command_trigger_or_flush_or_prebuf_playback_stream,
323 [PA_COMMAND_PREBUF_PLAYBACK_STREAM] = command_trigger_or_flush_or_prebuf_playback_stream,
324
325 [PA_COMMAND_CORK_RECORD_STREAM] = command_cork_record_stream,
326 [PA_COMMAND_FLUSH_RECORD_STREAM] = command_flush_record_stream,
327
328 [PA_COMMAND_SET_DEFAULT_SINK] = command_set_default_sink_or_source,
329 [PA_COMMAND_SET_DEFAULT_SOURCE] = command_set_default_sink_or_source,
330 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME] = command_set_stream_name,
331 [PA_COMMAND_SET_RECORD_STREAM_NAME] = command_set_stream_name,
332 [PA_COMMAND_KILL_CLIENT] = command_kill,
333 [PA_COMMAND_KILL_SINK_INPUT] = command_kill,
334 [PA_COMMAND_KILL_SOURCE_OUTPUT] = command_kill,
335 [PA_COMMAND_LOAD_MODULE] = command_load_module,
336 [PA_COMMAND_UNLOAD_MODULE] = command_unload_module,
337 [PA_COMMAND_GET_AUTOLOAD_INFO] = command_get_autoload_info,
338 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST] = command_get_autoload_info_list,
339 [PA_COMMAND_ADD_AUTOLOAD] = command_add_autoload,
340 [PA_COMMAND_REMOVE_AUTOLOAD] = command_remove_autoload,
341
342 [PA_COMMAND_MOVE_SINK_INPUT] = command_move_stream,
343 [PA_COMMAND_MOVE_SOURCE_OUTPUT] = command_move_stream,
344
345 [PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR] = command_set_stream_buffer_attr,
346 [PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR] = command_set_stream_buffer_attr,
347
348 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE] = command_update_stream_sample_rate,
349 [PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE] = command_update_stream_sample_rate,
350
351 [PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST] = command_update_proplist,
352 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST] = command_update_proplist,
353 [PA_COMMAND_UPDATE_CLIENT_PROPLIST] = command_update_proplist,
354
355 [PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST] = command_remove_proplist,
356 [PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST] = command_remove_proplist,
357 [PA_COMMAND_REMOVE_CLIENT_PROPLIST] = command_remove_proplist,
358 };
359
360 /* structure management */
361
362 static void upload_stream_unlink(upload_stream *s) {
363 pa_assert(s);
364
365 if (!s->connection)
366 return;
367
368 pa_assert_se(pa_idxset_remove_by_data(s->connection->output_streams, s, NULL) == s);
369 s->connection = NULL;
370 upload_stream_unref(s);
371 }
372
373 static void upload_stream_free(pa_object *o) {
374 upload_stream *s = UPLOAD_STREAM(o);
375 pa_assert(s);
376
377 upload_stream_unlink(s);
378
379 pa_xfree(s->name);
380
381 if (s->proplist)
382 pa_proplist_free(s->proplist);
383
384 if (s->memchunk.memblock)
385 pa_memblock_unref(s->memchunk.memblock);
386
387 pa_xfree(s);
388 }
389
390 static upload_stream* upload_stream_new(
391 connection *c,
392 const pa_sample_spec *ss,
393 const pa_channel_map *map,
394 const char *name,
395 size_t length,
396 pa_proplist *p) {
397
398 upload_stream *s;
399
400 pa_assert(c);
401 pa_assert(ss);
402 pa_assert(name);
403 pa_assert(length > 0);
404 pa_assert(p);
405
406 s = pa_msgobject_new(upload_stream);
407 s->parent.parent.parent.free = upload_stream_free;
408 s->connection = c;
409 s->sample_spec = *ss;
410 s->channel_map = *map;
411 s->name = pa_xstrdup(name);
412 pa_memchunk_reset(&s->memchunk);
413 s->length = length;
414 s->proplist = pa_proplist_copy(p);
415 pa_proplist_update(s->proplist, PA_UPDATE_MERGE, c->client->proplist);
416
417 pa_idxset_put(c->output_streams, s, &s->index);
418
419 return s;
420 }
421
422 static void record_stream_unlink(record_stream *s) {
423 pa_assert(s);
424
425 if (!s->connection)
426 return;
427
428 if (s->source_output) {
429 pa_source_output_unlink(s->source_output);
430 pa_source_output_unref(s->source_output);
431 s->source_output = NULL;
432 }
433
434 pa_assert_se(pa_idxset_remove_by_data(s->connection->record_streams, s, NULL) == s);
435 s->connection = NULL;
436 record_stream_unref(s);
437 }
438
439 static void record_stream_free(pa_object *o) {
440 record_stream *s = RECORD_STREAM(o);
441 pa_assert(s);
442
443 record_stream_unlink(s);
444
445 pa_memblockq_free(s->memblockq);
446 pa_xfree(s);
447 }
448
449 static int record_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
450 record_stream *s = RECORD_STREAM(o);
451 record_stream_assert_ref(s);
452
453 if (!s->connection)
454 return -1;
455
456 switch (code) {
457
458 case RECORD_STREAM_MESSAGE_POST_DATA:
459
460 if (pa_memblockq_push_align(s->memblockq, chunk) < 0) {
461 /* pa_log_warn("Failed to push data into output queue."); */
462 return -1;
463 }
464
465 if (!pa_pstream_is_pending(s->connection->pstream))
466 send_memblock(s->connection);
467
468 break;
469 }
470
471 return 0;
472 }
473
474 static void fix_record_buffer_attr_pre(record_stream *s, pa_bool_t adjust_latency, uint32_t *maxlength, uint32_t *fragsize) {
475 pa_assert(s);
476 pa_assert(maxlength);
477 pa_assert(fragsize);
478
479 if (*maxlength <= 0 || *maxlength > MAX_MEMBLOCKQ_LENGTH)
480 *maxlength = MAX_MEMBLOCKQ_LENGTH;
481
482 if (*fragsize <= 0)
483 *fragsize = pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC*PA_USEC_PER_MSEC, &s->source_output->sample_spec);
484
485 if (adjust_latency) {
486 pa_usec_t fragsize_usec;
487
488 /* So, the user asked us to adjust the latency according to
489 * what the source can provide. Half the latency will be
490 * spent on the hw buffer, half of it in the async buffer
491 * queue we maintain for each client. */
492
493 fragsize_usec = pa_bytes_to_usec(*fragsize, &s->source_output->sample_spec);
494
495 s->source_latency = pa_source_output_set_requested_latency(s->source_output, fragsize_usec/2);
496
497 if (fragsize_usec >= s->source_latency*2)
498 fragsize_usec -= s->source_latency;
499 else
500 fragsize_usec = s->source_latency;
501
502 *fragsize = pa_usec_to_bytes(fragsize_usec, &s->source_output->sample_spec);
503 } else
504 s->source_latency = 0;
505 }
506
507 static void fix_record_buffer_attr_post(record_stream *s, uint32_t *maxlength, uint32_t *fragsize) {
508 size_t base;
509
510 pa_assert(s);
511 pa_assert(maxlength);
512 pa_assert(fragsize);
513
514 *maxlength = pa_memblockq_get_maxlength(s->memblockq);
515
516 base = pa_frame_size(&s->source_output->sample_spec);
517
518 s->fragment_size = (*fragsize/base)*base;
519 if (s->fragment_size <= 0)
520 s->fragment_size = base;
521
522 if (s->fragment_size > *maxlength)
523 s->fragment_size = *maxlength;
524
525 *fragsize = s->fragment_size;
526 }
527
528 static record_stream* record_stream_new(
529 connection *c,
530 pa_source *source,
531 pa_sample_spec *ss,
532 pa_channel_map *map,
533 pa_bool_t peak_detect,
534 uint32_t *maxlength,
535 uint32_t *fragsize,
536 pa_source_output_flags_t flags,
537 pa_proplist *p,
538 pa_bool_t adjust_latency,
539 pa_sink_input *direct_on_input) {
540
541 record_stream *s;
542 pa_source_output *source_output;
543 size_t base;
544 pa_source_output_new_data data;
545
546 pa_assert(c);
547 pa_assert(ss);
548 pa_assert(maxlength);
549 pa_assert(p);
550
551 pa_source_output_new_data_init(&data);
552
553 pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
554 pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
555 data.driver = __FILE__;
556 data.module = c->protocol->module;
557 data.client = c->client;
558 data.source = source;
559 data.direct_on_input = direct_on_input;
560 pa_source_output_new_data_set_sample_spec(&data, ss);
561 pa_source_output_new_data_set_channel_map(&data, map);
562 if (peak_detect)
563 data.resample_method = PA_RESAMPLER_PEAKS;
564
565 source_output = pa_source_output_new(c->protocol->core, &data, flags);
566
567 pa_source_output_new_data_done(&data);
568
569 if (!source_output)
570 return NULL;
571
572 s = pa_msgobject_new(record_stream);
573 s->parent.parent.free = record_stream_free;
574 s->parent.process_msg = record_stream_process_msg;
575 s->connection = c;
576 s->source_output = source_output;
577
578 s->source_output->push = source_output_push_cb;
579 s->source_output->kill = source_output_kill_cb;
580 s->source_output->get_latency = source_output_get_latency_cb;
581 s->source_output->moved = source_output_moved_cb;
582 s->source_output->suspend = source_output_suspend_cb;
583 s->source_output->userdata = s;
584
585 fix_record_buffer_attr_pre(s, adjust_latency, maxlength, fragsize);
586
587 s->memblockq = pa_memblockq_new(
588 0,
589 *maxlength,
590 0,
591 base = pa_frame_size(&source_output->sample_spec),
592 1,
593 0,
594 0,
595 NULL);
596
597 fix_record_buffer_attr_post(s, maxlength, fragsize);
598
599 *ss = s->source_output->sample_spec;
600 *map = s->source_output->channel_map;
601
602 pa_idxset_put(c->record_streams, s, &s->index);
603
604 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
605 ((double) pa_bytes_to_usec(s->fragment_size, &source_output->sample_spec) + (double) s->source_latency) / PA_USEC_PER_MSEC,
606 (double) pa_bytes_to_usec(s->fragment_size, &source_output->sample_spec) / PA_USEC_PER_MSEC,
607 (double) s->source_latency / PA_USEC_PER_MSEC);
608
609 pa_source_output_put(s->source_output);
610 return s;
611 }
612
613 static void playback_stream_unlink(playback_stream *s) {
614 pa_assert(s);
615
616 if (!s->connection)
617 return;
618
619 if (s->sink_input) {
620 pa_sink_input_unlink(s->sink_input);
621 pa_sink_input_unref(s->sink_input);
622 s->sink_input = NULL;
623 }
624
625 if (s->drain_request)
626 pa_pstream_send_error(s->connection->pstream, s->drain_tag, PA_ERR_NOENTITY);
627
628 pa_assert_se(pa_idxset_remove_by_data(s->connection->output_streams, s, NULL) == s);
629 s->connection = NULL;
630 playback_stream_unref(s);
631 }
632
633 static void playback_stream_free(pa_object* o) {
634 playback_stream *s = PLAYBACK_STREAM(o);
635 pa_assert(s);
636
637 playback_stream_unlink(s);
638
639 pa_memblockq_free(s->memblockq);
640 pa_xfree(s);
641 }
642
643 static int playback_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
644 playback_stream *s = PLAYBACK_STREAM(o);
645 playback_stream_assert_ref(s);
646
647 if (!s->connection)
648 return -1;
649
650 switch (code) {
651 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA: {
652 pa_tagstruct *t;
653 uint32_t l = 0;
654
655 for (;;) {
656 if ((l = pa_atomic_load(&s->missing)) <= 0)
657 break;
658
659 if (pa_atomic_cmpxchg(&s->missing, l, 0))
660 break;
661 }
662
663 if (l <= 0)
664 break;
665
666 t = pa_tagstruct_new(NULL, 0);
667 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
668 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
669 pa_tagstruct_putu32(t, s->index);
670 pa_tagstruct_putu32(t, l);
671 pa_pstream_send_tagstruct(s->connection->pstream, t);
672
673 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
674 break;
675 }
676
677 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW: {
678 pa_tagstruct *t;
679
680 /* Report that we're empty */
681 t = pa_tagstruct_new(NULL, 0);
682 pa_tagstruct_putu32(t, PA_COMMAND_UNDERFLOW);
683 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
684 pa_tagstruct_putu32(t, s->index);
685 pa_pstream_send_tagstruct(s->connection->pstream, t);
686 break;
687 }
688
689 case PLAYBACK_STREAM_MESSAGE_OVERFLOW: {
690 pa_tagstruct *t;
691
692 /* Notify the user we're overflowed*/
693 t = pa_tagstruct_new(NULL, 0);
694 pa_tagstruct_putu32(t, PA_COMMAND_OVERFLOW);
695 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
696 pa_tagstruct_putu32(t, s->index);
697 pa_pstream_send_tagstruct(s->connection->pstream, t);
698 break;
699 }
700
701 case PLAYBACK_STREAM_MESSAGE_STARTED:
702
703 if (s->connection->version >= 13) {
704 pa_tagstruct *t;
705
706 /* Notify the user we're overflowed*/
707 t = pa_tagstruct_new(NULL, 0);
708 pa_tagstruct_putu32(t, PA_COMMAND_STARTED);
709 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
710 pa_tagstruct_putu32(t, s->index);
711 pa_pstream_send_tagstruct(s->connection->pstream, t);
712 }
713
714 break;
715
716 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK:
717 pa_pstream_send_simple_ack(s->connection->pstream, PA_PTR_TO_UINT(userdata));
718 break;
719 }
720
721 return 0;
722 }
723
724 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) {
725 size_t frame_size;
726 pa_usec_t tlength_usec, minreq_usec, sink_usec;
727
728 pa_assert(s);
729 pa_assert(maxlength);
730 pa_assert(tlength);
731 pa_assert(prebuf);
732 pa_assert(minreq);
733
734 if (*maxlength <= 0 || *maxlength > MAX_MEMBLOCKQ_LENGTH)
735 *maxlength = MAX_MEMBLOCKQ_LENGTH;
736 if (*tlength <= 0)
737 *tlength = pa_usec_to_bytes(DEFAULT_TLENGTH_MSEC*PA_USEC_PER_MSEC, &s->sink_input->sample_spec);
738 if (*minreq <= 0)
739 *minreq = pa_usec_to_bytes(DEFAULT_PROCESS_MSEC*PA_USEC_PER_MSEC, &s->sink_input->sample_spec);
740
741 frame_size = pa_frame_size(&s->sink_input->sample_spec);
742 if (*minreq <= 0)
743 *minreq = frame_size;
744 if (*tlength < *minreq+frame_size)
745 *tlength = *minreq+frame_size;
746
747 tlength_usec = pa_bytes_to_usec(*tlength, &s->sink_input->sample_spec);
748 minreq_usec = pa_bytes_to_usec(*minreq, &s->sink_input->sample_spec);
749
750 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
751 (double) tlength_usec / PA_USEC_PER_MSEC,
752 (double) minreq_usec / PA_USEC_PER_MSEC);
753
754 if (adjust_latency) {
755
756 /* So, the user asked us to adjust the latency of the stream
757 * buffer according to the what the sink can provide. The
758 * tlength passed in shall be the overall latency. Roughly
759 * half the latency will be spent on the hw buffer, the other
760 * half of it in the async buffer queue we maintain for each
761 * client. In between we'll have a safety space of size
762 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
763 * empty and needs to be filled, then our buffer must have
764 * enough data to fulfill this request immediatly and thus
765 * have at least the same tlength as the size of the hw
766 * buffer. It additionally needs space for 2 times minreq
767 * because if the buffer ran empty and a partial fillup
768 * happens immediately on the next iteration we need to be
769 * able to fulfill it and give the application also minreq
770 * time to fill it up again for the next request Makes 2 times
771 * minreq in plus.. */
772
773 if (tlength_usec > minreq_usec*2)
774 sink_usec = (tlength_usec - minreq_usec*2)/2;
775 else
776 sink_usec = 0;
777
778 } else {
779
780 /* Ok, the user didn't ask us to adjust the latency, but we
781 * still need to make sure that the parameters from the user
782 * do make sense. */
783
784 if (tlength_usec > minreq_usec*2)
785 sink_usec = (tlength_usec - minreq_usec*2);
786 else
787 sink_usec = 0;
788 }
789
790 s->sink_latency = pa_sink_input_set_requested_latency(s->sink_input, sink_usec);
791
792 if (adjust_latency) {
793 /* Ok, we didn't necessarily get what we were asking for, so
794 * let's subtract from what we asked for for the remaining
795 * buffer space */
796
797 if (tlength_usec >= s->sink_latency)
798 tlength_usec -= s->sink_latency;
799 }
800
801 if (tlength_usec < s->sink_latency + 2*minreq_usec)
802 tlength_usec = s->sink_latency + 2*minreq_usec;
803
804 *tlength = pa_usec_to_bytes(tlength_usec, &s->sink_input->sample_spec);
805 *minreq = pa_usec_to_bytes(minreq_usec, &s->sink_input->sample_spec);
806
807 if (*minreq <= 0) {
808 *minreq += frame_size;
809 *tlength += frame_size*2;
810 }
811
812 if (*tlength <= *minreq)
813 *tlength = *minreq*2 + frame_size;
814
815 if (*prebuf <= 0 || *prebuf > *tlength)
816 *prebuf = *tlength;
817 }
818
819 static void fix_playback_buffer_attr_post(playback_stream *s, uint32_t *maxlength, uint32_t *tlength, uint32_t* prebuf, uint32_t* minreq) {
820 pa_assert(s);
821 pa_assert(maxlength);
822 pa_assert(tlength);
823 pa_assert(prebuf);
824 pa_assert(minreq);
825
826 *maxlength = (uint32_t) pa_memblockq_get_maxlength(s->memblockq);
827 *tlength = (uint32_t) pa_memblockq_get_tlength(s->memblockq);
828 *prebuf = (uint32_t) pa_memblockq_get_prebuf(s->memblockq);
829 *minreq = (uint32_t) pa_memblockq_get_minreq(s->memblockq);
830
831 s->minreq = *minreq;
832 }
833
834 static playback_stream* playback_stream_new(
835 connection *c,
836 pa_sink *sink,
837 pa_sample_spec *ss,
838 pa_channel_map *map,
839 uint32_t *maxlength,
840 uint32_t *tlength,
841 uint32_t *prebuf,
842 uint32_t *minreq,
843 pa_cvolume *volume,
844 pa_bool_t muted,
845 uint32_t syncid,
846 uint32_t *missing,
847 pa_sink_input_flags_t flags,
848 pa_proplist *p,
849 pa_bool_t adjust_latency) {
850
851 playback_stream *s, *ssync;
852 pa_sink_input *sink_input;
853 pa_memchunk silence;
854 uint32_t idx;
855 int64_t start_index;
856 pa_sink_input_new_data data;
857
858 pa_assert(c);
859 pa_assert(ss);
860 pa_assert(maxlength);
861 pa_assert(tlength);
862 pa_assert(prebuf);
863 pa_assert(minreq);
864 pa_assert(volume);
865 pa_assert(missing);
866 pa_assert(p);
867
868 /* Find syncid group */
869 for (ssync = pa_idxset_first(c->output_streams, &idx); ssync; ssync = pa_idxset_next(c->output_streams, &idx)) {
870
871 if (!playback_stream_isinstance(ssync))
872 continue;
873
874 if (ssync->syncid == syncid)
875 break;
876 }
877
878 /* Synced streams must connect to the same sink */
879 if (ssync) {
880
881 if (!sink)
882 sink = ssync->sink_input->sink;
883 else if (sink != ssync->sink_input->sink)
884 return NULL;
885 }
886
887 pa_sink_input_new_data_init(&data);
888
889 pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
890 pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
891 data.driver = __FILE__;
892 data.module = c->protocol->module;
893 data.client = c->client;
894 data.sink = sink;
895 pa_sink_input_new_data_set_sample_spec(&data, ss);
896 pa_sink_input_new_data_set_channel_map(&data, map);
897 pa_sink_input_new_data_set_volume(&data, volume);
898 pa_sink_input_new_data_set_muted(&data, muted);
899 data.sync_base = ssync ? ssync->sink_input : NULL;
900
901 sink_input = pa_sink_input_new(c->protocol->core, &data, flags);
902
903 pa_sink_input_new_data_done(&data);
904
905 if (!sink_input)
906 return NULL;
907
908 s = pa_msgobject_new(playback_stream);
909 s->parent.parent.parent.free = playback_stream_free;
910 s->parent.parent.process_msg = playback_stream_process_msg;
911 s->connection = c;
912 s->syncid = syncid;
913 s->sink_input = sink_input;
914
915 s->sink_input->parent.process_msg = sink_input_process_msg;
916 s->sink_input->pop = sink_input_pop_cb;
917 s->sink_input->process_rewind = sink_input_process_rewind_cb;
918 s->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
919 s->sink_input->kill = sink_input_kill_cb;
920 s->sink_input->moved = sink_input_moved_cb;
921 s->sink_input->suspend = sink_input_suspend_cb;
922 s->sink_input->userdata = s;
923
924 start_index = ssync ? pa_memblockq_get_read_index(ssync->memblockq) : 0;
925
926 fix_playback_buffer_attr_pre(s, adjust_latency, maxlength, tlength, prebuf, minreq);
927 pa_sink_input_get_silence(sink_input, &silence);
928
929 s->memblockq = pa_memblockq_new(
930 start_index,
931 *maxlength,
932 *tlength,
933 pa_frame_size(&sink_input->sample_spec),
934 *prebuf,
935 *minreq,
936 0,
937 &silence);
938
939 pa_memblock_unref(silence.memblock);
940 fix_playback_buffer_attr_post(s, maxlength, tlength, prebuf, minreq);
941
942 *missing = (uint32_t) pa_memblockq_pop_missing(s->memblockq);
943
944 *ss = s->sink_input->sample_spec;
945 *map = s->sink_input->channel_map;
946
947 pa_atomic_store(&s->missing, 0);
948 s->drain_request = FALSE;
949
950 pa_idxset_put(c->output_streams, s, &s->index);
951
952 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
953 ((double) pa_bytes_to_usec(*tlength, &sink_input->sample_spec) + (double) s->sink_latency) / PA_USEC_PER_MSEC,
954 (double) pa_bytes_to_usec(*tlength-*minreq*2, &sink_input->sample_spec) / PA_USEC_PER_MSEC,
955 (double) pa_bytes_to_usec(*minreq, &sink_input->sample_spec) / PA_USEC_PER_MSEC,
956 (double) s->sink_latency / PA_USEC_PER_MSEC);
957
958 pa_sink_input_put(s->sink_input);
959 return s;
960 }
961
962 static int connection_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
963 connection *c = CONNECTION(o);
964 connection_assert_ref(c);
965
966 if (!c->protocol)
967 return -1;
968
969 switch (code) {
970
971 case CONNECTION_MESSAGE_REVOKE:
972 pa_pstream_send_revoke(c->pstream, PA_PTR_TO_UINT(userdata));
973 break;
974
975 case CONNECTION_MESSAGE_RELEASE:
976 pa_pstream_send_release(c->pstream, PA_PTR_TO_UINT(userdata));
977 break;
978 }
979
980 return 0;
981 }
982
983 static void connection_unlink(connection *c) {
984 record_stream *r;
985 output_stream *o;
986
987 pa_assert(c);
988
989 if (!c->protocol)
990 return;
991
992 while ((r = pa_idxset_first(c->record_streams, NULL)))
993 record_stream_unlink(r);
994
995 while ((o = pa_idxset_first(c->output_streams, NULL)))
996 if (playback_stream_isinstance(o))
997 playback_stream_unlink(PLAYBACK_STREAM(o));
998 else
999 upload_stream_unlink(UPLOAD_STREAM(o));
1000
1001 if (c->subscription)
1002 pa_subscription_free(c->subscription);
1003
1004 if (c->pstream)
1005 pa_pstream_unlink(c->pstream);
1006
1007 if (c->auth_timeout_event) {
1008 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
1009 c->auth_timeout_event = NULL;
1010 }
1011
1012 pa_assert_se(pa_idxset_remove_by_data(c->protocol->connections, c, NULL) == c);
1013 c->protocol = NULL;
1014 connection_unref(c);
1015 }
1016
1017 static void connection_free(pa_object *o) {
1018 connection *c = CONNECTION(o);
1019
1020 pa_assert(c);
1021
1022 connection_unlink(c);
1023
1024 pa_idxset_free(c->record_streams, NULL, NULL);
1025 pa_idxset_free(c->output_streams, NULL, NULL);
1026
1027 pa_pdispatch_unref(c->pdispatch);
1028 pa_pstream_unref(c->pstream);
1029 pa_client_free(c->client);
1030
1031 pa_xfree(c);
1032 }
1033
1034 /* Called from thread context */
1035 static void request_bytes(playback_stream *s) {
1036 size_t m, previous_missing;
1037
1038 playback_stream_assert_ref(s);
1039
1040 m = pa_memblockq_pop_missing(s->memblockq);
1041
1042 if (m <= 0)
1043 return;
1044
1045 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
1046
1047 previous_missing = pa_atomic_add(&s->missing, m);
1048
1049 if (pa_memblockq_prebuf_active(s->memblockq) ||
1050 (previous_missing < s->minreq && previous_missing+m >= s->minreq))
1051 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
1052 }
1053
1054 static void send_memblock(connection *c) {
1055 uint32_t start;
1056 record_stream *r;
1057
1058 start = PA_IDXSET_INVALID;
1059 for (;;) {
1060 pa_memchunk chunk;
1061
1062 if (!(r = RECORD_STREAM(pa_idxset_rrobin(c->record_streams, &c->rrobin_index))))
1063 return;
1064
1065 if (start == PA_IDXSET_INVALID)
1066 start = c->rrobin_index;
1067 else if (start == c->rrobin_index)
1068 return;
1069
1070 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
1071 pa_memchunk schunk = chunk;
1072
1073 if (schunk.length > r->fragment_size)
1074 schunk.length = r->fragment_size;
1075
1076 pa_pstream_send_memblock(c->pstream, r->index, 0, PA_SEEK_RELATIVE, &schunk);
1077
1078 pa_memblockq_drop(r->memblockq, schunk.length);
1079 pa_memblock_unref(schunk.memblock);
1080
1081 return;
1082 }
1083 }
1084 }
1085
1086 static void send_playback_stream_killed(playback_stream *p) {
1087 pa_tagstruct *t;
1088 playback_stream_assert_ref(p);
1089
1090 t = pa_tagstruct_new(NULL, 0);
1091 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
1092 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
1093 pa_tagstruct_putu32(t, p->index);
1094 pa_pstream_send_tagstruct(p->connection->pstream, t);
1095 }
1096
1097 static void send_record_stream_killed(record_stream *r) {
1098 pa_tagstruct *t;
1099 record_stream_assert_ref(r);
1100
1101 t = pa_tagstruct_new(NULL, 0);
1102 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
1103 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
1104 pa_tagstruct_putu32(t, r->index);
1105 pa_pstream_send_tagstruct(r->connection->pstream, t);
1106 }
1107
1108 /*** sink input callbacks ***/
1109
1110 static void handle_seek(playback_stream *s, int64_t indexw) {
1111 playback_stream_assert_ref(s);
1112
1113 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1114
1115 if (s->sink_input->thread_info.underrun_for > 0) {
1116
1117 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1118
1119 if (pa_memblockq_is_readable(s->memblockq)) {
1120
1121 /* We just ended an underrun, let's ask the sink
1122 * for a complete rewind rewrite */
1123
1124 pa_log_debug("Requesting rewind due to end of underrun.");
1125 pa_sink_input_request_rewind(s->sink_input,
1126 s->sink_input->thread_info.underrun_for == (size_t) -1 ? 0 : s->sink_input->thread_info.underrun_for,
1127 FALSE, TRUE);
1128 }
1129
1130 } else {
1131 int64_t indexr;
1132
1133 indexr = pa_memblockq_get_read_index(s->memblockq);
1134
1135 if (indexw < indexr) {
1136 /* OK, the sink already asked for this data, so
1137 * let's have it usk us again */
1138
1139 pa_log_debug("Requesting rewind due to rewrite.");
1140 pa_sink_input_request_rewind(s->sink_input, indexr - indexw, TRUE, FALSE);
1141 }
1142 }
1143
1144 request_bytes(s);
1145 }
1146
1147 /* Called from thread context */
1148 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
1149 pa_sink_input *i = PA_SINK_INPUT(o);
1150 playback_stream *s;
1151
1152 pa_sink_input_assert_ref(i);
1153 s = PLAYBACK_STREAM(i->userdata);
1154 playback_stream_assert_ref(s);
1155
1156 switch (code) {
1157
1158 case SINK_INPUT_MESSAGE_SEEK: {
1159 int64_t windex;
1160
1161 windex = pa_memblockq_get_write_index(s->memblockq);
1162 pa_memblockq_seek(s->memblockq, offset, PA_PTR_TO_UINT(userdata));
1163
1164 handle_seek(s, windex);
1165 return 0;
1166 }
1167
1168 case SINK_INPUT_MESSAGE_POST_DATA: {
1169 int64_t windex;
1170
1171 pa_assert(chunk);
1172
1173 windex = pa_memblockq_get_write_index(s->memblockq);
1174
1175 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1176
1177 if (pa_memblockq_push_align(s->memblockq, chunk) < 0) {
1178 pa_log_warn("Failed to push data into queue");
1179 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_OVERFLOW, NULL, 0, NULL, NULL);
1180 pa_memblockq_seek(s->memblockq, chunk->length, PA_SEEK_RELATIVE);
1181 }
1182
1183 handle_seek(s, windex);
1184
1185 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1186
1187 return 0;
1188 }
1189
1190 case SINK_INPUT_MESSAGE_DRAIN:
1191 case SINK_INPUT_MESSAGE_FLUSH:
1192 case SINK_INPUT_MESSAGE_PREBUF_FORCE:
1193 case SINK_INPUT_MESSAGE_TRIGGER: {
1194
1195 int64_t windex;
1196 pa_sink_input *isync;
1197 void (*func)(pa_memblockq *bq);
1198
1199 switch (code) {
1200 case SINK_INPUT_MESSAGE_FLUSH:
1201 func = pa_memblockq_flush;
1202 break;
1203
1204 case SINK_INPUT_MESSAGE_PREBUF_FORCE:
1205 func = pa_memblockq_prebuf_force;
1206 break;
1207
1208 case SINK_INPUT_MESSAGE_DRAIN:
1209 case SINK_INPUT_MESSAGE_TRIGGER:
1210 func = pa_memblockq_prebuf_disable;
1211 break;
1212
1213 default:
1214 pa_assert_not_reached();
1215 }
1216
1217 windex = pa_memblockq_get_write_index(s->memblockq);
1218 func(s->memblockq);
1219 handle_seek(s, windex);
1220
1221 /* Do the same for all other members in the sync group */
1222 for (isync = i->sync_prev; isync; isync = isync->sync_prev) {
1223 playback_stream *ssync = PLAYBACK_STREAM(isync->userdata);
1224 windex = pa_memblockq_get_write_index(ssync->memblockq);
1225 func(ssync->memblockq);
1226 handle_seek(ssync, windex);
1227 }
1228
1229 for (isync = i->sync_next; isync; isync = isync->sync_next) {
1230 playback_stream *ssync = PLAYBACK_STREAM(isync->userdata);
1231 windex = pa_memblockq_get_write_index(ssync->memblockq);
1232 func(ssync->memblockq);
1233 handle_seek(ssync, windex);
1234 }
1235
1236 if (code == SINK_INPUT_MESSAGE_DRAIN) {
1237 if (!pa_memblockq_is_readable(s->memblockq))
1238 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK, userdata, 0, NULL, NULL);
1239 else {
1240 s->drain_tag = PA_PTR_TO_UINT(userdata);
1241 s->drain_request = TRUE;
1242 }
1243 }
1244
1245 return 0;
1246 }
1247
1248 case SINK_INPUT_MESSAGE_UPDATE_LATENCY:
1249
1250 s->read_index = pa_memblockq_get_read_index(s->memblockq);
1251 s->write_index = pa_memblockq_get_write_index(s->memblockq);
1252 s->render_memblockq_length = pa_memblockq_get_length(s->sink_input->thread_info.render_memblockq);
1253 return 0;
1254
1255 case PA_SINK_INPUT_MESSAGE_SET_STATE: {
1256 int64_t windex;
1257
1258 windex = pa_memblockq_get_write_index(s->memblockq);
1259
1260 pa_memblockq_prebuf_force(s->memblockq);
1261
1262 handle_seek(s, windex);
1263
1264 /* Fall through to the default handler */
1265 break;
1266 }
1267
1268 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
1269 pa_usec_t *r = userdata;
1270
1271 *r = pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &i->sample_spec);
1272
1273 /* Fall through, the default handler will add in the extra
1274 * latency added by the resampler */
1275 break;
1276 }
1277 }
1278
1279 return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
1280 }
1281
1282 /* Called from thread context */
1283 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
1284 playback_stream *s;
1285
1286 pa_sink_input_assert_ref(i);
1287 s = PLAYBACK_STREAM(i->userdata);
1288 playback_stream_assert_ref(s);
1289 pa_assert(chunk);
1290
1291 if (pa_memblockq_peek(s->memblockq, chunk) < 0) {
1292
1293 /* pa_log("UNDERRUN: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1294
1295 if (s->drain_request && pa_sink_input_safe_to_remove(i)) {
1296 s->drain_request = FALSE;
1297 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);
1298 } else if (i->thread_info.playing_for > 0)
1299 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_UNDERFLOW, NULL, 0, NULL, NULL);
1300
1301 /* pa_log("adding %llu bytes", (unsigned long long) nbytes); */
1302
1303 request_bytes(s);
1304
1305 return -1;
1306 }
1307
1308 /* pa_log("NOTUNDERRUN %lu", (unsigned long) chunk->length); */
1309
1310 chunk->length = PA_MIN(nbytes, chunk->length);
1311
1312 if (i->thread_info.underrun_for > 0)
1313 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_STARTED, NULL, 0, NULL, NULL);
1314
1315 pa_memblockq_drop(s->memblockq, chunk->length);
1316 request_bytes(s);
1317
1318 return 0;
1319 }
1320
1321 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
1322 playback_stream *s;
1323
1324 pa_sink_input_assert_ref(i);
1325 s = PLAYBACK_STREAM(i->userdata);
1326 playback_stream_assert_ref(s);
1327
1328 /* If we are in an underrun, then we don't rewind */
1329 if (i->thread_info.underrun_for > 0)
1330 return;
1331
1332 pa_memblockq_rewind(s->memblockq, nbytes);
1333 }
1334
1335 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
1336 playback_stream *s;
1337
1338 pa_sink_input_assert_ref(i);
1339 s = PLAYBACK_STREAM(i->userdata);
1340 playback_stream_assert_ref(s);
1341
1342 pa_memblockq_set_maxrewind(s->memblockq, nbytes);
1343 }
1344
1345 /* Called from main context */
1346 static void sink_input_kill_cb(pa_sink_input *i) {
1347 playback_stream *s;
1348
1349 pa_sink_input_assert_ref(i);
1350 s = PLAYBACK_STREAM(i->userdata);
1351 playback_stream_assert_ref(s);
1352
1353 send_playback_stream_killed(s);
1354 playback_stream_unlink(s);
1355 }
1356
1357 /* Called from main context */
1358 static void sink_input_suspend_cb(pa_sink_input *i, pa_bool_t suspend) {
1359 playback_stream *s;
1360 pa_tagstruct *t;
1361
1362 pa_sink_input_assert_ref(i);
1363 s = PLAYBACK_STREAM(i->userdata);
1364 playback_stream_assert_ref(s);
1365
1366 if (s->connection->version < 12)
1367 return;
1368
1369 t = pa_tagstruct_new(NULL, 0);
1370 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED);
1371 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
1372 pa_tagstruct_putu32(t, s->index);
1373 pa_tagstruct_put_boolean(t, suspend);
1374 pa_pstream_send_tagstruct(s->connection->pstream, t);
1375 }
1376
1377 /* Called from main context */
1378 static void sink_input_moved_cb(pa_sink_input *i) {
1379 playback_stream *s;
1380 pa_tagstruct *t;
1381 uint32_t maxlength, tlength, prebuf, minreq;
1382
1383 pa_sink_input_assert_ref(i);
1384 s = PLAYBACK_STREAM(i->userdata);
1385 playback_stream_assert_ref(s);
1386
1387 maxlength = (uint32_t) pa_memblockq_get_maxlength(s->memblockq);
1388 tlength = (uint32_t) pa_memblockq_get_tlength(s->memblockq);
1389 prebuf = (uint32_t) pa_memblockq_get_prebuf(s->memblockq);
1390 minreq = (uint32_t) pa_memblockq_get_minreq(s->memblockq);
1391
1392 fix_playback_buffer_attr_pre(s, TRUE, &maxlength, &tlength, &prebuf, &minreq);
1393 pa_memblockq_set_maxlength(s->memblockq, maxlength);
1394 pa_memblockq_set_tlength(s->memblockq, tlength);
1395 pa_memblockq_set_prebuf(s->memblockq, prebuf);
1396 pa_memblockq_set_minreq(s->memblockq, minreq);
1397 fix_playback_buffer_attr_post(s, &maxlength, &tlength, &prebuf, &minreq);
1398
1399 if (s->connection->version < 12)
1400 return;
1401
1402 t = pa_tagstruct_new(NULL, 0);
1403 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_MOVED);
1404 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
1405 pa_tagstruct_putu32(t, s->index);
1406 pa_tagstruct_putu32(t, i->sink->index);
1407 pa_tagstruct_puts(t, i->sink->name);
1408 pa_tagstruct_put_boolean(t, pa_sink_get_state(i->sink) == PA_SINK_SUSPENDED);
1409
1410 if (s->connection->version >= 13) {
1411 pa_tagstruct_putu32(t, maxlength);
1412 pa_tagstruct_putu32(t, tlength);
1413 pa_tagstruct_putu32(t, prebuf);
1414 pa_tagstruct_putu32(t, minreq);
1415 pa_tagstruct_put_usec(t, s->sink_latency);
1416 }
1417
1418 pa_pstream_send_tagstruct(s->connection->pstream, t);
1419 }
1420
1421 /*** source_output callbacks ***/
1422
1423 /* Called from thread context */
1424 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
1425 record_stream *s;
1426
1427 pa_source_output_assert_ref(o);
1428 s = RECORD_STREAM(o->userdata);
1429 record_stream_assert_ref(s);
1430 pa_assert(chunk);
1431
1432 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), RECORD_STREAM_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
1433 }
1434
1435 static void source_output_kill_cb(pa_source_output *o) {
1436 record_stream *s;
1437
1438 pa_source_output_assert_ref(o);
1439 s = RECORD_STREAM(o->userdata);
1440 record_stream_assert_ref(s);
1441
1442 send_record_stream_killed(s);
1443 record_stream_unlink(s);
1444 }
1445
1446 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
1447 record_stream *s;
1448
1449 pa_source_output_assert_ref(o);
1450 s = RECORD_STREAM(o->userdata);
1451 record_stream_assert_ref(s);
1452
1453 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1454
1455 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &o->sample_spec);
1456 }
1457
1458 /* Called from main context */
1459 static void source_output_suspend_cb(pa_source_output *o, pa_bool_t suspend) {
1460 record_stream *s;
1461 pa_tagstruct *t;
1462
1463 pa_source_output_assert_ref(o);
1464 s = RECORD_STREAM(o->userdata);
1465 record_stream_assert_ref(s);
1466
1467 if (s->connection->version < 12)
1468 return;
1469
1470 t = pa_tagstruct_new(NULL, 0);
1471 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_SUSPENDED);
1472 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
1473 pa_tagstruct_putu32(t, s->index);
1474 pa_tagstruct_put_boolean(t, suspend);
1475 pa_pstream_send_tagstruct(s->connection->pstream, t);
1476 }
1477
1478 /* Called from main context */
1479 static void source_output_moved_cb(pa_source_output *o) {
1480 record_stream *s;
1481 pa_tagstruct *t;
1482 uint32_t maxlength, fragsize;
1483
1484 pa_source_output_assert_ref(o);
1485 s = RECORD_STREAM(o->userdata);
1486 record_stream_assert_ref(s);
1487
1488 fragsize = (uint32_t) s->fragment_size;
1489 maxlength = (uint32_t) pa_memblockq_get_length(s->memblockq);
1490
1491 fix_record_buffer_attr_pre(s, TRUE, &maxlength, &fragsize);
1492 pa_memblockq_set_maxlength(s->memblockq, maxlength);
1493 fix_record_buffer_attr_post(s, &maxlength, &fragsize);
1494
1495 if (s->connection->version < 12)
1496 return;
1497
1498 t = pa_tagstruct_new(NULL, 0);
1499 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_MOVED);
1500 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
1501 pa_tagstruct_putu32(t, s->index);
1502 pa_tagstruct_putu32(t, o->source->index);
1503 pa_tagstruct_puts(t, o->source->name);
1504 pa_tagstruct_put_boolean(t, pa_source_get_state(o->source) == PA_SOURCE_SUSPENDED);
1505
1506 if (s->connection->version >= 13) {
1507 pa_tagstruct_putu32(t, maxlength);
1508 pa_tagstruct_putu32(t, fragsize);
1509 pa_tagstruct_put_usec(t, s->source_latency);
1510 }
1511
1512 pa_pstream_send_tagstruct(s->connection->pstream, t);
1513 }
1514
1515 /*** pdispatch callbacks ***/
1516
1517 static void protocol_error(connection *c) {
1518 pa_log("protocol error, kicking client");
1519 connection_unlink(c);
1520 }
1521
1522 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1523 if (!(expression)) { \
1524 pa_pstream_send_error((pstream), (tag), (error)); \
1525 return; \
1526 } \
1527 } while(0);
1528
1529 static pa_tagstruct *reply_new(uint32_t tag) {
1530 pa_tagstruct *reply;
1531
1532 reply = pa_tagstruct_new(NULL, 0);
1533 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1534 pa_tagstruct_putu32(reply, tag);
1535 return reply;
1536 }
1537
1538 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) {
1539 connection *c = CONNECTION(userdata);
1540 playback_stream *s;
1541 uint32_t maxlength, tlength, prebuf, minreq, sink_index, syncid, missing;
1542 const char *name = NULL, *sink_name;
1543 pa_sample_spec ss;
1544 pa_channel_map map;
1545 pa_tagstruct *reply;
1546 pa_sink *sink = NULL;
1547 pa_cvolume volume;
1548 pa_bool_t
1549 corked = FALSE,
1550 no_remap = FALSE,
1551 no_remix = FALSE,
1552 fix_format = FALSE,
1553 fix_rate = FALSE,
1554 fix_channels = FALSE,
1555 no_move = FALSE,
1556 variable_rate = FALSE,
1557 muted = FALSE,
1558 adjust_latency = FALSE;
1559
1560 pa_sink_input_flags_t flags = 0;
1561 pa_proplist *p;
1562
1563 connection_assert_ref(c);
1564 pa_assert(t);
1565
1566 if ((c->version < 13 && (pa_tagstruct_gets(t, &name) < 0 || !name)) ||
1567 pa_tagstruct_get(
1568 t,
1569 PA_TAG_SAMPLE_SPEC, &ss,
1570 PA_TAG_CHANNEL_MAP, &map,
1571 PA_TAG_U32, &sink_index,
1572 PA_TAG_STRING, &sink_name,
1573 PA_TAG_U32, &maxlength,
1574 PA_TAG_BOOLEAN, &corked,
1575 PA_TAG_U32, &tlength,
1576 PA_TAG_U32, &prebuf,
1577 PA_TAG_U32, &minreq,
1578 PA_TAG_U32, &syncid,
1579 PA_TAG_CVOLUME, &volume,
1580 PA_TAG_INVALID) < 0) {
1581
1582 protocol_error(c);
1583 return;
1584 }
1585
1586 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1587 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(sink_name)), tag, PA_ERR_INVALID);
1588 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1589 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1590 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
1591 CHECK_VALIDITY(c->pstream, map.channels == ss.channels && volume.channels == ss.channels, tag, PA_ERR_INVALID);
1592
1593 p = pa_proplist_new();
1594
1595 if (name)
1596 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
1597
1598 if (c->version >= 12) {
1599 /* Since 0.9.8 the user can ask for a couple of additional flags */
1600
1601 if (pa_tagstruct_get_boolean(t, &no_remap) < 0 ||
1602 pa_tagstruct_get_boolean(t, &no_remix) < 0 ||
1603 pa_tagstruct_get_boolean(t, &fix_format) < 0 ||
1604 pa_tagstruct_get_boolean(t, &fix_rate) < 0 ||
1605 pa_tagstruct_get_boolean(t, &fix_channels) < 0 ||
1606 pa_tagstruct_get_boolean(t, &no_move) < 0 ||
1607 pa_tagstruct_get_boolean(t, &variable_rate) < 0) {
1608
1609 protocol_error(c);
1610 pa_proplist_free(p);
1611 return;
1612 }
1613 }
1614
1615 if (c->version >= 13) {
1616
1617 if (pa_tagstruct_get_boolean(t, &muted) < 0 ||
1618 pa_tagstruct_get_boolean(t, &adjust_latency) < 0 ||
1619 pa_tagstruct_get_proplist(t, p) < 0) {
1620 protocol_error(c);
1621 pa_proplist_free(p);
1622 return;
1623 }
1624 }
1625
1626 if (!pa_tagstruct_eof(t)) {
1627 protocol_error(c);
1628 pa_proplist_free(p);
1629 return;
1630 }
1631
1632 if (sink_index != PA_INVALID_INDEX) {
1633
1634 if (!(sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index))) {
1635 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1636 pa_proplist_free(p);
1637 return;
1638 }
1639
1640 } else if (sink_name) {
1641
1642 if (!(sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1))) {
1643 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1644 pa_proplist_free(p);
1645 return;
1646 }
1647 }
1648
1649 flags =
1650 (corked ? PA_SINK_INPUT_START_CORKED : 0) |
1651 (no_remap ? PA_SINK_INPUT_NO_REMAP : 0) |
1652 (no_remix ? PA_SINK_INPUT_NO_REMIX : 0) |
1653 (fix_format ? PA_SINK_INPUT_FIX_FORMAT : 0) |
1654 (fix_rate ? PA_SINK_INPUT_FIX_RATE : 0) |
1655 (fix_channels ? PA_SINK_INPUT_FIX_CHANNELS : 0) |
1656 (no_move ? PA_SINK_INPUT_DONT_MOVE : 0) |
1657 (variable_rate ? PA_SINK_INPUT_VARIABLE_RATE : 0);
1658
1659 s = playback_stream_new(c, sink, &ss, &map, &maxlength, &tlength, &prebuf, &minreq, &volume, muted, syncid, &missing, flags, p, adjust_latency);
1660 pa_proplist_free(p);
1661
1662 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1663
1664 reply = reply_new(tag);
1665 pa_tagstruct_putu32(reply, s->index);
1666 pa_assert(s->sink_input);
1667 pa_tagstruct_putu32(reply, s->sink_input->index);
1668 pa_tagstruct_putu32(reply, missing);
1669
1670 /* pa_log("initial request is %u", missing); */
1671
1672 if (c->version >= 9) {
1673 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1674
1675 pa_tagstruct_putu32(reply, (uint32_t) maxlength);
1676 pa_tagstruct_putu32(reply, (uint32_t) tlength);
1677 pa_tagstruct_putu32(reply, (uint32_t) prebuf);
1678 pa_tagstruct_putu32(reply, (uint32_t) minreq);
1679 }
1680
1681 if (c->version >= 12) {
1682 /* Since 0.9.8 we support sending the chosen sample
1683 * spec/channel map/device/suspend status back to the
1684 * client */
1685
1686 pa_tagstruct_put_sample_spec(reply, &ss);
1687 pa_tagstruct_put_channel_map(reply, &map);
1688
1689 pa_tagstruct_putu32(reply, s->sink_input->sink->index);
1690 pa_tagstruct_puts(reply, s->sink_input->sink->name);
1691
1692 pa_tagstruct_put_boolean(reply, pa_sink_get_state(s->sink_input->sink) == PA_SINK_SUSPENDED);
1693 }
1694
1695 if (c->version >= 13)
1696 pa_tagstruct_put_usec(reply, s->sink_latency);
1697
1698 pa_pstream_send_tagstruct(c->pstream, reply);
1699 }
1700
1701 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1702 connection *c = CONNECTION(userdata);
1703 uint32_t channel;
1704
1705 connection_assert_ref(c);
1706 pa_assert(t);
1707
1708 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1709 !pa_tagstruct_eof(t)) {
1710 protocol_error(c);
1711 return;
1712 }
1713
1714 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1715
1716 switch (command) {
1717
1718 case PA_COMMAND_DELETE_PLAYBACK_STREAM: {
1719 playback_stream *s;
1720 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || !playback_stream_isinstance(s)) {
1721 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1722 return;
1723 }
1724
1725 playback_stream_unlink(s);
1726 break;
1727 }
1728
1729 case PA_COMMAND_DELETE_RECORD_STREAM: {
1730 record_stream *s;
1731 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
1732 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1733 return;
1734 }
1735
1736 record_stream_unlink(s);
1737 break;
1738 }
1739
1740 case PA_COMMAND_DELETE_UPLOAD_STREAM: {
1741 upload_stream *s;
1742
1743 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || !upload_stream_isinstance(s)) {
1744 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1745 return;
1746 }
1747
1748 upload_stream_unlink(s);
1749 break;
1750 }
1751
1752 default:
1753 pa_assert_not_reached();
1754 }
1755
1756 pa_pstream_send_simple_ack(c->pstream, tag);
1757 }
1758
1759 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) {
1760 connection *c = CONNECTION(userdata);
1761 record_stream *s;
1762 uint32_t maxlength, fragment_size;
1763 uint32_t source_index;
1764 const char *name = NULL, *source_name;
1765 pa_sample_spec ss;
1766 pa_channel_map map;
1767 pa_tagstruct *reply;
1768 pa_source *source = NULL;
1769 pa_bool_t
1770 corked = FALSE,
1771 no_remap = FALSE,
1772 no_remix = FALSE,
1773 fix_format = FALSE,
1774 fix_rate = FALSE,
1775 fix_channels = FALSE,
1776 no_move = FALSE,
1777 variable_rate = FALSE,
1778 adjust_latency = FALSE,
1779 peak_detect = FALSE;
1780 pa_source_output_flags_t flags = 0;
1781 pa_proplist *p;
1782 uint32_t direct_on_input_idx = PA_INVALID_INDEX;
1783 pa_sink_input *direct_on_input = NULL;
1784
1785 connection_assert_ref(c);
1786 pa_assert(t);
1787
1788 if ((c->version < 13 && (pa_tagstruct_gets(t, &name) < 0 || !name)) ||
1789 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
1790 pa_tagstruct_get_channel_map(t, &map) < 0 ||
1791 pa_tagstruct_getu32(t, &source_index) < 0 ||
1792 pa_tagstruct_gets(t, &source_name) < 0 ||
1793 pa_tagstruct_getu32(t, &maxlength) < 0 ||
1794 pa_tagstruct_get_boolean(t, &corked) < 0 ||
1795 pa_tagstruct_getu32(t, &fragment_size) < 0) {
1796 protocol_error(c);
1797 return;
1798 }
1799
1800 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1801 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1802 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1803 CHECK_VALIDITY(c->pstream, source_index != PA_INVALID_INDEX || !source_name || (*source_name && pa_utf8_valid(source_name)), tag, PA_ERR_INVALID);
1804 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
1805
1806 p = pa_proplist_new();
1807
1808 if (name)
1809 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
1810
1811 if (c->version >= 12) {
1812 /* Since 0.9.8 the user can ask for a couple of additional flags */
1813
1814 if (pa_tagstruct_get_boolean(t, &no_remap) < 0 ||
1815 pa_tagstruct_get_boolean(t, &no_remix) < 0 ||
1816 pa_tagstruct_get_boolean(t, &fix_format) < 0 ||
1817 pa_tagstruct_get_boolean(t, &fix_rate) < 0 ||
1818 pa_tagstruct_get_boolean(t, &fix_channels) < 0 ||
1819 pa_tagstruct_get_boolean(t, &no_move) < 0 ||
1820 pa_tagstruct_get_boolean(t, &variable_rate) < 0) {
1821
1822 protocol_error(c);
1823 pa_proplist_free(p);
1824 return;
1825 }
1826 }
1827
1828 if (c->version >= 13) {
1829
1830 if (pa_tagstruct_get_boolean(t, &peak_detect) < 0 ||
1831 pa_tagstruct_get_boolean(t, &adjust_latency) < 0 ||
1832 pa_tagstruct_get_proplist(t, p) < 0 ||
1833 pa_tagstruct_getu32(t, &direct_on_input_idx) < 0) {
1834 protocol_error(c);
1835 pa_proplist_free(p);
1836 return;
1837 }
1838 }
1839
1840 if (!pa_tagstruct_eof(t)) {
1841 protocol_error(c);
1842 pa_proplist_free(p);
1843 return;
1844 }
1845
1846 if (source_index != PA_INVALID_INDEX) {
1847
1848 if (!(source = pa_idxset_get_by_index(c->protocol->core->sources, source_index))) {
1849 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1850 pa_proplist_free(p);
1851 return;
1852 }
1853
1854 } else if (source_name) {
1855
1856 if (!(source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1))) {
1857 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1858 pa_proplist_free(p);
1859 return;
1860 }
1861 }
1862
1863 if (direct_on_input_idx != PA_INVALID_INDEX) {
1864
1865 if (!(direct_on_input = pa_idxset_get_by_index(c->protocol->core->sink_inputs, direct_on_input_idx))) {
1866 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1867 pa_proplist_free(p);
1868 return;
1869 }
1870 }
1871
1872 flags =
1873 (corked ? PA_SOURCE_OUTPUT_START_CORKED : 0) |
1874 (no_remap ? PA_SOURCE_OUTPUT_NO_REMAP : 0) |
1875 (no_remix ? PA_SOURCE_OUTPUT_NO_REMIX : 0) |
1876 (fix_format ? PA_SOURCE_OUTPUT_FIX_FORMAT : 0) |
1877 (fix_rate ? PA_SOURCE_OUTPUT_FIX_RATE : 0) |
1878 (fix_channels ? PA_SOURCE_OUTPUT_FIX_CHANNELS : 0) |
1879 (no_move ? PA_SOURCE_OUTPUT_DONT_MOVE : 0) |
1880 (variable_rate ? PA_SOURCE_OUTPUT_VARIABLE_RATE : 0);
1881
1882 s = record_stream_new(c, source, &ss, &map, peak_detect, &maxlength, &fragment_size, flags, p, adjust_latency, direct_on_input);
1883 pa_proplist_free(p);
1884
1885 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1886
1887 reply = reply_new(tag);
1888 pa_tagstruct_putu32(reply, s->index);
1889 pa_assert(s->source_output);
1890 pa_tagstruct_putu32(reply, s->source_output->index);
1891
1892 if (c->version >= 9) {
1893 /* Since 0.9 we support sending the buffer metrics back to the client */
1894
1895 pa_tagstruct_putu32(reply, (uint32_t) maxlength);
1896 pa_tagstruct_putu32(reply, (uint32_t) fragment_size);
1897 }
1898
1899 if (c->version >= 12) {
1900 /* Since 0.9.8 we support sending the chosen sample
1901 * spec/channel map/device/suspend status back to the
1902 * client */
1903
1904 pa_tagstruct_put_sample_spec(reply, &ss);
1905 pa_tagstruct_put_channel_map(reply, &map);
1906
1907 pa_tagstruct_putu32(reply, s->source_output->source->index);
1908 pa_tagstruct_puts(reply, s->source_output->source->name);
1909
1910 pa_tagstruct_put_boolean(reply, pa_source_get_state(s->source_output->source) == PA_SOURCE_SUSPENDED);
1911 }
1912
1913 if (c->version >= 13)
1914 pa_tagstruct_put_usec(reply, s->source_latency);
1915
1916 pa_pstream_send_tagstruct(c->pstream, reply);
1917 }
1918
1919 static void command_exit(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1920 connection *c = CONNECTION(userdata);
1921
1922 connection_assert_ref(c);
1923 pa_assert(t);
1924
1925 if (!pa_tagstruct_eof(t)) {
1926 protocol_error(c);
1927 return;
1928 }
1929
1930 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1931
1932 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
1933 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
1934 }
1935
1936 static void command_auth(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1937 connection *c = CONNECTION(userdata);
1938 const void*cookie;
1939 pa_tagstruct *reply;
1940 pa_bool_t shm_on_remote, do_shm;
1941
1942 connection_assert_ref(c);
1943 pa_assert(t);
1944
1945 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
1946 pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
1947 !pa_tagstruct_eof(t)) {
1948 protocol_error(c);
1949 return;
1950 }
1951
1952 /* Minimum supported version */
1953 if (c->version < 8) {
1954 pa_pstream_send_error(c->pstream, tag, PA_ERR_VERSION);
1955 return;
1956 }
1957
1958 /* Starting with protocol version 13 the MSB of the version tag
1959 reflects if shm is available for this connection or
1960 not. */
1961 if (c->version >= 13) {
1962 shm_on_remote = !!(c->version & 0x80000000U);
1963 c->version &= 0x7FFFFFFFU;
1964 }
1965
1966 pa_log_debug("Protocol version: remote %u, local %u", c->version, PA_PROTOCOL_VERSION);
1967
1968 pa_proplist_setf(c->client->proplist, "native-protocol.version", "%u", c->version);
1969
1970 if (!c->authorized) {
1971 pa_bool_t success = FALSE;
1972
1973 #ifdef HAVE_CREDS
1974 const pa_creds *creds;
1975
1976 if ((creds = pa_pdispatch_creds(pd))) {
1977 if (creds->uid == getuid())
1978 success = TRUE;
1979 else if (c->protocol->auth_group) {
1980 int r;
1981 gid_t gid;
1982
1983 if ((gid = pa_get_gid_of_group(c->protocol->auth_group)) == (gid_t) -1)
1984 pa_log_warn("Failed to get GID of group '%s'", c->protocol->auth_group);
1985 else if (gid == creds->gid)
1986 success = TRUE;
1987
1988 if (!success) {
1989 if ((r = pa_uid_in_group(creds->uid, c->protocol->auth_group)) < 0)
1990 pa_log_warn("Failed to check group membership.");
1991 else if (r > 0)
1992 success = TRUE;
1993 }
1994 }
1995
1996 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
1997 (unsigned long) creds->uid,
1998 (unsigned long) creds->gid,
1999 (int) success);
2000 }
2001 #endif
2002
2003 if (!success && memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) == 0)
2004 success = TRUE;
2005
2006 if (!success) {
2007 pa_log_warn("Denied access to client with invalid authorization data.");
2008 pa_pstream_send_error(c->pstream, tag, PA_ERR_ACCESS);
2009 return;
2010 }
2011
2012 c->authorized = TRUE;
2013 if (c->auth_timeout_event) {
2014 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
2015 c->auth_timeout_event = NULL;
2016 }
2017 }
2018
2019 /* Enable shared memory support if possible */
2020 do_shm =
2021 pa_mempool_is_shared(c->protocol->core->mempool) &&
2022 c->is_local;
2023
2024 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm));
2025
2026 if (do_shm)
2027 if (c->version < 10 || (c->version >= 13 && !shm_on_remote))
2028 do_shm = FALSE;
2029
2030 if (do_shm) {
2031 /* Only enable SHM if both sides are owned by the same
2032 * user. This is a security measure because otherwise data
2033 * private to the user might leak. */
2034
2035 const pa_creds *creds;
2036 if (!(creds = pa_pdispatch_creds(pd)) || getuid() != creds->uid)
2037 do_shm = FALSE;
2038 }
2039
2040 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm));
2041 pa_pstream_enable_shm(c->pstream, do_shm);
2042
2043 reply = reply_new(tag);
2044 pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION | (do_shm ? 0x80000000 : 0));
2045
2046 #ifdef HAVE_CREDS
2047 {
2048 /* SHM support is only enabled after both sides made sure they are the same user. */
2049
2050 pa_creds ucred;
2051
2052 ucred.uid = getuid();
2053 ucred.gid = getgid();
2054
2055 pa_pstream_send_tagstruct_with_creds(c->pstream, reply, &ucred);
2056 }
2057 #else
2058 pa_pstream_send_tagstruct(c->pstream, reply);
2059 #endif
2060 }
2061
2062 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) {
2063 connection *c = CONNECTION(userdata);
2064 const char *name = NULL;
2065 pa_proplist *p;
2066 pa_tagstruct *reply;
2067
2068 connection_assert_ref(c);
2069 pa_assert(t);
2070
2071 p = pa_proplist_new();
2072
2073 if ((c->version < 13 && pa_tagstruct_gets(t, &name) < 0) ||
2074 (c->version >= 13 && pa_tagstruct_get_proplist(t, p) < 0) ||
2075 !pa_tagstruct_eof(t)) {
2076
2077 protocol_error(c);
2078 pa_proplist_free(p);
2079 return;
2080 }
2081
2082 if (name)
2083 if (pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, name) < 0) {
2084 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
2085 pa_proplist_free(p);
2086 return;
2087 }
2088
2089 pa_proplist_update(c->client->proplist, PA_UPDATE_REPLACE, p);
2090 pa_proplist_free(p);
2091
2092 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
2093
2094 reply = reply_new(tag);
2095
2096 if (c->version >= 13)
2097 pa_tagstruct_putu32(reply, c->client->index);
2098
2099 pa_pstream_send_tagstruct(c->pstream, reply);
2100 }
2101
2102 static void command_lookup(PA_GCC_UNUSED pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2103 connection *c = CONNECTION(userdata);
2104 const char *name;
2105 uint32_t idx = PA_IDXSET_INVALID;
2106
2107 connection_assert_ref(c);
2108 pa_assert(t);
2109
2110 if (pa_tagstruct_gets(t, &name) < 0 ||
2111 !pa_tagstruct_eof(t)) {
2112 protocol_error(c);
2113 return;
2114 }
2115
2116 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2117 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
2118
2119 if (command == PA_COMMAND_LOOKUP_SINK) {
2120 pa_sink *sink;
2121 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
2122 idx = sink->index;
2123 } else {
2124 pa_source *source;
2125 pa_assert(command == PA_COMMAND_LOOKUP_SOURCE);
2126 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
2127 idx = source->index;
2128 }
2129
2130 if (idx == PA_IDXSET_INVALID)
2131 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2132 else {
2133 pa_tagstruct *reply;
2134 reply = reply_new(tag);
2135 pa_tagstruct_putu32(reply, idx);
2136 pa_pstream_send_tagstruct(c->pstream, reply);
2137 }
2138 }
2139
2140 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) {
2141 connection *c = CONNECTION(userdata);
2142 uint32_t idx;
2143 playback_stream *s;
2144
2145 connection_assert_ref(c);
2146 pa_assert(t);
2147
2148 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2149 !pa_tagstruct_eof(t)) {
2150 protocol_error(c);
2151 return;
2152 }
2153
2154 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2155 s = pa_idxset_get_by_index(c->output_streams, idx);
2156 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2157 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2158
2159 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);
2160 }
2161
2162 static void command_stat(PA_GCC_UNUSED pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2163 connection *c = CONNECTION(userdata);
2164 pa_tagstruct *reply;
2165 const pa_mempool_stat *stat;
2166
2167 connection_assert_ref(c);
2168 pa_assert(t);
2169
2170 if (!pa_tagstruct_eof(t)) {
2171 protocol_error(c);
2172 return;
2173 }
2174
2175 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2176
2177 stat = pa_mempool_get_stat(c->protocol->core->mempool);
2178
2179 reply = reply_new(tag);
2180 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->n_allocated));
2181 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->allocated_size));
2182 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->n_accumulated));
2183 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->accumulated_size));
2184 pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
2185 pa_pstream_send_tagstruct(c->pstream, reply);
2186 }
2187
2188 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) {
2189 connection *c = CONNECTION(userdata);
2190 pa_tagstruct *reply;
2191 playback_stream *s;
2192 struct timeval tv, now;
2193 uint32_t idx;
2194 pa_usec_t latency;
2195
2196 connection_assert_ref(c);
2197 pa_assert(t);
2198
2199 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2200 pa_tagstruct_get_timeval(t, &tv) < 0 ||
2201 !pa_tagstruct_eof(t)) {
2202 protocol_error(c);
2203 return;
2204 }
2205
2206 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2207 s = pa_idxset_get_by_index(c->output_streams, idx);
2208 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2209 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2210 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)
2211
2212 reply = reply_new(tag);
2213
2214 latency = pa_sink_get_latency(s->sink_input->sink);
2215 latency += pa_bytes_to_usec(s->render_memblockq_length, &s->sink_input->sample_spec);
2216
2217 pa_tagstruct_put_usec(reply, latency);
2218
2219 pa_tagstruct_put_usec(reply, 0);
2220 pa_tagstruct_put_boolean(reply, s->sink_input->thread_info.playing_for > 0);
2221 pa_tagstruct_put_timeval(reply, &tv);
2222 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
2223 pa_tagstruct_puts64(reply, s->write_index);
2224 pa_tagstruct_puts64(reply, s->read_index);
2225
2226 if (c->version >= 13) {
2227 pa_tagstruct_putu64(reply, s->sink_input->thread_info.underrun_for);
2228 pa_tagstruct_putu64(reply, s->sink_input->thread_info.playing_for);
2229 }
2230
2231 pa_pstream_send_tagstruct(c->pstream, reply);
2232 }
2233
2234 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) {
2235 connection *c = CONNECTION(userdata);
2236 pa_tagstruct *reply;
2237 record_stream *s;
2238 struct timeval tv, now;
2239 uint32_t idx;
2240
2241 connection_assert_ref(c);
2242 pa_assert(t);
2243
2244 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2245 pa_tagstruct_get_timeval(t, &tv) < 0 ||
2246 !pa_tagstruct_eof(t)) {
2247 protocol_error(c);
2248 return;
2249 }
2250
2251 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2252 s = pa_idxset_get_by_index(c->record_streams, idx);
2253 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2254
2255 reply = reply_new(tag);
2256 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
2257 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
2258 pa_tagstruct_put_boolean(reply, pa_source_get_state(s->source_output->source) == PA_SOURCE_RUNNING);
2259 pa_tagstruct_put_timeval(reply, &tv);
2260 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
2261 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
2262 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
2263 pa_pstream_send_tagstruct(c->pstream, reply);
2264 }
2265
2266 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) {
2267 connection *c = CONNECTION(userdata);
2268 upload_stream *s;
2269 uint32_t length;
2270 const char *name = NULL;
2271 pa_sample_spec ss;
2272 pa_channel_map map;
2273 pa_tagstruct *reply;
2274 pa_proplist *p;
2275
2276 connection_assert_ref(c);
2277 pa_assert(t);
2278
2279 if (pa_tagstruct_gets(t, &name) < 0 ||
2280 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
2281 pa_tagstruct_get_channel_map(t, &map) < 0 ||
2282 pa_tagstruct_getu32(t, &length) < 0) {
2283 protocol_error(c);
2284 return;
2285 }
2286
2287 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2288 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
2289 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
2290 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
2291 CHECK_VALIDITY(c->pstream, (length % pa_frame_size(&ss)) == 0 && length > 0, tag, PA_ERR_INVALID);
2292 CHECK_VALIDITY(c->pstream, length <= PA_SCACHE_ENTRY_SIZE_MAX, tag, PA_ERR_TOOLARGE);
2293
2294 p = pa_proplist_new();
2295
2296 if (c->version >= 13 && pa_tagstruct_get_proplist(t, p) < 0) {
2297 protocol_error(c);
2298 pa_proplist_free(p);
2299 return;
2300 }
2301
2302 if (c->version < 13)
2303 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
2304 else if (!name)
2305 if (!(name = pa_proplist_gets(p, PA_PROP_EVENT_ID)))
2306 name = pa_proplist_gets(p, PA_PROP_MEDIA_NAME);
2307
2308 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
2309
2310 s = upload_stream_new(c, &ss, &map, name, length, p);
2311 pa_proplist_free(p);
2312
2313 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
2314
2315 reply = reply_new(tag);
2316 pa_tagstruct_putu32(reply, s->index);
2317 pa_tagstruct_putu32(reply, length);
2318 pa_pstream_send_tagstruct(c->pstream, reply);
2319 }
2320
2321 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) {
2322 connection *c = CONNECTION(userdata);
2323 uint32_t channel;
2324 upload_stream *s;
2325 uint32_t idx;
2326
2327 connection_assert_ref(c);
2328 pa_assert(t);
2329
2330 if (pa_tagstruct_getu32(t, &channel) < 0 ||
2331 !pa_tagstruct_eof(t)) {
2332 protocol_error(c);
2333 return;
2334 }
2335
2336 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2337
2338 s = pa_idxset_get_by_index(c->output_streams, channel);
2339 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2340 CHECK_VALIDITY(c->pstream, upload_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2341
2342 if (pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->channel_map, &s->memchunk, s->proplist, &idx) < 0)
2343 pa_pstream_send_error(c->pstream, tag, PA_ERR_INTERNAL);
2344 else
2345 pa_pstream_send_simple_ack(c->pstream, tag);
2346
2347 upload_stream_unlink(s);
2348 }
2349
2350 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) {
2351 connection *c = CONNECTION(userdata);
2352 uint32_t sink_index;
2353 pa_volume_t volume;
2354 pa_sink *sink;
2355 const char *name, *sink_name;
2356 uint32_t idx;
2357 pa_proplist *p;
2358 pa_tagstruct *reply;
2359
2360 connection_assert_ref(c);
2361 pa_assert(t);
2362
2363 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2364
2365 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
2366 pa_tagstruct_gets(t, &sink_name) < 0 ||
2367 pa_tagstruct_getu32(t, &volume) < 0 ||
2368 pa_tagstruct_gets(t, &name) < 0) {
2369 protocol_error(c);
2370 return;
2371 }
2372
2373 CHECK_VALIDITY(c->pstream, sink_index != PA_INVALID_INDEX || !sink_name || (*sink_name && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
2374 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
2375
2376 if (sink_index != PA_INVALID_INDEX)
2377 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
2378 else
2379 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
2380
2381 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
2382
2383 p = pa_proplist_new();
2384
2385 if ((c->version >= 13 && pa_tagstruct_get_proplist(t, p) < 0) ||
2386 !pa_tagstruct_eof(t)) {
2387 protocol_error(c);
2388 pa_proplist_free(p);
2389 return;
2390 }
2391
2392 if (pa_scache_play_item(c->protocol->core, name, sink, volume, p, &idx) < 0) {
2393 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2394 pa_proplist_free(p);
2395 return;
2396 }
2397
2398 pa_proplist_free(p);
2399
2400 reply = reply_new(tag);
2401
2402 if (c->version >= 13)
2403 pa_tagstruct_putu32(reply, idx);
2404
2405 pa_pstream_send_tagstruct(c->pstream, reply);
2406 }
2407
2408 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) {
2409 connection *c = CONNECTION(userdata);
2410 const char *name;
2411
2412 connection_assert_ref(c);
2413 pa_assert(t);
2414
2415 if (pa_tagstruct_gets(t, &name) < 0 ||
2416 !pa_tagstruct_eof(t)) {
2417 protocol_error(c);
2418 return;
2419 }
2420
2421 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2422 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
2423
2424 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
2425 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2426 return;
2427 }
2428
2429 pa_pstream_send_simple_ack(c->pstream, tag);
2430 }
2431
2432 static void fixup_sample_spec(connection *c, pa_sample_spec *fixed, const pa_sample_spec *original) {
2433 pa_assert(c);
2434 pa_assert(fixed);
2435 pa_assert(original);
2436
2437 *fixed = *original;
2438
2439 if (c->version < 12) {
2440 /* Before protocol version 12 we didn't support S32 samples,
2441 * so we need to lie about this to the client */
2442
2443 if (fixed->format == PA_SAMPLE_S32LE)
2444 fixed->format = PA_SAMPLE_FLOAT32LE;
2445 if (fixed->format == PA_SAMPLE_S32BE)
2446 fixed->format = PA_SAMPLE_FLOAT32BE;
2447 }
2448 }
2449
2450 static void sink_fill_tagstruct(connection *c, pa_tagstruct *t, pa_sink *sink) {
2451 pa_sample_spec fixed_ss;
2452
2453 pa_assert(t);
2454 pa_sink_assert_ref(sink);
2455
2456 fixup_sample_spec(c, &fixed_ss, &sink->sample_spec);
2457
2458 pa_tagstruct_put(
2459 t,
2460 PA_TAG_U32, sink->index,
2461 PA_TAG_STRING, sink->name,
2462 PA_TAG_STRING, pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION)),
2463 PA_TAG_SAMPLE_SPEC, &fixed_ss,
2464 PA_TAG_CHANNEL_MAP, &sink->channel_map,
2465 PA_TAG_U32, sink->module ? sink->module->index : PA_INVALID_INDEX,
2466 PA_TAG_CVOLUME, pa_sink_get_volume(sink),
2467 PA_TAG_BOOLEAN, pa_sink_get_mute(sink),
2468 PA_TAG_U32, sink->monitor_source ? sink->monitor_source->index : PA_INVALID_INDEX,
2469 PA_TAG_STRING, sink->monitor_source ? sink->monitor_source->name : NULL,
2470 PA_TAG_USEC, pa_sink_get_latency(sink),
2471 PA_TAG_STRING, sink->driver,
2472 PA_TAG_U32, sink->flags,
2473 PA_TAG_INVALID);
2474
2475 if (c->version >= 13) {
2476 pa_tagstruct_put_proplist(t, sink->proplist);
2477 pa_tagstruct_put_usec(t, pa_sink_get_requested_latency(sink));
2478 }
2479 }
2480
2481 static void source_fill_tagstruct(connection *c, pa_tagstruct *t, pa_source *source) {
2482 pa_sample_spec fixed_ss;
2483
2484 pa_assert(t);
2485 pa_source_assert_ref(source);
2486
2487 fixup_sample_spec(c, &fixed_ss, &source->sample_spec);
2488
2489 pa_tagstruct_put(
2490 t,
2491 PA_TAG_U32, source->index,
2492 PA_TAG_STRING, source->name,
2493 PA_TAG_STRING, pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION)),
2494 PA_TAG_SAMPLE_SPEC, &fixed_ss,
2495 PA_TAG_CHANNEL_MAP, &source->channel_map,
2496 PA_TAG_U32, source->module ? source->module->index : PA_INVALID_INDEX,
2497 PA_TAG_CVOLUME, pa_source_get_volume(source),
2498 PA_TAG_BOOLEAN, pa_source_get_mute(source),
2499 PA_TAG_U32, source->monitor_of ? source->monitor_of->index : PA_INVALID_INDEX,
2500 PA_TAG_STRING, source->monitor_of ? source->monitor_of->name : NULL,
2501 PA_TAG_USEC, pa_source_get_latency(source),
2502 PA_TAG_STRING, source->driver,
2503 PA_TAG_U32, source->flags,
2504 PA_TAG_INVALID);
2505
2506 if (c->version >= 13) {
2507 pa_tagstruct_put_proplist(t, source->proplist);
2508 pa_tagstruct_put_usec(t, pa_source_get_requested_latency(source));
2509 }
2510 }
2511
2512
2513 static void client_fill_tagstruct(connection *c, pa_tagstruct *t, pa_client *client) {
2514 pa_assert(t);
2515 pa_assert(client);
2516
2517 pa_tagstruct_putu32(t, client->index);
2518 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(client->proplist, PA_PROP_APPLICATION_NAME)));
2519 pa_tagstruct_putu32(t, client->module ? client->module->index : PA_INVALID_INDEX);
2520 pa_tagstruct_puts(t, client->driver);
2521
2522 if (c->version >= 13)
2523 pa_tagstruct_put_proplist(t, client->proplist);
2524
2525 }
2526
2527 static void module_fill_tagstruct(pa_tagstruct *t, pa_module *module) {
2528 pa_assert(t);
2529 pa_assert(module);
2530
2531 pa_tagstruct_putu32(t, module->index);
2532 pa_tagstruct_puts(t, module->name);
2533 pa_tagstruct_puts(t, module->argument);
2534 pa_tagstruct_putu32(t, module->n_used);
2535 pa_tagstruct_put_boolean(t, module->auto_unload);
2536 }
2537
2538 static void sink_input_fill_tagstruct(connection *c, pa_tagstruct *t, pa_sink_input *s) {
2539 pa_sample_spec fixed_ss;
2540
2541 pa_assert(t);
2542 pa_sink_input_assert_ref(s);
2543
2544 fixup_sample_spec(c, &fixed_ss, &s->sample_spec);
2545
2546 pa_tagstruct_putu32(t, s->index);
2547 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME)));
2548 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
2549 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
2550 pa_tagstruct_putu32(t, s->sink->index);
2551 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2552 pa_tagstruct_put_channel_map(t, &s->channel_map);
2553 pa_tagstruct_put_cvolume(t, &s->volume);
2554 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
2555 pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
2556 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
2557 pa_tagstruct_puts(t, s->driver);
2558 if (c->version >= 11)
2559 pa_tagstruct_put_boolean(t, pa_sink_input_get_mute(s));
2560 if (c->version >= 13)
2561 pa_tagstruct_put_proplist(t, s->proplist);
2562 }
2563
2564 static void source_output_fill_tagstruct(connection *c, pa_tagstruct *t, pa_source_output *s) {
2565 pa_sample_spec fixed_ss;
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));
2580 pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
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 }