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