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