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
, 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
);
206 static void send_memblock(connection
*c
);
207 static void request_bytes(struct playback_stream
*s
);
209 static void source_output_kill_cb(pa_source_output
*o
);
210 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
211 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
213 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
);
215 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
216 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
217 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
218 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
219 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
220 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
221 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
222 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
223 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
224 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
225 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
226 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
227 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
228 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
229 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
230 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
231 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
232 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
233 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
234 static void command_set_volume(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
235 static void command_set_mute(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
236 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
237 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
238 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
239 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
240 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
241 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
242 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
243 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
244 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
245 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
246 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
247 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
248 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
249 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
250 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
252 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
253 [PA_COMMAND_ERROR
] = NULL
,
254 [PA_COMMAND_TIMEOUT
] = NULL
,
255 [PA_COMMAND_REPLY
] = NULL
,
256 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
257 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
258 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
259 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
260 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
261 [PA_COMMAND_AUTH
] = command_auth
,
262 [PA_COMMAND_REQUEST
] = NULL
,
263 [PA_COMMAND_EXIT
] = command_exit
,
264 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
265 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
266 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
267 [PA_COMMAND_STAT
] = command_stat
,
268 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
269 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
270 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
271 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
272 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
273 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
274 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
275 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
276 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
277 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
278 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
279 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
280 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
281 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
282 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
283 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
284 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
285 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
286 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
287 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
288 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
289 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
290 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
292 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
293 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
294 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
296 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
297 [PA_COMMAND_SET_SINK_INPUT_MUTE
] = command_set_mute
,
298 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
300 [PA_COMMAND_SUSPEND_SINK
] = command_suspend
,
301 [PA_COMMAND_SUSPEND_SOURCE
] = command_suspend
,
303 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
304 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
305 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
306 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
308 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
309 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
311 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
312 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
313 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
314 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
315 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
316 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
317 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
318 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
319 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
320 [PA_COMMAND_GET_AUTOLOAD_INFO
] = command_get_autoload_info
,
321 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = command_get_autoload_info_list
,
322 [PA_COMMAND_ADD_AUTOLOAD
] = command_add_autoload
,
323 [PA_COMMAND_REMOVE_AUTOLOAD
] = command_remove_autoload
,
325 [PA_COMMAND_MOVE_SINK_INPUT
] = command_move_stream
,
326 [PA_COMMAND_MOVE_SOURCE_OUTPUT
] = command_move_stream
329 /* structure management */
331 static void upload_stream_unlink(upload_stream
*s
) {
337 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
338 s
->connection
= NULL
;
339 upload_stream_unref(s
);
342 static void upload_stream_free(pa_object
*o
) {
343 upload_stream
*s
= UPLOAD_STREAM(o
);
346 upload_stream_unlink(s
);
350 if (s
->memchunk
.memblock
)
351 pa_memblock_unref(s
->memchunk
.memblock
);
356 static upload_stream
* upload_stream_new(
358 const pa_sample_spec
*ss
,
359 const pa_channel_map
*map
,
360 const char *name
, size_t length
) {
367 pa_assert(length
> 0);
369 s
= pa_msgobject_new(upload_stream
);
370 s
->parent
.parent
.parent
.free
= upload_stream_free
;
372 s
->sample_spec
= *ss
;
373 s
->channel_map
= *map
;
374 s
->name
= pa_xstrdup(name
);
375 pa_memchunk_reset(&s
->memchunk
);
378 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
383 static void record_stream_unlink(record_stream
*s
) {
389 if (s
->source_output
) {
390 pa_source_output_disconnect(s
->source_output
);
391 pa_source_output_unref(s
->source_output
);
392 s
->source_output
= NULL
;
395 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->record_streams
, s
, NULL
) == s
);
396 s
->connection
= NULL
;
397 record_stream_unref(s
);
400 static void record_stream_free(pa_object
*o
) {
401 record_stream
*s
= RECORD_STREAM(o
);
404 record_stream_unlink(s
);
406 pa_memblockq_free(s
->memblockq
);
410 static int record_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
411 record_stream
*s
= RECORD_STREAM(o
);
412 record_stream_assert_ref(s
);
419 case RECORD_STREAM_MESSAGE_POST_DATA
:
421 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
422 /* pa_log_warn("Failed to push data into output queue."); */
426 if (!pa_pstream_is_pending(s
->connection
->pstream
))
427 send_memblock(s
->connection
);
429 pa_pstream_send_memblock(s
->connection
->pstream
, s
->index
, 0, PA_SEEK_RELATIVE
, chunk
);
436 static record_stream
* record_stream_new(
439 const pa_sample_spec
*ss
,
440 const pa_channel_map
*map
,
443 size_t fragment_size
,
447 pa_source_output
*source_output
;
449 pa_source_output_new_data data
;
454 pa_assert(maxlength
);
455 pa_assert(*maxlength
> 0);
457 pa_source_output_new_data_init(&data
);
458 data
.module
= c
->protocol
->module
;
459 data
.client
= c
->client
;
460 data
.source
= source
;
461 data
.driver
= __FILE__
;
463 data
.start_corked
= corked
;
464 pa_source_output_new_data_set_sample_spec(&data
, ss
);
465 pa_source_output_new_data_set_channel_map(&data
, map
);
467 if (!(source_output
= pa_source_output_new(c
->protocol
->core
, &data
, 0)))
470 s
= pa_msgobject_new(record_stream
);
471 s
->parent
.parent
.free
= record_stream_free
;
472 s
->parent
.process_msg
= record_stream_process_msg
;
474 s
->source_output
= source_output
;
475 s
->source_output
->push
= source_output_push_cb
;
476 s
->source_output
->kill
= source_output_kill_cb
;
477 s
->source_output
->get_latency
= source_output_get_latency_cb
;
478 s
->source_output
->userdata
= s
;
480 s
->memblockq
= pa_memblockq_new(
484 base
= pa_frame_size(ss
),
489 s
->fragment_size
= (fragment_size
/base
)*base
;
490 if (s
->fragment_size
<= 0)
491 s
->fragment_size
= base
;
492 *maxlength
= pa_memblockq_get_maxlength(s
->memblockq
);
494 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
496 pa_source_output_put(s
->source_output
);
500 static void playback_stream_unlink(playback_stream
*s
) {
507 pa_sink_input_disconnect(s
->sink_input
);
508 pa_sink_input_unref(s
->sink_input
);
509 s
->sink_input
= NULL
;
512 if (s
->drain_request
)
513 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
515 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
516 s
->connection
= NULL
;
517 playback_stream_unref(s
);
520 static void playback_stream_free(pa_object
* o
) {
521 playback_stream
*s
= PLAYBACK_STREAM(o
);
524 playback_stream_unlink(s
);
526 pa_memblockq_free(s
->memblockq
);
530 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
531 playback_stream
*s
= PLAYBACK_STREAM(o
);
532 playback_stream_assert_ref(s
);
538 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
542 if ((l
= pa_atomic_load(&s
->missing
)) <= 0)
545 pa_assert_se(pa_atomic_sub(&s
->missing
, l
) >= l
);
547 t
= pa_tagstruct_new(NULL
, 0);
548 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
549 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
550 pa_tagstruct_putu32(t
, s
->index
);
551 pa_tagstruct_putu32(t
, l
);
552 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
554 /* pa_log("Requesting %u bytes", l); */
558 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
561 /* Report that we're empty */
562 t
= pa_tagstruct_new(NULL
, 0);
563 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
564 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
565 pa_tagstruct_putu32(t
, s
->index
);
566 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
570 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
573 /* Notify the user we're overflowed*/
574 t
= pa_tagstruct_new(NULL
, 0);
575 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
576 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
577 pa_tagstruct_putu32(t
, s
->index
);
578 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
582 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
583 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
591 static playback_stream
* playback_stream_new(
594 const pa_sample_spec
*ss
,
595 const pa_channel_map
*map
,
606 playback_stream
*s
, *ssync
;
607 pa_sink_input
*sink_input
;
608 pa_memblock
*silence
;
611 pa_sink_input_new_data data
;
616 pa_assert(maxlength
);
618 /* Find syncid group */
619 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
621 if (!playback_stream_isinstance(ssync
))
624 if (ssync
->syncid
== syncid
)
628 /* Synced streams must connect to the same sink */
632 sink
= ssync
->sink_input
->sink
;
633 else if (sink
!= ssync
->sink_input
->sink
)
637 pa_sink_input_new_data_init(&data
);
639 data
.driver
= __FILE__
;
641 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
642 pa_sink_input_new_data_set_channel_map(&data
, map
);
643 pa_sink_input_new_data_set_volume(&data
, volume
);
644 data
.module
= c
->protocol
->module
;
645 data
.client
= c
->client
;
646 data
.start_corked
= corked
;
647 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
649 if (!(sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, 0)))
652 s
= pa_msgobject_new(playback_stream
);
653 s
->parent
.parent
.parent
.free
= playback_stream_free
;
654 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
657 s
->sink_input
= sink_input
;
660 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
661 s
->sink_input
->peek
= sink_input_peek_cb
;
662 s
->sink_input
->drop
= sink_input_drop_cb
;
663 s
->sink_input
->kill
= sink_input_kill_cb
;
664 s
->sink_input
->userdata
= s
;
666 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
668 silence
= pa_silence_memblock_new(c
->protocol
->core
->mempool
, ss
, 0);
670 s
->memblockq
= pa_memblockq_new(
679 pa_memblock_unref(silence
);
681 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
682 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
683 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
684 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
685 *missing
= (uint32_t) pa_memblockq_missing(s
->memblockq
);
687 pa_atomic_store(&s
->missing
, 0);
688 s
->last_missing
= *missing
;
689 s
->drain_request
= 0;
691 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
693 pa_sink_input_put(s
->sink_input
);
698 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
699 connection
*c
= CONNECTION(o
);
700 connection_assert_ref(c
);
707 case CONNECTION_MESSAGE_REVOKE
:
708 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
711 case CONNECTION_MESSAGE_RELEASE
:
712 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
719 static void connection_unlink(connection
*c
) {
728 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
729 record_stream_unlink(r
);
731 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
732 if (playback_stream_isinstance(o
))
733 playback_stream_unlink(PLAYBACK_STREAM(o
));
735 upload_stream_unlink(UPLOAD_STREAM(o
));
738 pa_subscription_free(c
->subscription
);
741 pa_pstream_unlink(c
->pstream
);
743 if (c
->auth_timeout_event
) {
744 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
745 c
->auth_timeout_event
= NULL
;
748 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
753 static void connection_free(pa_object
*o
) {
754 connection
*c
= CONNECTION(o
);
758 connection_unlink(c
);
760 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
761 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
763 pa_pdispatch_unref(c
->pdispatch
);
764 pa_pstream_unref(c
->pstream
);
765 pa_client_free(c
->client
);
770 /* Called from thread context */
771 static void request_bytes(playback_stream
*s
) {
772 size_t new_missing
, delta
, previous_missing
;
774 /* pa_log("request_bytes()"); */
775 playback_stream_assert_ref(s
);
777 new_missing
= pa_memblockq_missing(s
->memblockq
);
779 if (new_missing
<= s
->last_missing
) {
780 s
->last_missing
= new_missing
;
784 delta
= new_missing
- s
->last_missing
;
785 s
->last_missing
= new_missing
;
787 /* pa_log("request_bytes(%u)", delta); */
789 previous_missing
= pa_atomic_add(&s
->missing
, delta
);
790 if (previous_missing
< pa_memblockq_get_minreq(s
->memblockq
) && previous_missing
+delta
>= pa_memblockq_get_minreq(s
->memblockq
))
791 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
794 static void send_memblock(connection
*c
) {
798 start
= PA_IDXSET_INVALID
;
802 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
805 if (start
== PA_IDXSET_INVALID
)
806 start
= c
->rrobin_index
;
807 else if (start
== c
->rrobin_index
)
810 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
811 pa_memchunk schunk
= chunk
;
813 if (schunk
.length
> r
->fragment_size
)
814 schunk
.length
= r
->fragment_size
;
816 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
818 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
819 pa_memblock_unref(schunk
.memblock
);
826 static void send_playback_stream_killed(playback_stream
*p
) {
828 playback_stream_assert_ref(p
);
830 t
= pa_tagstruct_new(NULL
, 0);
831 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
832 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
833 pa_tagstruct_putu32(t
, p
->index
);
834 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
837 static void send_record_stream_killed(record_stream
*r
) {
839 record_stream_assert_ref(r
);
841 t
= pa_tagstruct_new(NULL
, 0);
842 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
843 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
844 pa_tagstruct_putu32(t
, r
->index
);
845 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
848 /*** sink input callbacks ***/
850 /* Called from thread context */
851 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
852 pa_sink_input
*i
= PA_SINK_INPUT(o
);
855 pa_sink_input_assert_ref(i
);
856 s
= PLAYBACK_STREAM(i
->userdata
);
857 playback_stream_assert_ref(s
);
861 case SINK_INPUT_MESSAGE_SEEK
:
862 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
866 case SINK_INPUT_MESSAGE_POST_DATA
: {
869 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
871 pa_log_warn("Failed to push data into queue");
872 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
873 pa_memblockq_seek(s
->memblockq
, chunk
->length
, PA_SEEK_RELATIVE
);
882 case SINK_INPUT_MESSAGE_DRAIN
: {
884 pa_memblockq_prebuf_disable(s
->memblockq
);
886 if (!pa_memblockq_is_readable(s
->memblockq
))
887 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
889 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
890 s
->drain_request
= 1;
897 case SINK_INPUT_MESSAGE_FLUSH
:
898 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
899 case SINK_INPUT_MESSAGE_TRIGGER
: {
901 pa_sink_input
*isync
;
902 void (*func
)(pa_memblockq
*bq
);
905 case SINK_INPUT_MESSAGE_FLUSH
:
906 func
= pa_memblockq_flush
;
909 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
910 func
= pa_memblockq_prebuf_force
;
913 case SINK_INPUT_MESSAGE_TRIGGER
:
914 func
= pa_memblockq_prebuf_disable
;
918 pa_assert_not_reached();
925 /* Do the same for all other members in the sync group */
926 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
927 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
928 func(ssync
->memblockq
);
930 request_bytes(ssync
);
933 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
934 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
935 func(ssync
->memblockq
);
937 request_bytes(ssync
);
943 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
945 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
946 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
947 s
->resampled_chunk_length
= s
->sink_input
->thread_info
.resampled_chunk
.memblock
? s
->sink_input
->thread_info
.resampled_chunk
.length
: 0;
950 case PA_SINK_INPUT_MESSAGE_SET_STATE
:
952 pa_memblockq_prebuf_force(s
->memblockq
);
956 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
957 pa_usec_t
*r
= userdata
;
959 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
961 /* Fall through, the default handler will add in the extra
962 * latency added by the resampler */
967 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
970 /* Called from thread context */
971 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
974 pa_sink_input_assert_ref(i
);
975 s
= PLAYBACK_STREAM(i
->userdata
);
976 playback_stream_assert_ref(s
);
979 if (pa_memblockq_get_length(s
->memblockq
) <= 0 && !s
->underrun
) {
980 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
984 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0) {
985 /* pa_log("peek: failure"); */
989 /* pa_log("peek: %u", chunk->length); */
996 /* Called from thread context */
997 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
1000 pa_sink_input_assert_ref(i
);
1001 s
= PLAYBACK_STREAM(i
->userdata
);
1002 playback_stream_assert_ref(s
);
1003 pa_assert(length
> 0);
1005 pa_memblockq_drop(s
->memblockq
, length
);
1007 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
1008 s
->drain_request
= 0;
1009 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
);
1014 /* pa_log("after_drop: %u %u", pa_memblockq_get_length(s->memblockq), pa_memblockq_is_readable(s->memblockq)); */
1017 static void sink_input_kill_cb(pa_sink_input
*i
) {
1020 pa_sink_input_assert_ref(i
);
1021 s
= PLAYBACK_STREAM(i
->userdata
);
1022 playback_stream_assert_ref(s
);
1024 send_playback_stream_killed(s
);
1025 playback_stream_unlink(s
);
1028 /*** source_output callbacks ***/
1030 /* Called from thread context */
1031 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1034 pa_source_output_assert_ref(o
);
1035 s
= RECORD_STREAM(o
->userdata
);
1036 record_stream_assert_ref(s
);
1039 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1042 static void source_output_kill_cb(pa_source_output
*o
) {
1045 pa_source_output_assert_ref(o
);
1046 s
= RECORD_STREAM(o
->userdata
);
1047 record_stream_assert_ref(s
);
1049 send_record_stream_killed(s
);
1050 record_stream_unlink(s
);
1053 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1056 pa_source_output_assert_ref(o
);
1057 s
= RECORD_STREAM(o
->userdata
);
1058 record_stream_assert_ref(s
);
1060 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1062 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1065 /*** pdispatch callbacks ***/
1067 static void protocol_error(connection
*c
) {
1068 pa_log("protocol error, kicking client");
1069 connection_unlink(c
);
1072 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1073 if (!(expression)) { \
1074 pa_pstream_send_error((pstream), (tag), (error)); \
1079 static pa_tagstruct
*reply_new(uint32_t tag
) {
1080 pa_tagstruct
*reply
;
1082 reply
= pa_tagstruct_new(NULL
, 0);
1083 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1084 pa_tagstruct_putu32(reply
, tag
);
1088 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
) {
1089 connection
*c
= CONNECTION(userdata
);
1091 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1092 const char *name
, *sink_name
;
1095 pa_tagstruct
*reply
;
1096 pa_sink
*sink
= NULL
;
1100 connection_assert_ref(c
);
1103 if (pa_tagstruct_get(
1105 PA_TAG_STRING
, &name
,
1106 PA_TAG_SAMPLE_SPEC
, &ss
,
1107 PA_TAG_CHANNEL_MAP
, &map
,
1108 PA_TAG_U32
, &sink_index
,
1109 PA_TAG_STRING
, &sink_name
,
1110 PA_TAG_U32
, &maxlength
,
1111 PA_TAG_BOOLEAN
, &corked
,
1112 PA_TAG_U32
, &tlength
,
1113 PA_TAG_U32
, &prebuf
,
1114 PA_TAG_U32
, &minreq
,
1115 PA_TAG_U32
, &syncid
,
1116 PA_TAG_CVOLUME
, &volume
,
1117 PA_TAG_INVALID
) < 0 ||
1118 !pa_tagstruct_eof(t
) ||
1124 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1125 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1126 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1127 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1128 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1129 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1130 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1131 CHECK_VALIDITY(c
->pstream
, maxlength
> 0 && maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
1132 CHECK_VALIDITY(c
->pstream
, maxlength
>= pa_frame_size(&ss
), tag
, PA_ERR_INVALID
);
1134 if (sink_index
!= PA_INVALID_INDEX
) {
1135 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1136 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
1137 } else if (sink_name
) {
1138 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1139 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
1142 s
= playback_stream_new(c
, sink
, &ss
, &map
, name
, &maxlength
, &tlength
, &prebuf
, &minreq
, &volume
, syncid
, corked
, &missing
);
1143 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1145 reply
= reply_new(tag
);
1146 pa_tagstruct_putu32(reply
, s
->index
);
1147 pa_assert(s
->sink_input
);
1148 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1149 pa_tagstruct_putu32(reply
, missing
);
1151 /* pa_log("initial request is %u", missing); */
1153 if (c
->version
>= 9) {
1154 /* Since 0.9 we support sending the buffer metrics back to the client */
1156 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1157 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1158 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1159 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1162 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1165 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1166 connection
*c
= CONNECTION(userdata
);
1169 connection_assert_ref(c
);
1172 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1173 !pa_tagstruct_eof(t
)) {
1178 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1182 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1184 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1185 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1189 playback_stream_unlink(s
);
1193 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1195 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1196 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1200 record_stream_unlink(s
);
1204 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1207 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1208 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1212 upload_stream_unlink(s
);
1217 pa_assert_not_reached();
1220 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1223 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
) {
1224 connection
*c
= CONNECTION(userdata
);
1226 uint32_t maxlength
, fragment_size
;
1227 uint32_t source_index
;
1228 const char *name
, *source_name
;
1231 pa_tagstruct
*reply
;
1232 pa_source
*source
= NULL
;
1235 connection_assert_ref(c
);
1238 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1239 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1240 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1241 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1242 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1243 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1244 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1245 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
1246 !pa_tagstruct_eof(t
)) {
1251 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1252 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1253 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1254 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1255 CHECK_VALIDITY(c
->pstream
, source_index
!= PA_INVALID_INDEX
|| !source_name
|| (*source_name
&& pa_utf8_valid(source_name
)), tag
, PA_ERR_INVALID
);
1256 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1257 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
1259 if (source_index
!= PA_INVALID_INDEX
) {
1260 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
1261 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
1262 } else if (source_name
) {
1263 source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1);
1264 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
1267 s
= record_stream_new(c
, source
, &ss
, &map
, name
, &maxlength
, fragment_size
, corked
);
1268 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1270 reply
= reply_new(tag
);
1271 pa_tagstruct_putu32(reply
, s
->index
);
1272 pa_assert(s
->source_output
);
1273 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
1275 if (c
->version
>= 9) {
1276 /* Since 0.9 we support sending the buffer metrics back to the client */
1278 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1279 pa_tagstruct_putu32(reply
, (uint32_t) s
->fragment_size
);
1282 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1285 static void command_exit(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1286 connection
*c
= CONNECTION(userdata
);
1288 connection_assert_ref(c
);
1291 if (!pa_tagstruct_eof(t
)) {
1296 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1298 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
1299 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
1302 static void command_auth(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1303 connection
*c
= CONNECTION(userdata
);
1305 pa_tagstruct
*reply
;
1307 connection_assert_ref(c
);
1310 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
1311 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
1312 !pa_tagstruct_eof(t
)) {
1317 /* Minimum supported version */
1318 if (c
->version
< 8) {
1319 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
1323 if (!c
->authorized
) {
1327 const pa_creds
*creds
;
1329 if ((creds
= pa_pdispatch_creds(pd
))) {
1330 if (creds
->uid
== getuid())
1332 else if (c
->protocol
->auth_group
) {
1336 if ((gid
= pa_get_gid_of_group(c
->protocol
->auth_group
)) == (gid_t
) -1)
1337 pa_log_warn("failed to get GID of group '%s'", c
->protocol
->auth_group
);
1338 else if (gid
== creds
->gid
)
1342 if ((r
= pa_uid_in_group(creds
->uid
, c
->protocol
->auth_group
)) < 0)
1343 pa_log_warn("failed to check group membership.");
1349 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
1350 (unsigned long) creds
->uid
,
1351 (unsigned long) creds
->gid
,
1354 if (c
->version
>= 10 &&
1355 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
1356 creds
->uid
== getuid()) {
1358 pa_pstream_use_shm(c
->pstream
, 1);
1359 pa_log_info("Enabled SHM for new connection");
1365 if (!success
&& memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
1369 pa_log_warn("Denied access to client with invalid authorization data.");
1370 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1375 if (c
->auth_timeout_event
) {
1376 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1377 c
->auth_timeout_event
= NULL
;
1381 reply
= reply_new(tag
);
1382 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
);
1386 /* SHM support is only enabled after both sides made sure they are the same user. */
1390 ucred
.uid
= getuid();
1391 ucred
.gid
= getgid();
1393 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
1396 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1400 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
) {
1401 connection
*c
= CONNECTION(userdata
);
1404 connection_assert_ref(c
);
1407 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1408 !pa_tagstruct_eof(t
)) {
1413 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1415 pa_client_set_name(c
->client
, name
);
1416 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1419 static void command_lookup(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1420 connection
*c
= CONNECTION(userdata
);
1422 uint32_t idx
= PA_IDXSET_INVALID
;
1424 connection_assert_ref(c
);
1427 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1428 !pa_tagstruct_eof(t
)) {
1433 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1434 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1436 if (command
== PA_COMMAND_LOOKUP_SINK
) {
1438 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
1442 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
1443 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
1444 idx
= source
->index
;
1447 if (idx
== PA_IDXSET_INVALID
)
1448 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1450 pa_tagstruct
*reply
;
1451 reply
= reply_new(tag
);
1452 pa_tagstruct_putu32(reply
, idx
);
1453 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1457 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
) {
1458 connection
*c
= CONNECTION(userdata
);
1462 connection_assert_ref(c
);
1465 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1466 !pa_tagstruct_eof(t
)) {
1471 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1472 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1473 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1474 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1476 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
);
1479 static void command_stat(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1480 connection
*c
= CONNECTION(userdata
);
1481 pa_tagstruct
*reply
;
1482 const pa_mempool_stat
*stat
;
1484 connection_assert_ref(c
);
1487 if (!pa_tagstruct_eof(t
)) {
1492 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1494 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
1496 reply
= reply_new(tag
);
1497 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
1498 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
1499 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
1500 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
1501 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
1502 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1505 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
) {
1506 connection
*c
= CONNECTION(userdata
);
1507 pa_tagstruct
*reply
;
1509 struct timeval tv
, now
;
1513 connection_assert_ref(c
);
1516 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1517 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1518 !pa_tagstruct_eof(t
)) {
1523 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1524 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1525 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1526 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1527 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
)
1529 reply
= reply_new(tag
);
1531 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
1532 latency
+= pa_bytes_to_usec(s
->resampled_chunk_length
, &s
->sink_input
->sample_spec
);
1534 pa_tagstruct_put_usec(reply
, latency
);
1536 pa_tagstruct_put_usec(reply
, 0);
1537 pa_tagstruct_put_boolean(reply
, pa_sink_input_get_state(s
->sink_input
) == PA_SINK_INPUT_RUNNING
);
1538 pa_tagstruct_put_timeval(reply
, &tv
);
1539 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
1540 pa_tagstruct_puts64(reply
, s
->write_index
);
1541 pa_tagstruct_puts64(reply
, s
->read_index
);
1542 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1545 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
) {
1546 connection
*c
= CONNECTION(userdata
);
1547 pa_tagstruct
*reply
;
1549 struct timeval tv
, now
;
1552 connection_assert_ref(c
);
1555 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1556 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1557 !pa_tagstruct_eof(t
)) {
1562 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1563 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
1564 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1566 reply
= reply_new(tag
);
1567 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
1568 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
1569 pa_tagstruct_put_boolean(reply
, 0);
1570 pa_tagstruct_put_timeval(reply
, &tv
);
1571 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
1572 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
1573 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
1574 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1577 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
) {
1578 connection
*c
= CONNECTION(userdata
);
1584 pa_tagstruct
*reply
;
1586 connection_assert_ref(c
);
1589 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1590 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1591 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1592 pa_tagstruct_getu32(t
, &length
) < 0 ||
1593 !pa_tagstruct_eof(t
)) {
1598 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1599 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1600 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1601 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1602 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
1603 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
1604 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1606 s
= upload_stream_new(c
, &ss
, &map
, name
, length
);
1607 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1609 reply
= reply_new(tag
);
1610 pa_tagstruct_putu32(reply
, s
->index
);
1611 pa_tagstruct_putu32(reply
, length
);
1612 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1615 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
) {
1616 connection
*c
= CONNECTION(userdata
);
1621 connection_assert_ref(c
);
1624 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1625 !pa_tagstruct_eof(t
)) {
1630 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1632 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
1633 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1634 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
1636 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, &idx
) < 0)
1637 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
1639 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1641 upload_stream_unlink(s
);
1644 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
) {
1645 connection
*c
= CONNECTION(userdata
);
1646 uint32_t sink_index
;
1649 const char *name
, *sink_name
;
1651 connection_assert_ref(c
);
1654 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1655 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1656 pa_tagstruct_getu32(t
, &volume
) < 0 ||
1657 pa_tagstruct_gets(t
, &name
) < 0 ||
1658 !pa_tagstruct_eof(t
)) {
1663 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1664 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1665 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1667 if (sink_index
!= PA_INVALID_INDEX
)
1668 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1670 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1672 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
1674 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
1675 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1679 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1682 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
) {
1683 connection
*c
= CONNECTION(userdata
);
1686 connection_assert_ref(c
);
1689 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1690 !pa_tagstruct_eof(t
)) {
1695 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1696 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1698 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
1699 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1703 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1706 static void sink_fill_tagstruct(pa_tagstruct
*t
, pa_sink
*sink
) {
1708 pa_sink_assert_ref(sink
);
1712 PA_TAG_U32
, sink
->index
,
1713 PA_TAG_STRING
, sink
->name
,
1714 PA_TAG_STRING
, sink
->description
,
1715 PA_TAG_SAMPLE_SPEC
, &sink
->sample_spec
,
1716 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
1717 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
1718 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
),
1719 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
),
1720 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
1721 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
1722 PA_TAG_USEC
, pa_sink_get_latency(sink
),
1723 PA_TAG_STRING
, sink
->driver
,
1725 (sink
->get_volume
? PA_SINK_HW_VOLUME_CTRL
: 0) | /* FIXME */
1726 (sink
->get_latency
? PA_SINK_LATENCY
: 0) | /* FIXME */
1727 (sink
->is_hardware
? PA_SINK_HARDWARE
: 0),
1731 static void source_fill_tagstruct(pa_tagstruct
*t
, pa_source
*source
) {
1733 pa_source_assert_ref(source
);
1737 PA_TAG_U32
, source
->index
,
1738 PA_TAG_STRING
, source
->name
,
1739 PA_TAG_STRING
, source
->description
,
1740 PA_TAG_SAMPLE_SPEC
, &source
->sample_spec
,
1741 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
1742 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
1743 PA_TAG_CVOLUME
, pa_source_get_volume(source
),
1744 PA_TAG_BOOLEAN
, pa_source_get_mute(source
),
1745 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
1746 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
1747 PA_TAG_USEC
, pa_source_get_latency(source
),
1748 PA_TAG_STRING
, source
->driver
,
1750 (source
->get_volume
? PA_SOURCE_HW_VOLUME_CTRL
: 0) | /* FIXME */
1751 (source
->get_latency
? PA_SOURCE_LATENCY
: 0) | /* FIXME */
1752 (source
->is_hardware
? PA_SOURCE_HARDWARE
: 0),
1756 static void client_fill_tagstruct(pa_tagstruct
*t
, pa_client
*client
) {
1760 pa_tagstruct_putu32(t
, client
->index
);
1761 pa_tagstruct_puts(t
, client
->name
);
1762 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: PA_INVALID_INDEX
);
1763 pa_tagstruct_puts(t
, client
->driver
);
1766 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
1770 pa_tagstruct_putu32(t
, module
->index
);
1771 pa_tagstruct_puts(t
, module
->name
);
1772 pa_tagstruct_puts(t
, module
->argument
);
1773 pa_tagstruct_putu32(t
, module
->n_used
);
1774 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
1777 static void sink_input_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
1779 pa_sink_input_assert_ref(s
);
1781 pa_tagstruct_putu32(t
, s
->index
);
1782 pa_tagstruct_puts(t
, s
->name
);
1783 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
1784 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
1785 pa_tagstruct_putu32(t
, s
->sink
->index
);
1786 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1787 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
1788 pa_tagstruct_put_cvolume(t
, &s
->volume
);
1789 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
1790 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
1791 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
1792 pa_tagstruct_puts(t
, s
->driver
);
1793 if (c
->version
>= 11)
1794 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
1797 static void source_output_fill_tagstruct(pa_tagstruct
*t
, pa_source_output
*s
) {
1799 pa_source_output_assert_ref(s
);
1801 pa_tagstruct_putu32(t
, s
->index
);
1802 pa_tagstruct_puts(t
, s
->name
);
1803 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
1804 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
1805 pa_tagstruct_putu32(t
, s
->source
->index
);
1806 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1807 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
1808 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
1809 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
1810 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
1811 pa_tagstruct_puts(t
, s
->driver
);
1814 static void scache_fill_tagstruct(pa_tagstruct
*t
, pa_scache_entry
*e
) {
1818 pa_tagstruct_putu32(t
, e
->index
);
1819 pa_tagstruct_puts(t
, e
->name
);
1820 pa_tagstruct_put_cvolume(t
, &e
->volume
);
1821 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1822 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1823 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
1824 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
1825 pa_tagstruct_put_boolean(t
, e
->lazy
);
1826 pa_tagstruct_puts(t
, e
->filename
);
1829 static void command_get_info(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1830 connection
*c
= CONNECTION(userdata
);
1832 pa_sink
*sink
= NULL
;
1833 pa_source
*source
= NULL
;
1834 pa_client
*client
= NULL
;
1835 pa_module
*module
= NULL
;
1836 pa_sink_input
*si
= NULL
;
1837 pa_source_output
*so
= NULL
;
1838 pa_scache_entry
*sce
= NULL
;
1840 pa_tagstruct
*reply
;
1842 connection_assert_ref(c
);
1845 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1846 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1847 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1848 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1849 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1850 pa_tagstruct_gets(t
, &name
) < 0) ||
1851 !pa_tagstruct_eof(t
)) {
1856 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1857 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1859 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1860 if (idx
!= PA_INVALID_INDEX
)
1861 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
1863 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1864 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1865 if (idx
!= PA_INVALID_INDEX
)
1866 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
1868 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1869 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1870 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
1871 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1872 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
1873 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1874 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
1875 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1876 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
1878 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
1879 if (idx
!= PA_INVALID_INDEX
)
1880 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
1882 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1885 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1886 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1890 reply
= reply_new(tag
);
1892 sink_fill_tagstruct(reply
, sink
);
1894 source_fill_tagstruct(reply
, source
);
1896 client_fill_tagstruct(reply
, client
);
1898 module_fill_tagstruct(reply
, module
);
1900 sink_input_fill_tagstruct(c
, reply
, si
);
1902 source_output_fill_tagstruct(reply
, so
);
1904 scache_fill_tagstruct(reply
, sce
);
1905 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1908 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1909 connection
*c
= CONNECTION(userdata
);
1913 pa_tagstruct
*reply
;
1915 connection_assert_ref(c
);
1918 if (!pa_tagstruct_eof(t
)) {
1923 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1925 reply
= reply_new(tag
);
1927 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1928 i
= c
->protocol
->core
->sinks
;
1929 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1930 i
= c
->protocol
->core
->sources
;
1931 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1932 i
= c
->protocol
->core
->clients
;
1933 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1934 i
= c
->protocol
->core
->modules
;
1935 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1936 i
= c
->protocol
->core
->sink_inputs
;
1937 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1938 i
= c
->protocol
->core
->source_outputs
;
1940 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1941 i
= c
->protocol
->core
->scache
;
1945 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
1946 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1947 sink_fill_tagstruct(reply
, p
);
1948 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1949 source_fill_tagstruct(reply
, p
);
1950 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1951 client_fill_tagstruct(reply
, p
);
1952 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1953 module_fill_tagstruct(reply
, p
);
1954 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1955 sink_input_fill_tagstruct(c
, reply
, p
);
1956 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1957 source_output_fill_tagstruct(reply
, p
);
1959 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1960 scache_fill_tagstruct(reply
, p
);
1965 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1968 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
) {
1969 connection
*c
= CONNECTION(userdata
);
1970 pa_tagstruct
*reply
;
1974 connection_assert_ref(c
);
1977 if (!pa_tagstruct_eof(t
)) {
1982 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1984 reply
= reply_new(tag
);
1985 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1986 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1987 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1988 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
1989 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1991 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
1992 pa_tagstruct_puts(reply
, n
);
1993 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
1994 pa_tagstruct_puts(reply
, n
);
1996 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
1998 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2001 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
2003 connection
*c
= CONNECTION(userdata
);
2005 connection_assert_ref(c
);
2007 t
= pa_tagstruct_new(NULL
, 0);
2008 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
2009 pa_tagstruct_putu32(t
, (uint32_t) -1);
2010 pa_tagstruct_putu32(t
, e
);
2011 pa_tagstruct_putu32(t
, idx
);
2012 pa_pstream_send_tagstruct(c
->pstream
, t
);
2015 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2016 connection
*c
= CONNECTION(userdata
);
2017 pa_subscription_mask_t m
;
2019 connection_assert_ref(c
);
2022 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
2023 !pa_tagstruct_eof(t
)) {
2028 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2029 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
2031 if (c
->subscription
)
2032 pa_subscription_free(c
->subscription
);
2035 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
2036 pa_assert(c
->subscription
);
2038 c
->subscription
= NULL
;
2040 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2043 static void command_set_volume(
2044 PA_GCC_UNUSED pa_pdispatch
*pd
,
2050 connection
*c
= CONNECTION(userdata
);
2053 pa_sink
*sink
= NULL
;
2054 pa_source
*source
= NULL
;
2055 pa_sink_input
*si
= NULL
;
2056 const char *name
= NULL
;
2058 connection_assert_ref(c
);
2061 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2062 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2063 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2064 pa_tagstruct_get_cvolume(t
, &volume
) ||
2065 !pa_tagstruct_eof(t
)) {
2070 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2071 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2072 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
2076 case PA_COMMAND_SET_SINK_VOLUME
:
2077 if (idx
!= PA_INVALID_INDEX
)
2078 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2080 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2083 case PA_COMMAND_SET_SOURCE_VOLUME
:
2084 if (idx
!= PA_INVALID_INDEX
)
2085 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2087 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2090 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
2091 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2095 pa_assert_not_reached();
2098 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2101 pa_sink_set_volume(sink
, &volume
);
2103 pa_source_set_volume(source
, &volume
);
2105 pa_sink_input_set_volume(si
, &volume
);
2107 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2110 static void command_set_mute(
2111 PA_GCC_UNUSED pa_pdispatch
*pd
,
2117 connection
*c
= CONNECTION(userdata
);
2120 pa_sink
*sink
= NULL
;
2121 pa_source
*source
= NULL
;
2122 pa_sink_input
*si
= NULL
;
2123 const char *name
= NULL
;
2125 connection_assert_ref(c
);
2128 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2129 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2130 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2131 pa_tagstruct_get_boolean(t
, &mute
) ||
2132 !pa_tagstruct_eof(t
)) {
2137 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2138 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2142 case PA_COMMAND_SET_SINK_MUTE
:
2144 if (idx
!= PA_INVALID_INDEX
)
2145 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2147 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2151 case PA_COMMAND_SET_SOURCE_MUTE
:
2152 if (idx
!= PA_INVALID_INDEX
)
2153 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2155 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2159 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
2160 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2164 pa_assert_not_reached();
2167 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2170 pa_sink_set_mute(sink
, mute
);
2172 pa_source_set_mute(source
, mute
);
2174 pa_sink_input_set_mute(si
, mute
);
2176 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2179 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
) {
2180 connection
*c
= CONNECTION(userdata
);
2185 connection_assert_ref(c
);
2188 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2189 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
2190 !pa_tagstruct_eof(t
)) {
2195 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2196 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2197 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2198 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2199 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2201 pa_sink_input_cork(s
->sink_input
, b
);
2202 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2205 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
) {
2206 connection
*c
= CONNECTION(userdata
);
2210 connection_assert_ref(c
);
2213 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2214 !pa_tagstruct_eof(t
)) {
2219 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2220 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2221 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2222 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2223 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2226 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
2227 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
2230 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
2231 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
2234 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
2235 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
2239 pa_assert_not_reached();
2242 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2245 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
) {
2246 connection
*c
= CONNECTION(userdata
);
2251 connection_assert_ref(c
);
2254 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2255 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
2256 !pa_tagstruct_eof(t
)) {
2261 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2262 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2263 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2265 pa_source_output_cork(s
->source_output
, b
);
2266 pa_memblockq_prebuf_force(s
->memblockq
);
2267 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2270 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
) {
2271 connection
*c
= CONNECTION(userdata
);
2275 connection_assert_ref(c
);
2278 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2279 !pa_tagstruct_eof(t
)) {
2284 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2285 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2286 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2288 pa_memblockq_flush(s
->memblockq
);
2289 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2292 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
) {
2293 connection
*c
= CONNECTION(userdata
);
2296 connection_assert_ref(c
);
2299 if (pa_tagstruct_gets(t
, &s
) < 0 ||
2300 !pa_tagstruct_eof(t
)) {
2305 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2306 CHECK_VALIDITY(c
->pstream
, !s
|| (*s
&& pa_utf8_valid(s
)), tag
, PA_ERR_INVALID
);
2308 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
2309 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2312 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2313 connection
*c
= CONNECTION(userdata
);
2317 connection_assert_ref(c
);
2320 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2321 pa_tagstruct_gets(t
, &name
) < 0 ||
2322 !pa_tagstruct_eof(t
)) {
2327 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2328 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2330 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
2333 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2334 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2335 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2337 pa_sink_input_set_name(s
->sink_input
, name
);
2342 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2343 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2345 pa_source_output_set_name(s
->source_output
, name
);
2348 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2351 static void command_kill(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2352 connection
*c
= CONNECTION(userdata
);
2355 connection_assert_ref(c
);
2358 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2359 !pa_tagstruct_eof(t
)) {
2364 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2366 if (command
== PA_COMMAND_KILL_CLIENT
) {
2369 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2370 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
2373 pa_client_kill(client
);
2375 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
2378 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2379 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2382 pa_sink_input_kill(s
);
2384 pa_source_output
*s
;
2386 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
2388 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2389 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2392 pa_source_output_kill(s
);
2395 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2396 connection_unref(c
);
2399 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
) {
2400 connection
*c
= CONNECTION(userdata
);
2402 const char *name
, *argument
;
2403 pa_tagstruct
*reply
;
2405 connection_assert_ref(c
);
2408 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2409 pa_tagstruct_gets(t
, &argument
) < 0 ||
2410 !pa_tagstruct_eof(t
)) {
2415 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2416 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
2417 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
2419 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
2420 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
2424 reply
= reply_new(tag
);
2425 pa_tagstruct_putu32(reply
, m
->index
);
2426 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2429 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
) {
2430 connection
*c
= CONNECTION(userdata
);
2434 connection_assert_ref(c
);
2437 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2438 !pa_tagstruct_eof(t
)) {
2443 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2444 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2445 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
2447 pa_module_unload_request(m
);
2448 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2451 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
) {
2452 connection
*c
= CONNECTION(userdata
);
2453 const char *name
, *module
, *argument
;
2456 pa_tagstruct
*reply
;
2458 connection_assert_ref(c
);
2461 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2462 pa_tagstruct_getu32(t
, &type
) < 0 ||
2463 pa_tagstruct_gets(t
, &module
) < 0 ||
2464 pa_tagstruct_gets(t
, &argument
) < 0 ||
2465 !pa_tagstruct_eof(t
)) {
2470 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2471 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2472 CHECK_VALIDITY(c
->pstream
, type
== 0 || type
== 1, tag
, PA_ERR_INVALID
);
2473 CHECK_VALIDITY(c
->pstream
, module
&& *module
&& pa_utf8_valid(module
), tag
, PA_ERR_INVALID
);
2474 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
2476 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
2477 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
2481 reply
= reply_new(tag
);
2482 pa_tagstruct_putu32(reply
, idx
);
2483 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2486 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
) {
2487 connection
*c
= CONNECTION(userdata
);
2488 const char *name
= NULL
;
2489 uint32_t type
, idx
= PA_IDXSET_INVALID
;
2492 connection_assert_ref(c
);
2495 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
2496 (pa_tagstruct_gets(t
, &name
) < 0 ||
2497 pa_tagstruct_getu32(t
, &type
) < 0)) ||
2498 !pa_tagstruct_eof(t
)) {
2503 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2504 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
2505 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& pa_utf8_valid(name
) && (type
== 0 || type
== 1)), tag
, PA_ERR_INVALID
);
2508 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
2510 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
2512 CHECK_VALIDITY(c
->pstream
, r
>= 0, tag
, PA_ERR_NOENTITY
);
2514 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2517 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
2520 pa_tagstruct_putu32(t
, e
->index
);
2521 pa_tagstruct_puts(t
, e
->name
);
2522 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
2523 pa_tagstruct_puts(t
, e
->module
);
2524 pa_tagstruct_puts(t
, e
->argument
);
2527 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
) {
2528 connection
*c
= CONNECTION(userdata
);
2529 const pa_autoload_entry
*a
= NULL
;
2532 pa_tagstruct
*reply
;
2534 connection_assert_ref(c
);
2537 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
2538 (pa_tagstruct_gets(t
, &name
) < 0 ||
2539 pa_tagstruct_getu32(t
, &type
) < 0)) ||
2540 !pa_tagstruct_eof(t
)) {
2545 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2546 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
2547 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& (type
== 0 || type
== 1) && pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2550 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
2552 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
2554 CHECK_VALIDITY(c
->pstream
, a
, tag
, PA_ERR_NOENTITY
);
2556 reply
= reply_new(tag
);
2557 autoload_fill_tagstruct(reply
, a
);
2558 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2561 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
) {
2562 connection
*c
= CONNECTION(userdata
);
2563 pa_tagstruct
*reply
;
2565 connection_assert_ref(c
);
2568 if (!pa_tagstruct_eof(t
)) {
2573 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2575 reply
= reply_new(tag
);
2577 if (c
->protocol
->core
->autoload_hashmap
) {
2578 pa_autoload_entry
*a
;
2581 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
2582 autoload_fill_tagstruct(reply
, a
);
2585 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2588 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2589 connection
*c
= CONNECTION(userdata
);
2590 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
2591 const char *name
= NULL
;
2593 connection_assert_ref(c
);
2596 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2597 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
2598 pa_tagstruct_gets(t
, &name
) < 0 ||
2599 !pa_tagstruct_eof(t
)) {
2604 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2605 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2606 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2608 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
2609 pa_sink_input
*si
= NULL
;
2610 pa_sink
*sink
= NULL
;
2612 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2614 if (idx_device
!= PA_INVALID_INDEX
)
2615 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
2617 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2619 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
2621 if (pa_sink_input_move_to(si
, sink
, 0) < 0) {
2622 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2626 pa_source_output
*so
= NULL
;
2629 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
2631 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2633 if (idx_device
!= PA_INVALID_INDEX
)
2634 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
2636 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2638 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
2640 if (pa_source_output_move_to(so
, source
) < 0) {
2641 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2646 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2649 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2650 connection
*c
= CONNECTION(userdata
);
2651 uint32_t idx
= PA_INVALID_INDEX
;
2652 const char *name
= NULL
;
2655 connection_assert_ref(c
);
2658 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2659 pa_tagstruct_gets(t
, &name
) < 0 ||
2660 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
2661 !pa_tagstruct_eof(t
)) {
2666 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2667 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| !*name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2669 if (command
== PA_COMMAND_SUSPEND_SINK
) {
2671 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
2673 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
2674 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2678 pa_sink
*sink
= NULL
;
2680 if (idx
!= PA_INVALID_INDEX
)
2681 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2683 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2685 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2687 if (pa_sink_suspend(sink
, b
) < 0) {
2688 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2694 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
2696 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
2698 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
2699 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2706 if (idx
!= PA_INVALID_INDEX
)
2707 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2709 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2711 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
2713 if (pa_source_suspend(source
, b
) < 0) {
2714 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2720 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2723 /*** pstream callbacks ***/
2725 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
2726 connection
*c
= CONNECTION(userdata
);
2730 connection_assert_ref(c
);
2732 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
2733 pa_log("invalid packet.");
2734 connection_unlink(c
);
2738 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
) {
2739 connection
*c
= CONNECTION(userdata
);
2740 output_stream
*stream
;
2744 connection_assert_ref(c
);
2746 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
2747 pa_log("client sent block for invalid stream.");
2752 if (playback_stream_isinstance(stream
)) {
2753 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
2755 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
2756 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
);
2758 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
2761 upload_stream
*u
= UPLOAD_STREAM(stream
);
2764 if (!u
->memchunk
.memblock
) {
2765 if (u
->length
== chunk
->length
) {
2766 u
->memchunk
= *chunk
;
2767 pa_memblock_ref(u
->memchunk
.memblock
);
2770 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
2771 u
->memchunk
.index
= u
->memchunk
.length
= 0;
2775 pa_assert(u
->memchunk
.memblock
);
2778 if (l
> chunk
->length
)
2784 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
2785 src
= pa_memblock_acquire(chunk
->memblock
);
2787 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
2788 (uint8_t*) src
+chunk
->index
, l
);
2790 pa_memblock_release(u
->memchunk
.memblock
);
2791 pa_memblock_release(chunk
->memblock
);
2793 u
->memchunk
.length
+= l
;
2799 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
2800 connection
*c
= CONNECTION(userdata
);
2803 connection_assert_ref(c
);
2805 connection_unlink(c
);
2806 pa_log_info("connection died.");
2809 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
2810 connection
*c
= CONNECTION(userdata
);
2813 connection_assert_ref(c
);
2818 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
2821 if (!(q
= pa_thread_mq_get()))
2822 pa_pstream_send_revoke(p
, block_id
);
2824 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
2827 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
2830 if (!(q
= pa_thread_mq_get()))
2831 pa_pstream_send_release(p
, block_id
);
2833 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
2836 /*** client callbacks ***/
2838 static void client_kill_cb(pa_client
*c
) {
2841 connection_unlink(CONNECTION(c
->userdata
));
2844 /*** socket server callbacks ***/
2846 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
2847 connection
*c
= CONNECTION(userdata
);
2851 connection_assert_ref(c
);
2852 pa_assert(c
->auth_timeout_event
== e
);
2855 connection_unlink(c
);
2858 static void on_connection(PA_GCC_UNUSED pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
2859 pa_protocol_native
*p
= userdata
;
2861 char cname
[256], pname
[128];
2867 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
2868 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
2869 pa_iochannel_free(io
);
2873 c
= pa_msgobject_new(connection
);
2874 c
->parent
.parent
.free
= connection_free
;
2875 c
->parent
.process_msg
= connection_process_msg
;
2877 c
->authorized
= !!p
->public;
2879 if (!c
->authorized
&& p
->auth_ip_acl
&& pa_ip_acl_check(p
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
2880 pa_log_info("Client authenticated by IP ACL.");
2884 if (!c
->authorized
) {
2886 pa_gettimeofday(&tv
);
2887 tv
.tv_sec
+= AUTH_TIMEOUT
;
2888 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
2890 c
->auth_timeout_event
= NULL
;
2894 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
2895 pa_snprintf(cname
, sizeof(cname
), "Native client (%s)", pname
);
2896 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
2897 c
->client
->kill
= client_kill_cb
;
2898 c
->client
->userdata
= c
;
2899 c
->client
->owner
= p
->module
;
2901 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
2903 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
2904 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
2905 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
2906 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
2907 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
2908 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
2910 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
2912 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
2913 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
2915 c
->rrobin_index
= PA_IDXSET_INVALID
;
2916 c
->subscription
= NULL
;
2918 pa_idxset_put(p
->connections
, c
, NULL
);
2921 if (pa_iochannel_creds_supported(io
))
2922 pa_iochannel_creds_enable(io
);
2927 /*** module entry points ***/
2929 static int load_key(pa_protocol_native
*p
, const char*fn
) {
2932 p
->auth_cookie_in_property
= 0;
2934 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
2935 pa_log_info("using already loaded auth cookie.");
2936 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2937 p
->auth_cookie_in_property
= 1;
2942 fn
= PA_NATIVE_COOKIE_FILE
;
2944 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
2947 pa_log_info("loading cookie from disk.");
2949 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
2950 p
->auth_cookie_in_property
= 1;
2955 static pa_protocol_native
* protocol_new_internal(pa_core
*c
, pa_module
*m
, pa_modargs
*ma
) {
2956 pa_protocol_native
*p
;
2963 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
2964 pa_log("auth-anonymous= expects a boolean argument.");
2968 p
= pa_xnew(pa_protocol_native
, 1);
2973 p
->auth_ip_acl
= NULL
;
2978 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &a
) < 0) {
2979 pa_log("auth-group-enabled= expects a boolean argument.");
2982 p
->auth_group
= a
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", c
->is_system_instance
? PA_ACCESS_GROUP
: NULL
)) : NULL
;
2985 pa_log_info("Allowing access to group '%s'.", p
->auth_group
);
2990 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
2992 if (!(p
->auth_ip_acl
= pa_ip_acl_new(acl
))) {
2993 pa_log("Failed to parse IP ACL '%s'", acl
);
2998 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0)
3001 p
->connections
= pa_idxset_new(NULL
, NULL
);
3007 pa_xfree(p
->auth_group
);
3010 pa_ip_acl_free(p
->auth_ip_acl
);
3015 pa_protocol_native
* pa_protocol_native_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
3017 pa_protocol_native
*p
;
3019 if (!(p
= protocol_new_internal(core
, m
, ma
)))
3023 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
3025 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
3027 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3028 l
= pa_strlist_prepend(l
, t
);
3029 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
3035 void pa_protocol_native_free(pa_protocol_native
*p
) {
3039 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
3040 connection_unlink(c
);
3041 pa_idxset_free(p
->connections
, NULL
, NULL
);
3046 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
3048 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3049 l
= pa_strlist_remove(l
, t
);
3052 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
3054 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
3057 pa_socket_server_unref(p
->server
);
3060 if (p
->auth_cookie_in_property
)
3061 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
3064 pa_ip_acl_free(p
->auth_ip_acl
);
3067 pa_xfree(p
->auth_group
);
3072 pa_protocol_native
* pa_protocol_native_new_iochannel(
3078 pa_protocol_native
*p
;
3080 if (!(p
= protocol_new_internal(core
, m
, ma
)))
3083 on_connection(NULL
, io
, p
);