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