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