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