4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/timeval.h>
35 #include <pulse/version.h>
36 #include <pulse/utf8.h>
37 #include <pulse/util.h>
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/native-common.h>
41 #include <pulsecore/packet.h>
42 #include <pulsecore/client.h>
43 #include <pulsecore/source-output.h>
44 #include <pulsecore/sink-input.h>
45 #include <pulsecore/pstream.h>
46 #include <pulsecore/tagstruct.h>
47 #include <pulsecore/pdispatch.h>
48 #include <pulsecore/pstream-util.h>
49 #include <pulsecore/authkey.h>
50 #include <pulsecore/namereg.h>
51 #include <pulsecore/core-scache.h>
52 #include <pulsecore/core-subscribe.h>
53 #include <pulsecore/log.h>
54 #include <pulsecore/autoload.h>
55 #include <pulsecore/authkey-prop.h>
56 #include <pulsecore/strlist.h>
57 #include <pulsecore/props.h>
58 #include <pulsecore/sample-util.h>
59 #include <pulsecore/llist.h>
60 #include <pulsecore/creds.h>
61 #include <pulsecore/core-util.h>
62 #include <pulsecore/ipacl.h>
63 #include <pulsecore/thread-mq.h>
65 #include "protocol-native.h"
67 /* Kick a client if it doesn't authenticate within this time */
68 #define AUTH_TIMEOUT 60
70 /* Don't accept more connection than this */
71 #define MAX_CONNECTIONS 64
73 #define MAX_MEMBLOCKQ_LENGTH (4*1024*1024) /* 4MB */
75 typedef struct connection connection
;
76 struct pa_protocol_native
;
78 typedef struct record_stream
{
81 connection
*connection
;
84 pa_source_output
*source_output
;
85 pa_memblockq
*memblockq
;
89 typedef struct output_stream
{
93 typedef struct playback_stream
{
96 connection
*connection
;
99 pa_sink_input
*sink_input
;
100 pa_memblockq
*memblockq
;
101 pa_bool_t drain_request
;
109 /* Only updated after SINK_INPUT_MESSAGE_UPDATE_LATENCY */
110 int64_t read_index
, write_index
;
111 size_t render_memblockq_length
;
114 typedef struct upload_stream
{
115 output_stream parent
;
117 connection
*connection
;
120 pa_memchunk memchunk
;
123 pa_sample_spec sample_spec
;
124 pa_channel_map channel_map
;
125 pa_proplist
*proplist
;
131 pa_bool_t authorized
;
133 pa_protocol_native
*protocol
;
136 pa_pdispatch
*pdispatch
;
137 pa_idxset
*record_streams
, *output_streams
;
138 uint32_t rrobin_index
;
139 pa_subscription
*subscription
;
140 pa_time_event
*auth_timeout_event
;
143 PA_DECLARE_CLASS(record_stream
);
144 #define RECORD_STREAM(o) (record_stream_cast(o))
145 static PA_DEFINE_CHECK_TYPE(record_stream
, pa_msgobject
);
147 PA_DECLARE_CLASS(output_stream
);
148 #define OUTPUT_STREAM(o) (output_stream_cast(o))
149 static PA_DEFINE_CHECK_TYPE(output_stream
, pa_msgobject
);
151 PA_DECLARE_CLASS(playback_stream
);
152 #define PLAYBACK_STREAM(o) (playback_stream_cast(o))
153 static PA_DEFINE_CHECK_TYPE(playback_stream
, output_stream
);
155 PA_DECLARE_CLASS(upload_stream
);
156 #define UPLOAD_STREAM(o) (upload_stream_cast(o))
157 static PA_DEFINE_CHECK_TYPE(upload_stream
, output_stream
);
159 PA_DECLARE_CLASS(connection
);
160 #define CONNECTION(o) (connection_cast(o))
161 static PA_DEFINE_CHECK_TYPE(connection
, pa_msgobject
);
163 struct pa_protocol_native
{
167 pa_socket_server
*server
;
168 pa_idxset
*connections
;
169 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
170 pa_bool_t auth_cookie_in_property
;
174 pa_ip_acl
*auth_ip_acl
;
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
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
195 RECORD_STREAM_MESSAGE_POST_DATA
/* data from source output to main loop */
199 CONNECTION_MESSAGE_RELEASE
,
200 CONNECTION_MESSAGE_REVOKE
203 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
);
204 static void sink_input_kill_cb(pa_sink_input
*i
);
205 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
);
206 static void sink_input_moved_cb(pa_sink_input
*i
);
208 static void send_memblock(connection
*c
);
209 static void request_bytes(struct playback_stream
*s
);
211 static void source_output_kill_cb(pa_source_output
*o
);
212 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
213 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
);
214 static void source_output_moved_cb(pa_source_output
*o
);
215 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
217 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
);
219 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
220 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
221 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
222 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
223 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
224 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
225 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
226 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
227 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
228 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
229 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
230 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
231 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
232 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
233 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
234 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
235 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
236 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
237 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
238 static void command_set_volume(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
239 static void command_set_mute(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
240 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
241 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
242 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
243 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
244 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
245 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
246 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
247 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
248 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
249 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
250 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
251 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
252 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
253 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
254 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
255 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
256 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
257 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
258 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
260 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
261 [PA_COMMAND_ERROR
] = NULL
,
262 [PA_COMMAND_TIMEOUT
] = NULL
,
263 [PA_COMMAND_REPLY
] = NULL
,
264 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
265 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
266 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
267 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
268 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
269 [PA_COMMAND_AUTH
] = command_auth
,
270 [PA_COMMAND_REQUEST
] = NULL
,
271 [PA_COMMAND_EXIT
] = command_exit
,
272 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
273 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
274 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
275 [PA_COMMAND_STAT
] = command_stat
,
276 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
277 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
278 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
279 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
280 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
281 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
282 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
283 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
284 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
285 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
286 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
287 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
288 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
289 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
290 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
291 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
292 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
293 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
294 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
295 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
296 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
297 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
298 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
300 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
301 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
302 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
304 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
305 [PA_COMMAND_SET_SINK_INPUT_MUTE
] = command_set_mute
,
306 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
308 [PA_COMMAND_SUSPEND_SINK
] = command_suspend
,
309 [PA_COMMAND_SUSPEND_SOURCE
] = command_suspend
,
311 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
312 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
313 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
314 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
316 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
317 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
319 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
320 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
321 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
322 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
323 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
324 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
325 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
326 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
327 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
328 [PA_COMMAND_GET_AUTOLOAD_INFO
] = command_get_autoload_info
,
329 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = command_get_autoload_info_list
,
330 [PA_COMMAND_ADD_AUTOLOAD
] = command_add_autoload
,
331 [PA_COMMAND_REMOVE_AUTOLOAD
] = command_remove_autoload
,
333 [PA_COMMAND_MOVE_SINK_INPUT
] = command_move_stream
,
334 [PA_COMMAND_MOVE_SOURCE_OUTPUT
] = command_move_stream
,
336 [PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
337 [PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
339 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
340 [PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
342 [PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
] = command_update_proplist
,
343 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
] = command_update_proplist
,
344 [PA_COMMAND_UPDATE_CLIENT_PROPLIST
] = command_update_proplist
,
346 [PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
] = command_remove_proplist
,
347 [PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
] = command_remove_proplist
,
348 [PA_COMMAND_REMOVE_CLIENT_PROPLIST
] = command_remove_proplist
,
351 /* structure management */
353 static void upload_stream_unlink(upload_stream
*s
) {
359 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
360 s
->connection
= NULL
;
361 upload_stream_unref(s
);
364 static void upload_stream_free(pa_object
*o
) {
365 upload_stream
*s
= UPLOAD_STREAM(o
);
368 upload_stream_unlink(s
);
373 pa_proplist_free(s
->proplist
);
375 if (s
->memchunk
.memblock
)
376 pa_memblock_unref(s
->memchunk
.memblock
);
381 static upload_stream
* upload_stream_new(
383 const pa_sample_spec
*ss
,
384 const pa_channel_map
*map
,
394 pa_assert(length
> 0);
397 s
= pa_msgobject_new(upload_stream
);
398 s
->parent
.parent
.parent
.free
= upload_stream_free
;
400 s
->sample_spec
= *ss
;
401 s
->channel_map
= *map
;
402 s
->name
= pa_xstrdup(name
);
403 pa_memchunk_reset(&s
->memchunk
);
405 s
->proplist
= pa_proplist_copy(p
);
406 pa_proplist_update(s
->proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
408 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
413 static void record_stream_unlink(record_stream
*s
) {
419 if (s
->source_output
) {
420 pa_source_output_unlink(s
->source_output
);
421 pa_source_output_unref(s
->source_output
);
422 s
->source_output
= NULL
;
425 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->record_streams
, s
, NULL
) == s
);
426 s
->connection
= NULL
;
427 record_stream_unref(s
);
430 static void record_stream_free(pa_object
*o
) {
431 record_stream
*s
= RECORD_STREAM(o
);
434 record_stream_unlink(s
);
436 pa_memblockq_free(s
->memblockq
);
440 static int record_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
441 record_stream
*s
= RECORD_STREAM(o
);
442 record_stream_assert_ref(s
);
449 case RECORD_STREAM_MESSAGE_POST_DATA
:
451 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
452 /* pa_log_warn("Failed to push data into output queue."); */
456 if (!pa_pstream_is_pending(s
->connection
->pstream
))
457 send_memblock(s
->connection
);
465 static record_stream
* record_stream_new(
472 uint32_t fragment_size
,
473 pa_source_output_flags_t flags
,
477 pa_source_output
*source_output
;
479 pa_source_output_new_data data
;
484 pa_assert(maxlength
);
485 pa_assert(*maxlength
> 0);
488 pa_source_output_new_data_init(&data
);
490 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
491 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
492 data
.driver
= __FILE__
;
493 data
.module
= c
->protocol
->module
;
494 data
.client
= c
->client
;
495 data
.source
= source
;
496 pa_source_output_new_data_set_sample_spec(&data
, ss
);
497 pa_source_output_new_data_set_channel_map(&data
, map
);
499 source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
);
501 pa_source_output_new_data_done(&data
);
506 s
= pa_msgobject_new(record_stream
);
507 s
->parent
.parent
.free
= record_stream_free
;
508 s
->parent
.process_msg
= record_stream_process_msg
;
510 s
->source_output
= source_output
;
511 s
->source_output
->push
= source_output_push_cb
;
512 s
->source_output
->kill
= source_output_kill_cb
;
513 s
->source_output
->get_latency
= source_output_get_latency_cb
;
514 s
->source_output
->moved
= source_output_moved_cb
;
515 s
->source_output
->suspend
= source_output_suspend_cb
;
516 s
->source_output
->userdata
= s
;
518 s
->memblockq
= pa_memblockq_new(
522 base
= pa_frame_size(&s
->source_output
->sample_spec
),
528 *maxlength
= pa_memblockq_get_maxlength(s
->memblockq
);
530 s
->fragment_size
= (fragment_size
/base
)*base
;
531 if (s
->fragment_size
<= 0)
532 s
->fragment_size
= base
;
534 if (s
->fragment_size
> *maxlength
)
535 s
->fragment_size
= *maxlength
;
537 *ss
= s
->source_output
->sample_spec
;
538 *map
= s
->source_output
->channel_map
;
540 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
542 pa_source_output_put(s
->source_output
);
546 static void playback_stream_unlink(playback_stream
*s
) {
553 pa_sink_input_unlink(s
->sink_input
);
554 pa_sink_input_unref(s
->sink_input
);
555 s
->sink_input
= NULL
;
558 if (s
->drain_request
)
559 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
561 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
562 s
->connection
= NULL
;
563 playback_stream_unref(s
);
566 static void playback_stream_free(pa_object
* o
) {
567 playback_stream
*s
= PLAYBACK_STREAM(o
);
570 playback_stream_unlink(s
);
572 pa_memblockq_free(s
->memblockq
);
576 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
577 playback_stream
*s
= PLAYBACK_STREAM(o
);
578 playback_stream_assert_ref(s
);
584 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
591 if ((k
= pa_atomic_load(&s
->missing
)) <= 0)
599 if (pa_atomic_sub(&s
->missing
, k
) <= k
)
606 t
= pa_tagstruct_new(NULL
, 0);
607 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
608 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
609 pa_tagstruct_putu32(t
, s
->index
);
610 pa_tagstruct_putu32(t
, l
);
611 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
613 /* pa_log("Requesting %u bytes", l); */
617 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
620 /* Report that we're empty */
621 t
= pa_tagstruct_new(NULL
, 0);
622 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
623 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
624 pa_tagstruct_putu32(t
, s
->index
);
625 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
629 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
632 /* Notify the user we're overflowed*/
633 t
= pa_tagstruct_new(NULL
, 0);
634 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
635 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
636 pa_tagstruct_putu32(t
, s
->index
);
637 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
641 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
642 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
650 static playback_stream
* playback_stream_new(
663 pa_sink_input_flags_t flags
,
666 playback_stream
*s
, *ssync
;
667 pa_sink_input
*sink_input
;
668 pa_memblock
*silence
;
671 pa_sink_input_new_data data
;
676 pa_assert(maxlength
);
679 /* Find syncid group */
680 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
682 if (!playback_stream_isinstance(ssync
))
685 if (ssync
->syncid
== syncid
)
689 /* Synced streams must connect to the same sink */
693 sink
= ssync
->sink_input
->sink
;
694 else if (sink
!= ssync
->sink_input
->sink
)
698 pa_sink_input_new_data_init(&data
);
700 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
701 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
702 data
.driver
= __FILE__
;
703 data
.module
= c
->protocol
->module
;
704 data
.client
= c
->client
;
706 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
707 pa_sink_input_new_data_set_channel_map(&data
, map
);
708 pa_sink_input_new_data_set_volume(&data
, volume
);
709 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
711 sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
);
713 pa_sink_input_new_data_done(&data
);
718 s
= pa_msgobject_new(playback_stream
);
719 s
->parent
.parent
.parent
.free
= playback_stream_free
;
720 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
723 s
->sink_input
= sink_input
;
726 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
727 s
->sink_input
->pop
= sink_input_pop_cb
;
728 s
->sink_input
->kill
= sink_input_kill_cb
;
729 s
->sink_input
->moved
= sink_input_moved_cb
;
730 s
->sink_input
->suspend
= sink_input_suspend_cb
;
731 s
->sink_input
->userdata
= s
;
733 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
735 silence
= pa_silence_memblock_new(c
->protocol
->core
->mempool
, &s
->sink_input
->sample_spec
, 0);
737 s
->memblockq
= pa_memblockq_new(
741 pa_frame_size(&s
->sink_input
->sample_spec
),
747 pa_memblock_unref(silence
);
749 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
750 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
751 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
752 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
753 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
755 *ss
= s
->sink_input
->sample_spec
;
756 *map
= s
->sink_input
->channel_map
;
758 s
->minreq
= pa_memblockq_get_minreq(s
->memblockq
);
759 pa_atomic_store(&s
->missing
, 0);
760 s
->drain_request
= FALSE
;
762 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
764 pa_sink_input_put(s
->sink_input
);
769 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
770 connection
*c
= CONNECTION(o
);
771 connection_assert_ref(c
);
778 case CONNECTION_MESSAGE_REVOKE
:
779 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
782 case CONNECTION_MESSAGE_RELEASE
:
783 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
790 static void connection_unlink(connection
*c
) {
799 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
800 record_stream_unlink(r
);
802 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
803 if (playback_stream_isinstance(o
))
804 playback_stream_unlink(PLAYBACK_STREAM(o
));
806 upload_stream_unlink(UPLOAD_STREAM(o
));
809 pa_subscription_free(c
->subscription
);
812 pa_pstream_unlink(c
->pstream
);
814 if (c
->auth_timeout_event
) {
815 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
816 c
->auth_timeout_event
= NULL
;
819 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
824 static void connection_free(pa_object
*o
) {
825 connection
*c
= CONNECTION(o
);
829 connection_unlink(c
);
831 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
832 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
834 pa_pdispatch_unref(c
->pdispatch
);
835 pa_pstream_unref(c
->pstream
);
836 pa_client_free(c
->client
);
841 /* Called from thread context */
842 static void request_bytes(playback_stream
*s
) {
843 size_t m
, previous_missing
;
845 playback_stream_assert_ref(s
);
847 m
= pa_memblockq_pop_missing(s
->memblockq
);
852 /* pa_log("request_bytes(%u)", m); */
854 previous_missing
= pa_atomic_add(&s
->missing
, m
);
855 if (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
) {
856 pa_assert(pa_thread_mq_get());
857 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
861 static void send_memblock(connection
*c
) {
865 start
= PA_IDXSET_INVALID
;
869 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
872 if (start
== PA_IDXSET_INVALID
)
873 start
= c
->rrobin_index
;
874 else if (start
== c
->rrobin_index
)
877 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
878 pa_memchunk schunk
= chunk
;
880 if (schunk
.length
> r
->fragment_size
)
881 schunk
.length
= r
->fragment_size
;
883 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
885 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
886 pa_memblock_unref(schunk
.memblock
);
893 static void send_playback_stream_killed(playback_stream
*p
) {
895 playback_stream_assert_ref(p
);
897 t
= pa_tagstruct_new(NULL
, 0);
898 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
899 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
900 pa_tagstruct_putu32(t
, p
->index
);
901 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
904 static void send_record_stream_killed(record_stream
*r
) {
906 record_stream_assert_ref(r
);
908 t
= pa_tagstruct_new(NULL
, 0);
909 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
910 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
911 pa_tagstruct_putu32(t
, r
->index
);
912 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
915 /*** sink input callbacks ***/
917 /* Called from thread context */
918 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
919 pa_sink_input
*i
= PA_SINK_INPUT(o
);
922 pa_sink_input_assert_ref(i
);
923 s
= PLAYBACK_STREAM(i
->userdata
);
924 playback_stream_assert_ref(s
);
928 case SINK_INPUT_MESSAGE_SEEK
:
929 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
933 case SINK_INPUT_MESSAGE_POST_DATA
: {
936 /* pa_log("sink input post: %u", chunk->length); */
938 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
940 pa_log_warn("Failed to push data into queue");
941 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
942 pa_memblockq_seek(s
->memblockq
, chunk
->length
, PA_SEEK_RELATIVE
);
951 case SINK_INPUT_MESSAGE_DRAIN
: {
953 pa_memblockq_prebuf_disable(s
->memblockq
);
955 if (!pa_memblockq_is_readable(s
->memblockq
))
956 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
958 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
959 s
->drain_request
= TRUE
;
966 case SINK_INPUT_MESSAGE_FLUSH
:
967 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
968 case SINK_INPUT_MESSAGE_TRIGGER
: {
970 pa_sink_input
*isync
;
971 void (*func
)(pa_memblockq
*bq
);
974 case SINK_INPUT_MESSAGE_FLUSH
:
975 func
= pa_memblockq_flush
;
978 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
979 func
= pa_memblockq_prebuf_force
;
982 case SINK_INPUT_MESSAGE_TRIGGER
:
983 func
= pa_memblockq_prebuf_disable
;
987 pa_assert_not_reached();
994 /* Do the same for all other members in the sync group */
995 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
996 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
997 func(ssync
->memblockq
);
998 ssync
->underrun
= FALSE
;
999 request_bytes(ssync
);
1002 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1003 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1004 func(ssync
->memblockq
);
1005 ssync
->underrun
= FALSE
;
1006 request_bytes(ssync
);
1012 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1014 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1015 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1016 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1019 case PA_SINK_INPUT_MESSAGE_SET_STATE
:
1021 pa_memblockq_prebuf_force(s
->memblockq
);
1025 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1026 pa_usec_t
*r
= userdata
;
1028 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1030 /* Fall through, the default handler will add in the extra
1031 * latency added by the resampler */
1036 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1039 /* Called from thread context */
1040 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
1043 pa_sink_input_assert_ref(i
);
1044 s
= PLAYBACK_STREAM(i
->userdata
);
1045 playback_stream_assert_ref(s
);
1048 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0) {
1050 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1051 s
->drain_request
= FALSE
;
1052 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
);
1053 } else if (!s
->underrun
) {
1055 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1061 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1067 /* Called from main context */
1068 static void sink_input_kill_cb(pa_sink_input
*i
) {
1071 pa_sink_input_assert_ref(i
);
1072 s
= PLAYBACK_STREAM(i
->userdata
);
1073 playback_stream_assert_ref(s
);
1075 send_playback_stream_killed(s
);
1076 playback_stream_unlink(s
);
1079 /* Called from main context */
1080 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1084 pa_sink_input_assert_ref(i
);
1085 s
= PLAYBACK_STREAM(i
->userdata
);
1086 playback_stream_assert_ref(s
);
1088 if (s
->connection
->version
< 12)
1091 t
= pa_tagstruct_new(NULL
, 0);
1092 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1093 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1094 pa_tagstruct_putu32(t
, s
->index
);
1095 pa_tagstruct_put_boolean(t
, suspend
);
1096 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1099 /* Called from main context */
1100 static void sink_input_moved_cb(pa_sink_input
*i
) {
1104 pa_sink_input_assert_ref(i
);
1105 s
= PLAYBACK_STREAM(i
->userdata
);
1106 playback_stream_assert_ref(s
);
1108 if (s
->connection
->version
< 12)
1111 t
= pa_tagstruct_new(NULL
, 0);
1112 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1113 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1114 pa_tagstruct_putu32(t
, s
->index
);
1115 pa_tagstruct_putu32(t
, i
->sink
->index
);
1116 pa_tagstruct_puts(t
, i
->sink
->name
);
1117 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1118 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1121 /*** source_output callbacks ***/
1123 /* Called from thread context */
1124 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1127 pa_source_output_assert_ref(o
);
1128 s
= RECORD_STREAM(o
->userdata
);
1129 record_stream_assert_ref(s
);
1132 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1135 static void source_output_kill_cb(pa_source_output
*o
) {
1138 pa_source_output_assert_ref(o
);
1139 s
= RECORD_STREAM(o
->userdata
);
1140 record_stream_assert_ref(s
);
1142 send_record_stream_killed(s
);
1143 record_stream_unlink(s
);
1146 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1149 pa_source_output_assert_ref(o
);
1150 s
= RECORD_STREAM(o
->userdata
);
1151 record_stream_assert_ref(s
);
1153 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1155 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1158 /* Called from main context */
1159 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1163 pa_source_output_assert_ref(o
);
1164 s
= RECORD_STREAM(o
->userdata
);
1165 record_stream_assert_ref(s
);
1167 if (s
->connection
->version
< 12)
1170 t
= pa_tagstruct_new(NULL
, 0);
1171 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1172 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1173 pa_tagstruct_putu32(t
, s
->index
);
1174 pa_tagstruct_put_boolean(t
, suspend
);
1175 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1178 /* Called from main context */
1179 static void source_output_moved_cb(pa_source_output
*o
) {
1183 pa_source_output_assert_ref(o
);
1184 s
= RECORD_STREAM(o
->userdata
);
1185 record_stream_assert_ref(s
);
1187 if (s
->connection
->version
< 12)
1190 t
= pa_tagstruct_new(NULL
, 0);
1191 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1192 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1193 pa_tagstruct_putu32(t
, s
->index
);
1194 pa_tagstruct_putu32(t
, o
->source
->index
);
1195 pa_tagstruct_puts(t
, o
->source
->name
);
1196 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1197 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1200 /*** pdispatch callbacks ***/
1202 static void protocol_error(connection
*c
) {
1203 pa_log("protocol error, kicking client");
1204 connection_unlink(c
);
1207 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1208 if (!(expression)) { \
1209 pa_pstream_send_error((pstream), (tag), (error)); \
1214 static pa_tagstruct
*reply_new(uint32_t tag
) {
1215 pa_tagstruct
*reply
;
1217 reply
= pa_tagstruct_new(NULL
, 0);
1218 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1219 pa_tagstruct_putu32(reply
, tag
);
1223 static void command_create_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1224 connection
*c
= CONNECTION(userdata
);
1226 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1227 const char *name
= NULL
, *sink_name
;
1230 pa_tagstruct
*reply
;
1231 pa_sink
*sink
= NULL
;
1234 int no_remap
= 0, no_remix
= 0, fix_format
= 0, fix_rate
= 0, fix_channels
= 0, no_move
= 0, variable_rate
= 0;
1235 pa_sink_input_flags_t flags
= 0;
1238 connection_assert_ref(c
);
1241 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1244 PA_TAG_SAMPLE_SPEC
, &ss
,
1245 PA_TAG_CHANNEL_MAP
, &map
,
1246 PA_TAG_U32
, &sink_index
,
1247 PA_TAG_STRING
, &sink_name
,
1248 PA_TAG_U32
, &maxlength
,
1249 PA_TAG_BOOLEAN
, &corked
,
1250 PA_TAG_U32
, &tlength
,
1251 PA_TAG_U32
, &prebuf
,
1252 PA_TAG_U32
, &minreq
,
1253 PA_TAG_U32
, &syncid
,
1254 PA_TAG_CVOLUME
, &volume
,
1255 PA_TAG_INVALID
) < 0) {
1261 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1262 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(sink_name
)), tag
, PA_ERR_INVALID
);
1263 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1264 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1265 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1266 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1267 CHECK_VALIDITY(c
->pstream
, maxlength
> 0, tag
, PA_ERR_INVALID
);
1268 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
1270 p
= pa_proplist_new();
1273 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1275 if (c
->version
>= 12) {
1276 /* Since 0.9.8 the user can ask for a couple of additional flags */
1278 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1279 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1280 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1281 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1282 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1283 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1284 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1287 pa_proplist_free(p
);
1292 if (c
->version
>= 13) {
1294 if (pa_tagstruct_get_proplist(t
, p
) < 0) {
1296 pa_proplist_free(p
);
1301 if (!pa_tagstruct_eof(t
)) {
1303 pa_proplist_free(p
);
1307 if (sink_index
!= PA_INVALID_INDEX
) {
1309 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1310 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1311 pa_proplist_free(p
);
1315 } else if (sink_name
) {
1317 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1))) {
1318 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1319 pa_proplist_free(p
);
1325 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1326 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1327 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1328 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1329 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1330 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1331 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1332 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0);
1334 s
= playback_stream_new(c
, sink
, &ss
, &map
, name
, &maxlength
, &tlength
, &prebuf
, &minreq
, &volume
, syncid
, &missing
, flags
, p
);
1335 pa_proplist_free(p
);
1337 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1339 reply
= reply_new(tag
);
1340 pa_tagstruct_putu32(reply
, s
->index
);
1341 pa_assert(s
->sink_input
);
1342 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1343 pa_tagstruct_putu32(reply
, missing
);
1345 /* pa_log("initial request is %u", missing); */
1347 if (c
->version
>= 9) {
1348 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1350 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1351 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1352 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1353 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1356 if (c
->version
>= 12) {
1357 /* Since 0.9.8 we support sending the chosen sample
1358 * spec/channel map/device/suspend status back to the
1361 pa_tagstruct_put_sample_spec(reply
, &ss
);
1362 pa_tagstruct_put_channel_map(reply
, &map
);
1364 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1365 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1367 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1370 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1373 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1374 connection
*c
= CONNECTION(userdata
);
1377 connection_assert_ref(c
);
1380 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1381 !pa_tagstruct_eof(t
)) {
1386 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1390 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1392 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1393 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1397 playback_stream_unlink(s
);
1401 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1403 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1404 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1408 record_stream_unlink(s
);
1412 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1415 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1416 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1420 upload_stream_unlink(s
);
1425 pa_assert_not_reached();
1428 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1431 static void command_create_record_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1432 connection
*c
= CONNECTION(userdata
);
1434 uint32_t maxlength
, fragment_size
;
1435 uint32_t source_index
;
1436 const char *name
, *source_name
;
1439 pa_tagstruct
*reply
;
1440 pa_source
*source
= NULL
;
1442 int no_remap
= 0, no_remix
= 0, fix_format
= 0, fix_rate
= 0, fix_channels
= 0, no_move
= 0, variable_rate
= 0;
1443 pa_source_output_flags_t flags
= 0;
1446 connection_assert_ref(c
);
1449 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1450 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1451 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1452 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1453 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1454 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1455 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1456 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1461 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1462 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1463 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1464 CHECK_VALIDITY(c
->pstream
, source_index
!= PA_INVALID_INDEX
|| !source_name
|| (*source_name
&& pa_utf8_valid(source_name
)), tag
, PA_ERR_INVALID
);
1465 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1466 CHECK_VALIDITY(c
->pstream
, maxlength
> 0, tag
, PA_ERR_INVALID
);
1467 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
1469 p
= pa_proplist_new();
1472 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1474 if (c
->version
>= 12) {
1475 /* Since 0.9.8 the user can ask for a couple of additional flags */
1477 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1478 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1479 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1480 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1481 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1482 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1483 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1486 pa_proplist_free(p
);
1491 if (c
->version
>= 13) {
1493 if (pa_tagstruct_get_proplist(t
, p
) < 0) {
1495 pa_proplist_free(p
);
1500 if (!pa_tagstruct_eof(t
)) {
1502 pa_proplist_free(p
);
1506 if (source_index
!= PA_INVALID_INDEX
) {
1508 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
1509 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1510 pa_proplist_free(p
);
1514 } else if (source_name
) {
1516 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1))) {
1517 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1518 pa_proplist_free(p
);
1524 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
1525 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
1526 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
1527 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
1528 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
1529 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
1530 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
1531 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0);
1533 s
= record_stream_new(c
, source
, &ss
, &map
, name
, &maxlength
, fragment_size
, flags
, p
);
1534 pa_proplist_free(p
);
1536 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1538 reply
= reply_new(tag
);
1539 pa_tagstruct_putu32(reply
, s
->index
);
1540 pa_assert(s
->source_output
);
1541 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
1543 if (c
->version
>= 9) {
1544 /* Since 0.9 we support sending the buffer metrics back to the client */
1546 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1547 pa_tagstruct_putu32(reply
, (uint32_t) s
->fragment_size
);
1550 if (c
->version
>= 12) {
1551 /* Since 0.9.8 we support sending the chosen sample
1552 * spec/channel map/device/suspend status back to the
1555 pa_tagstruct_put_sample_spec(reply
, &ss
);
1556 pa_tagstruct_put_channel_map(reply
, &map
);
1558 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
1559 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
1561 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
1564 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1567 static void command_exit(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1568 connection
*c
= CONNECTION(userdata
);
1570 connection_assert_ref(c
);
1573 if (!pa_tagstruct_eof(t
)) {
1578 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1580 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
1581 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
1584 static void command_auth(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1585 connection
*c
= CONNECTION(userdata
);
1587 pa_tagstruct
*reply
;
1590 connection_assert_ref(c
);
1593 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
1594 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
1595 !pa_tagstruct_eof(t
)) {
1600 /* Minimum supported version */
1601 if (c
->version
< 8) {
1602 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
1606 pa_snprintf(tmp
, sizeof(tmp
), "%u", c
->version
);
1607 pa_proplist_sets(c
->client
->proplist
, "native-protocol.version", tmp
);
1609 if (!c
->authorized
) {
1613 const pa_creds
*creds
;
1615 if ((creds
= pa_pdispatch_creds(pd
))) {
1616 if (creds
->uid
== getuid())
1618 else if (c
->protocol
->auth_group
) {
1622 if ((gid
= pa_get_gid_of_group(c
->protocol
->auth_group
)) == (gid_t
) -1)
1623 pa_log_warn("failed to get GID of group '%s'", c
->protocol
->auth_group
);
1624 else if (gid
== creds
->gid
)
1628 if ((r
= pa_uid_in_group(creds
->uid
, c
->protocol
->auth_group
)) < 0)
1629 pa_log_warn("failed to check group membership.");
1635 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
1636 (unsigned long) creds
->uid
,
1637 (unsigned long) creds
->gid
,
1640 if (c
->version
>= 10 &&
1641 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
1642 creds
->uid
== getuid()) {
1644 pa_pstream_use_shm(c
->pstream
, 1);
1645 pa_log_info("Enabled SHM for new connection");
1651 if (!success
&& memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
1655 pa_log_warn("Denied access to client with invalid authorization data.");
1656 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1660 c
->authorized
= TRUE
;
1661 if (c
->auth_timeout_event
) {
1662 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1663 c
->auth_timeout_event
= NULL
;
1667 reply
= reply_new(tag
);
1668 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
);
1672 /* SHM support is only enabled after both sides made sure they are the same user. */
1676 ucred
.uid
= getuid();
1677 ucred
.gid
= getgid();
1679 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
1682 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1686 static void command_set_client_name(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1687 connection
*c
= CONNECTION(userdata
);
1688 const char *name
= NULL
;
1690 pa_tagstruct
*reply
;
1692 connection_assert_ref(c
);
1695 p
= pa_proplist_new();
1697 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
1698 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
1699 !pa_tagstruct_eof(t
)) {
1702 pa_proplist_free(p
);
1707 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
1708 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
1709 pa_proplist_free(p
);
1713 pa_proplist_update(c
->client
->proplist
, PA_UPDATE_REPLACE
, p
);
1714 pa_proplist_free(p
);
1716 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
1718 reply
= reply_new(tag
);
1720 if (c
->version
>= 13)
1721 pa_tagstruct_putu32(reply
, c
->client
->index
);
1723 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1726 static void command_lookup(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1727 connection
*c
= CONNECTION(userdata
);
1729 uint32_t idx
= PA_IDXSET_INVALID
;
1731 connection_assert_ref(c
);
1734 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1735 !pa_tagstruct_eof(t
)) {
1740 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1741 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1743 if (command
== PA_COMMAND_LOOKUP_SINK
) {
1745 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
1749 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
1750 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
1751 idx
= source
->index
;
1754 if (idx
== PA_IDXSET_INVALID
)
1755 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1757 pa_tagstruct
*reply
;
1758 reply
= reply_new(tag
);
1759 pa_tagstruct_putu32(reply
, idx
);
1760 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1764 static void command_drain_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1765 connection
*c
= CONNECTION(userdata
);
1769 connection_assert_ref(c
);
1772 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1773 !pa_tagstruct_eof(t
)) {
1778 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1779 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1780 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1781 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1783 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
);
1786 static void command_stat(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1787 connection
*c
= CONNECTION(userdata
);
1788 pa_tagstruct
*reply
;
1789 const pa_mempool_stat
*stat
;
1791 connection_assert_ref(c
);
1794 if (!pa_tagstruct_eof(t
)) {
1799 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1801 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
1803 reply
= reply_new(tag
);
1804 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
1805 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
1806 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
1807 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
1808 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
1809 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1812 static void command_get_playback_latency(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1813 connection
*c
= CONNECTION(userdata
);
1814 pa_tagstruct
*reply
;
1816 struct timeval tv
, now
;
1820 connection_assert_ref(c
);
1823 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1824 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1825 !pa_tagstruct_eof(t
)) {
1830 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1831 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1832 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1833 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1834 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
)
1836 reply
= reply_new(tag
);
1838 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
1839 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
1841 pa_tagstruct_put_usec(reply
, latency
);
1843 pa_tagstruct_put_usec(reply
, 0);
1844 pa_tagstruct_put_boolean(reply
, pa_sink_input_get_state(s
->sink_input
) == PA_SINK_INPUT_RUNNING
);
1845 pa_tagstruct_put_timeval(reply
, &tv
);
1846 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
1847 pa_tagstruct_puts64(reply
, s
->write_index
);
1848 pa_tagstruct_puts64(reply
, s
->read_index
);
1849 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1852 static void command_get_record_latency(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1853 connection
*c
= CONNECTION(userdata
);
1854 pa_tagstruct
*reply
;
1856 struct timeval tv
, now
;
1859 connection_assert_ref(c
);
1862 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1863 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1864 !pa_tagstruct_eof(t
)) {
1869 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1870 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
1871 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1873 reply
= reply_new(tag
);
1874 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
1875 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
1876 pa_tagstruct_put_boolean(reply
, 0);
1877 pa_tagstruct_put_timeval(reply
, &tv
);
1878 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
1879 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
1880 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
1881 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1884 static void command_create_upload_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1885 connection
*c
= CONNECTION(userdata
);
1888 const char *name
= NULL
;
1891 pa_tagstruct
*reply
;
1894 connection_assert_ref(c
);
1897 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
1898 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1899 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1900 pa_tagstruct_getu32(t
, &length
) < 0) {
1905 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1906 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1907 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1908 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1909 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
1910 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
1912 if (c
->version
< 13)
1913 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1915 p
= pa_proplist_new();
1917 if (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) {
1919 pa_proplist_free(p
);
1923 if (c
->version
< 13)
1924 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1926 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
1927 pa_proplist_free(p
);
1929 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1931 reply
= reply_new(tag
);
1932 pa_tagstruct_putu32(reply
, s
->index
);
1933 pa_tagstruct_putu32(reply
, length
);
1934 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1937 static void command_finish_upload_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1938 connection
*c
= CONNECTION(userdata
);
1943 connection_assert_ref(c
);
1946 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1947 !pa_tagstruct_eof(t
)) {
1952 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1954 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
1955 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1956 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1958 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
1959 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
1961 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1963 upload_stream_unlink(s
);
1966 static void command_play_sample(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1967 connection
*c
= CONNECTION(userdata
);
1968 uint32_t sink_index
;
1971 const char *name
, *sink_name
;
1974 pa_tagstruct
*reply
;
1976 connection_assert_ref(c
);
1979 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1981 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1982 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1983 pa_tagstruct_getu32(t
, &volume
) < 0 ||
1984 pa_tagstruct_gets(t
, &name
) < 0) {
1989 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1990 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1992 if (sink_index
!= PA_INVALID_INDEX
)
1993 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1995 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1997 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
1999 p
= pa_proplist_new();
2001 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2002 !pa_tagstruct_eof(t
)) {
2004 pa_proplist_free(p
);
2008 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2009 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2010 pa_proplist_free(p
);
2014 pa_proplist_free(p
);
2016 reply
= reply_new(tag
);
2018 if (c
->version
>= 13)
2019 pa_tagstruct_putu32(reply
, idx
);
2021 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2024 static void command_remove_sample(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2025 connection
*c
= CONNECTION(userdata
);
2028 connection_assert_ref(c
);
2031 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2032 !pa_tagstruct_eof(t
)) {
2037 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2038 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2040 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2041 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2045 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2048 static void fixup_sample_spec(connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2051 pa_assert(original
);
2055 if (c
->version
< 12) {
2056 /* Before protocol version 12 we didn't support S32 samples,
2057 * so we need to lie about this to the client */
2059 if (fixed
->format
== PA_SAMPLE_S32LE
)
2060 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2061 if (fixed
->format
== PA_SAMPLE_S32BE
)
2062 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2066 static void sink_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2067 pa_sample_spec fixed_ss
;
2070 pa_sink_assert_ref(sink
);
2072 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2076 PA_TAG_U32
, sink
->index
,
2077 PA_TAG_STRING
, sink
->name
,
2078 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2079 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2080 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2081 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2082 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
),
2083 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
),
2084 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2085 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2086 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2087 PA_TAG_STRING
, sink
->driver
,
2088 PA_TAG_U32
, sink
->flags
,
2091 if (c
->version
>= 13)
2092 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2095 static void source_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2096 pa_sample_spec fixed_ss
;
2099 pa_source_assert_ref(source
);
2101 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2105 PA_TAG_U32
, source
->index
,
2106 PA_TAG_STRING
, source
->name
,
2107 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2108 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2109 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2110 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2111 PA_TAG_CVOLUME
, pa_source_get_volume(source
),
2112 PA_TAG_BOOLEAN
, pa_source_get_mute(source
),
2113 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2114 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2115 PA_TAG_USEC
, pa_source_get_latency(source
),
2116 PA_TAG_STRING
, source
->driver
,
2117 PA_TAG_U32
, source
->flags
,
2120 if (c
->version
>= 13)
2121 pa_tagstruct_put_proplist(t
, source
->proplist
);
2124 static void client_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2128 pa_tagstruct_putu32(t
, client
->index
);
2129 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2130 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2131 pa_tagstruct_puts(t
, client
->driver
);
2133 if (c
->version
>= 13)
2134 pa_tagstruct_put_proplist(t
, client
->proplist
);
2138 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
2142 pa_tagstruct_putu32(t
, module
->index
);
2143 pa_tagstruct_puts(t
, module
->name
);
2144 pa_tagstruct_puts(t
, module
->argument
);
2145 pa_tagstruct_putu32(t
, module
->n_used
);
2146 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
2149 static void sink_input_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2150 pa_sample_spec fixed_ss
;
2153 pa_sink_input_assert_ref(s
);
2155 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2157 pa_tagstruct_putu32(t
, s
->index
);
2158 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2159 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2160 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2161 pa_tagstruct_putu32(t
, s
->sink
->index
);
2162 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2163 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2164 pa_tagstruct_put_cvolume(t
, &s
->volume
);
2165 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
2166 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
2167 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2168 pa_tagstruct_puts(t
, s
->driver
);
2169 if (c
->version
>= 11)
2170 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2171 if (c
->version
>= 13)
2172 pa_tagstruct_put_proplist(t
, s
->proplist
);
2175 static void source_output_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2176 pa_sample_spec fixed_ss
;
2179 pa_source_output_assert_ref(s
);
2181 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2183 pa_tagstruct_putu32(t
, s
->index
);
2184 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2185 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2186 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2187 pa_tagstruct_putu32(t
, s
->source
->index
);
2188 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2189 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2190 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
2191 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
2192 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2193 pa_tagstruct_puts(t
, s
->driver
);
2195 if (c
->version
>= 13)
2196 pa_tagstruct_put_proplist(t
, s
->proplist
);
2199 static void scache_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2200 pa_sample_spec fixed_ss
;
2205 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2207 pa_tagstruct_putu32(t
, e
->index
);
2208 pa_tagstruct_puts(t
, e
->name
);
2209 pa_tagstruct_put_cvolume(t
, &e
->volume
);
2210 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
2211 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2212 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2213 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
2214 pa_tagstruct_put_boolean(t
, e
->lazy
);
2215 pa_tagstruct_puts(t
, e
->filename
);
2217 if (c
->version
>= 13)
2218 pa_tagstruct_put_proplist(t
, e
->proplist
);
2221 static void command_get_info(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2222 connection
*c
= CONNECTION(userdata
);
2224 pa_sink
*sink
= NULL
;
2225 pa_source
*source
= NULL
;
2226 pa_client
*client
= NULL
;
2227 pa_module
*module
= NULL
;
2228 pa_sink_input
*si
= NULL
;
2229 pa_source_output
*so
= NULL
;
2230 pa_scache_entry
*sce
= NULL
;
2232 pa_tagstruct
*reply
;
2234 connection_assert_ref(c
);
2237 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2238 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2239 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2240 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2241 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2242 pa_tagstruct_gets(t
, &name
) < 0) ||
2243 !pa_tagstruct_eof(t
)) {
2248 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2249 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2251 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2252 if (idx
!= PA_INVALID_INDEX
)
2253 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2255 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2256 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2257 if (idx
!= PA_INVALID_INDEX
)
2258 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2260 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2261 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2262 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2263 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2264 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2265 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2266 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2267 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2268 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2270 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2271 if (idx
!= PA_INVALID_INDEX
)
2272 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2274 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
2277 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
2278 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2282 reply
= reply_new(tag
);
2284 sink_fill_tagstruct(c
, reply
, sink
);
2286 source_fill_tagstruct(c
, reply
, source
);
2288 client_fill_tagstruct(c
, reply
, client
);
2290 module_fill_tagstruct(reply
, module
);
2292 sink_input_fill_tagstruct(c
, reply
, si
);
2294 source_output_fill_tagstruct(c
, reply
, so
);
2296 scache_fill_tagstruct(c
, reply
, sce
);
2297 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2300 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2301 connection
*c
= CONNECTION(userdata
);
2305 pa_tagstruct
*reply
;
2307 connection_assert_ref(c
);
2310 if (!pa_tagstruct_eof(t
)) {
2315 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2317 reply
= reply_new(tag
);
2319 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2320 i
= c
->protocol
->core
->sinks
;
2321 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2322 i
= c
->protocol
->core
->sources
;
2323 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2324 i
= c
->protocol
->core
->clients
;
2325 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2326 i
= c
->protocol
->core
->modules
;
2327 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2328 i
= c
->protocol
->core
->sink_inputs
;
2329 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2330 i
= c
->protocol
->core
->source_outputs
;
2332 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2333 i
= c
->protocol
->core
->scache
;
2337 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2338 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2339 sink_fill_tagstruct(c
, reply
, p
);
2340 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2341 source_fill_tagstruct(c
, reply
, p
);
2342 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2343 client_fill_tagstruct(c
, reply
, p
);
2344 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2345 module_fill_tagstruct(reply
, p
);
2346 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2347 sink_input_fill_tagstruct(c
, reply
, p
);
2348 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2349 source_output_fill_tagstruct(c
, reply
, p
);
2351 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2352 scache_fill_tagstruct(c
, reply
, p
);
2357 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2360 static void command_get_server_info(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2361 connection
*c
= CONNECTION(userdata
);
2362 pa_tagstruct
*reply
;
2365 pa_sample_spec fixed_ss
;
2367 connection_assert_ref(c
);
2370 if (!pa_tagstruct_eof(t
)) {
2375 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2377 reply
= reply_new(tag
);
2378 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
2379 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
2380 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
2381 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
2383 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
2384 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
2386 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
2387 pa_tagstruct_puts(reply
, n
);
2388 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
2389 pa_tagstruct_puts(reply
, n
);
2391 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
2393 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2396 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
2398 connection
*c
= CONNECTION(userdata
);
2400 connection_assert_ref(c
);
2402 t
= pa_tagstruct_new(NULL
, 0);
2403 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
2404 pa_tagstruct_putu32(t
, (uint32_t) -1);
2405 pa_tagstruct_putu32(t
, e
);
2406 pa_tagstruct_putu32(t
, idx
);
2407 pa_pstream_send_tagstruct(c
->pstream
, t
);
2410 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2411 connection
*c
= CONNECTION(userdata
);
2412 pa_subscription_mask_t m
;
2414 connection_assert_ref(c
);
2417 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
2418 !pa_tagstruct_eof(t
)) {
2423 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2424 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
2426 if (c
->subscription
)
2427 pa_subscription_free(c
->subscription
);
2430 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
2431 pa_assert(c
->subscription
);
2433 c
->subscription
= NULL
;
2435 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2438 static void command_set_volume(
2439 PA_GCC_UNUSED pa_pdispatch
*pd
,
2445 connection
*c
= CONNECTION(userdata
);
2448 pa_sink
*sink
= NULL
;
2449 pa_source
*source
= NULL
;
2450 pa_sink_input
*si
= NULL
;
2451 const char *name
= NULL
;
2453 connection_assert_ref(c
);
2456 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2457 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2458 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2459 pa_tagstruct_get_cvolume(t
, &volume
) ||
2460 !pa_tagstruct_eof(t
)) {
2465 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2466 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2467 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
2471 case PA_COMMAND_SET_SINK_VOLUME
:
2472 if (idx
!= PA_INVALID_INDEX
)
2473 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2475 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2478 case PA_COMMAND_SET_SOURCE_VOLUME
:
2479 if (idx
!= PA_INVALID_INDEX
)
2480 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2482 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2485 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
2486 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2490 pa_assert_not_reached();
2493 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2496 pa_sink_set_volume(sink
, &volume
);
2498 pa_source_set_volume(source
, &volume
);
2500 pa_sink_input_set_volume(si
, &volume
);
2502 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2505 static void command_set_mute(
2506 PA_GCC_UNUSED pa_pdispatch
*pd
,
2512 connection
*c
= CONNECTION(userdata
);
2515 pa_sink
*sink
= NULL
;
2516 pa_source
*source
= NULL
;
2517 pa_sink_input
*si
= NULL
;
2518 const char *name
= NULL
;
2520 connection_assert_ref(c
);
2523 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2524 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2525 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2526 pa_tagstruct_get_boolean(t
, &mute
) ||
2527 !pa_tagstruct_eof(t
)) {
2532 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2533 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2537 case PA_COMMAND_SET_SINK_MUTE
:
2539 if (idx
!= PA_INVALID_INDEX
)
2540 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2542 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2546 case PA_COMMAND_SET_SOURCE_MUTE
:
2547 if (idx
!= PA_INVALID_INDEX
)
2548 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2550 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2554 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
2555 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2559 pa_assert_not_reached();
2562 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2565 pa_sink_set_mute(sink
, mute
);
2567 pa_source_set_mute(source
, mute
);
2569 pa_sink_input_set_mute(si
, mute
);
2571 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2574 static void command_cork_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2575 connection
*c
= CONNECTION(userdata
);
2580 connection_assert_ref(c
);
2583 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2584 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
2585 !pa_tagstruct_eof(t
)) {
2590 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2591 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2592 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2593 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2594 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2596 pa_sink_input_cork(s
->sink_input
, b
);
2597 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2600 static void command_trigger_or_flush_or_prebuf_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2601 connection
*c
= CONNECTION(userdata
);
2605 connection_assert_ref(c
);
2608 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2609 !pa_tagstruct_eof(t
)) {
2614 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2615 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2616 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2617 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2618 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2621 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
2622 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
2625 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
2626 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
2629 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
2630 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
2634 pa_assert_not_reached();
2637 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2640 static void command_cork_record_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2641 connection
*c
= CONNECTION(userdata
);
2646 connection_assert_ref(c
);
2649 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2650 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
2651 !pa_tagstruct_eof(t
)) {
2656 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2657 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2658 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2660 pa_source_output_cork(s
->source_output
, b
);
2661 pa_memblockq_prebuf_force(s
->memblockq
);
2662 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2665 static void command_flush_record_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2666 connection
*c
= CONNECTION(userdata
);
2670 connection_assert_ref(c
);
2673 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2674 !pa_tagstruct_eof(t
)) {
2679 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2680 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2681 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2683 pa_memblockq_flush(s
->memblockq
);
2684 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2687 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2688 connection
*c
= CONNECTION(userdata
);
2690 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
2691 pa_tagstruct
*reply
;
2693 connection_assert_ref(c
);
2696 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
2701 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2703 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
2706 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2707 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2708 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2710 if (pa_tagstruct_get(
2712 PA_TAG_U32
, &maxlength
,
2713 PA_TAG_U32
, &tlength
,
2714 PA_TAG_U32
, &prebuf
,
2715 PA_TAG_U32
, &minreq
,
2716 PA_TAG_INVALID
) < 0 ||
2717 !pa_tagstruct_eof(t
)) {
2722 CHECK_VALIDITY(c
->pstream
, maxlength
> 0, tag
, PA_ERR_INVALID
);
2723 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
2725 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
2726 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
2727 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
2728 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
2730 reply
= reply_new(tag
);
2731 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
));
2732 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_tlength(s
->memblockq
));
2733 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
));
2734 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_minreq(s
->memblockq
));
2739 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
2741 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2742 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2744 if (pa_tagstruct_get(
2746 PA_TAG_U32
, &maxlength
,
2747 PA_TAG_U32
, &fragsize
,
2748 PA_TAG_INVALID
) < 0 ||
2749 !pa_tagstruct_eof(t
)) {
2754 CHECK_VALIDITY(c
->pstream
, maxlength
> 0, tag
, PA_ERR_INVALID
);
2755 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
2757 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
2759 base
= pa_frame_size(&s
->source_output
->sample_spec
);
2760 s
->fragment_size
= (fragsize
/base
)*base
;
2761 if (s
->fragment_size
<= 0)
2762 s
->fragment_size
= base
;
2764 if (s
->fragment_size
> pa_memblockq_get_maxlength(s
->memblockq
))
2765 s
->fragment_size
= pa_memblockq_get_maxlength(s
->memblockq
);
2767 reply
= reply_new(tag
);
2768 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
));
2769 pa_tagstruct_putu32(reply
, s
->fragment_size
);
2772 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2775 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2776 connection
*c
= CONNECTION(userdata
);
2780 connection_assert_ref(c
);
2783 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2784 pa_tagstruct_getu32(t
, &rate
) < 0 ||
2785 !pa_tagstruct_eof(t
)) {
2790 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2791 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
2793 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
2796 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2797 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2798 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2800 pa_sink_input_set_rate(s
->sink_input
, rate
);
2804 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
2806 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2807 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2809 pa_source_output_set_rate(s
->source_output
, rate
);
2812 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2815 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2816 connection
*c
= CONNECTION(userdata
);
2821 connection_assert_ref(c
);
2824 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2826 p
= pa_proplist_new();
2828 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
2830 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
2831 pa_tagstruct_get_proplist(t
, p
) < 0 ||
2832 !pa_tagstruct_eof(t
)) {
2834 pa_proplist_free(p
);
2840 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2841 pa_tagstruct_getu32(t
, &mode
) < 0 ||
2842 pa_tagstruct_get_proplist(t
, p
) < 0 ||
2843 !pa_tagstruct_eof(t
)) {
2845 pa_proplist_free(p
);
2850 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
2852 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
2855 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2856 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2857 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2859 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
2860 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
2862 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
2865 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2866 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2868 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
2869 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
2871 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
2873 pa_proplist_update(c
->client
->proplist
, mode
, p
);
2874 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2877 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2880 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2881 connection
*c
= CONNECTION(userdata
);
2883 unsigned changed
= 0;
2885 pa_strlist
*l
= NULL
;
2887 connection_assert_ref(c
);
2890 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2892 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
2894 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
2900 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
2903 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2904 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2905 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2907 p
= s
->sink_input
->proplist
;
2909 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
2912 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2913 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2915 p
= s
->source_output
->proplist
;
2917 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
2919 p
= c
->client
->proplist
;
2925 if (pa_tagstruct_gets(t
, &k
) < 0) {
2934 l
= pa_strlist_prepend(l
, k
);
2937 if (!pa_tagstruct_eof(t
)) {
2946 l
= pa_strlist_pop(l
, &z
);
2951 changed
+= pa_proplist_unset(p
, z
) >= 0;
2955 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2958 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
2961 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2962 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
2964 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
2967 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2968 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
2971 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
2972 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2977 static void command_set_default_sink_or_source(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2978 connection
*c
= CONNECTION(userdata
);
2981 connection_assert_ref(c
);
2984 if (pa_tagstruct_gets(t
, &s
) < 0 ||
2985 !pa_tagstruct_eof(t
)) {
2990 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2991 CHECK_VALIDITY(c
->pstream
, !s
|| (*s
&& pa_utf8_valid(s
)), tag
, PA_ERR_INVALID
);
2993 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
2994 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2997 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2998 connection
*c
= CONNECTION(userdata
);
3002 connection_assert_ref(c
);
3005 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3006 pa_tagstruct_gets(t
, &name
) < 0 ||
3007 !pa_tagstruct_eof(t
)) {
3012 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3013 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3015 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3018 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3019 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3020 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3022 pa_sink_input_set_name(s
->sink_input
, name
);
3026 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3028 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3029 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3031 pa_source_output_set_name(s
->source_output
, name
);
3034 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3037 static void command_kill(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3038 connection
*c
= CONNECTION(userdata
);
3041 connection_assert_ref(c
);
3044 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3045 !pa_tagstruct_eof(t
)) {
3050 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3052 if (command
== PA_COMMAND_KILL_CLIENT
) {
3055 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3056 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3059 pa_client_kill(client
);
3061 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3064 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3065 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3068 pa_sink_input_kill(s
);
3070 pa_source_output
*s
;
3072 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3074 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3075 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3078 pa_source_output_kill(s
);
3081 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3082 connection_unref(c
);
3085 static void command_load_module(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3086 connection
*c
= CONNECTION(userdata
);
3088 const char *name
, *argument
;
3089 pa_tagstruct
*reply
;
3091 connection_assert_ref(c
);
3094 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3095 pa_tagstruct_gets(t
, &argument
) < 0 ||
3096 !pa_tagstruct_eof(t
)) {
3101 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3102 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3103 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3105 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3106 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3110 reply
= reply_new(tag
);
3111 pa_tagstruct_putu32(reply
, m
->index
);
3112 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3115 static void command_unload_module(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3116 connection
*c
= CONNECTION(userdata
);
3120 connection_assert_ref(c
);
3123 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3124 !pa_tagstruct_eof(t
)) {
3129 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3130 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3131 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3133 pa_module_unload_request(m
);
3134 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3137 static void command_add_autoload(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3138 connection
*c
= CONNECTION(userdata
);
3139 const char *name
, *module
, *argument
;
3142 pa_tagstruct
*reply
;
3144 connection_assert_ref(c
);
3147 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3148 pa_tagstruct_getu32(t
, &type
) < 0 ||
3149 pa_tagstruct_gets(t
, &module
) < 0 ||
3150 pa_tagstruct_gets(t
, &argument
) < 0 ||
3151 !pa_tagstruct_eof(t
)) {
3156 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3157 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3158 CHECK_VALIDITY(c
->pstream
, type
== 0 || type
== 1, tag
, PA_ERR_INVALID
);
3159 CHECK_VALIDITY(c
->pstream
, module
&& *module
&& pa_utf8_valid(module
), tag
, PA_ERR_INVALID
);
3160 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3162 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
3163 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
3167 reply
= reply_new(tag
);
3168 pa_tagstruct_putu32(reply
, idx
);
3169 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3172 static void command_remove_autoload(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3173 connection
*c
= CONNECTION(userdata
);
3174 const char *name
= NULL
;
3175 uint32_t type
, idx
= PA_IDXSET_INVALID
;
3178 connection_assert_ref(c
);
3181 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3182 (pa_tagstruct_gets(t
, &name
) < 0 ||
3183 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3184 !pa_tagstruct_eof(t
)) {
3189 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3190 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3191 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& pa_utf8_valid(name
) && (type
== 0 || type
== 1)), tag
, PA_ERR_INVALID
);
3194 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3196 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
3198 CHECK_VALIDITY(c
->pstream
, r
>= 0, tag
, PA_ERR_NOENTITY
);
3200 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3203 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
3206 pa_tagstruct_putu32(t
, e
->index
);
3207 pa_tagstruct_puts(t
, e
->name
);
3208 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
3209 pa_tagstruct_puts(t
, e
->module
);
3210 pa_tagstruct_puts(t
, e
->argument
);
3213 static void command_get_autoload_info(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3214 connection
*c
= CONNECTION(userdata
);
3215 const pa_autoload_entry
*a
= NULL
;
3218 pa_tagstruct
*reply
;
3220 connection_assert_ref(c
);
3223 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3224 (pa_tagstruct_gets(t
, &name
) < 0 ||
3225 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3226 !pa_tagstruct_eof(t
)) {
3231 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3232 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3233 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& (type
== 0 || type
== 1) && pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
3236 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3238 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
3240 CHECK_VALIDITY(c
->pstream
, a
, tag
, PA_ERR_NOENTITY
);
3242 reply
= reply_new(tag
);
3243 autoload_fill_tagstruct(reply
, a
);
3244 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3247 static void command_get_autoload_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3248 connection
*c
= CONNECTION(userdata
);
3249 pa_tagstruct
*reply
;
3251 connection_assert_ref(c
);
3254 if (!pa_tagstruct_eof(t
)) {
3259 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3261 reply
= reply_new(tag
);
3263 if (c
->protocol
->core
->autoload_hashmap
) {
3264 pa_autoload_entry
*a
;
3267 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
3268 autoload_fill_tagstruct(reply
, a
);
3271 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3274 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3275 connection
*c
= CONNECTION(userdata
);
3276 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3277 const char *name
= NULL
;
3279 connection_assert_ref(c
);
3282 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3283 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3284 pa_tagstruct_gets(t
, &name
) < 0 ||
3285 !pa_tagstruct_eof(t
)) {
3290 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3291 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3292 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
3294 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3295 pa_sink_input
*si
= NULL
;
3296 pa_sink
*sink
= NULL
;
3298 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3300 if (idx_device
!= PA_INVALID_INDEX
)
3301 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3303 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3305 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3307 if (pa_sink_input_move_to(si
, sink
, 0) < 0) {
3308 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3312 pa_source_output
*so
= NULL
;
3315 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3317 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3319 if (idx_device
!= PA_INVALID_INDEX
)
3320 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3322 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3324 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3326 if (pa_source_output_move_to(so
, source
) < 0) {
3327 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3332 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3335 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3336 connection
*c
= CONNECTION(userdata
);
3337 uint32_t idx
= PA_INVALID_INDEX
;
3338 const char *name
= NULL
;
3341 connection_assert_ref(c
);
3344 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3345 pa_tagstruct_gets(t
, &name
) < 0 ||
3346 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3347 !pa_tagstruct_eof(t
)) {
3352 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3353 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| !*name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3355 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3357 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3359 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3360 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3364 pa_sink
*sink
= NULL
;
3366 if (idx
!= PA_INVALID_INDEX
)
3367 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3369 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3371 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3373 if (pa_sink_suspend(sink
, b
) < 0) {
3374 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3380 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3382 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3384 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3385 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3392 if (idx
!= PA_INVALID_INDEX
)
3393 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3395 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3397 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3399 if (pa_source_suspend(source
, b
) < 0) {
3400 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3406 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3409 /*** pstream callbacks ***/
3411 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
3412 connection
*c
= CONNECTION(userdata
);
3416 connection_assert_ref(c
);
3418 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
3419 pa_log("invalid packet.");
3420 connection_unlink(c
);
3424 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
) {
3425 connection
*c
= CONNECTION(userdata
);
3426 output_stream
*stream
;
3430 connection_assert_ref(c
);
3432 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
3433 pa_log("client sent block for invalid stream.");
3438 if (playback_stream_isinstance(stream
)) {
3439 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
3441 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
3442 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
);
3444 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
3447 upload_stream
*u
= UPLOAD_STREAM(stream
);
3450 if (!u
->memchunk
.memblock
) {
3451 if (u
->length
== chunk
->length
) {
3452 u
->memchunk
= *chunk
;
3453 pa_memblock_ref(u
->memchunk
.memblock
);
3456 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
3457 u
->memchunk
.index
= u
->memchunk
.length
= 0;
3461 pa_assert(u
->memchunk
.memblock
);
3464 if (l
> chunk
->length
)
3470 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
3471 src
= pa_memblock_acquire(chunk
->memblock
);
3473 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
3474 (uint8_t*) src
+chunk
->index
, l
);
3476 pa_memblock_release(u
->memchunk
.memblock
);
3477 pa_memblock_release(chunk
->memblock
);
3479 u
->memchunk
.length
+= l
;
3485 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
3486 connection
*c
= CONNECTION(userdata
);
3489 connection_assert_ref(c
);
3491 connection_unlink(c
);
3492 pa_log_info("connection died.");
3495 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
3496 connection
*c
= CONNECTION(userdata
);
3499 connection_assert_ref(c
);
3504 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
3507 if (!(q
= pa_thread_mq_get()))
3508 pa_pstream_send_revoke(p
, block_id
);
3510 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
3513 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
3516 if (!(q
= pa_thread_mq_get()))
3517 pa_pstream_send_release(p
, block_id
);
3519 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
3522 /*** client callbacks ***/
3524 static void client_kill_cb(pa_client
*c
) {
3527 connection_unlink(CONNECTION(c
->userdata
));
3530 /*** socket server callbacks ***/
3532 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
3533 connection
*c
= CONNECTION(userdata
);
3537 connection_assert_ref(c
);
3538 pa_assert(c
->auth_timeout_event
== e
);
3541 connection_unlink(c
);
3544 static void on_connection(PA_GCC_UNUSED pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
3545 pa_protocol_native
*p
= userdata
;
3547 char cname
[256], pname
[128];
3553 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
3554 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
3555 pa_iochannel_free(io
);
3559 c
= pa_msgobject_new(connection
);
3560 c
->parent
.parent
.free
= connection_free
;
3561 c
->parent
.process_msg
= connection_process_msg
;
3563 c
->authorized
= p
->public;
3565 if (!c
->authorized
&& p
->auth_ip_acl
&& pa_ip_acl_check(p
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
3566 pa_log_info("Client authenticated by IP ACL.");
3567 c
->authorized
= TRUE
;
3570 if (!c
->authorized
) {
3572 pa_gettimeofday(&tv
);
3573 tv
.tv_sec
+= AUTH_TIMEOUT
;
3574 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
3576 c
->auth_timeout_event
= NULL
;
3580 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
3581 pa_snprintf(cname
, sizeof(cname
), "Native client (%s)", pname
);
3582 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
3583 c
->client
->kill
= client_kill_cb
;
3584 c
->client
->userdata
= c
;
3585 c
->client
->module
= p
->module
;
3587 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
3589 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
3590 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
3591 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
3592 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
3593 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
3594 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
3596 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
3598 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
3599 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
3601 c
->rrobin_index
= PA_IDXSET_INVALID
;
3602 c
->subscription
= NULL
;
3604 pa_idxset_put(p
->connections
, c
, NULL
);
3607 if (pa_iochannel_creds_supported(io
))
3608 pa_iochannel_creds_enable(io
);
3613 /*** module entry points ***/
3615 static int load_key(pa_protocol_native
*p
, const char*fn
) {
3618 p
->auth_cookie_in_property
= FALSE
;
3620 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
3621 pa_log_info("using already loaded auth cookie.");
3622 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
3623 p
->auth_cookie_in_property
= TRUE
;
3628 fn
= PA_NATIVE_COOKIE_FILE
;
3630 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
3633 pa_log_info("loading cookie from disk.");
3635 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
3636 p
->auth_cookie_in_property
= TRUE
;
3641 static pa_protocol_native
* protocol_new_internal(pa_core
*c
, pa_module
*m
, pa_modargs
*ma
) {
3642 pa_protocol_native
*p
;
3643 pa_bool_t
public = FALSE
;
3649 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
3650 pa_log("auth-anonymous= expects a boolean argument.");
3654 p
= pa_xnew(pa_protocol_native
, 1);
3659 p
->auth_ip_acl
= NULL
;
3664 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &a
) < 0) {
3665 pa_log("auth-group-enabled= expects a boolean argument.");
3668 p
->auth_group
= a
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", c
->is_system_instance
? PA_ACCESS_GROUP
: NULL
)) : NULL
;
3671 pa_log_info("Allowing access to group '%s'.", p
->auth_group
);
3676 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
3678 if (!(p
->auth_ip_acl
= pa_ip_acl_new(acl
))) {
3679 pa_log("Failed to parse IP ACL '%s'", acl
);
3684 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0)
3687 p
->connections
= pa_idxset_new(NULL
, NULL
);
3693 pa_xfree(p
->auth_group
);
3696 pa_ip_acl_free(p
->auth_ip_acl
);
3701 pa_protocol_native
* pa_protocol_native_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
3703 pa_protocol_native
*p
;
3705 if (!(p
= protocol_new_internal(core
, m
, ma
)))
3709 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
3711 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
3713 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3714 l
= pa_strlist_prepend(l
, t
);
3715 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
3721 void pa_protocol_native_free(pa_protocol_native
*p
) {
3725 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
3726 connection_unlink(c
);
3727 pa_idxset_free(p
->connections
, NULL
, NULL
);
3732 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
3734 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3735 l
= pa_strlist_remove(l
, t
);
3738 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
3740 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3743 pa_socket_server_unref(p
->server
);
3746 if (p
->auth_cookie_in_property
)
3747 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
3750 pa_ip_acl_free(p
->auth_ip_acl
);
3753 pa_xfree(p
->auth_group
);
3758 pa_protocol_native
* pa_protocol_native_new_iochannel(
3764 pa_protocol_native
*p
;
3766 if (!(p
= protocol_new_internal(core
, m
, ma
)))
3769 on_connection(NULL
, io
, p
);