]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-native.c
Add support for 24bit samples encoded in the LSB of 32 bit words
[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
2734 static void module_fill_tagstruct(pa_tagstruct *t, pa_module *module) {
2735 pa_assert(t);
2736 pa_assert(module);
2737
2738 pa_tagstruct_putu32(t, module->index);
2739 pa_tagstruct_puts(t, module->name);
2740 pa_tagstruct_puts(t, module->argument);
2741 pa_tagstruct_putu32(t, (uint32_t) pa_module_get_n_used(module));
2742 pa_tagstruct_put_boolean(t, FALSE); /* autoload is obsolete */
2743 }
2744
2745 static void sink_input_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_sink_input *s) {
2746 pa_sample_spec fixed_ss;
2747 pa_usec_t sink_latency;
2748
2749 pa_assert(t);
2750 pa_sink_input_assert_ref(s);
2751
2752 fixup_sample_spec(c, &fixed_ss, &s->sample_spec);
2753
2754 pa_tagstruct_putu32(t, s->index);
2755 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME)));
2756 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
2757 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
2758 pa_tagstruct_putu32(t, s->sink->index);
2759 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2760 pa_tagstruct_put_channel_map(t, &s->channel_map);
2761 pa_tagstruct_put_cvolume(t, pa_sink_input_get_volume(s));
2762 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s, &sink_latency));
2763 pa_tagstruct_put_usec(t, sink_latency);
2764 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
2765 pa_tagstruct_puts(t, s->driver);
2766 if (c->version >= 11)
2767 pa_tagstruct_put_boolean(t, pa_sink_input_get_mute(s));
2768 if (c->version >= 13)
2769 pa_tagstruct_put_proplist(t, s->proplist);
2770 }
2771
2772 static void source_output_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_source_output *s) {
2773 pa_sample_spec fixed_ss;
2774 pa_usec_t source_latency;
2775
2776 pa_assert(t);
2777 pa_source_output_assert_ref(s);
2778
2779 fixup_sample_spec(c, &fixed_ss, &s->sample_spec);
2780
2781 pa_tagstruct_putu32(t, s->index);
2782 pa_tagstruct_puts(t, pa_strnull(pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME)));
2783 pa_tagstruct_putu32(t, s->module ? s->module->index : PA_INVALID_INDEX);
2784 pa_tagstruct_putu32(t, s->client ? s->client->index : PA_INVALID_INDEX);
2785 pa_tagstruct_putu32(t, s->source->index);
2786 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2787 pa_tagstruct_put_channel_map(t, &s->channel_map);
2788 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s, &source_latency));
2789 pa_tagstruct_put_usec(t, source_latency);
2790 pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
2791 pa_tagstruct_puts(t, s->driver);
2792
2793 if (c->version >= 13)
2794 pa_tagstruct_put_proplist(t, s->proplist);
2795 }
2796
2797 static void scache_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_scache_entry *e) {
2798 pa_sample_spec fixed_ss;
2799
2800 pa_assert(t);
2801 pa_assert(e);
2802
2803 if (e->memchunk.memblock)
2804 fixup_sample_spec(c, &fixed_ss, &e->sample_spec);
2805 else
2806 memset(&fixed_ss, 0, sizeof(fixed_ss));
2807
2808 pa_tagstruct_putu32(t, e->index);
2809 pa_tagstruct_puts(t, e->name);
2810 pa_tagstruct_put_cvolume(t, &e->volume);
2811 pa_tagstruct_put_usec(t, e->memchunk.memblock ? pa_bytes_to_usec(e->memchunk.length, &e->sample_spec) : 0);
2812 pa_tagstruct_put_sample_spec(t, &fixed_ss);
2813 pa_tagstruct_put_channel_map(t, &e->channel_map);
2814 pa_tagstruct_putu32(t, (uint32_t) e->memchunk.length);
2815 pa_tagstruct_put_boolean(t, e->lazy);
2816 pa_tagstruct_puts(t, e->filename);
2817
2818 if (c->version >= 13)
2819 pa_tagstruct_put_proplist(t, e->proplist);
2820 }
2821
2822 static void command_get_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2823 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2824 uint32_t idx;
2825 pa_sink *sink = NULL;
2826 pa_source *source = NULL;
2827 pa_client *client = NULL;
2828 pa_module *module = NULL;
2829 pa_sink_input *si = NULL;
2830 pa_source_output *so = NULL;
2831 pa_scache_entry *sce = NULL;
2832 const char *name = NULL;
2833 pa_tagstruct *reply;
2834
2835 pa_native_connection_assert_ref(c);
2836 pa_assert(t);
2837
2838 if (pa_tagstruct_getu32(t, &idx) < 0 ||
2839 (command != PA_COMMAND_GET_CLIENT_INFO &&
2840 command != PA_COMMAND_GET_MODULE_INFO &&
2841 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
2842 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
2843 pa_tagstruct_gets(t, &name) < 0) ||
2844 !pa_tagstruct_eof(t)) {
2845 protocol_error(c);
2846 return;
2847 }
2848
2849 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2850 CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
2851 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
2852 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
2853 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
2854
2855 if (command == PA_COMMAND_GET_SINK_INFO) {
2856 if (idx != PA_INVALID_INDEX)
2857 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
2858 else
2859 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK);
2860 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
2861 if (idx != PA_INVALID_INDEX)
2862 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
2863 else
2864 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE);
2865 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
2866 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
2867 else if (command == PA_COMMAND_GET_MODULE_INFO)
2868 module = pa_idxset_get_by_index(c->protocol->core->modules, idx);
2869 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
2870 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
2871 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
2872 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
2873 else {
2874 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO);
2875 if (idx != PA_INVALID_INDEX)
2876 sce = pa_idxset_get_by_index(c->protocol->core->scache, idx);
2877 else
2878 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE);
2879 }
2880
2881 if (!sink && !source && !client && !module && !si && !so && !sce) {
2882 pa_pstream_send_error(c->pstream, tag, PA_ERR_NOENTITY);
2883 return;
2884 }
2885
2886 reply = reply_new(tag);
2887 if (sink)
2888 sink_fill_tagstruct(c, reply, sink);
2889 else if (source)
2890 source_fill_tagstruct(c, reply, source);
2891 else if (client)
2892 client_fill_tagstruct(c, reply, client);
2893 else if (module)
2894 module_fill_tagstruct(reply, module);
2895 else if (si)
2896 sink_input_fill_tagstruct(c, reply, si);
2897 else if (so)
2898 source_output_fill_tagstruct(c, reply, so);
2899 else
2900 scache_fill_tagstruct(c, reply, sce);
2901 pa_pstream_send_tagstruct(c->pstream, reply);
2902 }
2903
2904 static void command_get_info_list(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2905 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2906 pa_idxset *i;
2907 uint32_t idx;
2908 void *p;
2909 pa_tagstruct *reply;
2910
2911 pa_native_connection_assert_ref(c);
2912 pa_assert(t);
2913
2914 if (!pa_tagstruct_eof(t)) {
2915 protocol_error(c);
2916 return;
2917 }
2918
2919 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2920
2921 reply = reply_new(tag);
2922
2923 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
2924 i = c->protocol->core->sinks;
2925 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
2926 i = c->protocol->core->sources;
2927 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
2928 i = c->protocol->core->clients;
2929 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
2930 i = c->protocol->core->modules;
2931 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
2932 i = c->protocol->core->sink_inputs;
2933 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
2934 i = c->protocol->core->source_outputs;
2935 else {
2936 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
2937 i = c->protocol->core->scache;
2938 }
2939
2940 if (i) {
2941 for (p = pa_idxset_first(i, &idx); p; p = pa_idxset_next(i, &idx)) {
2942 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
2943 sink_fill_tagstruct(c, reply, p);
2944 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
2945 source_fill_tagstruct(c, reply, p);
2946 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
2947 client_fill_tagstruct(c, reply, p);
2948 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
2949 module_fill_tagstruct(reply, p);
2950 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
2951 sink_input_fill_tagstruct(c, reply, p);
2952 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
2953 source_output_fill_tagstruct(c, reply, p);
2954 else {
2955 pa_assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
2956 scache_fill_tagstruct(c, reply, p);
2957 }
2958 }
2959 }
2960
2961 pa_pstream_send_tagstruct(c->pstream, reply);
2962 }
2963
2964 static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2965 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
2966 pa_tagstruct *reply;
2967 char txt[256];
2968 const char *n;
2969 pa_sample_spec fixed_ss;
2970
2971 pa_native_connection_assert_ref(c);
2972 pa_assert(t);
2973
2974 if (!pa_tagstruct_eof(t)) {
2975 protocol_error(c);
2976 return;
2977 }
2978
2979 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
2980
2981 reply = reply_new(tag);
2982 pa_tagstruct_puts(reply, PACKAGE_NAME);
2983 pa_tagstruct_puts(reply, PACKAGE_VERSION);
2984 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
2985 pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
2986
2987 fixup_sample_spec(c, &fixed_ss, &c->protocol->core->default_sample_spec);
2988 pa_tagstruct_put_sample_spec(reply, &fixed_ss);
2989
2990 n = pa_namereg_get_default_sink_name(c->protocol->core);
2991 pa_tagstruct_puts(reply, n);
2992 n = pa_namereg_get_default_source_name(c->protocol->core);
2993 pa_tagstruct_puts(reply, n);
2994
2995 pa_tagstruct_putu32(reply, c->protocol->core->cookie);
2996
2997 pa_pstream_send_tagstruct(c->pstream, reply);
2998 }
2999
3000 static void subscription_cb(pa_core *core, pa_subscription_event_type_t e, uint32_t idx, void *userdata) {
3001 pa_tagstruct *t;
3002 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3003
3004 pa_native_connection_assert_ref(c);
3005
3006 t = pa_tagstruct_new(NULL, 0);
3007 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
3008 pa_tagstruct_putu32(t, (uint32_t) -1);
3009 pa_tagstruct_putu32(t, e);
3010 pa_tagstruct_putu32(t, idx);
3011 pa_pstream_send_tagstruct(c->pstream, t);
3012 }
3013
3014 static void command_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3015 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3016 pa_subscription_mask_t m;
3017
3018 pa_native_connection_assert_ref(c);
3019 pa_assert(t);
3020
3021 if (pa_tagstruct_getu32(t, &m) < 0 ||
3022 !pa_tagstruct_eof(t)) {
3023 protocol_error(c);
3024 return;
3025 }
3026
3027 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3028 CHECK_VALIDITY(c->pstream, (m & ~PA_SUBSCRIPTION_MASK_ALL) == 0, tag, PA_ERR_INVALID);
3029
3030 if (c->subscription)
3031 pa_subscription_free(c->subscription);
3032
3033 if (m != 0) {
3034 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
3035 pa_assert(c->subscription);
3036 } else
3037 c->subscription = NULL;
3038
3039 pa_pstream_send_simple_ack(c->pstream, tag);
3040 }
3041
3042 static void command_set_volume(
3043 pa_pdispatch *pd,
3044 uint32_t command,
3045 uint32_t tag,
3046 pa_tagstruct *t,
3047 void *userdata) {
3048
3049 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3050 uint32_t idx;
3051 pa_cvolume volume;
3052 pa_sink *sink = NULL;
3053 pa_source *source = NULL;
3054 pa_sink_input *si = NULL;
3055 const char *name = NULL;
3056
3057 pa_native_connection_assert_ref(c);
3058 pa_assert(t);
3059
3060 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3061 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
3062 (command == PA_COMMAND_SET_SOURCE_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
3063 pa_tagstruct_get_cvolume(t, &volume) ||
3064 !pa_tagstruct_eof(t)) {
3065 protocol_error(c);
3066 return;
3067 }
3068
3069 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3070 CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
3071 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
3072 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
3073 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3074 CHECK_VALIDITY(c->pstream, pa_cvolume_valid(&volume), tag, PA_ERR_INVALID);
3075
3076 switch (command) {
3077
3078 case PA_COMMAND_SET_SINK_VOLUME:
3079 if (idx != PA_INVALID_INDEX)
3080 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
3081 else
3082 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK);
3083 break;
3084
3085 case PA_COMMAND_SET_SOURCE_VOLUME:
3086 if (idx != PA_INVALID_INDEX)
3087 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
3088 else
3089 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE);
3090 break;
3091
3092 case PA_COMMAND_SET_SINK_INPUT_VOLUME:
3093 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3094 break;
3095
3096 default:
3097 pa_assert_not_reached();
3098 }
3099
3100 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
3101
3102 if (sink)
3103 pa_sink_set_volume(sink, &volume);
3104 else if (source)
3105 pa_source_set_volume(source, &volume);
3106 else if (si)
3107 pa_sink_input_set_volume(si, &volume);
3108
3109 pa_pstream_send_simple_ack(c->pstream, tag);
3110 }
3111
3112 static void command_set_mute(
3113 pa_pdispatch *pd,
3114 uint32_t command,
3115 uint32_t tag,
3116 pa_tagstruct *t,
3117 void *userdata) {
3118
3119 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3120 uint32_t idx;
3121 pa_bool_t mute;
3122 pa_sink *sink = NULL;
3123 pa_source *source = NULL;
3124 pa_sink_input *si = NULL;
3125 const char *name = NULL;
3126
3127 pa_native_connection_assert_ref(c);
3128 pa_assert(t);
3129
3130 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3131 (command == PA_COMMAND_SET_SINK_MUTE && pa_tagstruct_gets(t, &name) < 0) ||
3132 (command == PA_COMMAND_SET_SOURCE_MUTE && pa_tagstruct_gets(t, &name) < 0) ||
3133 pa_tagstruct_get_boolean(t, &mute) ||
3134 !pa_tagstruct_eof(t)) {
3135 protocol_error(c);
3136 return;
3137 }
3138
3139 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3140 CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID);
3141 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
3142 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
3143 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3144
3145 switch (command) {
3146
3147 case PA_COMMAND_SET_SINK_MUTE:
3148
3149 if (idx != PA_INVALID_INDEX)
3150 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
3151 else
3152 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK);
3153
3154 break;
3155
3156 case PA_COMMAND_SET_SOURCE_MUTE:
3157 if (idx != PA_INVALID_INDEX)
3158 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
3159 else
3160 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE);
3161
3162 break;
3163
3164 case PA_COMMAND_SET_SINK_INPUT_MUTE:
3165 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3166 break;
3167
3168 default:
3169 pa_assert_not_reached();
3170 }
3171
3172 CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
3173
3174 if (sink)
3175 pa_sink_set_mute(sink, mute);
3176 else if (source)
3177 pa_source_set_mute(source, mute);
3178 else if (si)
3179 pa_sink_input_set_mute(si, mute);
3180
3181 pa_pstream_send_simple_ack(c->pstream, tag);
3182 }
3183
3184 static void command_cork_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3185 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3186 uint32_t idx;
3187 pa_bool_t b;
3188 playback_stream *s;
3189
3190 pa_native_connection_assert_ref(c);
3191 pa_assert(t);
3192
3193 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3194 pa_tagstruct_get_boolean(t, &b) < 0 ||
3195 !pa_tagstruct_eof(t)) {
3196 protocol_error(c);
3197 return;
3198 }
3199
3200 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3201 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3202 s = pa_idxset_get_by_index(c->output_streams, idx);
3203 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3204 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3205
3206 pa_sink_input_cork(s->sink_input, b);
3207
3208 if (b)
3209 s->is_underrun = TRUE;
3210
3211 pa_pstream_send_simple_ack(c->pstream, tag);
3212 }
3213
3214 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3215 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3216 uint32_t idx;
3217 playback_stream *s;
3218
3219 pa_native_connection_assert_ref(c);
3220 pa_assert(t);
3221
3222 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3223 !pa_tagstruct_eof(t)) {
3224 protocol_error(c);
3225 return;
3226 }
3227
3228 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3229 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3230 s = pa_idxset_get_by_index(c->output_streams, idx);
3231 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3232 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3233
3234 switch (command) {
3235 case PA_COMMAND_FLUSH_PLAYBACK_STREAM:
3236 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_FLUSH, NULL, 0, NULL);
3237 break;
3238
3239 case PA_COMMAND_PREBUF_PLAYBACK_STREAM:
3240 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_PREBUF_FORCE, NULL, 0, NULL);
3241 break;
3242
3243 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM:
3244 pa_asyncmsgq_send(s->sink_input->sink->asyncmsgq, PA_MSGOBJECT(s->sink_input), SINK_INPUT_MESSAGE_TRIGGER, NULL, 0, NULL);
3245 break;
3246
3247 default:
3248 pa_assert_not_reached();
3249 }
3250
3251 pa_pstream_send_simple_ack(c->pstream, tag);
3252 }
3253
3254 static void command_cork_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3255 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3256 uint32_t idx;
3257 record_stream *s;
3258 pa_bool_t b;
3259
3260 pa_native_connection_assert_ref(c);
3261 pa_assert(t);
3262
3263 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3264 pa_tagstruct_get_boolean(t, &b) < 0 ||
3265 !pa_tagstruct_eof(t)) {
3266 protocol_error(c);
3267 return;
3268 }
3269
3270 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3271 s = pa_idxset_get_by_index(c->record_streams, idx);
3272 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3273
3274 pa_source_output_cork(s->source_output, b);
3275 pa_memblockq_prebuf_force(s->memblockq);
3276 pa_pstream_send_simple_ack(c->pstream, tag);
3277 }
3278
3279 static void command_flush_record_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3280 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3281 uint32_t idx;
3282 record_stream *s;
3283
3284 pa_native_connection_assert_ref(c);
3285 pa_assert(t);
3286
3287 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3288 !pa_tagstruct_eof(t)) {
3289 protocol_error(c);
3290 return;
3291 }
3292
3293 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3294 s = pa_idxset_get_by_index(c->record_streams, idx);
3295 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3296
3297 pa_memblockq_flush_read(s->memblockq);
3298 pa_pstream_send_simple_ack(c->pstream, tag);
3299 }
3300
3301 static void command_set_stream_buffer_attr(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3302 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3303 uint32_t idx;
3304 uint32_t maxlength, tlength, prebuf, minreq, fragsize;
3305 pa_tagstruct *reply;
3306
3307 pa_native_connection_assert_ref(c);
3308 pa_assert(t);
3309
3310 if (pa_tagstruct_getu32(t, &idx) < 0) {
3311 protocol_error(c);
3312 return;
3313 }
3314
3315 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3316
3317 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR) {
3318 playback_stream *s;
3319 pa_bool_t adjust_latency = FALSE, early_requests = FALSE;
3320
3321 s = pa_idxset_get_by_index(c->output_streams, idx);
3322 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3323 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3324
3325 if (pa_tagstruct_get(
3326 t,
3327 PA_TAG_U32, &maxlength,
3328 PA_TAG_U32, &tlength,
3329 PA_TAG_U32, &prebuf,
3330 PA_TAG_U32, &minreq,
3331 PA_TAG_INVALID) < 0 ||
3332 (c->version >= 13 && pa_tagstruct_get_boolean(t, &adjust_latency) < 0) ||
3333 (c->version >= 14 && pa_tagstruct_get_boolean(t, &early_requests) < 0) ||
3334 !pa_tagstruct_eof(t)) {
3335 protocol_error(c);
3336 return;
3337 }
3338
3339 fix_playback_buffer_attr_pre(s, adjust_latency, early_requests, &maxlength, &tlength, &prebuf, &minreq);
3340 pa_memblockq_set_maxlength(s->memblockq, maxlength);
3341 pa_memblockq_set_tlength(s->memblockq, tlength);
3342 pa_memblockq_set_prebuf(s->memblockq, prebuf);
3343 pa_memblockq_set_minreq(s->memblockq, minreq);
3344 fix_playback_buffer_attr_post(s, &maxlength, &tlength, &prebuf, &minreq);
3345
3346 reply = reply_new(tag);
3347 pa_tagstruct_putu32(reply, maxlength);
3348 pa_tagstruct_putu32(reply, tlength);
3349 pa_tagstruct_putu32(reply, prebuf);
3350 pa_tagstruct_putu32(reply, minreq);
3351
3352 if (c->version >= 13)
3353 pa_tagstruct_put_usec(reply, s->sink_latency);
3354
3355 } else {
3356 record_stream *s;
3357 pa_bool_t adjust_latency = FALSE, early_requests = FALSE;
3358 pa_assert(command == PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR);
3359
3360 s = pa_idxset_get_by_index(c->record_streams, idx);
3361 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3362
3363 if (pa_tagstruct_get(
3364 t,
3365 PA_TAG_U32, &maxlength,
3366 PA_TAG_U32, &fragsize,
3367 PA_TAG_INVALID) < 0 ||
3368 (c->version >= 13 && pa_tagstruct_get_boolean(t, &adjust_latency) < 0) ||
3369 (c->version >= 14 && pa_tagstruct_get_boolean(t, &early_requests) < 0) ||
3370 !pa_tagstruct_eof(t)) {
3371 protocol_error(c);
3372 return;
3373 }
3374
3375 fix_record_buffer_attr_pre(s, adjust_latency, early_requests, &maxlength, &fragsize);
3376 pa_memblockq_set_maxlength(s->memblockq, maxlength);
3377 fix_record_buffer_attr_post(s, &maxlength, &fragsize);
3378
3379 reply = reply_new(tag);
3380 pa_tagstruct_putu32(reply, maxlength);
3381 pa_tagstruct_putu32(reply, fragsize);
3382
3383 if (c->version >= 13)
3384 pa_tagstruct_put_usec(reply, s->source_latency);
3385 }
3386
3387 pa_pstream_send_tagstruct(c->pstream, reply);
3388 }
3389
3390 static void command_update_stream_sample_rate(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3391 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3392 uint32_t idx;
3393 uint32_t rate;
3394
3395 pa_native_connection_assert_ref(c);
3396 pa_assert(t);
3397
3398 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3399 pa_tagstruct_getu32(t, &rate) < 0 ||
3400 !pa_tagstruct_eof(t)) {
3401 protocol_error(c);
3402 return;
3403 }
3404
3405 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3406 CHECK_VALIDITY(c->pstream, rate > 0 && rate <= PA_RATE_MAX, tag, PA_ERR_INVALID);
3407
3408 if (command == PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE) {
3409 playback_stream *s;
3410
3411 s = pa_idxset_get_by_index(c->output_streams, idx);
3412 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3413 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3414
3415 pa_sink_input_set_rate(s->sink_input, rate);
3416
3417 } else {
3418 record_stream *s;
3419 pa_assert(command == PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE);
3420
3421 s = pa_idxset_get_by_index(c->record_streams, idx);
3422 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3423
3424 pa_source_output_set_rate(s->source_output, rate);
3425 }
3426
3427 pa_pstream_send_simple_ack(c->pstream, tag);
3428 }
3429
3430 static void command_update_proplist(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3431 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3432 uint32_t idx;
3433 uint32_t mode;
3434 pa_proplist *p;
3435
3436 pa_native_connection_assert_ref(c);
3437 pa_assert(t);
3438
3439 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3440
3441 p = pa_proplist_new();
3442
3443 if (command == PA_COMMAND_UPDATE_CLIENT_PROPLIST) {
3444
3445 if (pa_tagstruct_getu32(t, &mode) < 0 ||
3446 pa_tagstruct_get_proplist(t, p) < 0 ||
3447 !pa_tagstruct_eof(t)) {
3448 protocol_error(c);
3449 pa_proplist_free(p);
3450 return;
3451 }
3452
3453 } else {
3454
3455 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3456 pa_tagstruct_getu32(t, &mode) < 0 ||
3457 pa_tagstruct_get_proplist(t, p) < 0 ||
3458 !pa_tagstruct_eof(t)) {
3459 protocol_error(c);
3460 pa_proplist_free(p);
3461 return;
3462 }
3463 }
3464
3465 CHECK_VALIDITY(c->pstream, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, tag, PA_ERR_INVALID);
3466
3467 if (command == PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST) {
3468 playback_stream *s;
3469
3470 s = pa_idxset_get_by_index(c->output_streams, idx);
3471 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3472 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3473
3474 pa_proplist_update(s->sink_input->proplist, mode, p);
3475 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->sink_input->index);
3476
3477 } else if (command == PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST) {
3478 record_stream *s;
3479
3480 s = pa_idxset_get_by_index(c->record_streams, idx);
3481 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3482
3483 pa_proplist_update(s->source_output->proplist, mode, p);
3484 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->source_output->index);
3485 } else {
3486 pa_assert(command == PA_COMMAND_UPDATE_CLIENT_PROPLIST);
3487
3488 pa_proplist_update(c->client->proplist, mode, p);
3489 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
3490 }
3491
3492 pa_pstream_send_simple_ack(c->pstream, tag);
3493 }
3494
3495 static void command_remove_proplist(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3496 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3497 uint32_t idx;
3498 unsigned changed = 0;
3499 pa_proplist *p;
3500 pa_strlist *l = NULL;
3501
3502 pa_native_connection_assert_ref(c);
3503 pa_assert(t);
3504
3505 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3506
3507 if (command != PA_COMMAND_REMOVE_CLIENT_PROPLIST) {
3508
3509 if (pa_tagstruct_getu32(t, &idx) < 0) {
3510 protocol_error(c);
3511 return;
3512 }
3513 }
3514
3515 if (command == PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST) {
3516 playback_stream *s;
3517
3518 s = pa_idxset_get_by_index(c->output_streams, idx);
3519 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3520 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3521
3522 p = s->sink_input->proplist;
3523
3524 } else if (command == PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST) {
3525 record_stream *s;
3526
3527 s = pa_idxset_get_by_index(c->record_streams, idx);
3528 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3529
3530 p = s->source_output->proplist;
3531 } else {
3532 pa_assert(command == PA_COMMAND_REMOVE_CLIENT_PROPLIST);
3533
3534 p = c->client->proplist;
3535 }
3536
3537 for (;;) {
3538 const char *k;
3539
3540 if (pa_tagstruct_gets(t, &k) < 0) {
3541 protocol_error(c);
3542 pa_strlist_free(l);
3543 return;
3544 }
3545
3546 if (!k)
3547 break;
3548
3549 l = pa_strlist_prepend(l, k);
3550 }
3551
3552 if (!pa_tagstruct_eof(t)) {
3553 protocol_error(c);
3554 pa_strlist_free(l);
3555 return;
3556 }
3557
3558 for (;;) {
3559 char *z;
3560
3561 l = pa_strlist_pop(l, &z);
3562
3563 if (!z)
3564 break;
3565
3566 changed += (unsigned) (pa_proplist_unset(p, z) >= 0);
3567 pa_xfree(z);
3568 }
3569
3570 pa_pstream_send_simple_ack(c->pstream, tag);
3571
3572 if (changed) {
3573 if (command == PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST) {
3574 playback_stream *s;
3575
3576 s = pa_idxset_get_by_index(c->output_streams, idx);
3577 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->sink_input->index);
3578
3579 } else if (command == PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST) {
3580 record_stream *s;
3581
3582 s = pa_idxset_get_by_index(c->record_streams, idx);
3583 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, s->source_output->index);
3584
3585 } else {
3586 pa_assert(command == PA_COMMAND_REMOVE_CLIENT_PROPLIST);
3587 pa_subscription_post(c->protocol->core, PA_SUBSCRIPTION_EVENT_CLIENT|PA_SUBSCRIPTION_EVENT_CHANGE, c->client->index);
3588 }
3589 }
3590 }
3591
3592 static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3593 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3594 const char *s;
3595
3596 pa_native_connection_assert_ref(c);
3597 pa_assert(t);
3598
3599 if (pa_tagstruct_gets(t, &s) < 0 ||
3600 !pa_tagstruct_eof(t)) {
3601 protocol_error(c);
3602 return;
3603 }
3604
3605 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3606 CHECK_VALIDITY(c->pstream, !s || pa_namereg_is_valid_name(s), tag, PA_ERR_INVALID);
3607
3608 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
3609 pa_pstream_send_simple_ack(c->pstream, tag);
3610 }
3611
3612 static void command_set_stream_name(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3613 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3614 uint32_t idx;
3615 const char *name;
3616
3617 pa_native_connection_assert_ref(c);
3618 pa_assert(t);
3619
3620 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3621 pa_tagstruct_gets(t, &name) < 0 ||
3622 !pa_tagstruct_eof(t)) {
3623 protocol_error(c);
3624 return;
3625 }
3626
3627 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3628 CHECK_VALIDITY(c->pstream, name && pa_utf8_valid(name), tag, PA_ERR_INVALID);
3629
3630 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
3631 playback_stream *s;
3632
3633 s = pa_idxset_get_by_index(c->output_streams, idx);
3634 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3635 CHECK_VALIDITY(c->pstream, playback_stream_isinstance(s), tag, PA_ERR_NOENTITY);
3636
3637 pa_sink_input_set_name(s->sink_input, name);
3638
3639 } else {
3640 record_stream *s;
3641 pa_assert(command == PA_COMMAND_SET_RECORD_STREAM_NAME);
3642
3643 s = pa_idxset_get_by_index(c->record_streams, idx);
3644 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3645
3646 pa_source_output_set_name(s->source_output, name);
3647 }
3648
3649 pa_pstream_send_simple_ack(c->pstream, tag);
3650 }
3651
3652 static void command_kill(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3653 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3654 uint32_t idx;
3655
3656 pa_native_connection_assert_ref(c);
3657 pa_assert(t);
3658
3659 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3660 !pa_tagstruct_eof(t)) {
3661 protocol_error(c);
3662 return;
3663 }
3664
3665 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3666
3667 if (command == PA_COMMAND_KILL_CLIENT) {
3668 pa_client *client;
3669
3670 client = pa_idxset_get_by_index(c->protocol->core->clients, idx);
3671 CHECK_VALIDITY(c->pstream, client, tag, PA_ERR_NOENTITY);
3672
3673 pa_native_connection_ref(c);
3674 pa_client_kill(client);
3675
3676 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
3677 pa_sink_input *s;
3678
3679 s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3680 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3681
3682 pa_native_connection_ref(c);
3683 pa_sink_input_kill(s);
3684 } else {
3685 pa_source_output *s;
3686
3687 pa_assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
3688
3689 s = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
3690 CHECK_VALIDITY(c->pstream, s, tag, PA_ERR_NOENTITY);
3691
3692 pa_native_connection_ref(c);
3693 pa_source_output_kill(s);
3694 }
3695
3696 pa_pstream_send_simple_ack(c->pstream, tag);
3697 pa_native_connection_unref(c);
3698 }
3699
3700 static void command_load_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3701 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3702 pa_module *m;
3703 const char *name, *argument;
3704 pa_tagstruct *reply;
3705
3706 pa_native_connection_assert_ref(c);
3707 pa_assert(t);
3708
3709 if (pa_tagstruct_gets(t, &name) < 0 ||
3710 pa_tagstruct_gets(t, &argument) < 0 ||
3711 !pa_tagstruct_eof(t)) {
3712 protocol_error(c);
3713 return;
3714 }
3715
3716 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3717 CHECK_VALIDITY(c->pstream, name && *name && pa_utf8_valid(name) && !strchr(name, '/'), tag, PA_ERR_INVALID);
3718 CHECK_VALIDITY(c->pstream, !argument || pa_utf8_valid(argument), tag, PA_ERR_INVALID);
3719
3720 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
3721 pa_pstream_send_error(c->pstream, tag, PA_ERR_MODINITFAILED);
3722 return;
3723 }
3724
3725 reply = reply_new(tag);
3726 pa_tagstruct_putu32(reply, m->index);
3727 pa_pstream_send_tagstruct(c->pstream, reply);
3728 }
3729
3730 static void command_unload_module(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3731 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3732 uint32_t idx;
3733 pa_module *m;
3734
3735 pa_native_connection_assert_ref(c);
3736 pa_assert(t);
3737
3738 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3739 !pa_tagstruct_eof(t)) {
3740 protocol_error(c);
3741 return;
3742 }
3743
3744 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3745 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
3746 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOENTITY);
3747
3748 pa_module_unload_request(m, FALSE);
3749 pa_pstream_send_simple_ack(c->pstream, tag);
3750 }
3751
3752 static void command_move_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3753 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3754 uint32_t idx = PA_INVALID_INDEX, idx_device = PA_INVALID_INDEX;
3755 const char *name_device = NULL;
3756
3757 pa_native_connection_assert_ref(c);
3758 pa_assert(t);
3759
3760 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3761 pa_tagstruct_getu32(t, &idx_device) < 0 ||
3762 pa_tagstruct_gets(t, &name_device) < 0 ||
3763 !pa_tagstruct_eof(t)) {
3764 protocol_error(c);
3765 return;
3766 }
3767
3768 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3769 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3770
3771 CHECK_VALIDITY(c->pstream, !name_device || pa_namereg_is_valid_name(name_device), tag, PA_ERR_INVALID);
3772 CHECK_VALIDITY(c->pstream, idx_device != PA_INVALID_INDEX || name_device, tag, PA_ERR_INVALID);
3773 CHECK_VALIDITY(c->pstream, idx_device == PA_INVALID_INDEX || !name_device, tag, PA_ERR_INVALID);
3774 CHECK_VALIDITY(c->pstream, !name_device || idx_device == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3775
3776 if (command == PA_COMMAND_MOVE_SINK_INPUT) {
3777 pa_sink_input *si = NULL;
3778 pa_sink *sink = NULL;
3779
3780 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, idx);
3781
3782 if (idx_device != PA_INVALID_INDEX)
3783 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx_device);
3784 else
3785 sink = pa_namereg_get(c->protocol->core, name_device, PA_NAMEREG_SINK);
3786
3787 CHECK_VALIDITY(c->pstream, si && sink, tag, PA_ERR_NOENTITY);
3788
3789 if (pa_sink_input_move_to(si, sink) < 0) {
3790 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3791 return;
3792 }
3793 } else {
3794 pa_source_output *so = NULL;
3795 pa_source *source;
3796
3797 pa_assert(command == PA_COMMAND_MOVE_SOURCE_OUTPUT);
3798
3799 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, idx);
3800
3801 if (idx_device != PA_INVALID_INDEX)
3802 source = pa_idxset_get_by_index(c->protocol->core->sources, idx_device);
3803 else
3804 source = pa_namereg_get(c->protocol->core, name_device, PA_NAMEREG_SOURCE);
3805
3806 CHECK_VALIDITY(c->pstream, so && source, tag, PA_ERR_NOENTITY);
3807
3808 if (pa_source_output_move_to(so, source) < 0) {
3809 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3810 return;
3811 }
3812 }
3813
3814 pa_pstream_send_simple_ack(c->pstream, tag);
3815 }
3816
3817 static void command_suspend(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3818 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3819 uint32_t idx = PA_INVALID_INDEX;
3820 const char *name = NULL;
3821 pa_bool_t b;
3822
3823 pa_native_connection_assert_ref(c);
3824 pa_assert(t);
3825
3826 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3827 pa_tagstruct_gets(t, &name) < 0 ||
3828 pa_tagstruct_get_boolean(t, &b) < 0 ||
3829 !pa_tagstruct_eof(t)) {
3830 protocol_error(c);
3831 return;
3832 }
3833
3834 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3835 CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name) || *name == 0, tag, PA_ERR_INVALID);
3836 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
3837 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
3838 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3839
3840 if (command == PA_COMMAND_SUSPEND_SINK) {
3841
3842 if (idx == PA_INVALID_INDEX && name && !*name) {
3843
3844 pa_log_debug("%s all sinks", b ? "Suspending" : "Resuming");
3845
3846 if (pa_sink_suspend_all(c->protocol->core, b) < 0) {
3847 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3848 return;
3849 }
3850 } else {
3851 pa_sink *sink = NULL;
3852
3853 if (idx != PA_INVALID_INDEX)
3854 sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx);
3855 else
3856 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK);
3857
3858 CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
3859
3860 if (pa_sink_suspend(sink, b) < 0) {
3861 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3862 return;
3863 }
3864 }
3865 } else {
3866
3867 pa_assert(command == PA_COMMAND_SUSPEND_SOURCE);
3868
3869 if (idx == PA_INVALID_INDEX && name && !*name) {
3870
3871 pa_log_debug("%s all sources", b ? "Suspending" : "Resuming");
3872
3873 if (pa_source_suspend_all(c->protocol->core, b) < 0) {
3874 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3875 return;
3876 }
3877
3878 } else {
3879 pa_source *source;
3880
3881 if (idx != PA_INVALID_INDEX)
3882 source = pa_idxset_get_by_index(c->protocol->core->sources, idx);
3883 else
3884 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE);
3885
3886 CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
3887
3888 if (pa_source_suspend(source, b) < 0) {
3889 pa_pstream_send_error(c->pstream, tag, PA_ERR_INVALID);
3890 return;
3891 }
3892 }
3893 }
3894
3895 pa_pstream_send_simple_ack(c->pstream, tag);
3896 }
3897
3898 static void command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
3899 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3900 uint32_t idx = PA_INVALID_INDEX;
3901 const char *name = NULL;
3902 pa_module *m;
3903 pa_native_protocol_ext_cb_t cb;
3904
3905 pa_native_connection_assert_ref(c);
3906 pa_assert(t);
3907
3908 if (pa_tagstruct_getu32(t, &idx) < 0 ||
3909 pa_tagstruct_gets(t, &name) < 0) {
3910 protocol_error(c);
3911 return;
3912 }
3913
3914 CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
3915 CHECK_VALIDITY(c->pstream, !name || pa_utf8_valid(name), tag, PA_ERR_INVALID);
3916 CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID);
3917 CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID);
3918 CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3919
3920 if (idx != PA_INVALID_INDEX)
3921 m = pa_idxset_get_by_index(c->protocol->core->modules, idx);
3922 else {
3923 for (m = pa_idxset_first(c->protocol->core->modules, &idx); m; m = pa_idxset_next(c->protocol->core->modules, &idx))
3924 if (strcmp(name, m->name) == 0)
3925 break;
3926 }
3927
3928 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOEXTENSION);
3929 CHECK_VALIDITY(c->pstream, m->load_once || idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID);
3930
3931 cb = (pa_native_protocol_ext_cb_t) pa_hashmap_get(c->protocol->extensions, m);
3932 CHECK_VALIDITY(c->pstream, m, tag, PA_ERR_NOEXTENSION);
3933
3934 if (cb(c->protocol, m, c, tag, t) < 0)
3935 protocol_error(c);
3936 }
3937
3938
3939 /*** pstream callbacks ***/
3940
3941 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
3942 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3943
3944 pa_assert(p);
3945 pa_assert(packet);
3946 pa_native_connection_assert_ref(c);
3947
3948 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0) {
3949 pa_log("invalid packet.");
3950 native_connection_unlink(c);
3951 }
3952 }
3953
3954 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) {
3955 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
3956 output_stream *stream;
3957
3958 pa_assert(p);
3959 pa_assert(chunk);
3960 pa_native_connection_assert_ref(c);
3961
3962 if (!(stream = OUTPUT_STREAM(pa_idxset_get_by_index(c->output_streams, channel)))) {
3963 pa_log("client sent block for invalid stream.");
3964 /* Ignoring */
3965 return;
3966 }
3967
3968 /* pa_log("got %lu bytes", (unsigned long) chunk->length); */
3969
3970 if (playback_stream_isinstance(stream)) {
3971 playback_stream *ps = PLAYBACK_STREAM(stream);
3972
3973 if (seek != PA_SEEK_RELATIVE || offset != 0)
3974 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);
3975
3976 pa_asyncmsgq_post(ps->sink_input->sink->asyncmsgq, PA_MSGOBJECT(ps->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
3977
3978 } else {
3979 upload_stream *u = UPLOAD_STREAM(stream);
3980 size_t l;
3981
3982 if (!u->memchunk.memblock) {
3983 if (u->length == chunk->length) {
3984 u->memchunk = *chunk;
3985 pa_memblock_ref(u->memchunk.memblock);
3986 u->length = 0;
3987 } else {
3988 u->memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, u->length);
3989 u->memchunk.index = u->memchunk.length = 0;
3990 }
3991 }
3992
3993 pa_assert(u->memchunk.memblock);
3994
3995 l = u->length;
3996 if (l > chunk->length)
3997 l = chunk->length;
3998
3999
4000 if (l > 0) {
4001 void *src, *dst;
4002 dst = pa_memblock_acquire(u->memchunk.memblock);
4003 src = pa_memblock_acquire(chunk->memblock);
4004
4005 memcpy((uint8_t*) dst + u->memchunk.index + u->memchunk.length,
4006 (uint8_t*) src+chunk->index, l);
4007
4008 pa_memblock_release(u->memchunk.memblock);
4009 pa_memblock_release(chunk->memblock);
4010
4011 u->memchunk.length += l;
4012 u->length -= l;
4013 }
4014 }
4015 }
4016
4017 static void pstream_die_callback(pa_pstream *p, void *userdata) {
4018 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
4019
4020 pa_assert(p);
4021 pa_native_connection_assert_ref(c);
4022
4023 native_connection_unlink(c);
4024 pa_log_info("Connection died.");
4025 }
4026
4027 static void pstream_drain_callback(pa_pstream *p, void *userdata) {
4028 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
4029
4030 pa_assert(p);
4031 pa_native_connection_assert_ref(c);
4032
4033 native_connection_send_memblock(c);
4034 }
4035
4036 static void pstream_revoke_callback(pa_pstream *p, uint32_t block_id, void *userdata) {
4037 pa_thread_mq *q;
4038
4039 if (!(q = pa_thread_mq_get()))
4040 pa_pstream_send_revoke(p, block_id);
4041 else
4042 pa_asyncmsgq_post(q->outq, PA_MSGOBJECT(userdata), CONNECTION_MESSAGE_REVOKE, PA_UINT_TO_PTR(block_id), 0, NULL, NULL);
4043 }
4044
4045 static void pstream_release_callback(pa_pstream *p, uint32_t block_id, void *userdata) {
4046 pa_thread_mq *q;
4047
4048 if (!(q = pa_thread_mq_get()))
4049 pa_pstream_send_release(p, block_id);
4050 else
4051 pa_asyncmsgq_post(q->outq, PA_MSGOBJECT(userdata), CONNECTION_MESSAGE_RELEASE, PA_UINT_TO_PTR(block_id), 0, NULL, NULL);
4052 }
4053
4054 /*** client callbacks ***/
4055
4056 static void client_kill_cb(pa_client *c) {
4057 pa_assert(c);
4058
4059 native_connection_unlink(PA_NATIVE_CONNECTION(c->userdata));
4060 pa_log_info("Connection killed.");
4061 }
4062
4063 /*** module entry points ***/
4064
4065 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
4066 pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
4067
4068 pa_assert(m);
4069 pa_assert(tv);
4070 pa_native_connection_assert_ref(c);
4071 pa_assert(c->auth_timeout_event == e);
4072
4073 if (!c->authorized) {
4074 native_connection_unlink(c);
4075 pa_log_info("Connection terminated due to authentication timeout.");
4076 }
4077 }
4078
4079 void pa_native_protocol_connect(pa_native_protocol *p, pa_iochannel *io, pa_native_options *o) {
4080 pa_native_connection *c;
4081 char pname[128];
4082 pa_client *client;
4083 pa_client_new_data data;
4084
4085 pa_assert(p);
4086 pa_assert(io);
4087 pa_assert(o);
4088
4089 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
4090 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
4091 pa_iochannel_free(io);
4092 return;
4093 }
4094
4095 pa_client_new_data_init(&data);
4096 data.module = o->module;
4097 data.driver = __FILE__;
4098 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
4099 pa_proplist_setf(data.proplist, PA_PROP_APPLICATION_NAME, "Native client (%s)", pname);
4100 pa_proplist_sets(data.proplist, "native-protocol.peer", pname);
4101 client = pa_client_new(p->core, &data);
4102 pa_client_new_data_done(&data);
4103
4104 if (!client)
4105 return;
4106
4107 c = pa_msgobject_new(pa_native_connection);
4108 c->parent.parent.free = native_connection_free;
4109 c->parent.process_msg = native_connection_process_msg;
4110 c->protocol = p;
4111 c->options = pa_native_options_ref(o);
4112 c->authorized = FALSE;
4113
4114 if (o->auth_anonymous) {
4115 pa_log_info("Client authenticated anonymously.");
4116 c->authorized = TRUE;
4117 }
4118
4119 if (!c->authorized &&
4120 o->auth_ip_acl &&
4121 pa_ip_acl_check(o->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
4122
4123 pa_log_info("Client authenticated by IP ACL.");
4124 c->authorized = TRUE;
4125 }
4126
4127 if (!c->authorized) {
4128 struct timeval tv;
4129 pa_gettimeofday(&tv);
4130 tv.tv_sec += AUTH_TIMEOUT;
4131 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
4132 } else
4133 c->auth_timeout_event = NULL;
4134
4135 c->is_local = pa_iochannel_socket_is_local(io);
4136 c->version = 8;
4137
4138 c->client = client;
4139 c->client->kill = client_kill_cb;
4140 c->client->userdata = c;
4141
4142 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->mempool);
4143 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
4144 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
4145 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
4146 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
4147 pa_pstream_set_revoke_callback(c->pstream, pstream_revoke_callback, c);
4148 pa_pstream_set_release_callback(c->pstream, pstream_release_callback, c);
4149
4150 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
4151
4152 c->record_streams = pa_idxset_new(NULL, NULL);
4153 c->output_streams = pa_idxset_new(NULL, NULL);
4154
4155 c->rrobin_index = PA_IDXSET_INVALID;
4156 c->subscription = NULL;
4157
4158 pa_idxset_put(p->connections, c, NULL);
4159
4160 #ifdef HAVE_CREDS
4161 if (pa_iochannel_creds_supported(io))
4162 pa_iochannel_creds_enable(io);
4163 #endif
4164
4165 pa_hook_fire(&p->hooks[PA_NATIVE_HOOK_CONNECTION_PUT], c);
4166 }
4167
4168 void pa_native_protocol_disconnect(pa_native_protocol *p, pa_module *m) {
4169 pa_native_connection *c;
4170 void *state = NULL;
4171
4172 pa_assert(p);
4173 pa_assert(m);
4174
4175 while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
4176 if (c->options->module == m)
4177 native_connection_unlink(c);
4178 }
4179
4180 static pa_native_protocol* native_protocol_new(pa_core *c) {
4181 pa_native_protocol *p;
4182 pa_native_hook_t h;
4183
4184 pa_assert(c);
4185
4186 p = pa_xnew(pa_native_protocol, 1);
4187 PA_REFCNT_INIT(p);
4188 p->core = c;
4189 p->connections = pa_idxset_new(NULL, NULL);
4190
4191 p->servers = NULL;
4192
4193 p->extensions = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
4194
4195 for (h = 0; h < PA_NATIVE_HOOK_MAX; h++)
4196 pa_hook_init(&p->hooks[h], p);
4197
4198 pa_assert_se(pa_shared_set(c, "native-protocol", p) >= 0);
4199
4200 return p;
4201 }
4202
4203 pa_native_protocol* pa_native_protocol_get(pa_core *c) {
4204 pa_native_protocol *p;
4205
4206 if ((p = pa_shared_get(c, "native-protocol")))
4207 return pa_native_protocol_ref(p);
4208
4209 return native_protocol_new(c);
4210 }
4211
4212 pa_native_protocol* pa_native_protocol_ref(pa_native_protocol *p) {
4213 pa_assert(p);
4214 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4215
4216 PA_REFCNT_INC(p);
4217
4218 return p;
4219 }
4220
4221 void pa_native_protocol_unref(pa_native_protocol *p) {
4222 pa_native_connection *c;
4223 pa_native_hook_t h;
4224
4225 pa_assert(p);
4226 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4227
4228 if (PA_REFCNT_DEC(p) > 0)
4229 return;
4230
4231 while ((c = pa_idxset_first(p->connections, NULL)))
4232 native_connection_unlink(c);
4233
4234 pa_idxset_free(p->connections, NULL, NULL);
4235
4236 pa_strlist_free(p->servers);
4237
4238 for (h = 0; h < PA_NATIVE_HOOK_MAX; h++)
4239 pa_hook_done(&p->hooks[h]);
4240
4241 pa_hashmap_free(p->extensions, NULL, NULL);
4242
4243 pa_assert_se(pa_shared_remove(p->core, "native-protocol") >= 0);
4244
4245 pa_xfree(p);
4246 }
4247
4248 void pa_native_protocol_add_server_string(pa_native_protocol *p, const char *name) {
4249 pa_assert(p);
4250 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4251 pa_assert(name);
4252
4253 p->servers = pa_strlist_prepend(p->servers, name);
4254
4255 pa_hook_fire(&p->hooks[PA_NATIVE_HOOK_SERVERS_CHANGED], p->servers);
4256 }
4257
4258 void pa_native_protocol_remove_server_string(pa_native_protocol *p, const char *name) {
4259 pa_assert(p);
4260 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4261 pa_assert(name);
4262
4263 p->servers = pa_strlist_remove(p->servers, name);
4264
4265 pa_hook_fire(&p->hooks[PA_NATIVE_HOOK_SERVERS_CHANGED], p->servers);
4266 }
4267
4268 pa_hook *pa_native_protocol_hooks(pa_native_protocol *p) {
4269 pa_assert(p);
4270 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4271
4272 return p->hooks;
4273 }
4274
4275 pa_strlist *pa_native_protocol_servers(pa_native_protocol *p) {
4276 pa_assert(p);
4277 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4278
4279 return p->servers;
4280 }
4281
4282 int pa_native_protocol_install_ext(pa_native_protocol *p, pa_module *m, pa_native_protocol_ext_cb_t cb) {
4283 pa_assert(p);
4284 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4285 pa_assert(m);
4286 pa_assert(cb);
4287 pa_assert(!pa_hashmap_get(p->extensions, m));
4288
4289 pa_assert_se(pa_hashmap_put(p->extensions, m, (void*) cb) == 0);
4290 return 0;
4291 }
4292
4293 void pa_native_protocol_remove_ext(pa_native_protocol *p, pa_module *m) {
4294 pa_assert(p);
4295 pa_assert(PA_REFCNT_VALUE(p) >= 1);
4296 pa_assert(m);
4297
4298 pa_assert_se(pa_hashmap_remove(p->extensions, m));
4299 }
4300
4301 pa_native_options* pa_native_options_new(void) {
4302 pa_native_options *o;
4303
4304 o = pa_xnew0(pa_native_options, 1);
4305 PA_REFCNT_INIT(o);
4306
4307 return o;
4308 }
4309
4310 pa_native_options* pa_native_options_ref(pa_native_options *o) {
4311 pa_assert(o);
4312 pa_assert(PA_REFCNT_VALUE(o) >= 1);
4313
4314 PA_REFCNT_INC(o);
4315
4316 return o;
4317 }
4318
4319 void pa_native_options_unref(pa_native_options *o) {
4320 pa_assert(o);
4321 pa_assert(PA_REFCNT_VALUE(o) >= 1);
4322
4323 if (PA_REFCNT_DEC(o) > 0)
4324 return;
4325
4326 pa_xfree(o->auth_group);
4327
4328 if (o->auth_ip_acl)
4329 pa_ip_acl_free(o->auth_ip_acl);
4330
4331 if (o->auth_cookie)
4332 pa_auth_cookie_unref(o->auth_cookie);
4333
4334 pa_xfree(o);
4335 }
4336
4337 int pa_native_options_parse(pa_native_options *o, pa_core *c, pa_modargs *ma) {
4338 pa_bool_t enabled;
4339 const char *acl;
4340
4341 pa_assert(o);
4342 pa_assert(PA_REFCNT_VALUE(o) >= 1);
4343 pa_assert(ma);
4344
4345 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &o->auth_anonymous) < 0) {
4346 pa_log("auth-anonymous= expects a boolean argument.");
4347 return -1;
4348 }
4349
4350 enabled = TRUE;
4351 if (pa_modargs_get_value_boolean(ma, "auth-group-enabled", &enabled) < 0) {
4352 pa_log("auth-group-enabled= expects a boolean argument.");
4353 return -1;
4354 }
4355
4356 pa_xfree(o->auth_group);
4357 o->auth_group = enabled ? pa_xstrdup(pa_modargs_get_value(ma, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP : NULL)) : NULL;
4358
4359 #ifndef HAVE_CREDS
4360 if (o->auth_group)
4361 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4362 #endif
4363
4364 if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
4365 pa_ip_acl *ipa;
4366
4367 if (!(ipa = pa_ip_acl_new(acl))) {
4368 pa_log("Failed to parse IP ACL '%s'", acl);
4369 return -1;
4370 }
4371
4372 if (o->auth_ip_acl)
4373 pa_ip_acl_free(o->auth_ip_acl);
4374
4375 o->auth_ip_acl = ipa;
4376 }
4377
4378 enabled = TRUE;
4379 if (pa_modargs_get_value_boolean(ma, "auth-cookie-enabled", &enabled) < 0) {
4380 pa_log("auth-cookie-enabled= expects a boolean argument.");
4381 return -1;
4382 }
4383
4384 if (o->auth_cookie)
4385 pa_auth_cookie_unref(o->auth_cookie);
4386
4387 if (enabled) {
4388 const char *cn;
4389
4390 /* The new name for this is 'auth-cookie', for compat reasons
4391 * we check the old name too */
4392 if (!(cn = pa_modargs_get_value(ma, "auth-cookie", NULL)))
4393 if (!(cn = pa_modargs_get_value(ma, "cookie", NULL)))
4394 cn = PA_NATIVE_COOKIE_FILE;
4395
4396 if (!(o->auth_cookie = pa_auth_cookie_get(c, cn, PA_NATIVE_COOKIE_LENGTH)))
4397 return -1;
4398
4399 } else
4400 o->auth_cookie = NULL;
4401
4402 return 0;
4403 }
4404
4405 pa_pstream* pa_native_connection_get_pstream(pa_native_connection *c) {
4406 pa_native_connection_assert_ref(c);
4407
4408 return c->pstream;
4409 }