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