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