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