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