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