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