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