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