]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-native.c
rework validity checking of sink/source/... names
[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(volume);
887 pa_assert(missing);
888 pa_assert(p);
889
890 /* Find syncid group */
891 for (ssync = pa_idxset_first(c->output_streams, &idx); ssync; ssync = pa_idxset_next(c->output_streams, &idx)) {
892
893 if (!playback_stream_isinstance(ssync))
894 continue;
895
896 if (ssync->syncid == syncid)
897 break;
898 }
899
900 /* Synced streams must connect to the same sink */
901 if (ssync) {
902
903 if (!sink)
904 sink = ssync->sink_input->sink;
905 else if (sink != ssync->sink_input->sink)
906 return NULL;
907 }
908
909 pa_sink_input_new_data_init(&data);
910
911 pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
912 pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
913 data.driver = __FILE__;
914 data.module = c->options->module;
915 data.client = c->client;
916 data.sink = sink;
917 pa_sink_input_new_data_set_sample_spec(&data, ss);
918 pa_sink_input_new_data_set_channel_map(&data, map);
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
1600 pa_native_connection_assert_ref(c);
1601 pa_assert(t);
1602
1603 if ((c->version < 13 && (pa_tagstruct_gets(t, &name) < 0 || !name)) ||
1604 pa_tagstruct_get(
1605 t,
1606 PA_TAG_SAMPLE_SPEC, &ss,
1607 PA_TAG_CHANNEL_MAP, &map,
1608 PA_TAG_U32, &sink_index,
1609 PA_TAG_STRING, &sink_name,
1610 PA_TAG_U32, &maxlength,
1611 PA_TAG_BOOLEAN, &corked,
1612 PA_TAG_U32, &tlength,
1613 PA_TAG_U32, &prebuf,
1614 PA_TAG_U32, &minreq,
1615 PA_TAG_U32, &syncid,
1616 PA_TAG_CVOLUME, &volume,
1617 PA_TAG_INVALID) < 0) {
1618
1619 protocol_error(c);
1620 return;
1621 }
1622
1623 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1624 CHECK_VALIDITY(c->pstream, !sink_name || pa_namereg_is_valid_name(sink_name), tag, PA_ERR_INVALID);
1625 CHECK_VALIDITY(c->pstream, sink_index == PA_INVALID_INDEX || !sink_name, tag, PA_ERR_INVALID);
1626 CHECK_VALIDITY(c->pstream, !sink_name || sink_index == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1627 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1628 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1629 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
1630 CHECK_VALIDITY(c->pstream, map.channels == ss.channels && volume.channels == ss.channels, tag, PA_ERR_INVALID);
1631
1632 p = pa_proplist_new();
1633
1634 if (name)
1635 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
1636
1637 if (c->version >= 12) {
1638 /* Since 0.9.8 the user can ask for a couple of additional flags */
1639
1640 if (pa_tagstruct_get_boolean(t, &no_remap) < 0 ||
1641 pa_tagstruct_get_boolean(t, &no_remix) < 0 ||
1642 pa_tagstruct_get_boolean(t, &fix_format) < 0 ||
1643 pa_tagstruct_get_boolean(t, &fix_rate) < 0 ||
1644 pa_tagstruct_get_boolean(t, &fix_channels) < 0 ||
1645 pa_tagstruct_get_boolean(t, &no_move) < 0 ||
1646 pa_tagstruct_get_boolean(t, &variable_rate) < 0) {
1647
1648 protocol_error(c);
1649 pa_proplist_free(p);
1650 return;
1651 }
1652 }
1653
1654 if (c->version >= 13) {
1655
1656 if (pa_tagstruct_get_boolean(t, &muted) < 0 ||
1657 pa_tagstruct_get_boolean(t, &adjust_latency) < 0 ||
1658 pa_tagstruct_get_proplist(t, p) < 0) {
1659 protocol_error(c);
1660 pa_proplist_free(p);
1661 return;
1662 }
1663 }
1664
1665 if (!pa_tagstruct_eof(t)) {
1666 protocol_error(c);
1667 pa_proplist_free(p);
1668 return;
1669 }
1670
1671 if (sink_index != PA_INVALID_INDEX) {
1672
1673 if (!(sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index))) {
1674 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1675 pa_proplist_free(p);
1676 return;
1677 }
1678
1679 } else if (sink_name) {
1680
1681 if (!(sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1))) {
1682 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1683 pa_proplist_free(p);
1684 return;
1685 }
1686 }
1687
1688 flags =
1689 (corked ? PA_SINK_INPUT_START_CORKED : 0) |
1690 (no_remap ? PA_SINK_INPUT_NO_REMAP : 0) |
1691 (no_remix ? PA_SINK_INPUT_NO_REMIX : 0) |
1692 (fix_format ? PA_SINK_INPUT_FIX_FORMAT : 0) |
1693 (fix_rate ? PA_SINK_INPUT_FIX_RATE : 0) |
1694 (fix_channels ? PA_SINK_INPUT_FIX_CHANNELS : 0) |
1695 (no_move ? PA_SINK_INPUT_DONT_MOVE : 0) |
1696 (variable_rate ? PA_SINK_INPUT_VARIABLE_RATE : 0);
1697
1698 s = playback_stream_new(c, sink, &ss, &map, &maxlength, &tlength, &prebuf, &minreq, &volume, muted, syncid, &missing, flags, p, adjust_latency);
1699 pa_proplist_free(p);
1700
1701 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1702
1703 reply = reply_new(tag);
1704 pa_tagstruct_putu32(reply, s->index);
1705 pa_assert(s->sink_input);
1706 pa_tagstruct_putu32(reply, s->sink_input->index);
1707 pa_tagstruct_putu32(reply, missing);
1708
1709 /* pa_log("initial request is %u", missing); */
1710
1711 if (c->version >= 9) {
1712 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1713
1714 pa_tagstruct_putu32(reply, (uint32_t) maxlength);
1715 pa_tagstruct_putu32(reply, (uint32_t) tlength);
1716 pa_tagstruct_putu32(reply, (uint32_t) prebuf);
1717 pa_tagstruct_putu32(reply, (uint32_t) minreq);
1718 }
1719
1720 if (c->version >= 12) {
1721 /* Since 0.9.8 we support sending the chosen sample
1722 * spec/channel map/device/suspend status back to the
1723 * client */
1724
1725 pa_tagstruct_put_sample_spec(reply, &ss);
1726 pa_tagstruct_put_channel_map(reply, &map);
1727
1728 pa_tagstruct_putu32(reply, s->sink_input->sink->index);
1729 pa_tagstruct_puts(reply, s->sink_input->sink->name);
1730
1731 pa_tagstruct_put_boolean(reply, pa_sink_get_state(s->sink_input->sink) == PA_SINK_SUSPENDED);
1732 }
1733
1734 if (c->version >= 13)
1735 pa_tagstruct_put_usec(reply, s->sink_latency);
1736
1737 pa_pstream_send_tagstruct(c->pstream, reply);
1738 }
1739
1740 static void command_delete_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1741 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
1742 uint32_t channel;
1743
1744 pa_native_connection_assert_ref(c);
1745 pa_assert(t);
1746
1747 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1748 !pa_tagstruct_eof(t)) {
1749 protocol_error(c);
1750 return;
1751 }
1752
1753 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1754
1755 switch (command) {
1756
1757 case PA_COMMAND_DELETE_PLAYBACK_STREAM: {
1758 playback_stream *s;
1759 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || !playback_stream_isinstance(s)) {
1760 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1761 return;
1762 }
1763
1764 playback_stream_unlink(s);
1765 break;
1766 }
1767
1768 case PA_COMMAND_DELETE_RECORD_STREAM: {
1769 record_stream *s;
1770 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
1771 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1772 return;
1773 }
1774
1775 record_stream_unlink(s);
1776 break;
1777 }
1778
1779 case PA_COMMAND_DELETE_UPLOAD_STREAM: {
1780 upload_stream *s;
1781
1782 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || !upload_stream_isinstance(s)) {
1783 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
1784 return;
1785 }
1786
1787 upload_stream_unlink(s);
1788 break;
1789 }
1790
1791 default:
1792 pa_assert_not_reached();
1793 }
1794
1795 pa_pstream_send_simple_ack(c->pstream, tag);
1796 }
1797
1798 static void command_create_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1799 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
1800 record_stream *s;
1801 uint32_t maxlength, fragment_size;
1802 uint32_t source_index;
1803 const char *name = NULL, *source_name;
1804 pa_sample_spec ss;
1805 pa_channel_map map;
1806 pa_tagstruct *reply;
1807 pa_source *source = NULL;
1808 pa_bool_t
1809 corked = FALSE,
1810 no_remap = FALSE,
1811 no_remix = FALSE,
1812 fix_format = FALSE,
1813 fix_rate = FALSE,
1814 fix_channels = FALSE,
1815 no_move = FALSE,
1816 variable_rate = FALSE,
1817 adjust_latency = FALSE,
1818 peak_detect = FALSE;
1819 pa_source_output_flags_t flags = 0;
1820 pa_proplist *p;
1821 uint32_t direct_on_input_idx = PA_INVALID_INDEX;
1822 pa_sink_input *direct_on_input = NULL;
1823
1824 pa_native_connection_assert_ref(c);
1825 pa_assert(t);
1826
1827 if ((c->version < 13 && (pa_tagstruct_gets(t, &name) < 0 || !name)) ||
1828 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
1829 pa_tagstruct_get_channel_map(t, &map) < 0 ||
1830 pa_tagstruct_getu32(t, &source_index) < 0 ||
1831 pa_tagstruct_gets(t, &source_name) < 0 ||
1832 pa_tagstruct_getu32(t, &maxlength) < 0 ||
1833 pa_tagstruct_get_boolean(t, &corked) < 0 ||
1834 pa_tagstruct_getu32(t, &fragment_size) < 0) {
1835 protocol_error(c);
1836 return;
1837 }
1838
1839 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1840 CHECK_VALIDITY(c->pstream, !source_name || pa_namereg_is_valid_name(source_name), tag, PA_ERR_INVALID);
1841 CHECK_VALIDITY(c->pstream, source_index == PA_INVALID_INDEX || !source_name, tag, PA_ERR_INVALID);
1842 CHECK_VALIDITY(c->pstream, !source_name || source_index == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
1843 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
1844 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
1845 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
1846
1847 p = pa_proplist_new();
1848
1849 if (name)
1850 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
1851
1852 if (c->version >= 12) {
1853 /* Since 0.9.8 the user can ask for a couple of additional flags */
1854
1855 if (pa_tagstruct_get_boolean(t, &no_remap) < 0 ||
1856 pa_tagstruct_get_boolean(t, &no_remix) < 0 ||
1857 pa_tagstruct_get_boolean(t, &fix_format) < 0 ||
1858 pa_tagstruct_get_boolean(t, &fix_rate) < 0 ||
1859 pa_tagstruct_get_boolean(t, &fix_channels) < 0 ||
1860 pa_tagstruct_get_boolean(t, &no_move) < 0 ||
1861 pa_tagstruct_get_boolean(t, &variable_rate) < 0) {
1862
1863 protocol_error(c);
1864 pa_proplist_free(p);
1865 return;
1866 }
1867 }
1868
1869 if (c->version >= 13) {
1870
1871 if (pa_tagstruct_get_boolean(t, &peak_detect) < 0 ||
1872 pa_tagstruct_get_boolean(t, &adjust_latency) < 0 ||
1873 pa_tagstruct_get_proplist(t, p) < 0 ||
1874 pa_tagstruct_getu32(t, &direct_on_input_idx) < 0) {
1875 protocol_error(c);
1876 pa_proplist_free(p);
1877 return;
1878 }
1879 }
1880
1881 if (!pa_tagstruct_eof(t)) {
1882 protocol_error(c);
1883 pa_proplist_free(p);
1884 return;
1885 }
1886
1887 if (source_index != PA_INVALID_INDEX) {
1888
1889 if (!(source = pa_idxset_get_by_index(c->protocol->core->sources, source_index))) {
1890 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1891 pa_proplist_free(p);
1892 return;
1893 }
1894
1895 } else if (source_name) {
1896
1897 if (!(source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1))) {
1898 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1899 pa_proplist_free(p);
1900 return;
1901 }
1902 }
1903
1904 if (direct_on_input_idx != PA_INVALID_INDEX) {
1905
1906 if (!(direct_on_input = pa_idxset_get_by_index(c->protocol->core->sink_inputs, direct_on_input_idx))) {
1907 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
1908 pa_proplist_free(p);
1909 return;
1910 }
1911 }
1912
1913 flags =
1914 (corked ? PA_SOURCE_OUTPUT_START_CORKED : 0) |
1915 (no_remap ? PA_SOURCE_OUTPUT_NO_REMAP : 0) |
1916 (no_remix ? PA_SOURCE_OUTPUT_NO_REMIX : 0) |
1917 (fix_format ? PA_SOURCE_OUTPUT_FIX_FORMAT : 0) |
1918 (fix_rate ? PA_SOURCE_OUTPUT_FIX_RATE : 0) |
1919 (fix_channels ? PA_SOURCE_OUTPUT_FIX_CHANNELS : 0) |
1920 (no_move ? PA_SOURCE_OUTPUT_DONT_MOVE : 0) |
1921 (variable_rate ? PA_SOURCE_OUTPUT_VARIABLE_RATE : 0);
1922
1923 s = record_stream_new(c, source, &ss, &map, peak_detect, &maxlength, &fragment_size, flags, p, adjust_latency, direct_on_input);
1924 pa_proplist_free(p);
1925
1926 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
1927
1928 reply = reply_new(tag);
1929 pa_tagstruct_putu32(reply, s->index);
1930 pa_assert(s->source_output);
1931 pa_tagstruct_putu32(reply, s->source_output->index);
1932
1933 if (c->version >= 9) {
1934 /* Since 0.9 we support sending the buffer metrics back to the client */
1935
1936 pa_tagstruct_putu32(reply, (uint32_t) maxlength);
1937 pa_tagstruct_putu32(reply, (uint32_t) fragment_size);
1938 }
1939
1940 if (c->version >= 12) {
1941 /* Since 0.9.8 we support sending the chosen sample
1942 * spec/channel map/device/suspend status back to the
1943 * client */
1944
1945 pa_tagstruct_put_sample_spec(reply, &ss);
1946 pa_tagstruct_put_channel_map(reply, &map);
1947
1948 pa_tagstruct_putu32(reply, s->source_output->source->index);
1949 pa_tagstruct_puts(reply, s->source_output->source->name);
1950
1951 pa_tagstruct_put_boolean(reply, pa_source_get_state(s->source_output->source) == PA_SOURCE_SUSPENDED);
1952 }
1953
1954 if (c->version >= 13)
1955 pa_tagstruct_put_usec(reply, s->source_latency);
1956
1957 pa_pstream_send_tagstruct(c->pstream, reply);
1958 }
1959
1960 static void command_exit(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1961 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
1962 int ret;
1963
1964 pa_native_connection_assert_ref(c);
1965 pa_assert(t);
1966
1967 if (!pa_tagstruct_eof(t)) {
1968 protocol_error(c);
1969 return;
1970 }
1971
1972 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
1973 ret = pa_core_exit(c->protocol->core, FALSE, 0);
1974 CHECK_VALIDITY(c->pstream, ret >= 0, tag, PA_ERR_ACCESS);
1975
1976 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
1977 }
1978
1979 static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1980 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
1981 const void*cookie;
1982 pa_tagstruct *reply;
1983 pa_bool_t shm_on_remote, do_shm;
1984
1985 pa_native_connection_assert_ref(c);
1986 pa_assert(t);
1987
1988 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
1989 pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
1990 !pa_tagstruct_eof(t)) {
1991 protocol_error(c);
1992 return;
1993 }
1994
1995 /* Minimum supported version */
1996 if (c->version < 8) {
1997 pa_pstream_send_error(c->pstream, tag, PA_ERR_VERSION);
1998 return;
1999 }
2000
2001 /* Starting with protocol version 13 the MSB of the version tag
2002 reflects if shm is available for this pa_native_connection or
2003 not. */
2004 if (c->version >= 13) {
2005 shm_on_remote = !!(c->version & 0x80000000U);
2006 c->version &= 0x7FFFFFFFU;
2007 }
2008
2009 pa_log_debug("Protocol version: remote %u, local %u", c->version, PA_PROTOCOL_VERSION);
2010
2011 pa_proplist_setf(c->client->proplist, "native-protocol.version", "%u", c->version);
2012
2013 if (!c->authorized) {
2014 pa_bool_t success = FALSE;
2015
2016 #ifdef HAVE_CREDS
2017 const pa_creds *creds;
2018
2019 if ((creds = pa_pdispatch_creds(pd))) {
2020 if (creds->uid == getuid())
2021 success = TRUE;
2022 else if (c->options->auth_group) {
2023 int r;
2024 gid_t gid;
2025
2026 if ((gid = pa_get_gid_of_group(c->options->auth_group)) == (gid_t) -1)
2027 pa_log_warn("Failed to get GID of group '%s'", c->options->auth_group);
2028 else if (gid == creds->gid)
2029 success = TRUE;
2030
2031 if (!success) {
2032 if ((r = pa_uid_in_group(creds->uid, c->options->auth_group)) < 0)
2033 pa_log_warn("Failed to check group membership.");
2034 else if (r > 0)
2035 success = TRUE;
2036 }
2037 }
2038
2039 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
2040 (unsigned long) creds->uid,
2041 (unsigned long) creds->gid,
2042 (int) success);
2043 }
2044 #endif
2045
2046 if (!success && c->options->auth_cookie) {
2047 const uint8_t *ac;
2048
2049 if ((ac = pa_auth_cookie_read(c->options->auth_cookie, PA_NATIVE_COOKIE_LENGTH)))
2050 if (memcmp(ac, cookie, PA_NATIVE_COOKIE_LENGTH) == 0)
2051 success = TRUE;
2052 }
2053
2054 if (!success) {
2055 pa_log_warn("Denied access to client with invalid authorization data.");
2056 pa_pstream_send_error(c->pstream, tag, PA_ERR_ACCESS);
2057 return;
2058 }
2059
2060 c->authorized = TRUE;
2061 if (c->auth_timeout_event) {
2062 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
2063 c->auth_timeout_event = NULL;
2064 }
2065 }
2066
2067 /* Enable shared memory support if possible */
2068 do_shm =
2069 pa_mempool_is_shared(c->protocol->core->mempool) &&
2070 c->is_local;
2071
2072 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm));
2073
2074 if (do_shm)
2075 if (c->version < 10 || (c->version >= 13 && !shm_on_remote))
2076 do_shm = FALSE;
2077
2078 if (do_shm) {
2079 /* Only enable SHM if both sides are owned by the same
2080 * user. This is a security measure because otherwise data
2081 * private to the user might leak. */
2082
2083 const pa_creds *creds;
2084 if (!(creds = pa_pdispatch_creds(pd)) || getuid() != creds->uid)
2085 do_shm = FALSE;
2086 }
2087
2088 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm));
2089 pa_pstream_enable_shm(c->pstream, do_shm);
2090
2091 reply = reply_new(tag);
2092 pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION | (do_shm ? 0x80000000 : 0));
2093
2094 #ifdef HAVE_CREDS
2095 {
2096 /* SHM support is only enabled after both sides made sure they are the same user. */
2097
2098 pa_creds ucred;
2099
2100 ucred.uid = getuid();
2101 ucred.gid = getgid();
2102
2103 pa_pstream_send_tagstruct_with_creds(c->pstream, reply, &ucred);
2104 }
2105 #else
2106 pa_pstream_send_tagstruct(c->pstream, reply);
2107 #endif
2108 }
2109
2110 static void command_set_client_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2111 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2112 const char *name = NULL;
2113 pa_proplist *p;
2114 pa_tagstruct *reply;
2115
2116 pa_native_connection_assert_ref(c);
2117 pa_assert(t);
2118
2119 p = pa_proplist_new();
2120
2121 if ((c->version < 13 && pa_tagstruct_gets(t, &name) < 0) ||
2122 (c->version >= 13 && pa_tagstruct_get_proplist(t, p) < 0) ||
2123 !pa_tagstruct_eof(t)) {
2124
2125 protocol_error(c);
2126 pa_proplist_free(p);
2127 return;
2128 }
2129
2130 if (name)
2131 if (pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, name) < 0) {
2132 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
2133 pa_proplist_free(p);
2134 return;
2135 }
2136
2137 pa_proplist_update(c->client->proplist, PA_UPDATE_REPLACE, p);
2138 pa_proplist_free(p);
2139
2140 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
2141
2142 reply = reply_new(tag);
2143
2144 if (c->version >= 13)
2145 pa_tagstruct_putu32(reply, c->client->index);
2146
2147 pa_pstream_send_tagstruct(c->pstream, reply);
2148 }
2149
2150 static void command_lookup(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2151 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2152 const char *name;
2153 uint32_t idx = PA_IDXSET_INVALID;
2154
2155 pa_native_connection_assert_ref(c);
2156 pa_assert(t);
2157
2158 if (pa_tagstruct_gets(t, &name) < 0 ||
2159 !pa_tagstruct_eof(t)) {
2160 protocol_error(c);
2161 return;
2162 }
2163
2164 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2165 CHECK_VALIDITY(c->pstream, name && pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
2166
2167 if (command == PA_COMMAND_LOOKUP_SINK) {
2168 pa_sink *sink;
2169 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
2170 idx = sink->index;
2171 } else {
2172 pa_source *source;
2173 pa_assert(command == PA_COMMAND_LOOKUP_SOURCE);
2174 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
2175 idx = source->index;
2176 }
2177
2178 if (idx == PA_IDXSET_INVALID)
2179 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2180 else {
2181 pa_tagstruct *reply;
2182 reply = reply_new(tag);
2183 pa_tagstruct_putu32(reply, idx);
2184 pa_pstream_send_tagstruct(c->pstream, reply);
2185 }
2186 }
2187
2188 static void command_drain_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2189 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2190 uint32_t idx;
2191 playback_stream *s;
2192
2193 pa_native_connection_assert_ref(c);
2194 pa_assert(t);
2195
2196 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2197 !pa_tagstruct_eof(t)) {
2198 protocol_error(c);
2199 return;
2200 }
2201
2202 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2203 s = pa_idxset_get_by_index(c->output_streams, idx);
2204 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2205 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2206
2207 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);
2208 }
2209
2210 static void command_stat(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2211 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2212 pa_tagstruct *reply;
2213 const pa_mempool_stat *stat;
2214
2215 pa_native_connection_assert_ref(c);
2216 pa_assert(t);
2217
2218 if (!pa_tagstruct_eof(t)) {
2219 protocol_error(c);
2220 return;
2221 }
2222
2223 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2224
2225 stat = pa_mempool_get_stat(c->protocol->core->mempool);
2226
2227 reply = reply_new(tag);
2228 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->n_allocated));
2229 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->allocated_size));
2230 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->n_accumulated));
2231 pa_tagstruct_putu32(reply, (uint32_t) pa_atomic_load(&stat->accumulated_size));
2232 pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
2233 pa_pstream_send_tagstruct(c->pstream, reply);
2234 }
2235
2236 static void command_get_playback_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2237 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2238 pa_tagstruct *reply;
2239 playback_stream *s;
2240 struct timeval tv, now;
2241 uint32_t idx;
2242 pa_usec_t latency;
2243
2244 pa_native_connection_assert_ref(c);
2245 pa_assert(t);
2246
2247 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2248 pa_tagstruct_get_timeval(t, &tv) < 0 ||
2249 !pa_tagstruct_eof(t)) {
2250 protocol_error(c);
2251 return;
2252 }
2253
2254 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2255 s = pa_idxset_get_by_index(c->output_streams, idx);
2256 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2257 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2258 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)
2259
2260 reply = reply_new(tag);
2261
2262 latency = pa_sink_get_latency(s->sink_input->sink);
2263 latency += pa_bytes_to_usec(s->render_memblockq_length, &s->sink_input->sample_spec);
2264
2265 pa_tagstruct_put_usec(reply, latency);
2266
2267 pa_tagstruct_put_usec(reply, 0);
2268 pa_tagstruct_put_boolean(reply, s->sink_input->thread_info.playing_for > 0);
2269 pa_tagstruct_put_timeval(reply, &tv);
2270 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
2271 pa_tagstruct_puts64(reply, s->write_index);
2272 pa_tagstruct_puts64(reply, s->read_index);
2273
2274 if (c->version >= 13) {
2275 pa_tagstruct_putu64(reply, s->sink_input->thread_info.underrun_for);
2276 pa_tagstruct_putu64(reply, s->sink_input->thread_info.playing_for);
2277 }
2278
2279 pa_pstream_send_tagstruct(c->pstream, reply);
2280 }
2281
2282 static void command_get_record_latency(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2283 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2284 pa_tagstruct *reply;
2285 record_stream *s;
2286 struct timeval tv, now;
2287 uint32_t idx;
2288
2289 pa_native_connection_assert_ref(c);
2290 pa_assert(t);
2291
2292 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2293 pa_tagstruct_get_timeval(t, &tv) < 0 ||
2294 !pa_tagstruct_eof(t)) {
2295 protocol_error(c);
2296 return;
2297 }
2298
2299 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2300 s = pa_idxset_get_by_index(c->record_streams, idx);
2301 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2302
2303 reply = reply_new(tag);
2304 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
2305 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
2306 pa_tagstruct_put_boolean(reply, pa_source_get_state(s->source_output->source) == PA_SOURCE_RUNNING);
2307 pa_tagstruct_put_timeval(reply, &tv);
2308 pa_tagstruct_put_timeval(reply, pa_gettimeofday(&now));
2309 pa_tagstruct_puts64(reply, pa_memblockq_get_write_index(s->memblockq));
2310 pa_tagstruct_puts64(reply, pa_memblockq_get_read_index(s->memblockq));
2311 pa_pstream_send_tagstruct(c->pstream, reply);
2312 }
2313
2314 static void command_create_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2315 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2316 upload_stream *s;
2317 uint32_t length;
2318 const char *name = NULL;
2319 pa_sample_spec ss;
2320 pa_channel_map map;
2321 pa_tagstruct *reply;
2322 pa_proplist *p;
2323
2324 pa_native_connection_assert_ref(c);
2325 pa_assert(t);
2326
2327 if (pa_tagstruct_gets(t, &name) < 0 ||
2328 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
2329 pa_tagstruct_get_channel_map(t, &map) < 0 ||
2330 pa_tagstruct_getu32(t, &length) < 0) {
2331 protocol_error(c);
2332 return;
2333 }
2334
2335 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2336 CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID);
2337 CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID);
2338 CHECK_VALIDITY(c->pstream, map.channels == ss.channels, tag, PA_ERR_INVALID);
2339 CHECK_VALIDITY(c->pstream, (length % pa_frame_size(&ss)) == 0 && length > 0, tag, PA_ERR_INVALID);
2340 CHECK_VALIDITY(c->pstream, length <= PA_SCACHE_ENTRY_SIZE_MAX, tag, PA_ERR_TOOLARGE);
2341
2342 p = pa_proplist_new();
2343
2344 if (c->version >= 13 && pa_tagstruct_get_proplist(t, p) < 0) {
2345 protocol_error(c);
2346 pa_proplist_free(p);
2347 return;
2348 }
2349
2350 if (c->version < 13)
2351 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
2352 else if (!name)
2353 if (!(name = pa_proplist_gets(p, PA_PROP_EVENT_ID)))
2354 name = pa_proplist_gets(p, PA_PROP_MEDIA_NAME);
2355
2356 CHECK_VALIDITY(c->pstream, name && pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
2357
2358 s = upload_stream_new(c, &ss, &map, name, length, p);
2359 pa_proplist_free(p);
2360
2361 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_INVALID);
2362
2363 reply = reply_new(tag);
2364 pa_tagstruct_putu32(reply, s->index);
2365 pa_tagstruct_putu32(reply, length);
2366 pa_pstream_send_tagstruct(c->pstream, reply);
2367 }
2368
2369 static void command_finish_upload_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2370 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2371 uint32_t channel;
2372 upload_stream *s;
2373 uint32_t idx;
2374
2375 pa_native_connection_assert_ref(c);
2376 pa_assert(t);
2377
2378 if (pa_tagstruct_getu32(t, &channel) < 0 ||
2379 !pa_tagstruct_eof(t)) {
2380 protocol_error(c);
2381 return;
2382 }
2383
2384 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2385
2386 s = pa_idxset_get_by_index(c->output_streams, channel);
2387 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
2388 CHECK_VALIDITY(c->pstream, upload_stream_isinstance(s), tag, PA_ERR_NOENTITY);
2389
2390 if (pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->channel_map, &s->memchunk, s->proplist, &idx) < 0)
2391 pa_pstream_send_error(c->pstream, tag, PA_ERR_INTERNAL);
2392 else
2393 pa_pstream_send_simple_ack(c->pstream, tag);
2394
2395 upload_stream_unlink(s);
2396 }
2397
2398 static void command_play_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2399 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2400 uint32_t sink_index;
2401 pa_volume_t volume;
2402 pa_sink *sink;
2403 const char *name, *sink_name;
2404 uint32_t idx;
2405 pa_proplist *p;
2406 pa_tagstruct *reply;
2407
2408 pa_native_connection_assert_ref(c);
2409 pa_assert(t);
2410
2411 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2412
2413 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
2414 pa_tagstruct_gets(t, &sink_name) < 0 ||
2415 pa_tagstruct_getu32(t, &volume) < 0 ||
2416 pa_tagstruct_gets(t, &name) < 0) {
2417 protocol_error(c);
2418 return;
2419 }
2420
2421 CHECK_VALIDITY(c->pstream, !sink_name || pa_namereg_is_valid_name(sink_name), tag, PA_ERR_INVALID);
2422 CHECK_VALIDITY(c->pstream, sink_index == PA_INVALID_INDEX || !sink_name, tag, PA_ERR_INVALID);
2423 CHECK_VALIDITY(c->pstream, !sink_name || sink_index == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2424 CHECK_VALIDITY(c->pstream, name && pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
2425
2426 if (sink_index != PA_INVALID_INDEX)
2427 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
2428 else
2429 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
2430
2431 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
2432
2433 p = pa_proplist_new();
2434
2435 if ((c->version >= 13 && pa_tagstruct_get_proplist(t, p) < 0) ||
2436 !pa_tagstruct_eof(t)) {
2437 protocol_error(c);
2438 pa_proplist_free(p);
2439 return;
2440 }
2441
2442 pa_proplist_update(p, PA_UPDATE_MERGE, c->client->proplist);
2443
2444 if (pa_scache_play_item(c->protocol->core, name, sink, volume, p, &idx) < 0) {
2445 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2446 pa_proplist_free(p);
2447 return;
2448 }
2449
2450 pa_proplist_free(p);
2451
2452 reply = reply_new(tag);
2453
2454 if (c->version >= 13)
2455 pa_tagstruct_putu32(reply, idx);
2456
2457 pa_pstream_send_tagstruct(c->pstream, reply);
2458 }
2459
2460 static void command_remove_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2461 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2462 const char *name;
2463
2464 pa_native_connection_assert_ref(c);
2465 pa_assert(t);
2466
2467 if (pa_tagstruct_gets(t, &name) < 0 ||
2468 !pa_tagstruct_eof(t)) {
2469 protocol_error(c);
2470 return;
2471 }
2472
2473 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2474 CHECK_VALIDITY(c->pstream, name && pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
2475
2476 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
2477 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2478 return;
2479 }
2480
2481 pa_pstream_send_simple_ack(c->pstream, tag);
2482 }
2483
2484 static void fixup_sample_spec(pa_native_connection *c, pa_sample_spec *fixed, const pa_sample_spec *original) {
2485 pa_assert(c);
2486 pa_assert(fixed);
2487 pa_assert(original);
2488
2489 *fixed = *original;
2490
2491 if (c->version < 12) {
2492 /* Before protocol version 12 we didn't support S32 samples,
2493 * so we need to lie about this to the client */
2494
2495 if (fixed->format == PA_SAMPLE_S32LE)
2496 fixed->format = PA_SAMPLE_FLOAT32LE;
2497 if (fixed->format == PA_SAMPLE_S32BE)
2498 fixed->format = PA_SAMPLE_FLOAT32BE;
2499 }
2500 }
2501
2502 static void sink_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_sink *sink) {
2503 pa_sample_spec fixed_ss;
2504
2505 pa_assert(t);
2506 pa_sink_assert_ref(sink);
2507
2508 fixup_sample_spec(c, &fixed_ss, &sink->sample_spec);
2509
2510 pa_tagstruct_put(
2511 t,
2512 PA_TAG_U32, sink->index,
2513 PA_TAG_STRING, sink->name,
2514 PA_TAG_STRING, pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION)),
2515 PA_TAG_SAMPLE_SPEC, &fixed_ss,
2516 PA_TAG_CHANNEL_MAP, &sink->channel_map,
2517 PA_TAG_U32, sink->module ? sink->module->index : PA_INVALID_INDEX,
2518 PA_TAG_CVOLUME, pa_sink_get_volume(sink, FALSE),
2519 PA_TAG_BOOLEAN, pa_sink_get_mute(sink, FALSE),
2520 PA_TAG_U32, sink->monitor_source ? sink->monitor_source->index : PA_INVALID_INDEX,
2521 PA_TAG_STRING, sink->monitor_source ? sink->monitor_source->name : NULL,
2522 PA_TAG_USEC, pa_sink_get_latency(sink),
2523 PA_TAG_STRING, sink->driver,
2524 PA_TAG_U32, sink->flags,
2525 PA_TAG_INVALID);
2526
2527 if (c->version >= 13) {
2528 pa_tagstruct_put_proplist(t, sink->proplist);
2529 pa_tagstruct_put_usec(t, pa_sink_get_requested_latency(sink));
2530 }
2531 }
2532
2533 static void source_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_source *source) {
2534 pa_sample_spec fixed_ss;
2535
2536 pa_assert(t);
2537 pa_source_assert_ref(source);
2538
2539 fixup_sample_spec(c, &fixed_ss, &source->sample_spec);
2540
2541 pa_tagstruct_put(
2542 t,
2543 PA_TAG_U32, source->index,
2544 PA_TAG_STRING, source->name,
2545 PA_TAG_STRING, pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION)),
2546 PA_TAG_SAMPLE_SPEC, &fixed_ss,
2547 PA_TAG_CHANNEL_MAP, &source->channel_map,
2548 PA_TAG_U32, source->module ? source->module->index : PA_INVALID_INDEX,
2549 PA_TAG_CVOLUME, pa_source_get_volume(source, FALSE),
2550 PA_TAG_BOOLEAN, pa_source_get_mute(source, FALSE),
2551 PA_TAG_U32, source->monitor_of ? source->monitor_of->index : PA_INVALID_INDEX,
2552 PA_TAG_STRING, source->monitor_of ? source->monitor_of->name : NULL,
2553 PA_TAG_USEC, pa_source_get_latency(source),
2554 PA_TAG_STRING, source->driver,
2555 PA_TAG_U32, source->flags,
2556 PA_TAG_INVALID);
2557
2558 if (c->version >= 13) {
2559 pa_tagstruct_put_proplist(t, source->proplist);
2560 pa_tagstruct_put_usec(t, pa_source_get_requested_latency(source));
2561 }
2562 }
2563
2564
2565 static void client_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_client *client) {
2566 pa_assert(t);
2567 pa_assert(client);
2568
2569 pa_tagstruct_putu32(t, client->index);
2570 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(client->proplist, PA_PROP_APPLICATION_NAME)));
2571 pa_tagstruct_putu32(t, client->module ? client->module->index : PA_INVALID_INDEX);
2572 pa_tagstruct_puts(t, client->driver);
2573
2574 if (c->version >= 13)
2575 pa_tagstruct_put_proplist(t, client->proplist);
2576
2577 }
2578
2579 static void module_fill_tagstruct(pa_tagstruct *t, pa_module *module) {
2580 pa_assert(t);
2581 pa_assert(module);
2582
2583 pa_tagstruct_putu32(t, module->index);
2584 pa_tagstruct_puts(t, module->name);
2585 pa_tagstruct_puts(t, module->argument);
2586 pa_tagstruct_putu32(t, module->n_used);
2587 pa_tagstruct_put_boolean(t, module->auto_unload);
2588 }
2589
2590 static void sink_input_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_sink_input *s) {
2591 pa_sample_spec fixed_ss;
2592 pa_usec_t sink_latency;
2593
2594 pa_assert(t);
2595 pa_sink_input_assert_ref(s);
2596
2597 fixup_sample_spec(c, &fixed_ss, &s->sample_spec);
2598
2599 pa_tagstruct_putu32(t, s->index);
2600 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME)));
2601 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
2602 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
2603 pa_tagstruct_putu32(t, s->sink->index);
2604 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2605 pa_tagstruct_put_channel_map(t, &s->channel_map);
2606 pa_tagstruct_put_cvolume(t, &s->volume);
2607 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s, &sink_latency));
2608 pa_tagstruct_put_usec(t, sink_latency);
2609 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
2610 pa_tagstruct_puts(t, s->driver);
2611 if (c->version >= 11)
2612 pa_tagstruct_put_boolean(t, pa_sink_input_get_mute(s));
2613 if (c->version >= 13)
2614 pa_tagstruct_put_proplist(t, s->proplist);
2615 }
2616
2617 static void source_output_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_source_output *s) {
2618 pa_sample_spec fixed_ss;
2619 pa_usec_t source_latency;
2620
2621 pa_assert(t);
2622 pa_source_output_assert_ref(s);
2623
2624 fixup_sample_spec(c, &fixed_ss, &s->sample_spec);
2625
2626 pa_tagstruct_putu32(t, s->index);
2627 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME)));
2628 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
2629 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
2630 pa_tagstruct_putu32(t, s->source->index);
2631 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2632 pa_tagstruct_put_channel_map(t, &s->channel_map);
2633 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s, &source_latency));
2634 pa_tagstruct_put_usec(t, source_latency);
2635 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
2636 pa_tagstruct_puts(t, s->driver);
2637
2638 if (c->version >= 13)
2639 pa_tagstruct_put_proplist(t, s->proplist);
2640 }
2641
2642 static void scache_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_scache_entry *e) {
2643 pa_sample_spec fixed_ss;
2644
2645 pa_assert(t);
2646 pa_assert(e);
2647
2648 if (e->memchunk.memblock)
2649 fixup_sample_spec(c, &fixed_ss, &e->sample_spec);
2650 else
2651 memset(&fixed_ss, 0, sizeof(fixed_ss));
2652
2653 pa_tagstruct_putu32(t, e->index);
2654 pa_tagstruct_puts(t, e->name);
2655 pa_tagstruct_put_cvolume(t, &e->volume);
2656 pa_tagstruct_put_usec(t, e->memchunk.memblock ? pa_bytes_to_usec(e->memchunk.length, &e->sample_spec) : 0);
2657 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2658 pa_tagstruct_put_channel_map(t, &e->channel_map);
2659 pa_tagstruct_putu32(t, e->memchunk.length);
2660 pa_tagstruct_put_boolean(t, e->lazy);
2661 pa_tagstruct_puts(t, e->filename);
2662
2663 if (c->version >= 13)
2664 pa_tagstruct_put_proplist(t, e->proplist);
2665 }
2666
2667 static void command_get_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2668 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2669 uint32_t idx;
2670 pa_sink *sink = NULL;
2671 pa_source *source = NULL;
2672 pa_client *client = NULL;
2673 pa_module *module = NULL;
2674 pa_sink_input *si = NULL;
2675 pa_source_output *so = NULL;
2676 pa_scache_entry *sce = NULL;
2677 const char *name;
2678 pa_tagstruct *reply;
2679
2680 pa_native_connection_assert_ref(c);
2681 pa_assert(t);
2682
2683 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2684 (command != PA_COMMAND_GET_CLIENT_INFO &&
2685 command != PA_COMMAND_GET_MODULE_INFO &&
2686 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
2687 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
2688 pa_tagstruct_gets(t, &name) < 0) ||
2689 !pa_tagstruct_eof(t)) {
2690 protocol_error(c);
2691 return;
2692 }
2693
2694 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2695 CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
2696 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
2697 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
2698 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2699
2700 if (command == PA_COMMAND_GET_SINK_INFO) {
2701 if (idx != PA_INVALID_INDEX)
2702 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2703 else
2704 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2705 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
2706 if (idx != PA_INVALID_INDEX)
2707 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2708 else
2709 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2710 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
2711 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
2712 else if (command == PA_COMMAND_GET_MODULE_INFO)
2713 module = pa_idxset_get_by_index(c->protocol->core->modules, idx);
2714 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
2715 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2716 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
2717 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
2718 else {
2719 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO);
2720 if (idx != PA_INVALID_INDEX)
2721 sce = pa_idxset_get_by_index(c->protocol->core->scache, idx);
2722 else
2723 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
2724 }
2725
2726 if (!sink && !source && !client && !module && !si && !so && !sce) {
2727 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2728 return;
2729 }
2730
2731 reply = reply_new(tag);
2732 if (sink)
2733 sink_fill_tagstruct(c, reply, sink);
2734 else if (source)
2735 source_fill_tagstruct(c, reply, source);
2736 else if (client)
2737 client_fill_tagstruct(c, reply, client);
2738 else if (module)
2739 module_fill_tagstruct(reply, module);
2740 else if (si)
2741 sink_input_fill_tagstruct(c, reply, si);
2742 else if (so)
2743 source_output_fill_tagstruct(c, reply, so);
2744 else
2745 scache_fill_tagstruct(c, reply, sce);
2746 pa_pstream_send_tagstruct(c->pstream, reply);
2747 }
2748
2749 static void command_get_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2750 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2751 pa_idxset *i;
2752 uint32_t idx;
2753 void *p;
2754 pa_tagstruct *reply;
2755
2756 pa_native_connection_assert_ref(c);
2757 pa_assert(t);
2758
2759 if (!pa_tagstruct_eof(t)) {
2760 protocol_error(c);
2761 return;
2762 }
2763
2764 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2765
2766 reply = reply_new(tag);
2767
2768 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
2769 i = c->protocol->core->sinks;
2770 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
2771 i = c->protocol->core->sources;
2772 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
2773 i = c->protocol->core->clients;
2774 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
2775 i = c->protocol->core->modules;
2776 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
2777 i = c->protocol->core->sink_inputs;
2778 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
2779 i = c->protocol->core->source_outputs;
2780 else {
2781 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
2782 i = c->protocol->core->scache;
2783 }
2784
2785 if (i) {
2786 for (p = pa_idxset_first(i, &idx); p; p = pa_idxset_next(i, &idx)) {
2787 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
2788 sink_fill_tagstruct(c, reply, p);
2789 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
2790 source_fill_tagstruct(c, reply, p);
2791 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
2792 client_fill_tagstruct(c, reply, p);
2793 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
2794 module_fill_tagstruct(reply, p);
2795 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
2796 sink_input_fill_tagstruct(c, reply, p);
2797 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
2798 source_output_fill_tagstruct(c, reply, p);
2799 else {
2800 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
2801 scache_fill_tagstruct(c, reply, p);
2802 }
2803 }
2804 }
2805
2806 pa_pstream_send_tagstruct(c->pstream, reply);
2807 }
2808
2809 static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2810 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2811 pa_tagstruct *reply;
2812 char txt[256];
2813 const char *n;
2814 pa_sample_spec fixed_ss;
2815
2816 pa_native_connection_assert_ref(c);
2817 pa_assert(t);
2818
2819 if (!pa_tagstruct_eof(t)) {
2820 protocol_error(c);
2821 return;
2822 }
2823
2824 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2825
2826 reply = reply_new(tag);
2827 pa_tagstruct_puts(reply, PACKAGE_NAME);
2828 pa_tagstruct_puts(reply, PACKAGE_VERSION);
2829 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
2830 pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
2831
2832 fixup_sample_spec(c, &fixed_ss, &c->protocol->core->default_sample_spec);
2833 pa_tagstruct_put_sample_spec(reply, &fixed_ss);
2834
2835 n = pa_namereg_get_default_sink_name(c->protocol->core);
2836 pa_tagstruct_puts(reply, n);
2837 n = pa_namereg_get_default_source_name(c->protocol->core);
2838 pa_tagstruct_puts(reply, n);
2839
2840 pa_tagstruct_putu32(reply, c->protocol->core->cookie);
2841
2842 pa_pstream_send_tagstruct(c->pstream, reply);
2843 }
2844
2845 static void subscription_cb(pa_core *core, pa_subscription_event_type_t e, uint32_t idx, void *userdata) {
2846 pa_tagstruct *t;
2847 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2848
2849 pa_native_connection_assert_ref(c);
2850
2851 t = pa_tagstruct_new(NULL, 0);
2852 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
2853 pa_tagstruct_putu32(t, (uint32_t) -1);
2854 pa_tagstruct_putu32(t, e);
2855 pa_tagstruct_putu32(t, idx);
2856 pa_pstream_send_tagstruct(c->pstream, t);
2857 }
2858
2859 static void command_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2860 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2861 pa_subscription_mask_t m;
2862
2863 pa_native_connection_assert_ref(c);
2864 pa_assert(t);
2865
2866 if (pa_tagstruct_getu32(t, &m) < 0 ||
2867 !pa_tagstruct_eof(t)) {
2868 protocol_error(c);
2869 return;
2870 }
2871
2872 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2873 CHECK_VALIDITY(c->pstream, (m & ~PA_SUBSCRIPTION_MASK_ALL) == 0, tag, PA_ERR_INVALID);
2874
2875 if (c->subscription)
2876 pa_subscription_free(c->subscription);
2877
2878 if (m != 0) {
2879 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
2880 pa_assert(c->subscription);
2881 } else
2882 c->subscription = NULL;
2883
2884 pa_pstream_send_simple_ack(c->pstream, tag);
2885 }
2886
2887 static void command_set_volume(
2888 pa_pdispatch *pd,
2889 uint32_t command,
2890 uint32_t tag,
2891 pa_tagstruct *t,
2892 void *userdata) {
2893
2894 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2895 uint32_t idx;
2896 pa_cvolume volume;
2897 pa_sink *sink = NULL;
2898 pa_source *source = NULL;
2899 pa_sink_input *si = NULL;
2900 const char *name = NULL;
2901
2902 pa_native_connection_assert_ref(c);
2903 pa_assert(t);
2904
2905 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2906 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
2907 (command == PA_COMMAND_SET_SOURCE_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
2908 pa_tagstruct_get_cvolume(t, &volume) ||
2909 !pa_tagstruct_eof(t)) {
2910 protocol_error(c);
2911 return;
2912 }
2913
2914 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2915 CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
2916 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
2917 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
2918 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2919 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
2920
2921 switch (command) {
2922
2923 case PA_COMMAND_SET_SINK_VOLUME:
2924 if (idx != PA_INVALID_INDEX)
2925 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2926 else
2927 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2928 break;
2929
2930 case PA_COMMAND_SET_SOURCE_VOLUME:
2931 if (idx != PA_INVALID_INDEX)
2932 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2933 else
2934 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
2935 break;
2936
2937 case PA_COMMAND_SET_SINK_INPUT_VOLUME:
2938 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2939 break;
2940
2941 default:
2942 pa_assert_not_reached();
2943 }
2944
2945 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
2946
2947 if (sink)
2948 pa_sink_set_volume(sink, &volume);
2949 else if (source)
2950 pa_source_set_volume(source, &volume);
2951 else if (si)
2952 pa_sink_input_set_volume(si, &volume);
2953
2954 pa_pstream_send_simple_ack(c->pstream, tag);
2955 }
2956
2957 static void command_set_mute(
2958 pa_pdispatch *pd,
2959 uint32_t command,
2960 uint32_t tag,
2961 pa_tagstruct *t,
2962 void *userdata) {
2963
2964 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2965 uint32_t idx;
2966 pa_bool_t mute;
2967 pa_sink *sink = NULL;
2968 pa_source *source = NULL;
2969 pa_sink_input *si = NULL;
2970 const char *name = NULL;
2971
2972 pa_native_connection_assert_ref(c);
2973 pa_assert(t);
2974
2975 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2976 (command == PA_COMMAND_SET_SINK_MUTE && pa_tagstruct_gets(t, &name) < 0) ||
2977 (command == PA_COMMAND_SET_SOURCE_MUTE && pa_tagstruct_gets(t, &name) < 0) ||
2978 pa_tagstruct_get_boolean(t, &mute) ||
2979 !pa_tagstruct_eof(t)) {
2980 protocol_error(c);
2981 return;
2982 }
2983
2984 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2985 CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
2986 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
2987 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
2988 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2989
2990 switch (command) {
2991
2992 case PA_COMMAND_SET_SINK_MUTE:
2993
2994 if (idx != PA_INVALID_INDEX)
2995 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2996 else
2997 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
2998
2999 break;
3000
3001 case PA_COMMAND_SET_SOURCE_MUTE:
3002 if (idx != PA_INVALID_INDEX)
3003 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
3004 else
3005 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
3006
3007 break;
3008
3009 case PA_COMMAND_SET_SINK_INPUT_MUTE:
3010 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3011 break;
3012
3013 default:
3014 pa_assert_not_reached();
3015 }
3016
3017 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
3018
3019 if (sink)
3020 pa_sink_set_mute(sink, mute);
3021 else if (source)
3022 pa_source_set_mute(source, mute);
3023 else if (si)
3024 pa_sink_input_set_mute(si, mute);
3025
3026 pa_pstream_send_simple_ack(c->pstream, tag);
3027 }
3028
3029 static void command_cork_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3030 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3031 uint32_t idx;
3032 pa_bool_t b;
3033 playback_stream *s;
3034
3035 pa_native_connection_assert_ref(c);
3036 pa_assert(t);
3037
3038 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3039 pa_tagstruct_get_boolean(t, &b) < 0 ||
3040 !pa_tagstruct_eof(t)) {
3041 protocol_error(c);
3042 return;
3043 }
3044
3045 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3046 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3047 s = pa_idxset_get_by_index(c->output_streams, idx);
3048 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3049 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3050
3051 pa_sink_input_cork(s->sink_input, b);
3052 pa_pstream_send_simple_ack(c->pstream, tag);
3053 }
3054
3055 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3056 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3057 uint32_t idx;
3058 playback_stream *s;
3059
3060 pa_native_connection_assert_ref(c);
3061 pa_assert(t);
3062
3063 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3064 !pa_tagstruct_eof(t)) {
3065 protocol_error(c);
3066 return;
3067 }
3068
3069 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3070 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3071 s = pa_idxset_get_by_index(c->output_streams, idx);
3072 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3073 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3074
3075 switch (command) {
3076 case PA_COMMAND_FLUSH_PLAYBACK_STREAM:
3077 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_FLUSH, NULL, 0, NULL);
3078 break;
3079
3080 case PA_COMMAND_PREBUF_PLAYBACK_STREAM:
3081 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_PREBUF_FORCE, NULL, 0, NULL);
3082 break;
3083
3084 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM:
3085 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_TRIGGER, NULL, 0, NULL);
3086 break;
3087
3088 default:
3089 pa_assert_not_reached();
3090 }
3091
3092 pa_pstream_send_simple_ack(c->pstream, tag);
3093 }
3094
3095 static void command_cork_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3096 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3097 uint32_t idx;
3098 record_stream *s;
3099 pa_bool_t b;
3100
3101 pa_native_connection_assert_ref(c);
3102 pa_assert(t);
3103
3104 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3105 pa_tagstruct_get_boolean(t, &b) < 0 ||
3106 !pa_tagstruct_eof(t)) {
3107 protocol_error(c);
3108 return;
3109 }
3110
3111 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3112 s = pa_idxset_get_by_index(c->record_streams, idx);
3113 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3114
3115 pa_source_output_cork(s->source_output, b);
3116 pa_memblockq_prebuf_force(s->memblockq);
3117 pa_pstream_send_simple_ack(c->pstream, tag);
3118 }
3119
3120 static void command_flush_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3121 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3122 uint32_t idx;
3123 record_stream *s;
3124
3125 pa_native_connection_assert_ref(c);
3126 pa_assert(t);
3127
3128 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3129 !pa_tagstruct_eof(t)) {
3130 protocol_error(c);
3131 return;
3132 }
3133
3134 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3135 s = pa_idxset_get_by_index(c->record_streams, idx);
3136 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3137
3138 pa_memblockq_flush_read(s->memblockq);
3139 pa_pstream_send_simple_ack(c->pstream, tag);
3140 }
3141
3142 static void command_set_stream_buffer_attr(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3143 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3144 uint32_t idx;
3145 uint32_t maxlength, tlength, prebuf, minreq, fragsize;
3146 pa_tagstruct *reply;
3147
3148 pa_native_connection_assert_ref(c);
3149 pa_assert(t);
3150
3151 if (pa_tagstruct_getu32(t, &idx) < 0) {
3152 protocol_error(c);
3153 return;
3154 }
3155
3156 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3157
3158 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR) {
3159 playback_stream *s;
3160 pa_bool_t adjust_latency = FALSE;
3161
3162 s = pa_idxset_get_by_index(c->output_streams, idx);
3163 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3164 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3165
3166 if (pa_tagstruct_get(
3167 t,
3168 PA_TAG_U32, &maxlength,
3169 PA_TAG_U32, &tlength,
3170 PA_TAG_U32, &prebuf,
3171 PA_TAG_U32, &minreq,
3172 PA_TAG_INVALID) < 0 ||
3173 (c->version >= 13 && pa_tagstruct_get_boolean(t, &adjust_latency) < 0) ||
3174 !pa_tagstruct_eof(t)) {
3175 protocol_error(c);
3176 return;
3177 }
3178
3179 fix_playback_buffer_attr_pre(s, adjust_latency, &maxlength, &tlength, &prebuf, &minreq);
3180 pa_memblockq_set_maxlength(s->memblockq, maxlength);
3181 pa_memblockq_set_tlength(s->memblockq, tlength);
3182 pa_memblockq_set_prebuf(s->memblockq, prebuf);
3183 pa_memblockq_set_minreq(s->memblockq, minreq);
3184 fix_playback_buffer_attr_post(s, &maxlength, &tlength, &prebuf, &minreq);
3185
3186 reply = reply_new(tag);
3187 pa_tagstruct_putu32(reply, maxlength);
3188 pa_tagstruct_putu32(reply, tlength);
3189 pa_tagstruct_putu32(reply, prebuf);
3190 pa_tagstruct_putu32(reply, minreq);
3191
3192 if (c->version >= 13)
3193 pa_tagstruct_put_usec(reply, s->sink_latency);
3194
3195 } else {
3196 record_stream *s;
3197 pa_bool_t adjust_latency = FALSE;
3198 pa_assert(command == PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR);
3199
3200 s = pa_idxset_get_by_index(c->record_streams, idx);
3201 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3202
3203 if (pa_tagstruct_get(
3204 t,
3205 PA_TAG_U32, &maxlength,
3206 PA_TAG_U32, &fragsize,
3207 PA_TAG_INVALID) < 0 ||
3208 (c->version >= 13 && pa_tagstruct_get_boolean(t, &adjust_latency) < 0) ||
3209 !pa_tagstruct_eof(t)) {
3210 protocol_error(c);
3211 return;
3212 }
3213
3214 fix_record_buffer_attr_pre(s, adjust_latency, &maxlength, &fragsize);
3215 pa_memblockq_set_maxlength(s->memblockq, maxlength);
3216 fix_record_buffer_attr_post(s, &maxlength, &fragsize);
3217
3218 reply = reply_new(tag);
3219 pa_tagstruct_putu32(reply, maxlength);
3220 pa_tagstruct_putu32(reply, fragsize);
3221
3222 if (c->version >= 13)
3223 pa_tagstruct_put_usec(reply, s->source_latency);
3224 }
3225
3226 pa_pstream_send_tagstruct(c->pstream, reply);
3227 }
3228
3229 static void command_update_stream_sample_rate(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3230 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3231 uint32_t idx;
3232 uint32_t rate;
3233
3234 pa_native_connection_assert_ref(c);
3235 pa_assert(t);
3236
3237 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3238 pa_tagstruct_getu32(t, &rate) < 0 ||
3239 !pa_tagstruct_eof(t)) {
3240 protocol_error(c);
3241 return;
3242 }
3243
3244 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3245 CHECK_VALIDITY(c->pstream, rate > 0 && rate <= PA_RATE_MAX, tag, PA_ERR_INVALID);
3246
3247 if (command == PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE) {
3248 playback_stream *s;
3249
3250 s = pa_idxset_get_by_index(c->output_streams, idx);
3251 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3252 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3253
3254 pa_sink_input_set_rate(s->sink_input, rate);
3255
3256 } else {
3257 record_stream *s;
3258 pa_assert(command == PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE);
3259
3260 s = pa_idxset_get_by_index(c->record_streams, idx);
3261 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3262
3263 pa_source_output_set_rate(s->source_output, rate);
3264 }
3265
3266 pa_pstream_send_simple_ack(c->pstream, tag);
3267 }
3268
3269 static void command_update_proplist(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3270 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3271 uint32_t idx;
3272 uint32_t mode;
3273 pa_proplist *p;
3274
3275 pa_native_connection_assert_ref(c);
3276 pa_assert(t);
3277
3278 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3279
3280 p = pa_proplist_new();
3281
3282 if (command == PA_COMMAND_UPDATE_CLIENT_PROPLIST) {
3283
3284 if (pa_tagstruct_getu32(t, &mode) < 0 ||
3285 pa_tagstruct_get_proplist(t, p) < 0 ||
3286 !pa_tagstruct_eof(t)) {
3287 protocol_error(c);
3288 pa_proplist_free(p);
3289 return;
3290 }
3291
3292 } else {
3293
3294 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3295 pa_tagstruct_getu32(t, &mode) < 0 ||
3296 pa_tagstruct_get_proplist(t, p) < 0 ||
3297 !pa_tagstruct_eof(t)) {
3298 protocol_error(c);
3299 pa_proplist_free(p);
3300 return;
3301 }
3302 }
3303
3304 CHECK_VALIDITY(c->pstream, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, tag, PA_ERR_INVALID);
3305
3306 if (command == PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST) {
3307 playback_stream *s;
3308
3309 s = pa_idxset_get_by_index(c->output_streams, idx);
3310 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3311 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3312
3313 pa_proplist_update(s->sink_input->proplist, mode, p);
3314 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->sink_input->index);
3315
3316 } else if (command == PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST) {
3317 record_stream *s;
3318
3319 s = pa_idxset_get_by_index(c->record_streams, idx);
3320 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3321
3322 pa_proplist_update(s->source_output->proplist, mode, p);
3323 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->source_output->index);
3324 } else {
3325 pa_assert(command == PA_COMMAND_UPDATE_CLIENT_PROPLIST);
3326
3327 pa_proplist_update(c->client->proplist, mode, p);
3328 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
3329 }
3330
3331 pa_pstream_send_simple_ack(c->pstream, tag);
3332 }
3333
3334 static void command_remove_proplist(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3335 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3336 uint32_t idx;
3337 unsigned changed = 0;
3338 pa_proplist *p;
3339 pa_strlist *l = NULL;
3340
3341 pa_native_connection_assert_ref(c);
3342 pa_assert(t);
3343
3344 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3345
3346 if (command != PA_COMMAND_REMOVE_CLIENT_PROPLIST) {
3347
3348 if (pa_tagstruct_getu32(t, &idx) < 0) {
3349 protocol_error(c);
3350 return;
3351 }
3352 }
3353
3354 if (command == PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST) {
3355 playback_stream *s;
3356
3357 s = pa_idxset_get_by_index(c->output_streams, idx);
3358 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3359 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3360
3361 p = s->sink_input->proplist;
3362
3363 } else if (command == PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST) {
3364 record_stream *s;
3365
3366 s = pa_idxset_get_by_index(c->record_streams, idx);
3367 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3368
3369 p = s->source_output->proplist;
3370 } else {
3371 pa_assert(command == PA_COMMAND_REMOVE_CLIENT_PROPLIST);
3372
3373 p = c->client->proplist;
3374 }
3375
3376 for (;;) {
3377 const char *k;
3378
3379 if (pa_tagstruct_gets(t, &k) < 0) {
3380 protocol_error(c);
3381 pa_strlist_free(l);
3382 return;
3383 }
3384
3385 if (!k)
3386 break;
3387
3388 l = pa_strlist_prepend(l, k);
3389 }
3390
3391 if (!pa_tagstruct_eof(t)) {
3392 protocol_error(c);
3393 pa_strlist_free(l);
3394 return;
3395 }
3396
3397 for (;;) {
3398 char *z;
3399
3400 l = pa_strlist_pop(l, &z);
3401
3402 if (!z)
3403 break;
3404
3405 changed += pa_proplist_unset(p, z) >= 0;
3406 pa_xfree(z);
3407 }
3408
3409 pa_pstream_send_simple_ack(c->pstream, tag);
3410
3411 if (changed) {
3412 if (command == PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST) {
3413 playback_stream *s;
3414
3415 s = pa_idxset_get_by_index(c->output_streams, idx);
3416 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->sink_input->index);
3417
3418 } else if (command == PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST) {
3419 record_stream *s;
3420
3421 s = pa_idxset_get_by_index(c->record_streams, idx);
3422 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->source_output->index);
3423
3424 } else {
3425 pa_assert(command == PA_COMMAND_REMOVE_CLIENT_PROPLIST);
3426 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
3427 }
3428 }
3429 }
3430
3431 static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3432 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3433 const char *s;
3434
3435 pa_native_connection_assert_ref(c);
3436 pa_assert(t);
3437
3438 if (pa_tagstruct_gets(t, &s) < 0 ||
3439 !pa_tagstruct_eof(t)) {
3440 protocol_error(c);
3441 return;
3442 }
3443
3444 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3445 CHECK_VALIDITY(c->pstream, !s || pa_namereg_is_valid_name(s), tag, PA_ERR_INVALID);
3446
3447 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
3448 pa_pstream_send_simple_ack(c->pstream, tag);
3449 }
3450
3451 static void command_set_stream_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3452 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3453 uint32_t idx;
3454 const char *name;
3455
3456 pa_native_connection_assert_ref(c);
3457 pa_assert(t);
3458
3459 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3460 pa_tagstruct_gets(t, &name) < 0 ||
3461 !pa_tagstruct_eof(t)) {
3462 protocol_error(c);
3463 return;
3464 }
3465
3466 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3467 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
3468
3469 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
3470 playback_stream *s;
3471
3472 s = pa_idxset_get_by_index(c->output_streams, idx);
3473 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3474 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3475
3476 pa_sink_input_set_name(s->sink_input, name);
3477
3478 } else {
3479 record_stream *s;
3480 pa_assert(command == PA_COMMAND_SET_RECORD_STREAM_NAME);
3481
3482 s = pa_idxset_get_by_index(c->record_streams, idx);
3483 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3484
3485 pa_source_output_set_name(s->source_output, name);
3486 }
3487
3488 pa_pstream_send_simple_ack(c->pstream, tag);
3489 }
3490
3491 static void command_kill(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3492 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3493 uint32_t idx;
3494
3495 pa_native_connection_assert_ref(c);
3496 pa_assert(t);
3497
3498 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3499 !pa_tagstruct_eof(t)) {
3500 protocol_error(c);
3501 return;
3502 }
3503
3504 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3505
3506 if (command == PA_COMMAND_KILL_CLIENT) {
3507 pa_client *client;
3508
3509 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
3510 CHECK_VALIDITY(c->pstream, client, tag, PA_ERR_NOENTITY);
3511
3512 pa_native_connection_ref(c);
3513 pa_client_kill(client);
3514
3515 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
3516 pa_sink_input *s;
3517
3518 s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3519 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3520
3521 pa_native_connection_ref(c);
3522 pa_sink_input_kill(s);
3523 } else {
3524 pa_source_output *s;
3525
3526 pa_assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
3527
3528 s = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
3529 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3530
3531 pa_native_connection_ref(c);
3532 pa_source_output_kill(s);
3533 }
3534
3535 pa_pstream_send_simple_ack(c->pstream, tag);
3536 pa_native_connection_unref(c);
3537 }
3538
3539 static void command_load_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3540 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3541 pa_module *m;
3542 const char *name, *argument;
3543 pa_tagstruct *reply;
3544
3545 pa_native_connection_assert_ref(c);
3546 pa_assert(t);
3547
3548 if (pa_tagstruct_gets(t, &name) < 0 ||
3549 pa_tagstruct_gets(t, &argument) < 0 ||
3550 !pa_tagstruct_eof(t)) {
3551 protocol_error(c);
3552 return;
3553 }
3554
3555 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3556 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name) && !strchr(name, '/'), tag, PA_ERR_INVALID);
3557 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
3558
3559 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
3560 pa_pstream_send_error(c->pstream, tag, PA_ERR_MODINITFAILED);
3561 return;
3562 }
3563
3564 reply = reply_new(tag);
3565 pa_tagstruct_putu32(reply, m->index);
3566 pa_pstream_send_tagstruct(c->pstream, reply);
3567 }
3568
3569 static void command_unload_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3570 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3571 uint32_t idx;
3572 pa_module *m;
3573
3574 pa_native_connection_assert_ref(c);
3575 pa_assert(t);
3576
3577 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3578 !pa_tagstruct_eof(t)) {
3579 protocol_error(c);
3580 return;
3581 }
3582
3583 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3584 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
3585 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOENTITY);
3586
3587 pa_module_unload_request(m, FALSE);
3588 pa_pstream_send_simple_ack(c->pstream, tag);
3589 }
3590
3591 static void command_add_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3592 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3593 const char *name, *module, *argument;
3594 uint32_t type;
3595 uint32_t idx;
3596 pa_tagstruct *reply;
3597
3598 pa_native_connection_assert_ref(c);
3599 pa_assert(t);
3600
3601 if (pa_tagstruct_gets(t, &name) < 0 ||
3602 pa_tagstruct_getu32(t, &type) < 0 ||
3603 pa_tagstruct_gets(t, &module) < 0 ||
3604 pa_tagstruct_gets(t, &argument) < 0 ||
3605 !pa_tagstruct_eof(t)) {
3606 protocol_error(c);
3607 return;
3608 }
3609
3610 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3611 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
3612 CHECK_VALIDITY(c->pstream, type == 0 || type == 1, tag, PA_ERR_INVALID);
3613 CHECK_VALIDITY(c->pstream, module && *module && pa_utf8_valid(module), tag, PA_ERR_INVALID);
3614 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
3615
3616 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument, &idx) < 0) {
3617 pa_pstream_send_error(c->pstream, tag, PA_ERR_EXIST);
3618 return;
3619 }
3620
3621 reply = reply_new(tag);
3622 pa_tagstruct_putu32(reply, idx);
3623 pa_pstream_send_tagstruct(c->pstream, reply);
3624 }
3625
3626 static void command_remove_autoload(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3627 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3628 const char *name = NULL;
3629 uint32_t type, idx = PA_IDXSET_INVALID;
3630 int r;
3631
3632 pa_native_connection_assert_ref(c);
3633 pa_assert(t);
3634
3635 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
3636 (pa_tagstruct_gets(t, &name) < 0 ||
3637 pa_tagstruct_getu32(t, &type) < 0)) ||
3638 !pa_tagstruct_eof(t)) {
3639 protocol_error(c);
3640 return;
3641 }
3642
3643 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3644 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
3645 CHECK_VALIDITY(c->pstream, !name || (*name && pa_utf8_valid(name) && (type == 0 || type == 1)), tag, PA_ERR_INVALID);
3646
3647 if (name)
3648 r = pa_autoload_remove_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
3649 else
3650 r = pa_autoload_remove_by_index(c->protocol->core, idx);
3651
3652 CHECK_VALIDITY(c->pstream, r >= 0, tag, PA_ERR_NOENTITY);
3653
3654 pa_pstream_send_simple_ack(c->pstream, tag);
3655 }
3656
3657 static void autoload_fill_tagstruct(pa_tagstruct *t, const pa_autoload_entry *e) {
3658 pa_assert(t && e);
3659
3660 pa_tagstruct_putu32(t, e->index);
3661 pa_tagstruct_puts(t, e->name);
3662 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
3663 pa_tagstruct_puts(t, e->module);
3664 pa_tagstruct_puts(t, e->argument);
3665 }
3666
3667 static void command_get_autoload_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3668 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3669 const pa_autoload_entry *a = NULL;
3670 uint32_t type, idx;
3671 const char *name;
3672 pa_tagstruct *reply;
3673
3674 pa_native_connection_assert_ref(c);
3675 pa_assert(t);
3676
3677 if ((pa_tagstruct_getu32(t, &idx) < 0 &&
3678 (pa_tagstruct_gets(t, &name) < 0 ||
3679 pa_tagstruct_getu32(t, &type) < 0)) ||
3680 !pa_tagstruct_eof(t)) {
3681 protocol_error(c);
3682 return;
3683 }
3684
3685 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3686 CHECK_VALIDITY(c->pstream, name || idx != PA_IDXSET_INVALID, tag, PA_ERR_INVALID);
3687 CHECK_VALIDITY(c->pstream, !name || (*name && (type == 0 || type == 1) && pa_utf8_valid(name)), tag, PA_ERR_INVALID);
3688
3689 if (name)
3690 a = pa_autoload_get_by_name(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE);
3691 else
3692 a = pa_autoload_get_by_index(c->protocol->core, idx);
3693
3694 CHECK_VALIDITY(c->pstream, a, tag, PA_ERR_NOENTITY);
3695
3696 reply = reply_new(tag);
3697 autoload_fill_tagstruct(reply, a);
3698 pa_pstream_send_tagstruct(c->pstream, reply);
3699 }
3700
3701 static void command_get_autoload_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3702 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3703 pa_tagstruct *reply;
3704
3705 pa_native_connection_assert_ref(c);
3706 pa_assert(t);
3707
3708 if (!pa_tagstruct_eof(t)) {
3709 protocol_error(c);
3710 return;
3711 }
3712
3713 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3714
3715 reply = reply_new(tag);
3716
3717 if (c->protocol->core->autoload_hashmap) {
3718 pa_autoload_entry *a;
3719 void *state = NULL;
3720
3721 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state, NULL)))
3722 autoload_fill_tagstruct(reply, a);
3723 }
3724
3725 pa_pstream_send_tagstruct(c->pstream, reply);
3726 }
3727
3728 static void command_move_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3729 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3730 uint32_t idx = PA_INVALID_INDEX, idx_device = PA_INVALID_INDEX;
3731 const char *name_device = NULL;
3732
3733 pa_native_connection_assert_ref(c);
3734 pa_assert(t);
3735
3736 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3737 pa_tagstruct_getu32(t, &idx_device) < 0 ||
3738 pa_tagstruct_gets(t, &name_device) < 0 ||
3739 !pa_tagstruct_eof(t)) {
3740 protocol_error(c);
3741 return;
3742 }
3743
3744 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3745 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3746
3747 CHECK_VALIDITY(c->pstream, !name_device || pa_namereg_is_valid_name(name_device), tag, PA_ERR_INVALID);
3748 CHECK_VALIDITY(c->pstream, idx_device != PA_INVALID_INDEX || name_device, tag, PA_ERR_INVALID);
3749 CHECK_VALIDITY(c->pstream, idx_device == PA_INVALID_INDEX || !name_device, tag, PA_ERR_INVALID);
3750 CHECK_VALIDITY(c->pstream, !name_device || idx_device == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3751
3752 if (command == PA_COMMAND_MOVE_SINK_INPUT) {
3753 pa_sink_input *si = NULL;
3754 pa_sink *sink = NULL;
3755
3756 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3757
3758 if (idx_device != PA_INVALID_INDEX)
3759 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx_device);
3760 else
3761 sink = pa_namereg_get(c->protocol->core, name_device, PA_NAMEREG_SINK, 1);
3762
3763 CHECK_VALIDITY(c->pstream, si && sink, tag, PA_ERR_NOENTITY);
3764
3765 if (pa_sink_input_move_to(si, sink) < 0) {
3766 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3767 return;
3768 }
3769 } else {
3770 pa_source_output *so = NULL;
3771 pa_source *source;
3772
3773 pa_assert(command == PA_COMMAND_MOVE_SOURCE_OUTPUT);
3774
3775 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
3776
3777 if (idx_device != PA_INVALID_INDEX)
3778 source = pa_idxset_get_by_index(c->protocol->core->sources, idx_device);
3779 else
3780 source = pa_namereg_get(c->protocol->core, name_device, PA_NAMEREG_SOURCE, 1);
3781
3782 CHECK_VALIDITY(c->pstream, so && source, tag, PA_ERR_NOENTITY);
3783
3784 if (pa_source_output_move_to(so, source) < 0) {
3785 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3786 return;
3787 }
3788 }
3789
3790 pa_pstream_send_simple_ack(c->pstream, tag);
3791 }
3792
3793 static void command_suspend(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3794 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3795 uint32_t idx = PA_INVALID_INDEX;
3796 const char *name = NULL;
3797 pa_bool_t b;
3798
3799 pa_native_connection_assert_ref(c);
3800 pa_assert(t);
3801
3802 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3803 pa_tagstruct_gets(t, &name) < 0 ||
3804 pa_tagstruct_get_boolean(t, &b) < 0 ||
3805 !pa_tagstruct_eof(t)) {
3806 protocol_error(c);
3807 return;
3808 }
3809
3810 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3811 CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
3812 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
3813 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
3814 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3815
3816 if (command == PA_COMMAND_SUSPEND_SINK) {
3817
3818 if (idx == PA_INVALID_INDEX && name && !*name) {
3819
3820 if (pa_sink_suspend_all(c->protocol->core, b) < 0) {
3821 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3822 return;
3823 }
3824 } else {
3825 pa_sink *sink = NULL;
3826
3827 if (idx != PA_INVALID_INDEX)
3828 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
3829 else
3830 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
3831
3832 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
3833
3834 if (pa_sink_suspend(sink, b) < 0) {
3835 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3836 return;
3837 }
3838 }
3839 } else {
3840
3841 pa_assert(command == PA_COMMAND_SUSPEND_SOURCE);
3842
3843 if (idx == PA_INVALID_INDEX && name && !*name) {
3844
3845 if (pa_source_suspend_all(c->protocol->core, b) < 0) {
3846 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3847 return;
3848 }
3849
3850 } else {
3851 pa_source *source;
3852
3853 if (idx != PA_INVALID_INDEX)
3854 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
3855 else
3856 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
3857
3858 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
3859
3860 if (pa_source_suspend(source, b) < 0) {
3861 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3862 return;
3863 }
3864 }
3865 }
3866
3867 pa_pstream_send_simple_ack(c->pstream, tag);
3868 }
3869
3870 static void command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3871 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3872 uint32_t idx = PA_INVALID_INDEX;
3873 const char *name = NULL;
3874 pa_module *m;
3875 pa_native_protocol_ext_cb_t cb;
3876
3877 pa_native_connection_assert_ref(c);
3878 pa_assert(t);
3879
3880 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3881 pa_tagstruct_gets(t, &name) < 0) {
3882 protocol_error(c);
3883 return;
3884 }
3885
3886 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3887 CHECK_VALIDITY(c->pstream, !name || pa_utf8_valid(name), tag, PA_ERR_INVALID);
3888 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
3889 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
3890 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3891
3892 if (idx != PA_INVALID_INDEX)
3893 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
3894 else {
3895 for (m = pa_idxset_first(c->protocol->core->modules, &idx); m; m = pa_idxset_next(c->protocol->core->modules, &idx))
3896 if (strcmp(name, m->name) == 0)
3897 break;
3898 }
3899
3900 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOEXTENSION);
3901 CHECK_VALIDITY(c->pstream, m->load_once || idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3902
3903 cb = (pa_native_protocol_ext_cb_t) pa_hashmap_get(c->protocol->extensions, m);
3904 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOEXTENSION);
3905
3906 if (cb(c->protocol, m, c, tag, t) < 0)
3907 protocol_error(c);
3908 }
3909
3910
3911 /*** pstream callbacks ***/
3912
3913 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
3914 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3915
3916 pa_assert(p);
3917 pa_assert(packet);
3918 pa_native_connection_assert_ref(c);
3919
3920 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0) {
3921 pa_log("invalid packet.");
3922 native_connection_unlink(c);
3923 }
3924 }
3925
3926 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) {
3927 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3928 output_stream *stream;
3929
3930 pa_assert(p);
3931 pa_assert(chunk);
3932 pa_native_connection_assert_ref(c);
3933
3934 if (!(stream = OUTPUT_STREAM(pa_idxset_get_by_index(c->output_streams, channel)))) {
3935 pa_log("client sent block for invalid stream.");
3936 /* Ignoring */
3937 return;
3938 }
3939
3940 if (playback_stream_isinstance(stream)) {
3941 playback_stream *ps = PLAYBACK_STREAM(stream);
3942
3943 if (seek != PA_SEEK_RELATIVE || offset != 0)
3944 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);
3945
3946 pa_asyncmsgq_post(ps->sink_input->sink->asyncmsgq, PA_MSGOBJECT(ps->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
3947
3948 } else {
3949 upload_stream *u = UPLOAD_STREAM(stream);
3950 size_t l;
3951
3952 if (!u->memchunk.memblock) {
3953 if (u->length == chunk->length) {
3954 u->memchunk = *chunk;
3955 pa_memblock_ref(u->memchunk.memblock);
3956 u->length = 0;
3957 } else {
3958 u->memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, u->length);
3959 u->memchunk.index = u->memchunk.length = 0;
3960 }
3961 }
3962
3963 pa_assert(u->memchunk.memblock);
3964
3965 l = u->length;
3966 if (l > chunk->length)
3967 l = chunk->length;
3968
3969
3970 if (l > 0) {
3971 void *src, *dst;
3972 dst = pa_memblock_acquire(u->memchunk.memblock);
3973 src = pa_memblock_acquire(chunk->memblock);
3974
3975 memcpy((uint8_t*) dst + u->memchunk.index + u->memchunk.length,
3976 (uint8_t*) src+chunk->index, l);
3977
3978 pa_memblock_release(u->memchunk.memblock);
3979 pa_memblock_release(chunk->memblock);
3980
3981 u->memchunk.length += l;
3982 u->length -= l;
3983 }
3984 }
3985 }
3986
3987 static void pstream_die_callback(pa_pstream *p, void *userdata) {
3988 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3989
3990 pa_assert(p);
3991 pa_native_connection_assert_ref(c);
3992
3993 native_connection_unlink(c);
3994 pa_log_info("Connection died.");
3995 }
3996
3997 static void pstream_drain_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_send_memblock(c);
4004 }
4005
4006 static void pstream_revoke_callback(pa_pstream *p, uint32_t block_id, void *userdata) {
4007 pa_thread_mq *q;
4008
4009 if (!(q = pa_thread_mq_get()))
4010 pa_pstream_send_revoke(p, block_id);
4011 else
4012 pa_asyncmsgq_post(q->outq, PA_MSGOBJECT(userdata), CONNECTION_MESSAGE_REVOKE, PA_UINT_TO_PTR(block_id), 0, NULL, NULL);
4013 }
4014
4015 static void pstream_release_callback(pa_pstream *p, uint32_t block_id, void *userdata) {
4016 pa_thread_mq *q;
4017
4018 if (!(q = pa_thread_mq_get()))
4019 pa_pstream_send_release(p, block_id);
4020 else
4021 pa_asyncmsgq_post(q->outq, PA_MSGOBJECT(userdata), CONNECTION_MESSAGE_RELEASE, PA_UINT_TO_PTR(block_id), 0, NULL, NULL);
4022 }
4023
4024 /*** client callbacks ***/
4025
4026 static void client_kill_cb(pa_client *c) {
4027 pa_assert(c);
4028
4029 native_connection_unlink(PA_NATIVE_CONNECTION(c->userdata));
4030 pa_log_info("Connection killed.");
4031 }
4032
4033 /*** module entry points ***/
4034
4035 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
4036 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
4037
4038 pa_assert(m);
4039 pa_assert(tv);
4040 pa_native_connection_assert_ref(c);
4041 pa_assert(c->auth_timeout_event == e);
4042
4043 if (!c->authorized) {
4044 native_connection_unlink(c);
4045 pa_log_info("Connection terminated due to authentication timeout.");
4046 }
4047 }
4048
4049 void pa_native_protocol_connect(pa_native_protocol *p, pa_iochannel *io, pa_native_options *o) {
4050 pa_native_connection *c;
4051 char cname[256], pname[128];
4052
4053 pa_assert(p);
4054 pa_assert(io);
4055 pa_assert(o);
4056
4057 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
4058 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
4059 pa_iochannel_free(io);
4060 return;
4061 }
4062
4063 c = pa_msgobject_new(pa_native_connection);
4064 c->parent.parent.free = native_connection_free;
4065 c->parent.process_msg = native_connection_process_msg;
4066 c->protocol = p;
4067 c->options = pa_native_options_ref(o);
4068 c->authorized = FALSE;
4069
4070 if (o->auth_anonymous) {
4071 pa_log_info("Client authenticated anonymously.");
4072 c->authorized = TRUE;
4073 }
4074
4075 if (!c->authorized &&
4076 o->auth_ip_acl &&
4077 pa_ip_acl_check(o->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
4078
4079 pa_log_info("Client authenticated by IP ACL.");
4080 c->authorized = TRUE;
4081 }
4082
4083 if (!c->authorized) {
4084 struct timeval tv;
4085 pa_gettimeofday(&tv);
4086 tv.tv_sec += AUTH_TIMEOUT;
4087 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
4088 } else
4089 c->auth_timeout_event = NULL;
4090
4091 c->is_local = pa_iochannel_socket_is_local(io);
4092 c->version = 8;
4093
4094 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
4095 pa_snprintf(cname, sizeof(cname), "Native client (%s)", pname);
4096 c->client = pa_client_new(p->core, __FILE__, cname);
4097 pa_proplist_sets(c->client->proplist, "native-protocol.peer", pname);
4098 c->client->kill = client_kill_cb;
4099 c->client->userdata = c;
4100 c->client->module = o->module;
4101
4102 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->mempool);
4103 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
4104 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
4105 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
4106 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
4107 pa_pstream_set_revoke_callback(c->pstream, pstream_revoke_callback, c);
4108 pa_pstream_set_release_callback(c->pstream, pstream_release_callback, c);
4109
4110 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
4111
4112 c->record_streams = pa_idxset_new(NULL, NULL);
4113 c->output_streams = pa_idxset_new(NULL, NULL);
4114
4115 c->rrobin_index = PA_IDXSET_INVALID;
4116 c->subscription = NULL;
4117
4118 pa_idxset_put(p->connections, c, NULL);
4119
4120 #ifdef HAVE_CREDS
4121 if (pa_iochannel_creds_supported(io))
4122 pa_iochannel_creds_enable(io);
4123 #endif
4124
4125 pa_hook_fire(&p->hooks[PA_NATIVE_HOOK_CONNECTION_PUT], c);
4126 }
4127
4128 void pa_native_protocol_disconnect(pa_native_protocol *p, pa_module *m) {
4129 pa_native_connection *c;
4130 void *state = NULL;
4131
4132 pa_assert(p);
4133 pa_assert(m);
4134
4135 while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
4136 if (c->options->module == m)
4137 native_connection_unlink(c);
4138 }
4139
4140 static pa_native_protocol* native_protocol_new(pa_core *c) {
4141 pa_native_protocol *p;
4142 pa_native_hook_t h;
4143
4144 pa_assert(c);
4145
4146 p = pa_xnew(pa_native_protocol, 1);
4147 PA_REFCNT_INIT(p);
4148 p->core = c;
4149 p->connections = pa_idxset_new(NULL, NULL);
4150
4151 p->servers = NULL;
4152
4153 p->extensions = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
4154
4155 for (h = 0; h < PA_NATIVE_HOOK_MAX; h++)
4156 pa_hook_init(&p->hooks[h], p);
4157
4158 pa_assert_se(pa_shared_set(c, "native-protocol", p) >= 0);
4159
4160 return p;
4161 }
4162
4163 pa_native_protocol* pa_native_protocol_get(pa_core *c) {
4164 pa_native_protocol *p;
4165
4166 if ((p = pa_shared_get(c, "native-protocol")))
4167 return pa_native_protocol_ref(p);
4168
4169 return native_protocol_new(c);
4170 }
4171
4172 pa_native_protocol* pa_native_protocol_ref(pa_native_protocol *p) {
4173 pa_assert(p);
4174 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4175
4176 PA_REFCNT_INC(p);
4177
4178 return p;
4179 }
4180
4181 void pa_native_protocol_unref(pa_native_protocol *p) {
4182 pa_native_connection *c;
4183 pa_native_hook_t h;
4184
4185 pa_assert(p);
4186 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4187
4188 if (PA_REFCNT_DEC(p) > 0)
4189 return;
4190
4191 while ((c = pa_idxset_first(p->connections, NULL)))
4192 native_connection_unlink(c);
4193
4194 pa_idxset_free(p->connections, NULL, NULL);
4195
4196 pa_strlist_free(p->servers);
4197
4198 for (h = 0; h < PA_NATIVE_HOOK_MAX; h++)
4199 pa_hook_done(&p->hooks[h]);
4200
4201 pa_hashmap_free(p->extensions, NULL, NULL);
4202
4203 pa_assert_se(pa_shared_remove(p->core, "native-protocol") >= 0);
4204
4205 pa_xfree(p);
4206 }
4207
4208 void pa_native_protocol_add_server_string(pa_native_protocol *p, const char *name) {
4209 pa_assert(p);
4210 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4211 pa_assert(name);
4212
4213 p->servers = pa_strlist_prepend(p->servers, name);
4214
4215 pa_hook_fire(&p->hooks[PA_NATIVE_HOOK_SERVERS_CHANGED], p->servers);
4216 }
4217
4218 void pa_native_protocol_remove_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_remove(p->servers, name);
4224
4225 pa_hook_fire(&p->hooks[PA_NATIVE_HOOK_SERVERS_CHANGED], p->servers);
4226 }
4227
4228 pa_hook *pa_native_protocol_hooks(pa_native_protocol *p) {
4229 pa_assert(p);
4230 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4231
4232 return p->hooks;
4233 }
4234
4235 pa_strlist *pa_native_protocol_servers(pa_native_protocol *p) {
4236 pa_assert(p);
4237 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4238
4239 return p->servers;
4240 }
4241
4242 int pa_native_protocol_install_ext(pa_native_protocol *p, pa_module *m, pa_native_protocol_ext_cb_t cb) {
4243 pa_assert(p);
4244 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4245 pa_assert(m);
4246 pa_assert(cb);
4247 pa_assert(!pa_hashmap_get(p->extensions, m));
4248
4249 pa_assert_se(pa_hashmap_put(p->extensions, m, (void*) cb) == 0);
4250 return 0;
4251 }
4252
4253 void pa_native_protocol_remove_ext(pa_native_protocol *p, pa_module *m) {
4254 pa_assert(p);
4255 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4256 pa_assert(m);
4257
4258 pa_assert_se(pa_hashmap_remove(p->extensions, m));
4259 }
4260
4261 pa_native_options* pa_native_options_new(void) {
4262 pa_native_options *o;
4263
4264 o = pa_xnew0(pa_native_options, 1);
4265 PA_REFCNT_INIT(o);
4266
4267 return o;
4268 }
4269
4270 pa_native_options* pa_native_options_ref(pa_native_options *o) {
4271 pa_assert(o);
4272 pa_assert(PA_REFCNT_VALUE(o) >= 1);
4273
4274 PA_REFCNT_INC(o);
4275
4276 return o;
4277 }
4278
4279 void pa_native_options_unref(pa_native_options *o) {
4280 pa_assert(o);
4281 pa_assert(PA_REFCNT_VALUE(o) >= 1);
4282
4283 if (PA_REFCNT_DEC(o) > 0)
4284 return;
4285
4286 pa_xfree(o->auth_group);
4287
4288 if (o->auth_ip_acl)
4289 pa_ip_acl_free(o->auth_ip_acl);
4290
4291 if (o->auth_cookie)
4292 pa_auth_cookie_unref(o->auth_cookie);
4293
4294 pa_xfree(o);
4295 }
4296
4297 int pa_native_options_parse(pa_native_options *o, pa_core *c, pa_modargs *ma) {
4298 pa_bool_t enabled;
4299 const char *acl;
4300
4301 pa_assert(o);
4302 pa_assert(PA_REFCNT_VALUE(o) >= 1);
4303 pa_assert(ma);
4304
4305 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &o->auth_anonymous) < 0) {
4306 pa_log("auth-anonymous= expects a boolean argument.");
4307 return -1;
4308 }
4309
4310 enabled = TRUE;
4311 if (pa_modargs_get_value_boolean(ma, "auth-group-enabled", &enabled) < 0) {
4312 pa_log("auth-group-enabled= expects a boolean argument.");
4313 return -1;
4314 }
4315
4316 pa_xfree(o->auth_group);
4317 o->auth_group = enabled ? pa_xstrdup(pa_modargs_get_value(ma, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP : NULL)) : NULL;
4318
4319 #ifndef HAVE_CREDS
4320 if (o->auth_group)
4321 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4322 #endif
4323
4324 if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
4325 pa_ip_acl *ipa;
4326
4327 if (!(ipa = pa_ip_acl_new(acl))) {
4328 pa_log("Failed to parse IP ACL '%s'", acl);
4329 return -1;
4330 }
4331
4332 if (o->auth_ip_acl)
4333 pa_ip_acl_free(o->auth_ip_acl);
4334
4335 o->auth_ip_acl = ipa;
4336 }
4337
4338 enabled = TRUE;
4339 if (pa_modargs_get_value_boolean(ma, "auth-cookie-enabled", &enabled) < 0) {
4340 pa_log("auth-cookie-enabled= expects a boolean argument.");
4341 return -1;
4342 }
4343
4344 if (o->auth_cookie)
4345 pa_auth_cookie_unref(o->auth_cookie);
4346
4347 if (enabled) {
4348 const char *cn;
4349
4350 /* The new name for this is 'auth-cookie', for compat reasons
4351 * we check the old name too */
4352 if (!(cn = pa_modargs_get_value(ma, "auth-cookie", NULL)))
4353 if (!(cn = pa_modargs_get_value(ma, "cookie", NULL)))
4354 cn = PA_NATIVE_COOKIE_FILE;
4355
4356 if (!(o->auth_cookie = pa_auth_cookie_get(c, cn, PA_NATIVE_COOKIE_LENGTH)))
4357 return -1;
4358
4359 } else
4360 o->auth_cookie = NULL;
4361
4362 return 0;
4363 }
4364
4365 pa_pstream* pa_native_connection_get_pstream(pa_native_connection *c) {
4366 pa_native_connection_assert_ref(c);
4367
4368 return c->pstream;
4369 }