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