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