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
;
109 /* Only updated after SINK_INPUT_MESSAGE_UPDATE_LATENCY */
110 int64_t read_index
, write_index
;
111 size_t resampled_chunk_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
;
132 pa_protocol_native
*protocol
;
135 pa_pdispatch
*pdispatch
;
136 pa_idxset
*record_streams
, *output_streams
;
137 uint32_t rrobin_index
;
138 pa_subscription
*subscription
;
139 pa_time_event
*auth_timeout_event
;
142 PA_DECLARE_CLASS(record_stream
);
143 #define RECORD_STREAM(o) (record_stream_cast(o))
144 static PA_DEFINE_CHECK_TYPE(record_stream
, pa_msgobject
);
146 PA_DECLARE_CLASS(output_stream
);
147 #define OUTPUT_STREAM(o) (output_stream_cast(o))
148 static PA_DEFINE_CHECK_TYPE(output_stream
, pa_msgobject
);
150 PA_DECLARE_CLASS(playback_stream
);
151 #define PLAYBACK_STREAM(o) (playback_stream_cast(o))
152 static PA_DEFINE_CHECK_TYPE(playback_stream
, output_stream
);
154 PA_DECLARE_CLASS(upload_stream
);
155 #define UPLOAD_STREAM(o) (upload_stream_cast(o))
156 static PA_DEFINE_CHECK_TYPE(upload_stream
, output_stream
);
158 PA_DECLARE_CLASS(connection
);
159 #define CONNECTION(o) (connection_cast(o))
160 static PA_DEFINE_CHECK_TYPE(connection
, pa_msgobject
);
162 struct pa_protocol_native
{
166 pa_socket_server
*server
;
167 pa_idxset
*connections
;
168 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
169 int auth_cookie_in_property
;
173 pa_ip_acl
*auth_ip_acl
;
177 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
178 SINK_INPUT_MESSAGE_DRAIN
, /* disabled prebuf, get playback started. */
179 SINK_INPUT_MESSAGE_FLUSH
,
180 SINK_INPUT_MESSAGE_TRIGGER
,
181 SINK_INPUT_MESSAGE_SEEK
,
182 SINK_INPUT_MESSAGE_PREBUF_FORCE
,
183 SINK_INPUT_MESSAGE_UPDATE_LATENCY
187 PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, /* data requested from sink input from the main loop */
188 PLAYBACK_STREAM_MESSAGE_UNDERFLOW
,
189 PLAYBACK_STREAM_MESSAGE_OVERFLOW
,
190 PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
194 RECORD_STREAM_MESSAGE_POST_DATA
/* data from source output to main loop */
198 CONNECTION_MESSAGE_RELEASE
,
199 CONNECTION_MESSAGE_REVOKE
202 static int sink_input_peek_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
);
203 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
);
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
);
258 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
259 [PA_COMMAND_ERROR
] = NULL
,
260 [PA_COMMAND_TIMEOUT
] = NULL
,
261 [PA_COMMAND_REPLY
] = NULL
,
262 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
263 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
264 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
265 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
266 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
267 [PA_COMMAND_AUTH
] = command_auth
,
268 [PA_COMMAND_REQUEST
] = NULL
,
269 [PA_COMMAND_EXIT
] = command_exit
,
270 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
271 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
272 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
273 [PA_COMMAND_STAT
] = command_stat
,
274 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
275 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
276 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
277 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
278 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
279 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
280 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
281 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
282 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
283 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
284 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
285 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
286 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
287 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
288 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
289 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
290 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
291 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
292 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
293 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
294 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
295 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
296 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
298 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
299 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
300 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
302 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
303 [PA_COMMAND_SET_SINK_INPUT_MUTE
] = command_set_mute
,
304 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
306 [PA_COMMAND_SUSPEND_SINK
] = command_suspend
,
307 [PA_COMMAND_SUSPEND_SOURCE
] = command_suspend
,
309 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
310 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
311 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
312 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
314 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
315 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
317 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
318 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
319 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
320 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
321 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
322 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
323 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
324 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
325 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
326 [PA_COMMAND_GET_AUTOLOAD_INFO
] = command_get_autoload_info
,
327 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = command_get_autoload_info_list
,
328 [PA_COMMAND_ADD_AUTOLOAD
] = command_add_autoload
,
329 [PA_COMMAND_REMOVE_AUTOLOAD
] = command_remove_autoload
,
331 [PA_COMMAND_MOVE_SINK_INPUT
] = command_move_stream
,
332 [PA_COMMAND_MOVE_SOURCE_OUTPUT
] = command_move_stream
,
334 [PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
335 [PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
337 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
338 [PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
341 /* structure management */
343 static void upload_stream_unlink(upload_stream
*s
) {
349 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
350 s
->connection
= NULL
;
351 upload_stream_unref(s
);
354 static void upload_stream_free(pa_object
*o
) {
355 upload_stream
*s
= UPLOAD_STREAM(o
);
358 upload_stream_unlink(s
);
362 if (s
->memchunk
.memblock
)
363 pa_memblock_unref(s
->memchunk
.memblock
);
368 static upload_stream
* upload_stream_new(
370 const pa_sample_spec
*ss
,
371 const pa_channel_map
*map
,
372 const char *name
, size_t length
) {
379 pa_assert(length
> 0);
381 s
= pa_msgobject_new(upload_stream
);
382 s
->parent
.parent
.parent
.free
= upload_stream_free
;
384 s
->sample_spec
= *ss
;
385 s
->channel_map
= *map
;
386 s
->name
= pa_xstrdup(name
);
387 pa_memchunk_reset(&s
->memchunk
);
390 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
395 static void record_stream_unlink(record_stream
*s
) {
401 if (s
->source_output
) {
402 pa_source_output_unlink(s
->source_output
);
403 pa_source_output_unref(s
->source_output
);
404 s
->source_output
= NULL
;
407 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->record_streams
, s
, NULL
) == s
);
408 s
->connection
= NULL
;
409 record_stream_unref(s
);
412 static void record_stream_free(pa_object
*o
) {
413 record_stream
*s
= RECORD_STREAM(o
);
416 record_stream_unlink(s
);
418 pa_memblockq_free(s
->memblockq
);
422 static int record_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
423 record_stream
*s
= RECORD_STREAM(o
);
424 record_stream_assert_ref(s
);
431 case RECORD_STREAM_MESSAGE_POST_DATA
:
433 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
434 /* pa_log_warn("Failed to push data into output queue."); */
438 if (!pa_pstream_is_pending(s
->connection
->pstream
))
439 send_memblock(s
->connection
);
447 static record_stream
* record_stream_new(
454 uint32_t fragment_size
,
455 pa_source_output_flags_t flags
) {
458 pa_source_output
*source_output
;
460 pa_source_output_new_data data
;
465 pa_assert(maxlength
);
466 pa_assert(*maxlength
> 0);
468 pa_source_output_new_data_init(&data
);
469 data
.module
= c
->protocol
->module
;
470 data
.client
= c
->client
;
471 data
.source
= source
;
472 data
.driver
= __FILE__
;
474 pa_source_output_new_data_set_sample_spec(&data
, ss
);
475 pa_source_output_new_data_set_channel_map(&data
, map
);
477 if (!(source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
)))
480 s
= pa_msgobject_new(record_stream
);
481 s
->parent
.parent
.free
= record_stream_free
;
482 s
->parent
.process_msg
= record_stream_process_msg
;
484 s
->source_output
= source_output
;
485 s
->source_output
->push
= source_output_push_cb
;
486 s
->source_output
->kill
= source_output_kill_cb
;
487 s
->source_output
->get_latency
= source_output_get_latency_cb
;
488 s
->source_output
->moved
= source_output_moved_cb
;
489 s
->source_output
->suspend
= source_output_suspend_cb
;
490 s
->source_output
->userdata
= s
;
492 s
->memblockq
= pa_memblockq_new(
496 base
= pa_frame_size(&s
->source_output
->sample_spec
),
501 *maxlength
= pa_memblockq_get_maxlength(s
->memblockq
);
503 s
->fragment_size
= (fragment_size
/base
)*base
;
504 if (s
->fragment_size
<= 0)
505 s
->fragment_size
= base
;
507 if (s
->fragment_size
> *maxlength
)
508 s
->fragment_size
= *maxlength
;
510 *ss
= s
->source_output
->sample_spec
;
511 *map
= s
->source_output
->channel_map
;
513 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
515 pa_source_output_put(s
->source_output
);
519 static void playback_stream_unlink(playback_stream
*s
) {
526 pa_sink_input_unlink(s
->sink_input
);
527 pa_sink_input_unref(s
->sink_input
);
528 s
->sink_input
= NULL
;
531 if (s
->drain_request
)
532 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
534 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
535 s
->connection
= NULL
;
536 playback_stream_unref(s
);
539 static void playback_stream_free(pa_object
* o
) {
540 playback_stream
*s
= PLAYBACK_STREAM(o
);
543 playback_stream_unlink(s
);
545 pa_memblockq_free(s
->memblockq
);
549 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
550 playback_stream
*s
= PLAYBACK_STREAM(o
);
551 playback_stream_assert_ref(s
);
557 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
564 if ((k
= pa_atomic_load(&s
->missing
)) <= 0)
572 if (pa_atomic_sub(&s
->missing
, k
) <= k
)
579 t
= pa_tagstruct_new(NULL
, 0);
580 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
581 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
582 pa_tagstruct_putu32(t
, s
->index
);
583 pa_tagstruct_putu32(t
, l
);
584 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
586 /* pa_log("Requesting %u bytes", l); */
590 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
593 /* Report that we're empty */
594 t
= pa_tagstruct_new(NULL
, 0);
595 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
596 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
597 pa_tagstruct_putu32(t
, s
->index
);
598 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
602 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
605 /* Notify the user we're overflowed*/
606 t
= pa_tagstruct_new(NULL
, 0);
607 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
608 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
609 pa_tagstruct_putu32(t
, s
->index
);
610 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
614 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
615 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
623 static playback_stream
* playback_stream_new(
636 pa_sink_input_flags_t flags
) {
638 playback_stream
*s
, *ssync
;
639 pa_sink_input
*sink_input
;
640 pa_memblock
*silence
;
643 pa_sink_input_new_data data
;
648 pa_assert(maxlength
);
650 /* Find syncid group */
651 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
653 if (!playback_stream_isinstance(ssync
))
656 if (ssync
->syncid
== syncid
)
660 /* Synced streams must connect to the same sink */
664 sink
= ssync
->sink_input
->sink
;
665 else if (sink
!= ssync
->sink_input
->sink
)
669 pa_sink_input_new_data_init(&data
);
671 data
.driver
= __FILE__
;
673 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
674 pa_sink_input_new_data_set_channel_map(&data
, map
);
675 pa_sink_input_new_data_set_volume(&data
, volume
);
676 data
.module
= c
->protocol
->module
;
677 data
.client
= c
->client
;
678 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
680 if (!(sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
)))
683 s
= pa_msgobject_new(playback_stream
);
684 s
->parent
.parent
.parent
.free
= playback_stream_free
;
685 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
688 s
->sink_input
= sink_input
;
691 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
692 s
->sink_input
->peek
= sink_input_peek_cb
;
693 s
->sink_input
->drop
= sink_input_drop_cb
;
694 s
->sink_input
->kill
= sink_input_kill_cb
;
695 s
->sink_input
->moved
= sink_input_moved_cb
;
696 s
->sink_input
->suspend
= sink_input_suspend_cb
;
697 s
->sink_input
->userdata
= s
;
699 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
701 silence
= pa_silence_memblock_new(c
->protocol
->core
->mempool
, &s
->sink_input
->sample_spec
, 0);
703 s
->memblockq
= pa_memblockq_new(
707 pa_frame_size(&s
->sink_input
->sample_spec
),
712 pa_memblock_unref(silence
);
714 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
715 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
716 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
717 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
718 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
720 *ss
= s
->sink_input
->sample_spec
;
721 *map
= s
->sink_input
->channel_map
;
723 s
->minreq
= pa_memblockq_get_minreq(s
->memblockq
);
724 pa_atomic_store(&s
->missing
, 0);
725 s
->drain_request
= 0;
727 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
729 pa_sink_input_put(s
->sink_input
);
734 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
735 connection
*c
= CONNECTION(o
);
736 connection_assert_ref(c
);
743 case CONNECTION_MESSAGE_REVOKE
:
744 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
747 case CONNECTION_MESSAGE_RELEASE
:
748 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
755 static void connection_unlink(connection
*c
) {
764 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
765 record_stream_unlink(r
);
767 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
768 if (playback_stream_isinstance(o
))
769 playback_stream_unlink(PLAYBACK_STREAM(o
));
771 upload_stream_unlink(UPLOAD_STREAM(o
));
774 pa_subscription_free(c
->subscription
);
777 pa_pstream_unlink(c
->pstream
);
779 if (c
->auth_timeout_event
) {
780 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
781 c
->auth_timeout_event
= NULL
;
784 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
789 static void connection_free(pa_object
*o
) {
790 connection
*c
= CONNECTION(o
);
794 connection_unlink(c
);
796 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
797 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
799 pa_pdispatch_unref(c
->pdispatch
);
800 pa_pstream_unref(c
->pstream
);
801 pa_client_free(c
->client
);
806 /* Called from thread context */
807 static void request_bytes(playback_stream
*s
) {
808 size_t m
, previous_missing
;
810 playback_stream_assert_ref(s
);
812 m
= pa_memblockq_pop_missing(s
->memblockq
);
817 /* pa_log("request_bytes(%u)", m); */
819 previous_missing
= pa_atomic_add(&s
->missing
, m
);
820 if (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
) {
821 pa_assert(pa_thread_mq_get());
822 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
826 static void send_memblock(connection
*c
) {
830 start
= PA_IDXSET_INVALID
;
834 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
837 if (start
== PA_IDXSET_INVALID
)
838 start
= c
->rrobin_index
;
839 else if (start
== c
->rrobin_index
)
842 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
843 pa_memchunk schunk
= chunk
;
845 if (schunk
.length
> r
->fragment_size
)
846 schunk
.length
= r
->fragment_size
;
848 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
850 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
851 pa_memblock_unref(schunk
.memblock
);
858 static void send_playback_stream_killed(playback_stream
*p
) {
860 playback_stream_assert_ref(p
);
862 t
= pa_tagstruct_new(NULL
, 0);
863 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
864 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
865 pa_tagstruct_putu32(t
, p
->index
);
866 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
869 static void send_record_stream_killed(record_stream
*r
) {
871 record_stream_assert_ref(r
);
873 t
= pa_tagstruct_new(NULL
, 0);
874 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
875 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
876 pa_tagstruct_putu32(t
, r
->index
);
877 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
880 /*** sink input callbacks ***/
882 /* Called from thread context */
883 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
884 pa_sink_input
*i
= PA_SINK_INPUT(o
);
887 pa_sink_input_assert_ref(i
);
888 s
= PLAYBACK_STREAM(i
->userdata
);
889 playback_stream_assert_ref(s
);
893 case SINK_INPUT_MESSAGE_SEEK
:
894 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
898 case SINK_INPUT_MESSAGE_POST_DATA
: {
901 /* pa_log("sink input post: %u", chunk->length); */
903 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
905 pa_log_warn("Failed to push data into queue");
906 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
907 pa_memblockq_seek(s
->memblockq
, chunk
->length
, PA_SEEK_RELATIVE
);
916 case SINK_INPUT_MESSAGE_DRAIN
: {
918 pa_memblockq_prebuf_disable(s
->memblockq
);
920 if (!pa_memblockq_is_readable(s
->memblockq
))
921 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
923 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
924 s
->drain_request
= 1;
931 case SINK_INPUT_MESSAGE_FLUSH
:
932 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
933 case SINK_INPUT_MESSAGE_TRIGGER
: {
935 pa_sink_input
*isync
;
936 void (*func
)(pa_memblockq
*bq
);
939 case SINK_INPUT_MESSAGE_FLUSH
:
940 func
= pa_memblockq_flush
;
943 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
944 func
= pa_memblockq_prebuf_force
;
947 case SINK_INPUT_MESSAGE_TRIGGER
:
948 func
= pa_memblockq_prebuf_disable
;
952 pa_assert_not_reached();
959 /* Do the same for all other members in the sync group */
960 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
961 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
962 func(ssync
->memblockq
);
964 request_bytes(ssync
);
967 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
968 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
969 func(ssync
->memblockq
);
971 request_bytes(ssync
);
977 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
979 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
980 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
981 s
->resampled_chunk_length
= s
->sink_input
->thread_info
.resampled_chunk
.memblock
? s
->sink_input
->thread_info
.resampled_chunk
.length
: 0;
984 case PA_SINK_INPUT_MESSAGE_SET_STATE
:
986 pa_memblockq_prebuf_force(s
->memblockq
);
990 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
991 pa_usec_t
*r
= userdata
;
993 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
995 /* Fall through, the default handler will add in the extra
996 * latency added by the resampler */
1001 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1004 /* Called from thread context */
1005 static int sink_input_peek_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
1008 pa_sink_input_assert_ref(i
);
1009 s
= PLAYBACK_STREAM(i
->userdata
);
1010 playback_stream_assert_ref(s
);
1013 if (pa_memblockq_get_length(s
->memblockq
) <= 0 && !s
->underrun
) {
1014 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1018 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0) {
1019 /* pa_log("peek: failure"); */
1023 /* pa_log("peek: %u", chunk->length); */
1030 /* Called from thread context */
1031 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
1034 pa_sink_input_assert_ref(i
);
1035 s
= PLAYBACK_STREAM(i
->userdata
);
1036 playback_stream_assert_ref(s
);
1037 pa_assert(length
> 0);
1039 pa_memblockq_drop(s
->memblockq
, length
);
1041 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
1042 s
->drain_request
= 0;
1043 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
);
1048 /* pa_log("after_drop: %u %u", pa_memblockq_get_length(s->memblockq), pa_memblockq_is_readable(s->memblockq)); */
1051 /* Called from main context */
1052 static void sink_input_kill_cb(pa_sink_input
*i
) {
1055 pa_sink_input_assert_ref(i
);
1056 s
= PLAYBACK_STREAM(i
->userdata
);
1057 playback_stream_assert_ref(s
);
1059 send_playback_stream_killed(s
);
1060 playback_stream_unlink(s
);
1063 /* Called from main context */
1064 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1068 pa_sink_input_assert_ref(i
);
1069 s
= PLAYBACK_STREAM(i
->userdata
);
1070 playback_stream_assert_ref(s
);
1072 if (s
->connection
->version
< 12)
1075 t
= pa_tagstruct_new(NULL
, 0);
1076 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1077 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1078 pa_tagstruct_putu32(t
, s
->index
);
1079 pa_tagstruct_put_boolean(t
, suspend
);
1080 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1083 /* Called from main context */
1084 static void sink_input_moved_cb(pa_sink_input
*i
) {
1088 pa_sink_input_assert_ref(i
);
1089 s
= PLAYBACK_STREAM(i
->userdata
);
1090 playback_stream_assert_ref(s
);
1092 if (s
->connection
->version
< 12)
1095 t
= pa_tagstruct_new(NULL
, 0);
1096 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1097 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1098 pa_tagstruct_putu32(t
, s
->index
);
1099 pa_tagstruct_putu32(t
, i
->sink
->index
);
1100 pa_tagstruct_puts(t
, i
->sink
->name
);
1101 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1102 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1105 /*** source_output callbacks ***/
1107 /* Called from thread context */
1108 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1111 pa_source_output_assert_ref(o
);
1112 s
= RECORD_STREAM(o
->userdata
);
1113 record_stream_assert_ref(s
);
1116 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1119 static void source_output_kill_cb(pa_source_output
*o
) {
1122 pa_source_output_assert_ref(o
);
1123 s
= RECORD_STREAM(o
->userdata
);
1124 record_stream_assert_ref(s
);
1126 send_record_stream_killed(s
);
1127 record_stream_unlink(s
);
1130 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1133 pa_source_output_assert_ref(o
);
1134 s
= RECORD_STREAM(o
->userdata
);
1135 record_stream_assert_ref(s
);
1137 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1139 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1142 /* Called from main context */
1143 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1147 pa_source_output_assert_ref(o
);
1148 s
= RECORD_STREAM(o
->userdata
);
1149 record_stream_assert_ref(s
);
1151 if (s
->connection
->version
< 12)
1154 t
= pa_tagstruct_new(NULL
, 0);
1155 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1156 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1157 pa_tagstruct_putu32(t
, s
->index
);
1158 pa_tagstruct_put_boolean(t
, suspend
);
1159 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1162 /* Called from main context */
1163 static void source_output_moved_cb(pa_source_output
*o
) {
1167 pa_source_output_assert_ref(o
);
1168 s
= RECORD_STREAM(o
->userdata
);
1169 record_stream_assert_ref(s
);
1171 if (s
->connection
->version
< 12)
1174 t
= pa_tagstruct_new(NULL
, 0);
1175 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1176 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1177 pa_tagstruct_putu32(t
, s
->index
);
1178 pa_tagstruct_putu32(t
, o
->source
->index
);
1179 pa_tagstruct_puts(t
, o
->source
->name
);
1180 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1181 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1184 /*** pdispatch callbacks ***/
1186 static void protocol_error(connection
*c
) {
1187 pa_log("protocol error, kicking client");
1188 connection_unlink(c
);
1191 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1192 if (!(expression)) { \
1193 pa_pstream_send_error((pstream), (tag), (error)); \
1198 static pa_tagstruct
*reply_new(uint32_t tag
) {
1199 pa_tagstruct
*reply
;
1201 reply
= pa_tagstruct_new(NULL
, 0);
1202 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1203 pa_tagstruct_putu32(reply
, tag
);
1207 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
) {
1208 connection
*c
= CONNECTION(userdata
);
1210 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1211 const char *name
, *sink_name
;
1214 pa_tagstruct
*reply
;
1215 pa_sink
*sink
= NULL
;
1218 int no_remap
= 0, no_remix
= 0, fix_format
= 0, fix_rate
= 0, fix_channels
= 0, no_move
= 0, variable_rate
= 0;
1219 pa_sink_input_flags_t flags
= 0;
1221 connection_assert_ref(c
);
1224 if (pa_tagstruct_get(
1226 PA_TAG_STRING
, &name
,
1227 PA_TAG_SAMPLE_SPEC
, &ss
,
1228 PA_TAG_CHANNEL_MAP
, &map
,
1229 PA_TAG_U32
, &sink_index
,
1230 PA_TAG_STRING
, &sink_name
,
1231 PA_TAG_U32
, &maxlength
,
1232 PA_TAG_BOOLEAN
, &corked
,
1233 PA_TAG_U32
, &tlength
,
1234 PA_TAG_U32
, &prebuf
,
1235 PA_TAG_U32
, &minreq
,
1236 PA_TAG_U32
, &syncid
,
1237 PA_TAG_CVOLUME
, &volume
,
1238 PA_TAG_INVALID
) < 0 || !name
) {
1243 if (c
->version
>= 12) {
1244 /* Since 0.9.8 the user can ask for a couple of additional flags */
1246 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1247 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1248 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1249 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1250 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1251 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1252 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1258 if (!pa_tagstruct_eof(t
)) {
1263 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1264 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1265 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1266 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1267 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1268 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1269 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1270 CHECK_VALIDITY(c
->pstream
, maxlength
> 0, tag
, PA_ERR_INVALID
);
1271 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
1273 if (sink_index
!= PA_INVALID_INDEX
) {
1274 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1275 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
1276 } else if (sink_name
) {
1277 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1278 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
1282 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1283 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1284 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1285 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1286 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1287 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1288 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1289 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0);
1291 s
= playback_stream_new(c
, sink
, &ss
, &map
, name
, &maxlength
, &tlength
, &prebuf
, &minreq
, &volume
, syncid
, &missing
, flags
);
1292 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1294 reply
= reply_new(tag
);
1295 pa_tagstruct_putu32(reply
, s
->index
);
1296 pa_assert(s
->sink_input
);
1297 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1298 pa_tagstruct_putu32(reply
, missing
);
1300 /* pa_log("initial request is %u", missing); */
1302 if (c
->version
>= 9) {
1303 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1305 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1306 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1307 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1308 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1311 if (c
->version
>= 12) {
1312 /* Since 0.9.8 we support sending the chosen sample
1313 * spec/channel map/device/suspend status back to the
1316 pa_tagstruct_put_sample_spec(reply
, &ss
);
1317 pa_tagstruct_put_channel_map(reply
, &map
);
1319 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1320 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1322 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1325 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1328 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1329 connection
*c
= CONNECTION(userdata
);
1332 connection_assert_ref(c
);
1335 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1336 !pa_tagstruct_eof(t
)) {
1341 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1345 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1347 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1348 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1352 playback_stream_unlink(s
);
1356 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1358 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1359 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1363 record_stream_unlink(s
);
1367 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1370 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1371 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1375 upload_stream_unlink(s
);
1380 pa_assert_not_reached();
1383 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1386 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
) {
1387 connection
*c
= CONNECTION(userdata
);
1389 uint32_t maxlength
, fragment_size
;
1390 uint32_t source_index
;
1391 const char *name
, *source_name
;
1394 pa_tagstruct
*reply
;
1395 pa_source
*source
= NULL
;
1397 int no_remap
= 0, no_remix
= 0, fix_format
= 0, fix_rate
= 0, fix_channels
= 0, no_move
= 0, variable_rate
= 0;
1398 pa_source_output_flags_t flags
= 0;
1400 connection_assert_ref(c
);
1403 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1404 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1405 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1406 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1407 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1408 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1409 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1410 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1415 if (c
->version
>= 12) {
1416 /* Since 0.9.8 the user can ask for a couple of additional flags */
1418 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1419 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1420 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1421 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1422 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1423 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1424 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1430 if (!pa_tagstruct_eof(t
)) {
1436 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
1437 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
1438 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
1439 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
1440 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
1441 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
1442 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
1443 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0);
1445 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1446 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1447 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1448 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1449 CHECK_VALIDITY(c
->pstream
, source_index
!= PA_INVALID_INDEX
|| !source_name
|| (*source_name
&& pa_utf8_valid(source_name
)), tag
, PA_ERR_INVALID
);
1450 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1451 CHECK_VALIDITY(c
->pstream
, maxlength
> 0, tag
, PA_ERR_INVALID
);
1452 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
1454 if (source_index
!= PA_INVALID_INDEX
) {
1455 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
1456 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
1457 } else if (source_name
) {
1458 source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1);
1459 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
1462 s
= record_stream_new(c
, source
, &ss
, &map
, name
, &maxlength
, fragment_size
, flags
);
1463 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1465 reply
= reply_new(tag
);
1466 pa_tagstruct_putu32(reply
, s
->index
);
1467 pa_assert(s
->source_output
);
1468 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
1470 if (c
->version
>= 9) {
1471 /* Since 0.9 we support sending the buffer metrics back to the client */
1473 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1474 pa_tagstruct_putu32(reply
, (uint32_t) s
->fragment_size
);
1477 if (c
->version
>= 12) {
1478 /* Since 0.9.8 we support sending the chosen sample
1479 * spec/channel map/device/suspend status back to the
1482 pa_tagstruct_put_sample_spec(reply
, &ss
);
1483 pa_tagstruct_put_channel_map(reply
, &map
);
1485 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
1486 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
1488 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
1491 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1494 static void command_exit(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1495 connection
*c
= CONNECTION(userdata
);
1497 connection_assert_ref(c
);
1500 if (!pa_tagstruct_eof(t
)) {
1505 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1507 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
1508 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
1511 static void command_auth(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1512 connection
*c
= CONNECTION(userdata
);
1514 pa_tagstruct
*reply
;
1516 connection_assert_ref(c
);
1519 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
1520 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
1521 !pa_tagstruct_eof(t
)) {
1526 /* Minimum supported version */
1527 if (c
->version
< 8) {
1528 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
1532 if (!c
->authorized
) {
1536 const pa_creds
*creds
;
1538 if ((creds
= pa_pdispatch_creds(pd
))) {
1539 if (creds
->uid
== getuid())
1541 else if (c
->protocol
->auth_group
) {
1545 if ((gid
= pa_get_gid_of_group(c
->protocol
->auth_group
)) == (gid_t
) -1)
1546 pa_log_warn("failed to get GID of group '%s'", c
->protocol
->auth_group
);
1547 else if (gid
== creds
->gid
)
1551 if ((r
= pa_uid_in_group(creds
->uid
, c
->protocol
->auth_group
)) < 0)
1552 pa_log_warn("failed to check group membership.");
1558 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
1559 (unsigned long) creds
->uid
,
1560 (unsigned long) creds
->gid
,
1563 if (c
->version
>= 10 &&
1564 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
1565 creds
->uid
== getuid()) {
1567 pa_pstream_use_shm(c
->pstream
, 1);
1568 pa_log_info("Enabled SHM for new connection");
1574 if (!success
&& memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
1578 pa_log_warn("Denied access to client with invalid authorization data.");
1579 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1584 if (c
->auth_timeout_event
) {
1585 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1586 c
->auth_timeout_event
= NULL
;
1590 reply
= reply_new(tag
);
1591 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
);
1595 /* SHM support is only enabled after both sides made sure they are the same user. */
1599 ucred
.uid
= getuid();
1600 ucred
.gid
= getgid();
1602 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
1605 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1609 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
) {
1610 connection
*c
= CONNECTION(userdata
);
1613 connection_assert_ref(c
);
1616 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1617 !pa_tagstruct_eof(t
)) {
1622 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1624 pa_client_set_name(c
->client
, name
);
1625 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1628 static void command_lookup(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1629 connection
*c
= CONNECTION(userdata
);
1631 uint32_t idx
= PA_IDXSET_INVALID
;
1633 connection_assert_ref(c
);
1636 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1637 !pa_tagstruct_eof(t
)) {
1642 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1643 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1645 if (command
== PA_COMMAND_LOOKUP_SINK
) {
1647 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
1651 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
1652 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
1653 idx
= source
->index
;
1656 if (idx
== PA_IDXSET_INVALID
)
1657 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1659 pa_tagstruct
*reply
;
1660 reply
= reply_new(tag
);
1661 pa_tagstruct_putu32(reply
, idx
);
1662 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1666 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
) {
1667 connection
*c
= CONNECTION(userdata
);
1671 connection_assert_ref(c
);
1674 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1675 !pa_tagstruct_eof(t
)) {
1680 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1681 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1682 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1683 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1685 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
);
1688 static void command_stat(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1689 connection
*c
= CONNECTION(userdata
);
1690 pa_tagstruct
*reply
;
1691 const pa_mempool_stat
*stat
;
1693 connection_assert_ref(c
);
1696 if (!pa_tagstruct_eof(t
)) {
1701 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1703 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
1705 reply
= reply_new(tag
);
1706 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
1707 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
1708 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
1709 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
1710 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
1711 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1714 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
) {
1715 connection
*c
= CONNECTION(userdata
);
1716 pa_tagstruct
*reply
;
1718 struct timeval tv
, now
;
1722 connection_assert_ref(c
);
1725 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1726 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1727 !pa_tagstruct_eof(t
)) {
1732 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1733 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1734 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1735 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1736 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
)
1738 reply
= reply_new(tag
);
1740 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
1741 latency
+= pa_bytes_to_usec(s
->resampled_chunk_length
, &s
->sink_input
->sample_spec
);
1743 pa_tagstruct_put_usec(reply
, latency
);
1745 pa_tagstruct_put_usec(reply
, 0);
1746 pa_tagstruct_put_boolean(reply
, pa_sink_input_get_state(s
->sink_input
) == PA_SINK_INPUT_RUNNING
);
1747 pa_tagstruct_put_timeval(reply
, &tv
);
1748 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
1749 pa_tagstruct_puts64(reply
, s
->write_index
);
1750 pa_tagstruct_puts64(reply
, s
->read_index
);
1751 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1754 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
) {
1755 connection
*c
= CONNECTION(userdata
);
1756 pa_tagstruct
*reply
;
1758 struct timeval tv
, now
;
1761 connection_assert_ref(c
);
1764 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1765 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1766 !pa_tagstruct_eof(t
)) {
1771 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1772 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
1773 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1775 reply
= reply_new(tag
);
1776 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
1777 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
1778 pa_tagstruct_put_boolean(reply
, 0);
1779 pa_tagstruct_put_timeval(reply
, &tv
);
1780 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
1781 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
1782 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
1783 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1786 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
) {
1787 connection
*c
= CONNECTION(userdata
);
1793 pa_tagstruct
*reply
;
1795 connection_assert_ref(c
);
1798 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1799 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1800 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1801 pa_tagstruct_getu32(t
, &length
) < 0 ||
1802 !pa_tagstruct_eof(t
)) {
1807 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1808 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1809 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1810 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1811 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
1812 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
1813 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1815 s
= upload_stream_new(c
, &ss
, &map
, name
, length
);
1816 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1818 reply
= reply_new(tag
);
1819 pa_tagstruct_putu32(reply
, s
->index
);
1820 pa_tagstruct_putu32(reply
, length
);
1821 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1824 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
) {
1825 connection
*c
= CONNECTION(userdata
);
1830 connection_assert_ref(c
);
1833 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1834 !pa_tagstruct_eof(t
)) {
1839 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1841 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
1842 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1843 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1845 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, &idx
) < 0)
1846 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
1848 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1850 upload_stream_unlink(s
);
1853 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
) {
1854 connection
*c
= CONNECTION(userdata
);
1855 uint32_t sink_index
;
1858 const char *name
, *sink_name
;
1860 connection_assert_ref(c
);
1863 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1864 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1865 pa_tagstruct_getu32(t
, &volume
) < 0 ||
1866 pa_tagstruct_gets(t
, &name
) < 0 ||
1867 !pa_tagstruct_eof(t
)) {
1872 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1873 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1874 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1876 if (sink_index
!= PA_INVALID_INDEX
)
1877 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1879 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1881 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
1883 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
1884 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1888 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1891 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
) {
1892 connection
*c
= CONNECTION(userdata
);
1895 connection_assert_ref(c
);
1898 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1899 !pa_tagstruct_eof(t
)) {
1904 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1905 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1907 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
1908 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1912 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1915 static void fixup_sample_spec(connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
1918 pa_assert(original
);
1922 if (c
->version
< 12) {
1923 /* Before protocol version 12 we didn't support S32 samples,
1924 * so we need to lie about this to the client */
1926 if (fixed
->format
== PA_SAMPLE_S32LE
)
1927 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
1928 if (fixed
->format
== PA_SAMPLE_S32BE
)
1929 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
1933 static void sink_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
1934 pa_sample_spec fixed_ss
;
1937 pa_sink_assert_ref(sink
);
1939 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
1943 PA_TAG_U32
, sink
->index
,
1944 PA_TAG_STRING
, sink
->name
,
1945 PA_TAG_STRING
, sink
->description
,
1946 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
1947 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
1948 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
1949 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
),
1950 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
),
1951 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
1952 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
1953 PA_TAG_USEC
, pa_sink_get_latency(sink
),
1954 PA_TAG_STRING
, sink
->driver
,
1955 PA_TAG_U32
, sink
->flags
,
1959 static void source_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
1960 pa_sample_spec fixed_ss
;
1963 pa_source_assert_ref(source
);
1965 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
1969 PA_TAG_U32
, source
->index
,
1970 PA_TAG_STRING
, source
->name
,
1971 PA_TAG_STRING
, source
->description
,
1972 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
1973 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
1974 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
1975 PA_TAG_CVOLUME
, pa_source_get_volume(source
),
1976 PA_TAG_BOOLEAN
, pa_source_get_mute(source
),
1977 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
1978 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
1979 PA_TAG_USEC
, pa_source_get_latency(source
),
1980 PA_TAG_STRING
, source
->driver
,
1981 PA_TAG_U32
, source
->flags
,
1985 static void client_fill_tagstruct(pa_tagstruct
*t
, pa_client
*client
) {
1989 pa_tagstruct_putu32(t
, client
->index
);
1990 pa_tagstruct_puts(t
, client
->name
);
1991 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: PA_INVALID_INDEX
);
1992 pa_tagstruct_puts(t
, client
->driver
);
1995 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
1999 pa_tagstruct_putu32(t
, module
->index
);
2000 pa_tagstruct_puts(t
, module
->name
);
2001 pa_tagstruct_puts(t
, module
->argument
);
2002 pa_tagstruct_putu32(t
, module
->n_used
);
2003 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
2006 static void sink_input_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2007 pa_sample_spec fixed_ss
;
2010 pa_sink_input_assert_ref(s
);
2012 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2014 pa_tagstruct_putu32(t
, s
->index
);
2015 pa_tagstruct_puts(t
, s
->name
);
2016 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2017 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2018 pa_tagstruct_putu32(t
, s
->sink
->index
);
2019 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2020 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2021 pa_tagstruct_put_cvolume(t
, &s
->volume
);
2022 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
2023 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
2024 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2025 pa_tagstruct_puts(t
, s
->driver
);
2026 if (c
->version
>= 11)
2027 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2030 static void source_output_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2031 pa_sample_spec fixed_ss
;
2034 pa_source_output_assert_ref(s
);
2036 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2038 pa_tagstruct_putu32(t
, s
->index
);
2039 pa_tagstruct_puts(t
, s
->name
);
2040 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2041 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2042 pa_tagstruct_putu32(t
, s
->source
->index
);
2043 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2044 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2045 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
2046 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
2047 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2048 pa_tagstruct_puts(t
, s
->driver
);
2051 static void scache_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2052 pa_sample_spec fixed_ss
;
2057 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2059 pa_tagstruct_putu32(t
, e
->index
);
2060 pa_tagstruct_puts(t
, e
->name
);
2061 pa_tagstruct_put_cvolume(t
, &e
->volume
);
2062 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
2063 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2064 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2065 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
2066 pa_tagstruct_put_boolean(t
, e
->lazy
);
2067 pa_tagstruct_puts(t
, e
->filename
);
2070 static void command_get_info(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2071 connection
*c
= CONNECTION(userdata
);
2073 pa_sink
*sink
= NULL
;
2074 pa_source
*source
= NULL
;
2075 pa_client
*client
= NULL
;
2076 pa_module
*module
= NULL
;
2077 pa_sink_input
*si
= NULL
;
2078 pa_source_output
*so
= NULL
;
2079 pa_scache_entry
*sce
= NULL
;
2081 pa_tagstruct
*reply
;
2083 connection_assert_ref(c
);
2086 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2087 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2088 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2089 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2090 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2091 pa_tagstruct_gets(t
, &name
) < 0) ||
2092 !pa_tagstruct_eof(t
)) {
2097 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2098 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2100 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2101 if (idx
!= PA_INVALID_INDEX
)
2102 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2104 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2105 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2106 if (idx
!= PA_INVALID_INDEX
)
2107 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2109 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2110 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2111 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2112 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2113 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2114 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2115 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2116 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2117 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2119 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2120 if (idx
!= PA_INVALID_INDEX
)
2121 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2123 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
2126 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
2127 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2131 reply
= reply_new(tag
);
2133 sink_fill_tagstruct(c
, reply
, sink
);
2135 source_fill_tagstruct(c
, reply
, source
);
2137 client_fill_tagstruct(reply
, client
);
2139 module_fill_tagstruct(reply
, module
);
2141 sink_input_fill_tagstruct(c
, reply
, si
);
2143 source_output_fill_tagstruct(c
, reply
, so
);
2145 scache_fill_tagstruct(c
, reply
, sce
);
2146 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2149 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2150 connection
*c
= CONNECTION(userdata
);
2154 pa_tagstruct
*reply
;
2156 connection_assert_ref(c
);
2159 if (!pa_tagstruct_eof(t
)) {
2164 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2166 reply
= reply_new(tag
);
2168 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2169 i
= c
->protocol
->core
->sinks
;
2170 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2171 i
= c
->protocol
->core
->sources
;
2172 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2173 i
= c
->protocol
->core
->clients
;
2174 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2175 i
= c
->protocol
->core
->modules
;
2176 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2177 i
= c
->protocol
->core
->sink_inputs
;
2178 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2179 i
= c
->protocol
->core
->source_outputs
;
2181 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2182 i
= c
->protocol
->core
->scache
;
2186 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2187 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2188 sink_fill_tagstruct(c
, reply
, p
);
2189 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2190 source_fill_tagstruct(c
, reply
, p
);
2191 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2192 client_fill_tagstruct(reply
, p
);
2193 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2194 module_fill_tagstruct(reply
, p
);
2195 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2196 sink_input_fill_tagstruct(c
, reply
, p
);
2197 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2198 source_output_fill_tagstruct(c
, reply
, p
);
2200 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2201 scache_fill_tagstruct(c
, reply
, p
);
2206 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2209 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
) {
2210 connection
*c
= CONNECTION(userdata
);
2211 pa_tagstruct
*reply
;
2214 pa_sample_spec fixed_ss
;
2216 connection_assert_ref(c
);
2219 if (!pa_tagstruct_eof(t
)) {
2224 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2226 reply
= reply_new(tag
);
2227 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
2228 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
2229 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
2230 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
2232 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
2233 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
2235 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
2236 pa_tagstruct_puts(reply
, n
);
2237 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
2238 pa_tagstruct_puts(reply
, n
);
2240 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
2242 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2245 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
2247 connection
*c
= CONNECTION(userdata
);
2249 connection_assert_ref(c
);
2251 t
= pa_tagstruct_new(NULL
, 0);
2252 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
2253 pa_tagstruct_putu32(t
, (uint32_t) -1);
2254 pa_tagstruct_putu32(t
, e
);
2255 pa_tagstruct_putu32(t
, idx
);
2256 pa_pstream_send_tagstruct(c
->pstream
, t
);
2259 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2260 connection
*c
= CONNECTION(userdata
);
2261 pa_subscription_mask_t m
;
2263 connection_assert_ref(c
);
2266 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
2267 !pa_tagstruct_eof(t
)) {
2272 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2273 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
2275 if (c
->subscription
)
2276 pa_subscription_free(c
->subscription
);
2279 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
2280 pa_assert(c
->subscription
);
2282 c
->subscription
= NULL
;
2284 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2287 static void command_set_volume(
2288 PA_GCC_UNUSED pa_pdispatch
*pd
,
2294 connection
*c
= CONNECTION(userdata
);
2297 pa_sink
*sink
= NULL
;
2298 pa_source
*source
= NULL
;
2299 pa_sink_input
*si
= NULL
;
2300 const char *name
= NULL
;
2302 connection_assert_ref(c
);
2305 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2306 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2307 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2308 pa_tagstruct_get_cvolume(t
, &volume
) ||
2309 !pa_tagstruct_eof(t
)) {
2314 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2315 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2316 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
2320 case PA_COMMAND_SET_SINK_VOLUME
:
2321 if (idx
!= PA_INVALID_INDEX
)
2322 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2324 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2327 case PA_COMMAND_SET_SOURCE_VOLUME
:
2328 if (idx
!= PA_INVALID_INDEX
)
2329 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2331 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2334 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
2335 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2339 pa_assert_not_reached();
2342 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2345 pa_sink_set_volume(sink
, &volume
);
2347 pa_source_set_volume(source
, &volume
);
2349 pa_sink_input_set_volume(si
, &volume
);
2351 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2354 static void command_set_mute(
2355 PA_GCC_UNUSED pa_pdispatch
*pd
,
2361 connection
*c
= CONNECTION(userdata
);
2364 pa_sink
*sink
= NULL
;
2365 pa_source
*source
= NULL
;
2366 pa_sink_input
*si
= NULL
;
2367 const char *name
= NULL
;
2369 connection_assert_ref(c
);
2372 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2373 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2374 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2375 pa_tagstruct_get_boolean(t
, &mute
) ||
2376 !pa_tagstruct_eof(t
)) {
2381 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2382 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2386 case PA_COMMAND_SET_SINK_MUTE
:
2388 if (idx
!= PA_INVALID_INDEX
)
2389 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2391 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2395 case PA_COMMAND_SET_SOURCE_MUTE
:
2396 if (idx
!= PA_INVALID_INDEX
)
2397 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2399 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2403 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
2404 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2408 pa_assert_not_reached();
2411 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2414 pa_sink_set_mute(sink
, mute
);
2416 pa_source_set_mute(source
, mute
);
2418 pa_sink_input_set_mute(si
, mute
);
2420 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2423 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
) {
2424 connection
*c
= CONNECTION(userdata
);
2429 connection_assert_ref(c
);
2432 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2433 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
2434 !pa_tagstruct_eof(t
)) {
2439 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2440 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2441 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2442 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2443 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2445 pa_sink_input_cork(s
->sink_input
, b
);
2446 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2449 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
) {
2450 connection
*c
= CONNECTION(userdata
);
2454 connection_assert_ref(c
);
2457 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2458 !pa_tagstruct_eof(t
)) {
2463 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2464 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2465 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2466 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2467 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2470 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
2471 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
2474 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
2475 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
2478 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
2479 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
2483 pa_assert_not_reached();
2486 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2489 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
) {
2490 connection
*c
= CONNECTION(userdata
);
2495 connection_assert_ref(c
);
2498 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2499 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
2500 !pa_tagstruct_eof(t
)) {
2505 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2506 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2507 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2509 pa_source_output_cork(s
->source_output
, b
);
2510 pa_memblockq_prebuf_force(s
->memblockq
);
2511 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2514 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
) {
2515 connection
*c
= CONNECTION(userdata
);
2519 connection_assert_ref(c
);
2522 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2523 !pa_tagstruct_eof(t
)) {
2528 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2529 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2530 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2532 pa_memblockq_flush(s
->memblockq
);
2533 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2536 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2537 connection
*c
= CONNECTION(userdata
);
2539 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
2540 pa_tagstruct
*reply
;
2542 connection_assert_ref(c
);
2545 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
2550 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2552 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
2555 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2556 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2557 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2559 if (pa_tagstruct_get(
2561 PA_TAG_U32
, &maxlength
,
2562 PA_TAG_U32
, &tlength
,
2563 PA_TAG_U32
, &prebuf
,
2564 PA_TAG_U32
, &minreq
,
2565 PA_TAG_INVALID
) < 0 ||
2566 !pa_tagstruct_eof(t
)) {
2571 CHECK_VALIDITY(c
->pstream
, maxlength
> 0, tag
, PA_ERR_INVALID
);
2572 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
2574 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
2575 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
2576 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
2577 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
2579 reply
= reply_new(tag
);
2580 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
));
2581 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_tlength(s
->memblockq
));
2582 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
));
2583 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_minreq(s
->memblockq
));
2588 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
2590 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2591 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2593 if (pa_tagstruct_get(
2595 PA_TAG_U32
, &maxlength
,
2596 PA_TAG_U32
, &fragsize
,
2597 PA_TAG_INVALID
) < 0 ||
2598 !pa_tagstruct_eof(t
)) {
2603 CHECK_VALIDITY(c
->pstream
, maxlength
> 0, tag
, PA_ERR_INVALID
);
2604 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
2606 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
2608 base
= pa_frame_size(&s
->source_output
->sample_spec
);
2609 s
->fragment_size
= (fragsize
/base
)*base
;
2610 if (s
->fragment_size
<= 0)
2611 s
->fragment_size
= base
;
2613 if (s
->fragment_size
> pa_memblockq_get_maxlength(s
->memblockq
))
2614 s
->fragment_size
= pa_memblockq_get_maxlength(s
->memblockq
);
2616 reply
= reply_new(tag
);
2617 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
));
2618 pa_tagstruct_putu32(reply
, s
->fragment_size
);
2621 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2624 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2625 connection
*c
= CONNECTION(userdata
);
2629 connection_assert_ref(c
);
2632 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2633 pa_tagstruct_getu32(t
, &rate
) < 0 ||
2634 !pa_tagstruct_eof(t
)) {
2639 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2640 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
2642 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
2645 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2646 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2647 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2649 pa_sink_input_set_rate(s
->sink_input
, rate
);
2653 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
2655 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2656 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2658 pa_source_output_set_rate(s
->source_output
, rate
);
2661 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2664 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
) {
2665 connection
*c
= CONNECTION(userdata
);
2668 connection_assert_ref(c
);
2671 if (pa_tagstruct_gets(t
, &s
) < 0 ||
2672 !pa_tagstruct_eof(t
)) {
2677 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2678 CHECK_VALIDITY(c
->pstream
, !s
|| (*s
&& pa_utf8_valid(s
)), tag
, PA_ERR_INVALID
);
2680 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
2681 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2684 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2685 connection
*c
= CONNECTION(userdata
);
2689 connection_assert_ref(c
);
2692 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2693 pa_tagstruct_gets(t
, &name
) < 0 ||
2694 !pa_tagstruct_eof(t
)) {
2699 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2700 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2702 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
2705 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2706 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2707 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2709 pa_sink_input_set_name(s
->sink_input
, name
);
2713 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
2715 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2716 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2718 pa_source_output_set_name(s
->source_output
, name
);
2721 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2724 static void command_kill(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2725 connection
*c
= CONNECTION(userdata
);
2728 connection_assert_ref(c
);
2731 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2732 !pa_tagstruct_eof(t
)) {
2737 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2739 if (command
== PA_COMMAND_KILL_CLIENT
) {
2742 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2743 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
2746 pa_client_kill(client
);
2748 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
2751 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2752 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2755 pa_sink_input_kill(s
);
2757 pa_source_output
*s
;
2759 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
2761 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2762 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2765 pa_source_output_kill(s
);
2768 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2769 connection_unref(c
);
2772 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
) {
2773 connection
*c
= CONNECTION(userdata
);
2775 const char *name
, *argument
;
2776 pa_tagstruct
*reply
;
2778 connection_assert_ref(c
);
2781 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2782 pa_tagstruct_gets(t
, &argument
) < 0 ||
2783 !pa_tagstruct_eof(t
)) {
2788 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2789 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
2790 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
2792 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
2793 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
2797 reply
= reply_new(tag
);
2798 pa_tagstruct_putu32(reply
, m
->index
);
2799 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2802 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
) {
2803 connection
*c
= CONNECTION(userdata
);
2807 connection_assert_ref(c
);
2810 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2811 !pa_tagstruct_eof(t
)) {
2816 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2817 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2818 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
2820 pa_module_unload_request(m
);
2821 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2824 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
) {
2825 connection
*c
= CONNECTION(userdata
);
2826 const char *name
, *module
, *argument
;
2829 pa_tagstruct
*reply
;
2831 connection_assert_ref(c
);
2834 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2835 pa_tagstruct_getu32(t
, &type
) < 0 ||
2836 pa_tagstruct_gets(t
, &module
) < 0 ||
2837 pa_tagstruct_gets(t
, &argument
) < 0 ||
2838 !pa_tagstruct_eof(t
)) {
2843 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2844 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2845 CHECK_VALIDITY(c
->pstream
, type
== 0 || type
== 1, tag
, PA_ERR_INVALID
);
2846 CHECK_VALIDITY(c
->pstream
, module
&& *module
&& pa_utf8_valid(module
), tag
, PA_ERR_INVALID
);
2847 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
2849 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
2850 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
2854 reply
= reply_new(tag
);
2855 pa_tagstruct_putu32(reply
, idx
);
2856 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2859 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
) {
2860 connection
*c
= CONNECTION(userdata
);
2861 const char *name
= NULL
;
2862 uint32_t type
, idx
= PA_IDXSET_INVALID
;
2865 connection_assert_ref(c
);
2868 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
2869 (pa_tagstruct_gets(t
, &name
) < 0 ||
2870 pa_tagstruct_getu32(t
, &type
) < 0)) ||
2871 !pa_tagstruct_eof(t
)) {
2876 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2877 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
2878 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& pa_utf8_valid(name
) && (type
== 0 || type
== 1)), tag
, PA_ERR_INVALID
);
2881 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
2883 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
2885 CHECK_VALIDITY(c
->pstream
, r
>= 0, tag
, PA_ERR_NOENTITY
);
2887 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2890 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
2893 pa_tagstruct_putu32(t
, e
->index
);
2894 pa_tagstruct_puts(t
, e
->name
);
2895 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
2896 pa_tagstruct_puts(t
, e
->module
);
2897 pa_tagstruct_puts(t
, e
->argument
);
2900 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
) {
2901 connection
*c
= CONNECTION(userdata
);
2902 const pa_autoload_entry
*a
= NULL
;
2905 pa_tagstruct
*reply
;
2907 connection_assert_ref(c
);
2910 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
2911 (pa_tagstruct_gets(t
, &name
) < 0 ||
2912 pa_tagstruct_getu32(t
, &type
) < 0)) ||
2913 !pa_tagstruct_eof(t
)) {
2918 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2919 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
2920 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& (type
== 0 || type
== 1) && pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2923 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
2925 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
2927 CHECK_VALIDITY(c
->pstream
, a
, tag
, PA_ERR_NOENTITY
);
2929 reply
= reply_new(tag
);
2930 autoload_fill_tagstruct(reply
, a
);
2931 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2934 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
) {
2935 connection
*c
= CONNECTION(userdata
);
2936 pa_tagstruct
*reply
;
2938 connection_assert_ref(c
);
2941 if (!pa_tagstruct_eof(t
)) {
2946 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2948 reply
= reply_new(tag
);
2950 if (c
->protocol
->core
->autoload_hashmap
) {
2951 pa_autoload_entry
*a
;
2954 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
2955 autoload_fill_tagstruct(reply
, a
);
2958 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2961 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2962 connection
*c
= CONNECTION(userdata
);
2963 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
2964 const char *name
= NULL
;
2966 connection_assert_ref(c
);
2969 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2970 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
2971 pa_tagstruct_gets(t
, &name
) < 0 ||
2972 !pa_tagstruct_eof(t
)) {
2977 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2978 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2979 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2981 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
2982 pa_sink_input
*si
= NULL
;
2983 pa_sink
*sink
= NULL
;
2985 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2987 if (idx_device
!= PA_INVALID_INDEX
)
2988 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
2990 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2992 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
2994 if (pa_sink_input_move_to(si
, sink
, 0) < 0) {
2995 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2999 pa_source_output
*so
= NULL
;
3002 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3004 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3006 if (idx_device
!= PA_INVALID_INDEX
)
3007 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3009 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3011 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3013 if (pa_source_output_move_to(so
, source
) < 0) {
3014 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3019 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3022 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3023 connection
*c
= CONNECTION(userdata
);
3024 uint32_t idx
= PA_INVALID_INDEX
;
3025 const char *name
= NULL
;
3028 connection_assert_ref(c
);
3031 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3032 pa_tagstruct_gets(t
, &name
) < 0 ||
3033 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3034 !pa_tagstruct_eof(t
)) {
3039 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3040 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| !*name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3042 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3044 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3046 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3047 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3051 pa_sink
*sink
= NULL
;
3053 if (idx
!= PA_INVALID_INDEX
)
3054 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3056 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3058 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3060 if (pa_sink_suspend(sink
, b
) < 0) {
3061 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3067 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3069 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3071 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3072 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3079 if (idx
!= PA_INVALID_INDEX
)
3080 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3082 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3084 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3086 if (pa_source_suspend(source
, b
) < 0) {
3087 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3093 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3096 /*** pstream callbacks ***/
3098 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
3099 connection
*c
= CONNECTION(userdata
);
3103 connection_assert_ref(c
);
3105 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
3106 pa_log("invalid packet.");
3107 connection_unlink(c
);
3111 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
) {
3112 connection
*c
= CONNECTION(userdata
);
3113 output_stream
*stream
;
3117 connection_assert_ref(c
);
3119 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
3120 pa_log("client sent block for invalid stream.");
3125 if (playback_stream_isinstance(stream
)) {
3126 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
3128 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
3129 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
);
3131 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
3134 upload_stream
*u
= UPLOAD_STREAM(stream
);
3137 if (!u
->memchunk
.memblock
) {
3138 if (u
->length
== chunk
->length
) {
3139 u
->memchunk
= *chunk
;
3140 pa_memblock_ref(u
->memchunk
.memblock
);
3143 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
3144 u
->memchunk
.index
= u
->memchunk
.length
= 0;
3148 pa_assert(u
->memchunk
.memblock
);
3151 if (l
> chunk
->length
)
3157 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
3158 src
= pa_memblock_acquire(chunk
->memblock
);
3160 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
3161 (uint8_t*) src
+chunk
->index
, l
);
3163 pa_memblock_release(u
->memchunk
.memblock
);
3164 pa_memblock_release(chunk
->memblock
);
3166 u
->memchunk
.length
+= l
;
3172 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
3173 connection
*c
= CONNECTION(userdata
);
3176 connection_assert_ref(c
);
3178 connection_unlink(c
);
3179 pa_log_info("connection died.");
3182 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
3183 connection
*c
= CONNECTION(userdata
);
3186 connection_assert_ref(c
);
3191 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
3194 if (!(q
= pa_thread_mq_get()))
3195 pa_pstream_send_revoke(p
, block_id
);
3197 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
3200 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
3203 if (!(q
= pa_thread_mq_get()))
3204 pa_pstream_send_release(p
, block_id
);
3206 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
3209 /*** client callbacks ***/
3211 static void client_kill_cb(pa_client
*c
) {
3214 connection_unlink(CONNECTION(c
->userdata
));
3217 /*** socket server callbacks ***/
3219 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
3220 connection
*c
= CONNECTION(userdata
);
3224 connection_assert_ref(c
);
3225 pa_assert(c
->auth_timeout_event
== e
);
3228 connection_unlink(c
);
3231 static void on_connection(PA_GCC_UNUSED pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
3232 pa_protocol_native
*p
= userdata
;
3234 char cname
[256], pname
[128];
3240 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
3241 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
3242 pa_iochannel_free(io
);
3246 c
= pa_msgobject_new(connection
);
3247 c
->parent
.parent
.free
= connection_free
;
3248 c
->parent
.process_msg
= connection_process_msg
;
3250 c
->authorized
= !!p
->public;
3252 if (!c
->authorized
&& p
->auth_ip_acl
&& pa_ip_acl_check(p
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
3253 pa_log_info("Client authenticated by IP ACL.");
3257 if (!c
->authorized
) {
3259 pa_gettimeofday(&tv
);
3260 tv
.tv_sec
+= AUTH_TIMEOUT
;
3261 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
3263 c
->auth_timeout_event
= NULL
;
3267 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
3268 pa_snprintf(cname
, sizeof(cname
), "Native client (%s)", pname
);
3269 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
3270 c
->client
->kill
= client_kill_cb
;
3271 c
->client
->userdata
= c
;
3272 c
->client
->owner
= p
->module
;
3274 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
3276 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
3277 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
3278 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
3279 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
3280 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
3281 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
3283 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
3285 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
3286 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
3288 c
->rrobin_index
= PA_IDXSET_INVALID
;
3289 c
->subscription
= NULL
;
3291 pa_idxset_put(p
->connections
, c
, NULL
);
3294 if (pa_iochannel_creds_supported(io
))
3295 pa_iochannel_creds_enable(io
);
3300 /*** module entry points ***/
3302 static int load_key(pa_protocol_native
*p
, const char*fn
) {
3305 p
->auth_cookie_in_property
= 0;
3307 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
3308 pa_log_info("using already loaded auth cookie.");
3309 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
3310 p
->auth_cookie_in_property
= 1;
3315 fn
= PA_NATIVE_COOKIE_FILE
;
3317 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
3320 pa_log_info("loading cookie from disk.");
3322 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
3323 p
->auth_cookie_in_property
= 1;
3328 static pa_protocol_native
* protocol_new_internal(pa_core
*c
, pa_module
*m
, pa_modargs
*ma
) {
3329 pa_protocol_native
*p
;
3330 pa_bool_t
public = FALSE
;
3336 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
3337 pa_log("auth-anonymous= expects a boolean argument.");
3341 p
= pa_xnew(pa_protocol_native
, 1);
3346 p
->auth_ip_acl
= NULL
;
3351 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &a
) < 0) {
3352 pa_log("auth-group-enabled= expects a boolean argument.");
3355 p
->auth_group
= a
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", c
->is_system_instance
? PA_ACCESS_GROUP
: NULL
)) : NULL
;
3358 pa_log_info("Allowing access to group '%s'.", p
->auth_group
);
3363 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
3365 if (!(p
->auth_ip_acl
= pa_ip_acl_new(acl
))) {
3366 pa_log("Failed to parse IP ACL '%s'", acl
);
3371 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0)
3374 p
->connections
= pa_idxset_new(NULL
, NULL
);
3380 pa_xfree(p
->auth_group
);
3383 pa_ip_acl_free(p
->auth_ip_acl
);
3388 pa_protocol_native
* pa_protocol_native_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
3390 pa_protocol_native
*p
;
3392 if (!(p
= protocol_new_internal(core
, m
, ma
)))
3396 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
3398 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
3400 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3401 l
= pa_strlist_prepend(l
, t
);
3402 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
3408 void pa_protocol_native_free(pa_protocol_native
*p
) {
3412 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
3413 connection_unlink(c
);
3414 pa_idxset_free(p
->connections
, NULL
, NULL
);
3419 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
3421 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3422 l
= pa_strlist_remove(l
, t
);
3425 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
3427 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3430 pa_socket_server_unref(p
->server
);
3433 if (p
->auth_cookie_in_property
)
3434 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
3437 pa_ip_acl_free(p
->auth_ip_acl
);
3440 pa_xfree(p
->auth_group
);
3445 pa_protocol_native
* pa_protocol_native_new_iochannel(
3451 pa_protocol_native
*p
;
3453 if (!(p
= protocol_new_internal(core
, m
, ma
)))
3456 on_connection(NULL
, io
, p
);