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