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 */
74 #define DEFAULT_TLENGTH_MSEC 2000 /* 2s */
75 #define DEFAULT_PROCESS_MSEC 20 /* 20ms */
76 #define DEFAULT_FRAGSIZE_MSEC DEFAULT_TLENGTH_MSEC
78 typedef struct connection connection
;
79 struct pa_protocol_native
;
81 typedef struct record_stream
{
84 connection
*connection
;
87 pa_source_output
*source_output
;
88 pa_memblockq
*memblockq
;
90 pa_usec_t source_latency
;
93 typedef struct output_stream
{
97 typedef struct playback_stream
{
100 connection
*connection
;
103 pa_sink_input
*sink_input
;
104 pa_memblockq
*memblockq
;
105 pa_bool_t drain_request
;
111 pa_usec_t sink_latency
;
113 /* Only updated after SINK_INPUT_MESSAGE_UPDATE_LATENCY */
114 int64_t read_index
, write_index
;
115 size_t render_memblockq_length
;
118 typedef struct upload_stream
{
119 output_stream parent
;
121 connection
*connection
;
124 pa_memchunk memchunk
;
127 pa_sample_spec sample_spec
;
128 pa_channel_map channel_map
;
129 pa_proplist
*proplist
;
135 pa_bool_t authorized
:1;
136 pa_bool_t is_local
:1;
138 pa_protocol_native
*protocol
;
141 pa_pdispatch
*pdispatch
;
142 pa_idxset
*record_streams
, *output_streams
;
143 uint32_t rrobin_index
;
144 pa_subscription
*subscription
;
145 pa_time_event
*auth_timeout_event
;
148 PA_DECLARE_CLASS(record_stream
);
149 #define RECORD_STREAM(o) (record_stream_cast(o))
150 static PA_DEFINE_CHECK_TYPE(record_stream
, pa_msgobject
);
152 PA_DECLARE_CLASS(output_stream
);
153 #define OUTPUT_STREAM(o) (output_stream_cast(o))
154 static PA_DEFINE_CHECK_TYPE(output_stream
, pa_msgobject
);
156 PA_DECLARE_CLASS(playback_stream
);
157 #define PLAYBACK_STREAM(o) (playback_stream_cast(o))
158 static PA_DEFINE_CHECK_TYPE(playback_stream
, output_stream
);
160 PA_DECLARE_CLASS(upload_stream
);
161 #define UPLOAD_STREAM(o) (upload_stream_cast(o))
162 static PA_DEFINE_CHECK_TYPE(upload_stream
, output_stream
);
164 PA_DECLARE_CLASS(connection
);
165 #define CONNECTION(o) (connection_cast(o))
166 static PA_DEFINE_CHECK_TYPE(connection
, pa_msgobject
);
168 struct pa_protocol_native
{
172 pa_socket_server
*server
;
173 pa_idxset
*connections
;
174 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
175 pa_bool_t auth_cookie_in_property
;
179 pa_ip_acl
*auth_ip_acl
;
183 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
184 SINK_INPUT_MESSAGE_DRAIN
, /* disabled prebuf, get playback started. */
185 SINK_INPUT_MESSAGE_FLUSH
,
186 SINK_INPUT_MESSAGE_TRIGGER
,
187 SINK_INPUT_MESSAGE_SEEK
,
188 SINK_INPUT_MESSAGE_PREBUF_FORCE
,
189 SINK_INPUT_MESSAGE_UPDATE_LATENCY
193 PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, /* data requested from sink input from the main loop */
194 PLAYBACK_STREAM_MESSAGE_UNDERFLOW
,
195 PLAYBACK_STREAM_MESSAGE_OVERFLOW
,
196 PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
,
197 PLAYBACK_STREAM_MESSAGE_STARTED
201 RECORD_STREAM_MESSAGE_POST_DATA
/* data from source output to main loop */
205 CONNECTION_MESSAGE_RELEASE
,
206 CONNECTION_MESSAGE_REVOKE
209 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
);
210 static void sink_input_kill_cb(pa_sink_input
*i
);
211 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
);
212 static void sink_input_moved_cb(pa_sink_input
*i
);
213 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
214 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
217 static void send_memblock(connection
*c
);
218 static void request_bytes(struct playback_stream
*s
);
220 static void source_output_kill_cb(pa_source_output
*o
);
221 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
222 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
);
223 static void source_output_moved_cb(pa_source_output
*o
);
224 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
226 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
);
228 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
229 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
230 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
231 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
232 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
233 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
234 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
235 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
236 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
237 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
238 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
239 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
240 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
241 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
242 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
243 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
244 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
245 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
246 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
247 static void command_set_volume(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
248 static void command_set_mute(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
249 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
250 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
251 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
252 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
253 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
254 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
255 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
256 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
257 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
258 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
259 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
260 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
261 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
262 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
263 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
264 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
265 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
266 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
267 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
269 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
270 [PA_COMMAND_ERROR
] = NULL
,
271 [PA_COMMAND_TIMEOUT
] = NULL
,
272 [PA_COMMAND_REPLY
] = NULL
,
273 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
274 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
275 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
276 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
277 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
278 [PA_COMMAND_AUTH
] = command_auth
,
279 [PA_COMMAND_REQUEST
] = NULL
,
280 [PA_COMMAND_EXIT
] = command_exit
,
281 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
282 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
283 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
284 [PA_COMMAND_STAT
] = command_stat
,
285 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
286 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
287 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
288 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
289 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
290 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
291 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
292 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
293 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
294 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
295 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
296 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
297 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
298 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
299 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
300 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
301 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
302 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
303 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
304 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
305 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
306 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
307 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
309 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
310 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
311 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
313 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
314 [PA_COMMAND_SET_SINK_INPUT_MUTE
] = command_set_mute
,
315 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
317 [PA_COMMAND_SUSPEND_SINK
] = command_suspend
,
318 [PA_COMMAND_SUSPEND_SOURCE
] = command_suspend
,
320 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
321 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
322 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
323 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
325 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
326 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
328 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
329 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
330 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
331 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
332 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
333 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
334 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
335 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
336 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
337 [PA_COMMAND_GET_AUTOLOAD_INFO
] = command_get_autoload_info
,
338 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = command_get_autoload_info_list
,
339 [PA_COMMAND_ADD_AUTOLOAD
] = command_add_autoload
,
340 [PA_COMMAND_REMOVE_AUTOLOAD
] = command_remove_autoload
,
342 [PA_COMMAND_MOVE_SINK_INPUT
] = command_move_stream
,
343 [PA_COMMAND_MOVE_SOURCE_OUTPUT
] = command_move_stream
,
345 [PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
346 [PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
348 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
349 [PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
351 [PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
] = command_update_proplist
,
352 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
] = command_update_proplist
,
353 [PA_COMMAND_UPDATE_CLIENT_PROPLIST
] = command_update_proplist
,
355 [PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
] = command_remove_proplist
,
356 [PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
] = command_remove_proplist
,
357 [PA_COMMAND_REMOVE_CLIENT_PROPLIST
] = command_remove_proplist
,
360 /* structure management */
362 static void upload_stream_unlink(upload_stream
*s
) {
368 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
369 s
->connection
= NULL
;
370 upload_stream_unref(s
);
373 static void upload_stream_free(pa_object
*o
) {
374 upload_stream
*s
= UPLOAD_STREAM(o
);
377 upload_stream_unlink(s
);
382 pa_proplist_free(s
->proplist
);
384 if (s
->memchunk
.memblock
)
385 pa_memblock_unref(s
->memchunk
.memblock
);
390 static upload_stream
* upload_stream_new(
392 const pa_sample_spec
*ss
,
393 const pa_channel_map
*map
,
403 pa_assert(length
> 0);
406 s
= pa_msgobject_new(upload_stream
);
407 s
->parent
.parent
.parent
.free
= upload_stream_free
;
409 s
->sample_spec
= *ss
;
410 s
->channel_map
= *map
;
411 s
->name
= pa_xstrdup(name
);
412 pa_memchunk_reset(&s
->memchunk
);
414 s
->proplist
= pa_proplist_copy(p
);
415 pa_proplist_update(s
->proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
417 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
422 static void record_stream_unlink(record_stream
*s
) {
428 if (s
->source_output
) {
429 pa_source_output_unlink(s
->source_output
);
430 pa_source_output_unref(s
->source_output
);
431 s
->source_output
= NULL
;
434 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->record_streams
, s
, NULL
) == s
);
435 s
->connection
= NULL
;
436 record_stream_unref(s
);
439 static void record_stream_free(pa_object
*o
) {
440 record_stream
*s
= RECORD_STREAM(o
);
443 record_stream_unlink(s
);
445 pa_memblockq_free(s
->memblockq
);
449 static int record_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
450 record_stream
*s
= RECORD_STREAM(o
);
451 record_stream_assert_ref(s
);
458 case RECORD_STREAM_MESSAGE_POST_DATA
:
460 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
461 /* pa_log_warn("Failed to push data into output queue."); */
465 if (!pa_pstream_is_pending(s
->connection
->pstream
))
466 send_memblock(s
->connection
);
474 static void fix_record_buffer_attr_pre(record_stream
*s
, pa_bool_t adjust_latency
, uint32_t *maxlength
, uint32_t *fragsize
) {
476 pa_assert(maxlength
);
479 if (*maxlength
<= 0 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
480 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
483 *fragsize
= pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC
*PA_USEC_PER_MSEC
, &s
->source_output
->sample_spec
);
485 if (adjust_latency
) {
486 pa_usec_t fragsize_usec
;
488 /* So, the user asked us to adjust the latency according to
489 * what the source can provide. Half the latency will be
490 * spent on the hw buffer, half of it in the async buffer
491 * queue we maintain for each client. */
493 fragsize_usec
= pa_bytes_to_usec(*fragsize
, &s
->source_output
->sample_spec
);
495 s
->source_latency
= pa_source_output_set_requested_latency(s
->source_output
, fragsize_usec
/2);
497 if (fragsize_usec
>= s
->source_latency
*2)
498 fragsize_usec
-= s
->source_latency
;
500 fragsize_usec
= s
->source_latency
;
502 *fragsize
= pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
);
504 s
->source_latency
= 0;
507 static void fix_record_buffer_attr_post(record_stream
*s
, uint32_t *maxlength
, uint32_t *fragsize
) {
511 pa_assert(maxlength
);
514 *maxlength
= pa_memblockq_get_maxlength(s
->memblockq
);
516 base
= pa_frame_size(&s
->source_output
->sample_spec
);
518 s
->fragment_size
= (*fragsize
/base
)*base
;
519 if (s
->fragment_size
<= 0)
520 s
->fragment_size
= base
;
522 if (s
->fragment_size
> *maxlength
)
523 s
->fragment_size
= *maxlength
;
525 *fragsize
= s
->fragment_size
;
528 static record_stream
* record_stream_new(
533 pa_bool_t peak_detect
,
536 pa_source_output_flags_t flags
,
538 pa_bool_t adjust_latency
,
539 pa_sink_input
*direct_on_input
) {
542 pa_source_output
*source_output
;
544 pa_source_output_new_data data
;
548 pa_assert(maxlength
);
551 pa_source_output_new_data_init(&data
);
553 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
554 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
555 data
.driver
= __FILE__
;
556 data
.module
= c
->protocol
->module
;
557 data
.client
= c
->client
;
558 data
.source
= source
;
559 data
.direct_on_input
= direct_on_input
;
560 pa_source_output_new_data_set_sample_spec(&data
, ss
);
561 pa_source_output_new_data_set_channel_map(&data
, map
);
563 data
.resample_method
= PA_RESAMPLER_PEAKS
;
565 source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
);
567 pa_source_output_new_data_done(&data
);
572 s
= pa_msgobject_new(record_stream
);
573 s
->parent
.parent
.free
= record_stream_free
;
574 s
->parent
.process_msg
= record_stream_process_msg
;
576 s
->source_output
= source_output
;
578 s
->source_output
->push
= source_output_push_cb
;
579 s
->source_output
->kill
= source_output_kill_cb
;
580 s
->source_output
->get_latency
= source_output_get_latency_cb
;
581 s
->source_output
->moved
= source_output_moved_cb
;
582 s
->source_output
->suspend
= source_output_suspend_cb
;
583 s
->source_output
->userdata
= s
;
585 fix_record_buffer_attr_pre(s
, adjust_latency
, maxlength
, fragsize
);
587 s
->memblockq
= pa_memblockq_new(
591 base
= pa_frame_size(&source_output
->sample_spec
),
597 fix_record_buffer_attr_post(s
, maxlength
, fragsize
);
599 *ss
= s
->source_output
->sample_spec
;
600 *map
= s
->source_output
->channel_map
;
602 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
604 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
605 ((double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) + (double) s
->source_latency
) / PA_USEC_PER_MSEC
,
606 (double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) / PA_USEC_PER_MSEC
,
607 (double) s
->source_latency
/ PA_USEC_PER_MSEC
);
609 pa_source_output_put(s
->source_output
);
613 static void playback_stream_unlink(playback_stream
*s
) {
620 pa_sink_input_unlink(s
->sink_input
);
621 pa_sink_input_unref(s
->sink_input
);
622 s
->sink_input
= NULL
;
625 if (s
->drain_request
)
626 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
628 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
629 s
->connection
= NULL
;
630 playback_stream_unref(s
);
633 static void playback_stream_free(pa_object
* o
) {
634 playback_stream
*s
= PLAYBACK_STREAM(o
);
637 playback_stream_unlink(s
);
639 pa_memblockq_free(s
->memblockq
);
643 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
644 playback_stream
*s
= PLAYBACK_STREAM(o
);
645 playback_stream_assert_ref(s
);
651 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
656 if ((l
= pa_atomic_load(&s
->missing
)) <= 0)
659 if (pa_atomic_cmpxchg(&s
->missing
, l
, 0))
666 t
= pa_tagstruct_new(NULL
, 0);
667 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
668 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
669 pa_tagstruct_putu32(t
, s
->index
);
670 pa_tagstruct_putu32(t
, l
);
671 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
673 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
677 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
680 /* Report that we're empty */
681 t
= pa_tagstruct_new(NULL
, 0);
682 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
683 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
684 pa_tagstruct_putu32(t
, s
->index
);
685 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
689 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
692 /* Notify the user we're overflowed*/
693 t
= pa_tagstruct_new(NULL
, 0);
694 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
695 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
696 pa_tagstruct_putu32(t
, s
->index
);
697 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
701 case PLAYBACK_STREAM_MESSAGE_STARTED
:
703 if (s
->connection
->version
>= 13) {
706 /* Notify the user we're overflowed*/
707 t
= pa_tagstruct_new(NULL
, 0);
708 pa_tagstruct_putu32(t
, PA_COMMAND_STARTED
);
709 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
710 pa_tagstruct_putu32(t
, s
->index
);
711 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
716 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
717 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
724 static void fix_playback_buffer_attr_pre(playback_stream
*s
, pa_bool_t adjust_latency
, uint32_t *maxlength
, uint32_t *tlength
, uint32_t* prebuf
, uint32_t* minreq
) {
726 pa_usec_t tlength_usec
, minreq_usec
, sink_usec
;
729 pa_assert(maxlength
);
734 if (*maxlength
<= 0 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
735 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
737 *tlength
= pa_usec_to_bytes(DEFAULT_TLENGTH_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
739 *minreq
= pa_usec_to_bytes(DEFAULT_PROCESS_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
741 frame_size
= pa_frame_size(&s
->sink_input
->sample_spec
);
743 *minreq
= frame_size
;
744 if (*tlength
< *minreq
+frame_size
)
745 *tlength
= *minreq
+frame_size
;
747 tlength_usec
= pa_bytes_to_usec(*tlength
, &s
->sink_input
->sample_spec
);
748 minreq_usec
= pa_bytes_to_usec(*minreq
, &s
->sink_input
->sample_spec
);
750 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
751 (double) tlength_usec
/ PA_USEC_PER_MSEC
,
752 (double) minreq_usec
/ PA_USEC_PER_MSEC
);
754 if (adjust_latency
) {
756 /* So, the user asked us to adjust the latency of the stream
757 * buffer according to the what the sink can provide. The
758 * tlength passed in shall be the overall latency. Roughly
759 * half the latency will be spent on the hw buffer, the other
760 * half of it in the async buffer queue we maintain for each
761 * client. In between we'll have a safety space of size
762 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
763 * empty and needs to be filled, then our buffer must have
764 * enough data to fulfill this request immediatly and thus
765 * have at least the same tlength as the size of the hw
766 * buffer. It additionally needs space for 2 times minreq
767 * because if the buffer ran empty and a partial fillup
768 * happens immediately on the next iteration we need to be
769 * able to fulfill it and give the application also minreq
770 * time to fill it up again for the next request Makes 2 times
771 * minreq in plus.. */
773 if (tlength_usec
> minreq_usec
*2)
774 sink_usec
= (tlength_usec
- minreq_usec
*2)/2;
780 /* Ok, the user didn't ask us to adjust the latency, but we
781 * still need to make sure that the parameters from the user
784 if (tlength_usec
> minreq_usec
*2)
785 sink_usec
= (tlength_usec
- minreq_usec
*2);
790 s
->sink_latency
= pa_sink_input_set_requested_latency(s
->sink_input
, sink_usec
);
792 if (adjust_latency
) {
793 /* Ok, we didn't necessarily get what we were asking for, so
794 * let's subtract from what we asked for for the remaining
797 if (tlength_usec
>= s
->sink_latency
)
798 tlength_usec
-= s
->sink_latency
;
801 if (tlength_usec
< s
->sink_latency
+ 2*minreq_usec
)
802 tlength_usec
= s
->sink_latency
+ 2*minreq_usec
;
804 *tlength
= pa_usec_to_bytes(tlength_usec
, &s
->sink_input
->sample_spec
);
805 *minreq
= pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
);
808 *minreq
+= frame_size
;
809 *tlength
+= frame_size
*2;
812 if (*tlength
<= *minreq
)
813 *tlength
= *minreq
*2 + frame_size
;
819 static void fix_playback_buffer_attr_post(playback_stream
*s
, uint32_t *maxlength
, uint32_t *tlength
, uint32_t* prebuf
, uint32_t* minreq
) {
821 pa_assert(maxlength
);
826 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
827 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
828 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
829 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
834 static playback_stream
* playback_stream_new(
847 pa_sink_input_flags_t flags
,
849 pa_bool_t adjust_latency
) {
851 playback_stream
*s
, *ssync
;
852 pa_sink_input
*sink_input
;
856 pa_sink_input_new_data data
;
860 pa_assert(maxlength
);
868 /* Find syncid group */
869 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
871 if (!playback_stream_isinstance(ssync
))
874 if (ssync
->syncid
== syncid
)
878 /* Synced streams must connect to the same sink */
882 sink
= ssync
->sink_input
->sink
;
883 else if (sink
!= ssync
->sink_input
->sink
)
887 pa_sink_input_new_data_init(&data
);
889 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
890 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
891 data
.driver
= __FILE__
;
892 data
.module
= c
->protocol
->module
;
893 data
.client
= c
->client
;
895 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
896 pa_sink_input_new_data_set_channel_map(&data
, map
);
897 pa_sink_input_new_data_set_volume(&data
, volume
);
898 pa_sink_input_new_data_set_muted(&data
, muted
);
899 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
901 sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
);
903 pa_sink_input_new_data_done(&data
);
908 s
= pa_msgobject_new(playback_stream
);
909 s
->parent
.parent
.parent
.free
= playback_stream_free
;
910 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
913 s
->sink_input
= sink_input
;
915 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
916 s
->sink_input
->pop
= sink_input_pop_cb
;
917 s
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
918 s
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
919 s
->sink_input
->kill
= sink_input_kill_cb
;
920 s
->sink_input
->moved
= sink_input_moved_cb
;
921 s
->sink_input
->suspend
= sink_input_suspend_cb
;
922 s
->sink_input
->userdata
= s
;
924 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
926 fix_playback_buffer_attr_pre(s
, adjust_latency
, maxlength
, tlength
, prebuf
, minreq
);
927 pa_sink_input_get_silence(sink_input
, &silence
);
929 s
->memblockq
= pa_memblockq_new(
933 pa_frame_size(&sink_input
->sample_spec
),
939 pa_memblock_unref(silence
.memblock
);
940 fix_playback_buffer_attr_post(s
, maxlength
, tlength
, prebuf
, minreq
);
942 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
944 *ss
= s
->sink_input
->sample_spec
;
945 *map
= s
->sink_input
->channel_map
;
947 pa_atomic_store(&s
->missing
, 0);
948 s
->drain_request
= FALSE
;
950 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
952 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
953 ((double) pa_bytes_to_usec(*tlength
, &sink_input
->sample_spec
) + (double) s
->sink_latency
) / PA_USEC_PER_MSEC
,
954 (double) pa_bytes_to_usec(*tlength
-*minreq
*2, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
955 (double) pa_bytes_to_usec(*minreq
, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
956 (double) s
->sink_latency
/ PA_USEC_PER_MSEC
);
958 pa_sink_input_put(s
->sink_input
);
962 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
963 connection
*c
= CONNECTION(o
);
964 connection_assert_ref(c
);
971 case CONNECTION_MESSAGE_REVOKE
:
972 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
975 case CONNECTION_MESSAGE_RELEASE
:
976 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
983 static void connection_unlink(connection
*c
) {
992 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
993 record_stream_unlink(r
);
995 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
996 if (playback_stream_isinstance(o
))
997 playback_stream_unlink(PLAYBACK_STREAM(o
));
999 upload_stream_unlink(UPLOAD_STREAM(o
));
1001 if (c
->subscription
)
1002 pa_subscription_free(c
->subscription
);
1005 pa_pstream_unlink(c
->pstream
);
1007 if (c
->auth_timeout_event
) {
1008 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1009 c
->auth_timeout_event
= NULL
;
1012 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
1014 connection_unref(c
);
1017 static void connection_free(pa_object
*o
) {
1018 connection
*c
= CONNECTION(o
);
1022 connection_unlink(c
);
1024 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
1025 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
1027 pa_pdispatch_unref(c
->pdispatch
);
1028 pa_pstream_unref(c
->pstream
);
1029 pa_client_free(c
->client
);
1034 /* Called from thread context */
1035 static void request_bytes(playback_stream
*s
) {
1036 size_t m
, previous_missing
;
1038 playback_stream_assert_ref(s
);
1040 m
= pa_memblockq_pop_missing(s
->memblockq
);
1045 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
1047 previous_missing
= pa_atomic_add(&s
->missing
, m
);
1049 if (pa_memblockq_prebuf_active(s
->memblockq
) ||
1050 (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
))
1051 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1054 static void send_memblock(connection
*c
) {
1058 start
= PA_IDXSET_INVALID
;
1062 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
1065 if (start
== PA_IDXSET_INVALID
)
1066 start
= c
->rrobin_index
;
1067 else if (start
== c
->rrobin_index
)
1070 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
1071 pa_memchunk schunk
= chunk
;
1073 if (schunk
.length
> r
->fragment_size
)
1074 schunk
.length
= r
->fragment_size
;
1076 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
1078 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
1079 pa_memblock_unref(schunk
.memblock
);
1086 static void send_playback_stream_killed(playback_stream
*p
) {
1088 playback_stream_assert_ref(p
);
1090 t
= pa_tagstruct_new(NULL
, 0);
1091 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
1092 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1093 pa_tagstruct_putu32(t
, p
->index
);
1094 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
1097 static void send_record_stream_killed(record_stream
*r
) {
1099 record_stream_assert_ref(r
);
1101 t
= pa_tagstruct_new(NULL
, 0);
1102 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
1103 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1104 pa_tagstruct_putu32(t
, r
->index
);
1105 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
1108 /*** sink input callbacks ***/
1110 static void handle_seek(playback_stream
*s
, int64_t indexw
) {
1111 playback_stream_assert_ref(s
);
1113 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1115 if (s
->sink_input
->thread_info
.underrun_for
> 0) {
1117 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1119 if (pa_memblockq_is_readable(s
->memblockq
)) {
1121 /* We just ended an underrun, let's ask the sink
1122 * for a complete rewind rewrite */
1124 pa_log_debug("Requesting rewind due to end of underrun.");
1125 pa_sink_input_request_rewind(s
->sink_input
,
1126 s
->sink_input
->thread_info
.underrun_for
== (size_t) -1 ? 0 : s
->sink_input
->thread_info
.underrun_for
,
1133 indexr
= pa_memblockq_get_read_index(s
->memblockq
);
1135 if (indexw
< indexr
) {
1136 /* OK, the sink already asked for this data, so
1137 * let's have it usk us again */
1139 pa_log_debug("Requesting rewind due to rewrite.");
1140 pa_sink_input_request_rewind(s
->sink_input
, indexr
- indexw
, TRUE
, FALSE
);
1147 /* Called from thread context */
1148 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1149 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1152 pa_sink_input_assert_ref(i
);
1153 s
= PLAYBACK_STREAM(i
->userdata
);
1154 playback_stream_assert_ref(s
);
1158 case SINK_INPUT_MESSAGE_SEEK
: {
1161 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1162 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
1164 handle_seek(s
, windex
);
1168 case SINK_INPUT_MESSAGE_POST_DATA
: {
1173 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1175 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1177 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
1178 pa_log_warn("Failed to push data into queue");
1179 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
1180 pa_memblockq_seek(s
->memblockq
, chunk
->length
, PA_SEEK_RELATIVE
);
1183 handle_seek(s
, windex
);
1185 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1190 case SINK_INPUT_MESSAGE_DRAIN
:
1191 case SINK_INPUT_MESSAGE_FLUSH
:
1192 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1193 case SINK_INPUT_MESSAGE_TRIGGER
: {
1196 pa_sink_input
*isync
;
1197 void (*func
)(pa_memblockq
*bq
);
1200 case SINK_INPUT_MESSAGE_FLUSH
:
1201 func
= pa_memblockq_flush
;
1204 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1205 func
= pa_memblockq_prebuf_force
;
1208 case SINK_INPUT_MESSAGE_DRAIN
:
1209 case SINK_INPUT_MESSAGE_TRIGGER
:
1210 func
= pa_memblockq_prebuf_disable
;
1214 pa_assert_not_reached();
1217 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1219 handle_seek(s
, windex
);
1221 /* Do the same for all other members in the sync group */
1222 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
1223 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1224 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1225 func(ssync
->memblockq
);
1226 handle_seek(ssync
, windex
);
1229 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1230 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1231 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1232 func(ssync
->memblockq
);
1233 handle_seek(ssync
, windex
);
1236 if (code
== SINK_INPUT_MESSAGE_DRAIN
) {
1237 if (!pa_memblockq_is_readable(s
->memblockq
))
1238 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
1240 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
1241 s
->drain_request
= TRUE
;
1248 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1250 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1251 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1252 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1255 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1258 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1260 pa_memblockq_prebuf_force(s
->memblockq
);
1262 handle_seek(s
, windex
);
1264 /* Fall through to the default handler */
1268 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1269 pa_usec_t
*r
= userdata
;
1271 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1273 /* Fall through, the default handler will add in the extra
1274 * latency added by the resampler */
1279 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1282 /* Called from thread context */
1283 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
1286 pa_sink_input_assert_ref(i
);
1287 s
= PLAYBACK_STREAM(i
->userdata
);
1288 playback_stream_assert_ref(s
);
1291 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0) {
1293 /* pa_log("UNDERRUN: %lu", pa_memblockq_get_length(s->memblockq)); */
1295 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1296 s
->drain_request
= FALSE
;
1297 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
);
1298 } else if (i
->thread_info
.playing_for
> 0)
1299 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1301 /* pa_log("adding %llu bytes", (unsigned long long) nbytes); */
1308 /* pa_log("NOTUNDERRUN %lu", (unsigned long) chunk->length); */
1310 chunk
->length
= PA_MIN(nbytes
, chunk
->length
);
1312 if (i
->thread_info
.underrun_for
> 0)
1313 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_STARTED
, NULL
, 0, NULL
, NULL
);
1315 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1321 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1324 pa_sink_input_assert_ref(i
);
1325 s
= PLAYBACK_STREAM(i
->userdata
);
1326 playback_stream_assert_ref(s
);
1328 /* If we are in an underrun, then we don't rewind */
1329 if (i
->thread_info
.underrun_for
> 0)
1332 pa_memblockq_rewind(s
->memblockq
, nbytes
);
1335 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1338 pa_sink_input_assert_ref(i
);
1339 s
= PLAYBACK_STREAM(i
->userdata
);
1340 playback_stream_assert_ref(s
);
1342 pa_memblockq_set_maxrewind(s
->memblockq
, nbytes
);
1345 /* Called from main context */
1346 static void sink_input_kill_cb(pa_sink_input
*i
) {
1349 pa_sink_input_assert_ref(i
);
1350 s
= PLAYBACK_STREAM(i
->userdata
);
1351 playback_stream_assert_ref(s
);
1353 send_playback_stream_killed(s
);
1354 playback_stream_unlink(s
);
1357 /* Called from main context */
1358 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1362 pa_sink_input_assert_ref(i
);
1363 s
= PLAYBACK_STREAM(i
->userdata
);
1364 playback_stream_assert_ref(s
);
1366 if (s
->connection
->version
< 12)
1369 t
= pa_tagstruct_new(NULL
, 0);
1370 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1371 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1372 pa_tagstruct_putu32(t
, s
->index
);
1373 pa_tagstruct_put_boolean(t
, suspend
);
1374 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1377 /* Called from main context */
1378 static void sink_input_moved_cb(pa_sink_input
*i
) {
1381 uint32_t maxlength
, tlength
, prebuf
, minreq
;
1383 pa_sink_input_assert_ref(i
);
1384 s
= PLAYBACK_STREAM(i
->userdata
);
1385 playback_stream_assert_ref(s
);
1387 maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
1388 tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
1389 prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
1390 minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
1392 fix_playback_buffer_attr_pre(s
, TRUE
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1393 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1394 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1395 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
1396 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
1397 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1399 if (s
->connection
->version
< 12)
1402 t
= pa_tagstruct_new(NULL
, 0);
1403 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1404 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1405 pa_tagstruct_putu32(t
, s
->index
);
1406 pa_tagstruct_putu32(t
, i
->sink
->index
);
1407 pa_tagstruct_puts(t
, i
->sink
->name
);
1408 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1410 if (s
->connection
->version
>= 13) {
1411 pa_tagstruct_putu32(t
, maxlength
);
1412 pa_tagstruct_putu32(t
, tlength
);
1413 pa_tagstruct_putu32(t
, prebuf
);
1414 pa_tagstruct_putu32(t
, minreq
);
1415 pa_tagstruct_put_usec(t
, s
->sink_latency
);
1418 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1421 /*** source_output callbacks ***/
1423 /* Called from thread context */
1424 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1427 pa_source_output_assert_ref(o
);
1428 s
= RECORD_STREAM(o
->userdata
);
1429 record_stream_assert_ref(s
);
1432 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1435 static void source_output_kill_cb(pa_source_output
*o
) {
1438 pa_source_output_assert_ref(o
);
1439 s
= RECORD_STREAM(o
->userdata
);
1440 record_stream_assert_ref(s
);
1442 send_record_stream_killed(s
);
1443 record_stream_unlink(s
);
1446 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1449 pa_source_output_assert_ref(o
);
1450 s
= RECORD_STREAM(o
->userdata
);
1451 record_stream_assert_ref(s
);
1453 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1455 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1458 /* Called from main context */
1459 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1463 pa_source_output_assert_ref(o
);
1464 s
= RECORD_STREAM(o
->userdata
);
1465 record_stream_assert_ref(s
);
1467 if (s
->connection
->version
< 12)
1470 t
= pa_tagstruct_new(NULL
, 0);
1471 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1472 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1473 pa_tagstruct_putu32(t
, s
->index
);
1474 pa_tagstruct_put_boolean(t
, suspend
);
1475 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1478 /* Called from main context */
1479 static void source_output_moved_cb(pa_source_output
*o
) {
1482 uint32_t maxlength
, fragsize
;
1484 pa_source_output_assert_ref(o
);
1485 s
= RECORD_STREAM(o
->userdata
);
1486 record_stream_assert_ref(s
);
1488 fragsize
= (uint32_t) s
->fragment_size
;
1489 maxlength
= (uint32_t) pa_memblockq_get_length(s
->memblockq
);
1491 fix_record_buffer_attr_pre(s
, TRUE
, &maxlength
, &fragsize
);
1492 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1493 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
1495 if (s
->connection
->version
< 12)
1498 t
= pa_tagstruct_new(NULL
, 0);
1499 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1500 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1501 pa_tagstruct_putu32(t
, s
->index
);
1502 pa_tagstruct_putu32(t
, o
->source
->index
);
1503 pa_tagstruct_puts(t
, o
->source
->name
);
1504 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1506 if (s
->connection
->version
>= 13) {
1507 pa_tagstruct_putu32(t
, maxlength
);
1508 pa_tagstruct_putu32(t
, fragsize
);
1509 pa_tagstruct_put_usec(t
, s
->source_latency
);
1512 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1515 /*** pdispatch callbacks ***/
1517 static void protocol_error(connection
*c
) {
1518 pa_log("protocol error, kicking client");
1519 connection_unlink(c
);
1522 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1523 if (!(expression)) { \
1524 pa_pstream_send_error((pstream), (tag), (error)); \
1529 static pa_tagstruct
*reply_new(uint32_t tag
) {
1530 pa_tagstruct
*reply
;
1532 reply
= pa_tagstruct_new(NULL
, 0);
1533 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1534 pa_tagstruct_putu32(reply
, tag
);
1538 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
) {
1539 connection
*c
= CONNECTION(userdata
);
1541 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1542 const char *name
= NULL
, *sink_name
;
1545 pa_tagstruct
*reply
;
1546 pa_sink
*sink
= NULL
;
1554 fix_channels
= FALSE
,
1556 variable_rate
= FALSE
,
1558 adjust_latency
= FALSE
;
1560 pa_sink_input_flags_t flags
= 0;
1563 connection_assert_ref(c
);
1566 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1569 PA_TAG_SAMPLE_SPEC
, &ss
,
1570 PA_TAG_CHANNEL_MAP
, &map
,
1571 PA_TAG_U32
, &sink_index
,
1572 PA_TAG_STRING
, &sink_name
,
1573 PA_TAG_U32
, &maxlength
,
1574 PA_TAG_BOOLEAN
, &corked
,
1575 PA_TAG_U32
, &tlength
,
1576 PA_TAG_U32
, &prebuf
,
1577 PA_TAG_U32
, &minreq
,
1578 PA_TAG_U32
, &syncid
,
1579 PA_TAG_CVOLUME
, &volume
,
1580 PA_TAG_INVALID
) < 0) {
1586 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1587 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(sink_name
)), tag
, PA_ERR_INVALID
);
1588 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1589 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1590 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1591 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1593 p
= pa_proplist_new();
1596 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1598 if (c
->version
>= 12) {
1599 /* Since 0.9.8 the user can ask for a couple of additional flags */
1601 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1602 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1603 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1604 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1605 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1606 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1607 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1610 pa_proplist_free(p
);
1615 if (c
->version
>= 13) {
1617 if (pa_tagstruct_get_boolean(t
, &muted
) < 0 ||
1618 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1619 pa_tagstruct_get_proplist(t
, p
) < 0) {
1621 pa_proplist_free(p
);
1626 if (!pa_tagstruct_eof(t
)) {
1628 pa_proplist_free(p
);
1632 if (sink_index
!= PA_INVALID_INDEX
) {
1634 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1635 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1636 pa_proplist_free(p
);
1640 } else if (sink_name
) {
1642 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1))) {
1643 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1644 pa_proplist_free(p
);
1650 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1651 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1652 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1653 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1654 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1655 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1656 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1657 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0);
1659 s
= playback_stream_new(c
, sink
, &ss
, &map
, &maxlength
, &tlength
, &prebuf
, &minreq
, &volume
, muted
, syncid
, &missing
, flags
, p
, adjust_latency
);
1660 pa_proplist_free(p
);
1662 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1664 reply
= reply_new(tag
);
1665 pa_tagstruct_putu32(reply
, s
->index
);
1666 pa_assert(s
->sink_input
);
1667 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1668 pa_tagstruct_putu32(reply
, missing
);
1670 /* pa_log("initial request is %u", missing); */
1672 if (c
->version
>= 9) {
1673 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1675 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1676 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1677 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1678 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1681 if (c
->version
>= 12) {
1682 /* Since 0.9.8 we support sending the chosen sample
1683 * spec/channel map/device/suspend status back to the
1686 pa_tagstruct_put_sample_spec(reply
, &ss
);
1687 pa_tagstruct_put_channel_map(reply
, &map
);
1689 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1690 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1692 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1695 if (c
->version
>= 13)
1696 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
1698 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1701 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1702 connection
*c
= CONNECTION(userdata
);
1705 connection_assert_ref(c
);
1708 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1709 !pa_tagstruct_eof(t
)) {
1714 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1718 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1720 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1721 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1725 playback_stream_unlink(s
);
1729 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1731 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1732 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1736 record_stream_unlink(s
);
1740 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1743 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1744 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1748 upload_stream_unlink(s
);
1753 pa_assert_not_reached();
1756 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1759 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
) {
1760 connection
*c
= CONNECTION(userdata
);
1762 uint32_t maxlength
, fragment_size
;
1763 uint32_t source_index
;
1764 const char *name
= NULL
, *source_name
;
1767 pa_tagstruct
*reply
;
1768 pa_source
*source
= NULL
;
1775 fix_channels
= FALSE
,
1777 variable_rate
= FALSE
,
1778 adjust_latency
= FALSE
,
1779 peak_detect
= FALSE
;
1780 pa_source_output_flags_t flags
= 0;
1782 uint32_t direct_on_input_idx
= PA_INVALID_INDEX
;
1783 pa_sink_input
*direct_on_input
= NULL
;
1785 connection_assert_ref(c
);
1788 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1789 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1790 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1791 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1792 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1793 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1794 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1795 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1800 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1801 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1802 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1803 CHECK_VALIDITY(c
->pstream
, source_index
!= PA_INVALID_INDEX
|| !source_name
|| (*source_name
&& pa_utf8_valid(source_name
)), tag
, PA_ERR_INVALID
);
1804 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1806 p
= pa_proplist_new();
1809 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1811 if (c
->version
>= 12) {
1812 /* Since 0.9.8 the user can ask for a couple of additional flags */
1814 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1815 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1816 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1817 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1818 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1819 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1820 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1823 pa_proplist_free(p
);
1828 if (c
->version
>= 13) {
1830 if (pa_tagstruct_get_boolean(t
, &peak_detect
) < 0 ||
1831 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1832 pa_tagstruct_get_proplist(t
, p
) < 0 ||
1833 pa_tagstruct_getu32(t
, &direct_on_input_idx
) < 0) {
1835 pa_proplist_free(p
);
1840 if (!pa_tagstruct_eof(t
)) {
1842 pa_proplist_free(p
);
1846 if (source_index
!= PA_INVALID_INDEX
) {
1848 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
1849 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1850 pa_proplist_free(p
);
1854 } else if (source_name
) {
1856 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1))) {
1857 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1858 pa_proplist_free(p
);
1863 if (direct_on_input_idx
!= PA_INVALID_INDEX
) {
1865 if (!(direct_on_input
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, direct_on_input_idx
))) {
1866 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1867 pa_proplist_free(p
);
1873 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
1874 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
1875 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
1876 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
1877 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
1878 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
1879 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
1880 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0);
1882 s
= record_stream_new(c
, source
, &ss
, &map
, peak_detect
, &maxlength
, &fragment_size
, flags
, p
, adjust_latency
, direct_on_input
);
1883 pa_proplist_free(p
);
1885 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1887 reply
= reply_new(tag
);
1888 pa_tagstruct_putu32(reply
, s
->index
);
1889 pa_assert(s
->source_output
);
1890 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
1892 if (c
->version
>= 9) {
1893 /* Since 0.9 we support sending the buffer metrics back to the client */
1895 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1896 pa_tagstruct_putu32(reply
, (uint32_t) fragment_size
);
1899 if (c
->version
>= 12) {
1900 /* Since 0.9.8 we support sending the chosen sample
1901 * spec/channel map/device/suspend status back to the
1904 pa_tagstruct_put_sample_spec(reply
, &ss
);
1905 pa_tagstruct_put_channel_map(reply
, &map
);
1907 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
1908 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
1910 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
1913 if (c
->version
>= 13)
1914 pa_tagstruct_put_usec(reply
, s
->source_latency
);
1916 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1919 static void command_exit(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1920 connection
*c
= CONNECTION(userdata
);
1922 connection_assert_ref(c
);
1925 if (!pa_tagstruct_eof(t
)) {
1930 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1932 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
1933 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
1936 static void command_auth(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1937 connection
*c
= CONNECTION(userdata
);
1939 pa_tagstruct
*reply
;
1940 pa_bool_t shm_on_remote
, do_shm
;
1942 connection_assert_ref(c
);
1945 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
1946 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
1947 !pa_tagstruct_eof(t
)) {
1952 /* Minimum supported version */
1953 if (c
->version
< 8) {
1954 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
1958 /* Starting with protocol version 13 the MSB of the version tag
1959 reflects if shm is available for this connection or
1961 if (c
->version
>= 13) {
1962 shm_on_remote
= !!(c
->version
& 0x80000000U
);
1963 c
->version
&= 0x7FFFFFFFU
;
1966 pa_log_debug("Protocol version: remote %u, local %u", c
->version
, PA_PROTOCOL_VERSION
);
1968 pa_proplist_setf(c
->client
->proplist
, "native-protocol.version", "%u", c
->version
);
1970 if (!c
->authorized
) {
1971 pa_bool_t success
= FALSE
;
1974 const pa_creds
*creds
;
1976 if ((creds
= pa_pdispatch_creds(pd
))) {
1977 if (creds
->uid
== getuid())
1979 else if (c
->protocol
->auth_group
) {
1983 if ((gid
= pa_get_gid_of_group(c
->protocol
->auth_group
)) == (gid_t
) -1)
1984 pa_log_warn("Failed to get GID of group '%s'", c
->protocol
->auth_group
);
1985 else if (gid
== creds
->gid
)
1989 if ((r
= pa_uid_in_group(creds
->uid
, c
->protocol
->auth_group
)) < 0)
1990 pa_log_warn("Failed to check group membership.");
1996 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
1997 (unsigned long) creds
->uid
,
1998 (unsigned long) creds
->gid
,
2003 if (!success
&& memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
2007 pa_log_warn("Denied access to client with invalid authorization data.");
2008 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2012 c
->authorized
= TRUE
;
2013 if (c
->auth_timeout_event
) {
2014 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
2015 c
->auth_timeout_event
= NULL
;
2019 /* Enable shared memory support if possible */
2021 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
2024 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm
));
2027 if (c
->version
< 10 || (c
->version
>= 13 && !shm_on_remote
))
2031 /* Only enable SHM if both sides are owned by the same
2032 * user. This is a security measure because otherwise data
2033 * private to the user might leak. */
2035 const pa_creds
*creds
;
2036 if (!(creds
= pa_pdispatch_creds(pd
)) || getuid() != creds
->uid
)
2040 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm
));
2041 pa_pstream_enable_shm(c
->pstream
, do_shm
);
2043 reply
= reply_new(tag
);
2044 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
| (do_shm
? 0x80000000 : 0));
2048 /* SHM support is only enabled after both sides made sure they are the same user. */
2052 ucred
.uid
= getuid();
2053 ucred
.gid
= getgid();
2055 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
2058 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2062 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
) {
2063 connection
*c
= CONNECTION(userdata
);
2064 const char *name
= NULL
;
2066 pa_tagstruct
*reply
;
2068 connection_assert_ref(c
);
2071 p
= pa_proplist_new();
2073 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
2074 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2075 !pa_tagstruct_eof(t
)) {
2078 pa_proplist_free(p
);
2083 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
2084 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2085 pa_proplist_free(p
);
2089 pa_proplist_update(c
->client
->proplist
, PA_UPDATE_REPLACE
, p
);
2090 pa_proplist_free(p
);
2092 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2094 reply
= reply_new(tag
);
2096 if (c
->version
>= 13)
2097 pa_tagstruct_putu32(reply
, c
->client
->index
);
2099 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2102 static void command_lookup(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2103 connection
*c
= CONNECTION(userdata
);
2105 uint32_t idx
= PA_IDXSET_INVALID
;
2107 connection_assert_ref(c
);
2110 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2111 !pa_tagstruct_eof(t
)) {
2116 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2117 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2119 if (command
== PA_COMMAND_LOOKUP_SINK
) {
2121 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
2125 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
2126 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
2127 idx
= source
->index
;
2130 if (idx
== PA_IDXSET_INVALID
)
2131 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2133 pa_tagstruct
*reply
;
2134 reply
= reply_new(tag
);
2135 pa_tagstruct_putu32(reply
, idx
);
2136 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2140 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
) {
2141 connection
*c
= CONNECTION(userdata
);
2145 connection_assert_ref(c
);
2148 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2149 !pa_tagstruct_eof(t
)) {
2154 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2155 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2156 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2157 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2159 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
);
2162 static void command_stat(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2163 connection
*c
= CONNECTION(userdata
);
2164 pa_tagstruct
*reply
;
2165 const pa_mempool_stat
*stat
;
2167 connection_assert_ref(c
);
2170 if (!pa_tagstruct_eof(t
)) {
2175 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2177 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
2179 reply
= reply_new(tag
);
2180 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
2181 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
2182 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
2183 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
2184 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
2185 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2188 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
) {
2189 connection
*c
= CONNECTION(userdata
);
2190 pa_tagstruct
*reply
;
2192 struct timeval tv
, now
;
2196 connection_assert_ref(c
);
2199 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2200 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2201 !pa_tagstruct_eof(t
)) {
2206 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2207 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2208 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2209 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2210 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
)
2212 reply
= reply_new(tag
);
2214 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
2215 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
2217 pa_tagstruct_put_usec(reply
, latency
);
2219 pa_tagstruct_put_usec(reply
, 0);
2220 pa_tagstruct_put_boolean(reply
, s
->sink_input
->thread_info
.playing_for
> 0);
2221 pa_tagstruct_put_timeval(reply
, &tv
);
2222 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2223 pa_tagstruct_puts64(reply
, s
->write_index
);
2224 pa_tagstruct_puts64(reply
, s
->read_index
);
2226 if (c
->version
>= 13) {
2227 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.underrun_for
);
2228 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.playing_for
);
2231 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2234 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
) {
2235 connection
*c
= CONNECTION(userdata
);
2236 pa_tagstruct
*reply
;
2238 struct timeval tv
, now
;
2241 connection_assert_ref(c
);
2244 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2245 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2246 !pa_tagstruct_eof(t
)) {
2251 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2252 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2253 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2255 reply
= reply_new(tag
);
2256 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
2257 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
2258 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_RUNNING
);
2259 pa_tagstruct_put_timeval(reply
, &tv
);
2260 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2261 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
2262 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
2263 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2266 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
) {
2267 connection
*c
= CONNECTION(userdata
);
2270 const char *name
= NULL
;
2273 pa_tagstruct
*reply
;
2276 connection_assert_ref(c
);
2279 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2280 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
2281 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
2282 pa_tagstruct_getu32(t
, &length
) < 0) {
2287 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2288 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
2289 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
2290 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
2291 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
2292 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
2294 p
= pa_proplist_new();
2296 if (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) {
2298 pa_proplist_free(p
);
2302 if (c
->version
< 13)
2303 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2305 if (!(name
= pa_proplist_gets(p
, PA_PROP_EVENT_ID
)))
2306 name
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
);
2308 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2310 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
2311 pa_proplist_free(p
);
2313 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2315 reply
= reply_new(tag
);
2316 pa_tagstruct_putu32(reply
, s
->index
);
2317 pa_tagstruct_putu32(reply
, length
);
2318 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2321 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
) {
2322 connection
*c
= CONNECTION(userdata
);
2327 connection_assert_ref(c
);
2330 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
2331 !pa_tagstruct_eof(t
)) {
2336 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2338 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
2339 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2340 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2342 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
2343 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
2345 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2347 upload_stream_unlink(s
);
2350 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
) {
2351 connection
*c
= CONNECTION(userdata
);
2352 uint32_t sink_index
;
2355 const char *name
, *sink_name
;
2358 pa_tagstruct
*reply
;
2360 connection_assert_ref(c
);
2363 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2365 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
2366 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
2367 pa_tagstruct_getu32(t
, &volume
) < 0 ||
2368 pa_tagstruct_gets(t
, &name
) < 0) {
2373 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2374 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2376 if (sink_index
!= PA_INVALID_INDEX
)
2377 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
2379 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
2381 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2383 p
= pa_proplist_new();
2385 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2386 !pa_tagstruct_eof(t
)) {
2388 pa_proplist_free(p
);
2392 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2393 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2394 pa_proplist_free(p
);
2398 pa_proplist_free(p
);
2400 reply
= reply_new(tag
);
2402 if (c
->version
>= 13)
2403 pa_tagstruct_putu32(reply
, idx
);
2405 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2408 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
) {
2409 connection
*c
= CONNECTION(userdata
);
2412 connection_assert_ref(c
);
2415 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2416 !pa_tagstruct_eof(t
)) {
2421 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2422 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2424 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2425 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2429 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2432 static void fixup_sample_spec(connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2435 pa_assert(original
);
2439 if (c
->version
< 12) {
2440 /* Before protocol version 12 we didn't support S32 samples,
2441 * so we need to lie about this to the client */
2443 if (fixed
->format
== PA_SAMPLE_S32LE
)
2444 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2445 if (fixed
->format
== PA_SAMPLE_S32BE
)
2446 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2450 static void sink_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2451 pa_sample_spec fixed_ss
;
2454 pa_sink_assert_ref(sink
);
2456 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2460 PA_TAG_U32
, sink
->index
,
2461 PA_TAG_STRING
, sink
->name
,
2462 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2463 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2464 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2465 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2466 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
),
2467 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
),
2468 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2469 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2470 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2471 PA_TAG_STRING
, sink
->driver
,
2472 PA_TAG_U32
, sink
->flags
,
2475 if (c
->version
>= 13) {
2476 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2477 pa_tagstruct_put_usec(t
, pa_sink_get_requested_latency(sink
));
2481 static void source_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2482 pa_sample_spec fixed_ss
;
2485 pa_source_assert_ref(source
);
2487 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2491 PA_TAG_U32
, source
->index
,
2492 PA_TAG_STRING
, source
->name
,
2493 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2494 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2495 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2496 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2497 PA_TAG_CVOLUME
, pa_source_get_volume(source
),
2498 PA_TAG_BOOLEAN
, pa_source_get_mute(source
),
2499 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2500 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2501 PA_TAG_USEC
, pa_source_get_latency(source
),
2502 PA_TAG_STRING
, source
->driver
,
2503 PA_TAG_U32
, source
->flags
,
2506 if (c
->version
>= 13) {
2507 pa_tagstruct_put_proplist(t
, source
->proplist
);
2508 pa_tagstruct_put_usec(t
, pa_source_get_requested_latency(source
));
2513 static void client_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2517 pa_tagstruct_putu32(t
, client
->index
);
2518 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2519 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2520 pa_tagstruct_puts(t
, client
->driver
);
2522 if (c
->version
>= 13)
2523 pa_tagstruct_put_proplist(t
, client
->proplist
);
2527 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
2531 pa_tagstruct_putu32(t
, module
->index
);
2532 pa_tagstruct_puts(t
, module
->name
);
2533 pa_tagstruct_puts(t
, module
->argument
);
2534 pa_tagstruct_putu32(t
, module
->n_used
);
2535 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
2538 static void sink_input_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2539 pa_sample_spec fixed_ss
;
2542 pa_sink_input_assert_ref(s
);
2544 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2546 pa_tagstruct_putu32(t
, s
->index
);
2547 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2548 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2549 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2550 pa_tagstruct_putu32(t
, s
->sink
->index
);
2551 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2552 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2553 pa_tagstruct_put_cvolume(t
, &s
->volume
);
2554 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
2555 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
2556 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2557 pa_tagstruct_puts(t
, s
->driver
);
2558 if (c
->version
>= 11)
2559 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2560 if (c
->version
>= 13)
2561 pa_tagstruct_put_proplist(t
, s
->proplist
);
2564 static void source_output_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2565 pa_sample_spec fixed_ss
;
2568 pa_source_output_assert_ref(s
);
2570 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2572 pa_tagstruct_putu32(t
, s
->index
);
2573 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2574 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2575 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2576 pa_tagstruct_putu32(t
, s
->source
->index
);
2577 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2578 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2579 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
2580 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
2581 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2582 pa_tagstruct_puts(t
, s
->driver
);
2584 if (c
->version
>= 13)
2585 pa_tagstruct_put_proplist(t
, s
->proplist
);
2588 static void scache_fill_tagstruct(connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2589 pa_sample_spec fixed_ss
;
2594 if (e
->memchunk
.memblock
)
2595 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2597 memset(&fixed_ss
, 0, sizeof(fixed_ss
));
2599 pa_tagstruct_putu32(t
, e
->index
);
2600 pa_tagstruct_puts(t
, e
->name
);
2601 pa_tagstruct_put_cvolume(t
, &e
->volume
);
2602 pa_tagstruct_put_usec(t
, e
->memchunk
.memblock
? pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
) : 0);
2603 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2604 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2605 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
2606 pa_tagstruct_put_boolean(t
, e
->lazy
);
2607 pa_tagstruct_puts(t
, e
->filename
);
2609 if (c
->version
>= 13)
2610 pa_tagstruct_put_proplist(t
, e
->proplist
);
2613 static void command_get_info(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2614 connection
*c
= CONNECTION(userdata
);
2616 pa_sink
*sink
= NULL
;
2617 pa_source
*source
= NULL
;
2618 pa_client
*client
= NULL
;
2619 pa_module
*module
= NULL
;
2620 pa_sink_input
*si
= NULL
;
2621 pa_source_output
*so
= NULL
;
2622 pa_scache_entry
*sce
= NULL
;
2624 pa_tagstruct
*reply
;
2626 connection_assert_ref(c
);
2629 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2630 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2631 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2632 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2633 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2634 pa_tagstruct_gets(t
, &name
) < 0) ||
2635 !pa_tagstruct_eof(t
)) {
2640 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2641 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2643 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2644 if (idx
!= PA_INVALID_INDEX
)
2645 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2647 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2648 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2649 if (idx
!= PA_INVALID_INDEX
)
2650 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2652 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2653 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2654 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2655 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2656 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2657 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2658 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2659 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2660 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2662 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2663 if (idx
!= PA_INVALID_INDEX
)
2664 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2666 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
2669 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
2670 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2674 reply
= reply_new(tag
);
2676 sink_fill_tagstruct(c
, reply
, sink
);
2678 source_fill_tagstruct(c
, reply
, source
);
2680 client_fill_tagstruct(c
, reply
, client
);
2682 module_fill_tagstruct(reply
, module
);
2684 sink_input_fill_tagstruct(c
, reply
, si
);
2686 source_output_fill_tagstruct(c
, reply
, so
);
2688 scache_fill_tagstruct(c
, reply
, sce
);
2689 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2692 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2693 connection
*c
= CONNECTION(userdata
);
2697 pa_tagstruct
*reply
;
2699 connection_assert_ref(c
);
2702 if (!pa_tagstruct_eof(t
)) {
2707 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2709 reply
= reply_new(tag
);
2711 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2712 i
= c
->protocol
->core
->sinks
;
2713 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2714 i
= c
->protocol
->core
->sources
;
2715 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2716 i
= c
->protocol
->core
->clients
;
2717 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2718 i
= c
->protocol
->core
->modules
;
2719 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2720 i
= c
->protocol
->core
->sink_inputs
;
2721 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2722 i
= c
->protocol
->core
->source_outputs
;
2724 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2725 i
= c
->protocol
->core
->scache
;
2729 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2730 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2731 sink_fill_tagstruct(c
, reply
, p
);
2732 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2733 source_fill_tagstruct(c
, reply
, p
);
2734 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2735 client_fill_tagstruct(c
, reply
, p
);
2736 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2737 module_fill_tagstruct(reply
, p
);
2738 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2739 sink_input_fill_tagstruct(c
, reply
, p
);
2740 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2741 source_output_fill_tagstruct(c
, reply
, p
);
2743 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2744 scache_fill_tagstruct(c
, reply
, p
);
2749 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2752 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
) {
2753 connection
*c
= CONNECTION(userdata
);
2754 pa_tagstruct
*reply
;
2757 pa_sample_spec fixed_ss
;
2759 connection_assert_ref(c
);
2762 if (!pa_tagstruct_eof(t
)) {
2767 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2769 reply
= reply_new(tag
);
2770 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
2771 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
2772 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
2773 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
2775 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
2776 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
2778 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
2779 pa_tagstruct_puts(reply
, n
);
2780 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
2781 pa_tagstruct_puts(reply
, n
);
2783 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
2785 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2788 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
2790 connection
*c
= CONNECTION(userdata
);
2792 connection_assert_ref(c
);
2794 t
= pa_tagstruct_new(NULL
, 0);
2795 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
2796 pa_tagstruct_putu32(t
, (uint32_t) -1);
2797 pa_tagstruct_putu32(t
, e
);
2798 pa_tagstruct_putu32(t
, idx
);
2799 pa_pstream_send_tagstruct(c
->pstream
, t
);
2802 static void command_subscribe(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
);
2804 pa_subscription_mask_t m
;
2806 connection_assert_ref(c
);
2809 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
2810 !pa_tagstruct_eof(t
)) {
2815 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2816 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
2818 if (c
->subscription
)
2819 pa_subscription_free(c
->subscription
);
2822 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
2823 pa_assert(c
->subscription
);
2825 c
->subscription
= NULL
;
2827 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2830 static void command_set_volume(
2831 PA_GCC_UNUSED pa_pdispatch
*pd
,
2837 connection
*c
= CONNECTION(userdata
);
2840 pa_sink
*sink
= NULL
;
2841 pa_source
*source
= NULL
;
2842 pa_sink_input
*si
= NULL
;
2843 const char *name
= NULL
;
2845 connection_assert_ref(c
);
2848 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2849 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2850 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2851 pa_tagstruct_get_cvolume(t
, &volume
) ||
2852 !pa_tagstruct_eof(t
)) {
2857 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2858 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2859 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
2863 case PA_COMMAND_SET_SINK_VOLUME
:
2864 if (idx
!= PA_INVALID_INDEX
)
2865 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2867 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2870 case PA_COMMAND_SET_SOURCE_VOLUME
:
2871 if (idx
!= PA_INVALID_INDEX
)
2872 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2874 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2877 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
2878 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2882 pa_assert_not_reached();
2885 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2888 pa_sink_set_volume(sink
, &volume
);
2890 pa_source_set_volume(source
, &volume
);
2892 pa_sink_input_set_volume(si
, &volume
);
2894 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2897 static void command_set_mute(
2898 PA_GCC_UNUSED pa_pdispatch
*pd
,
2904 connection
*c
= CONNECTION(userdata
);
2907 pa_sink
*sink
= NULL
;
2908 pa_source
*source
= NULL
;
2909 pa_sink_input
*si
= NULL
;
2910 const char *name
= NULL
;
2912 connection_assert_ref(c
);
2915 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2916 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2917 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2918 pa_tagstruct_get_boolean(t
, &mute
) ||
2919 !pa_tagstruct_eof(t
)) {
2924 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2925 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2929 case PA_COMMAND_SET_SINK_MUTE
:
2931 if (idx
!= PA_INVALID_INDEX
)
2932 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2934 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2938 case PA_COMMAND_SET_SOURCE_MUTE
:
2939 if (idx
!= PA_INVALID_INDEX
)
2940 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2942 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2946 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
2947 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2951 pa_assert_not_reached();
2954 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2957 pa_sink_set_mute(sink
, mute
);
2959 pa_source_set_mute(source
, mute
);
2961 pa_sink_input_set_mute(si
, mute
);
2963 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2966 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
) {
2967 connection
*c
= CONNECTION(userdata
);
2972 connection_assert_ref(c
);
2975 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2976 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
2977 !pa_tagstruct_eof(t
)) {
2982 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2983 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2984 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2985 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2986 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2988 pa_sink_input_cork(s
->sink_input
, b
);
2989 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2992 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
) {
2993 connection
*c
= CONNECTION(userdata
);
2997 connection_assert_ref(c
);
3000 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3001 !pa_tagstruct_eof(t
)) {
3006 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3007 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3008 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3009 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3010 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3013 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3014 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3017 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3018 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3021 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3022 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3026 pa_assert_not_reached();
3029 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3032 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
) {
3033 connection
*c
= CONNECTION(userdata
);
3038 connection_assert_ref(c
);
3041 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3042 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3043 !pa_tagstruct_eof(t
)) {
3048 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3049 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3050 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3052 pa_source_output_cork(s
->source_output
, b
);
3053 pa_memblockq_prebuf_force(s
->memblockq
);
3054 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3057 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
) {
3058 connection
*c
= CONNECTION(userdata
);
3062 connection_assert_ref(c
);
3065 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3066 !pa_tagstruct_eof(t
)) {
3071 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3072 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3073 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3075 pa_memblockq_flush(s
->memblockq
);
3076 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3079 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3080 connection
*c
= CONNECTION(userdata
);
3082 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3083 pa_tagstruct
*reply
;
3085 connection_assert_ref(c
);
3088 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3093 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3095 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3097 pa_bool_t adjust_latency
= FALSE
;
3099 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3100 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3101 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3103 if (pa_tagstruct_get(
3105 PA_TAG_U32
, &maxlength
,
3106 PA_TAG_U32
, &tlength
,
3107 PA_TAG_U32
, &prebuf
,
3108 PA_TAG_U32
, &minreq
,
3109 PA_TAG_INVALID
) < 0 ||
3110 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3111 !pa_tagstruct_eof(t
)) {
3116 fix_playback_buffer_attr_pre(s
, adjust_latency
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3117 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3118 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3119 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3120 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3121 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3123 reply
= reply_new(tag
);
3124 pa_tagstruct_putu32(reply
, maxlength
);
3125 pa_tagstruct_putu32(reply
, tlength
);
3126 pa_tagstruct_putu32(reply
, prebuf
);
3127 pa_tagstruct_putu32(reply
, minreq
);
3129 if (c
->version
>= 13)
3130 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3134 pa_bool_t adjust_latency
= FALSE
;
3135 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3137 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3138 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3140 if (pa_tagstruct_get(
3142 PA_TAG_U32
, &maxlength
,
3143 PA_TAG_U32
, &fragsize
,
3144 PA_TAG_INVALID
) < 0 ||
3145 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3146 !pa_tagstruct_eof(t
)) {
3151 fix_record_buffer_attr_pre(s
, adjust_latency
, &maxlength
, &fragsize
);
3152 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3153 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3155 reply
= reply_new(tag
);
3156 pa_tagstruct_putu32(reply
, maxlength
);
3157 pa_tagstruct_putu32(reply
, fragsize
);
3159 if (c
->version
>= 13)
3160 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3163 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3166 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3167 connection
*c
= CONNECTION(userdata
);
3171 connection_assert_ref(c
);
3174 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3175 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3176 !pa_tagstruct_eof(t
)) {
3181 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3182 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3184 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3187 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3188 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3189 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3191 pa_sink_input_set_rate(s
->sink_input
, rate
);
3195 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3197 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3198 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3200 pa_source_output_set_rate(s
->source_output
, rate
);
3203 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3206 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3207 connection
*c
= CONNECTION(userdata
);
3212 connection_assert_ref(c
);
3215 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3217 p
= pa_proplist_new();
3219 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3221 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3222 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3223 !pa_tagstruct_eof(t
)) {
3225 pa_proplist_free(p
);
3231 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3232 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3233 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3234 !pa_tagstruct_eof(t
)) {
3236 pa_proplist_free(p
);
3241 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3243 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3246 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3247 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3248 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3250 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
3251 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3253 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3256 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3257 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3259 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
3260 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3262 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3264 pa_proplist_update(c
->client
->proplist
, mode
, p
);
3265 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3268 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3271 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3272 connection
*c
= CONNECTION(userdata
);
3274 unsigned changed
= 0;
3276 pa_strlist
*l
= NULL
;
3278 connection_assert_ref(c
);
3281 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3283 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3285 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3291 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3294 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3295 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3296 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3298 p
= s
->sink_input
->proplist
;
3300 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3303 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3304 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3306 p
= s
->source_output
->proplist
;
3308 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3310 p
= c
->client
->proplist
;
3316 if (pa_tagstruct_gets(t
, &k
) < 0) {
3325 l
= pa_strlist_prepend(l
, k
);
3328 if (!pa_tagstruct_eof(t
)) {
3337 l
= pa_strlist_pop(l
, &z
);
3342 changed
+= pa_proplist_unset(p
, z
) >= 0;
3346 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3349 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3352 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3353 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3355 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3358 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3359 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3362 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3363 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3368 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
) {
3369 connection
*c
= CONNECTION(userdata
);
3372 connection_assert_ref(c
);
3375 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3376 !pa_tagstruct_eof(t
)) {
3381 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3382 CHECK_VALIDITY(c
->pstream
, !s
|| (*s
&& pa_utf8_valid(s
)), tag
, PA_ERR_INVALID
);
3384 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
3385 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3388 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3389 connection
*c
= CONNECTION(userdata
);
3393 connection_assert_ref(c
);
3396 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3397 pa_tagstruct_gets(t
, &name
) < 0 ||
3398 !pa_tagstruct_eof(t
)) {
3403 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3404 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3406 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3409 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3410 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3411 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3413 pa_sink_input_set_name(s
->sink_input
, name
);
3417 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3419 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3420 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3422 pa_source_output_set_name(s
->source_output
, name
);
3425 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3428 static void command_kill(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3429 connection
*c
= CONNECTION(userdata
);
3432 connection_assert_ref(c
);
3435 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3436 !pa_tagstruct_eof(t
)) {
3441 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3443 if (command
== PA_COMMAND_KILL_CLIENT
) {
3446 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3447 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3450 pa_client_kill(client
);
3452 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3455 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3456 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3459 pa_sink_input_kill(s
);
3461 pa_source_output
*s
;
3463 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3465 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3466 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3469 pa_source_output_kill(s
);
3472 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3473 connection_unref(c
);
3476 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
) {
3477 connection
*c
= CONNECTION(userdata
);
3479 const char *name
, *argument
;
3480 pa_tagstruct
*reply
;
3482 connection_assert_ref(c
);
3485 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3486 pa_tagstruct_gets(t
, &argument
) < 0 ||
3487 !pa_tagstruct_eof(t
)) {
3492 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3493 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3494 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3496 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3497 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3501 reply
= reply_new(tag
);
3502 pa_tagstruct_putu32(reply
, m
->index
);
3503 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3506 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
) {
3507 connection
*c
= CONNECTION(userdata
);
3511 connection_assert_ref(c
);
3514 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3515 !pa_tagstruct_eof(t
)) {
3520 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3521 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3522 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3524 pa_module_unload_request(m
);
3525 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3528 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
) {
3529 connection
*c
= CONNECTION(userdata
);
3530 const char *name
, *module
, *argument
;
3533 pa_tagstruct
*reply
;
3535 connection_assert_ref(c
);
3538 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3539 pa_tagstruct_getu32(t
, &type
) < 0 ||
3540 pa_tagstruct_gets(t
, &module
) < 0 ||
3541 pa_tagstruct_gets(t
, &argument
) < 0 ||
3542 !pa_tagstruct_eof(t
)) {
3547 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3548 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3549 CHECK_VALIDITY(c
->pstream
, type
== 0 || type
== 1, tag
, PA_ERR_INVALID
);
3550 CHECK_VALIDITY(c
->pstream
, module
&& *module
&& pa_utf8_valid(module
), tag
, PA_ERR_INVALID
);
3551 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3553 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
3554 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
3558 reply
= reply_new(tag
);
3559 pa_tagstruct_putu32(reply
, idx
);
3560 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3563 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
) {
3564 connection
*c
= CONNECTION(userdata
);
3565 const char *name
= NULL
;
3566 uint32_t type
, idx
= PA_IDXSET_INVALID
;
3569 connection_assert_ref(c
);
3572 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3573 (pa_tagstruct_gets(t
, &name
) < 0 ||
3574 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3575 !pa_tagstruct_eof(t
)) {
3580 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3581 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3582 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& pa_utf8_valid(name
) && (type
== 0 || type
== 1)), tag
, PA_ERR_INVALID
);
3585 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3587 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
3589 CHECK_VALIDITY(c
->pstream
, r
>= 0, tag
, PA_ERR_NOENTITY
);
3591 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3594 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
3597 pa_tagstruct_putu32(t
, e
->index
);
3598 pa_tagstruct_puts(t
, e
->name
);
3599 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
3600 pa_tagstruct_puts(t
, e
->module
);
3601 pa_tagstruct_puts(t
, e
->argument
);
3604 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
) {
3605 connection
*c
= CONNECTION(userdata
);
3606 const pa_autoload_entry
*a
= NULL
;
3609 pa_tagstruct
*reply
;
3611 connection_assert_ref(c
);
3614 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3615 (pa_tagstruct_gets(t
, &name
) < 0 ||
3616 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3617 !pa_tagstruct_eof(t
)) {
3622 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3623 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3624 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& (type
== 0 || type
== 1) && pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
3627 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3629 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
3631 CHECK_VALIDITY(c
->pstream
, a
, tag
, PA_ERR_NOENTITY
);
3633 reply
= reply_new(tag
);
3634 autoload_fill_tagstruct(reply
, a
);
3635 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3638 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
) {
3639 connection
*c
= CONNECTION(userdata
);
3640 pa_tagstruct
*reply
;
3642 connection_assert_ref(c
);
3645 if (!pa_tagstruct_eof(t
)) {
3650 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3652 reply
= reply_new(tag
);
3654 if (c
->protocol
->core
->autoload_hashmap
) {
3655 pa_autoload_entry
*a
;
3658 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
3659 autoload_fill_tagstruct(reply
, a
);
3662 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3665 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3666 connection
*c
= CONNECTION(userdata
);
3667 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3668 const char *name
= NULL
;
3670 connection_assert_ref(c
);
3673 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3674 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3675 pa_tagstruct_gets(t
, &name
) < 0 ||
3676 !pa_tagstruct_eof(t
)) {
3681 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3682 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3683 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
3685 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3686 pa_sink_input
*si
= NULL
;
3687 pa_sink
*sink
= NULL
;
3689 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3691 if (idx_device
!= PA_INVALID_INDEX
)
3692 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3694 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3696 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3698 if (pa_sink_input_move_to(si
, sink
) < 0) {
3699 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3703 pa_source_output
*so
= NULL
;
3706 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3708 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3710 if (idx_device
!= PA_INVALID_INDEX
)
3711 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3713 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3715 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3717 if (pa_source_output_move_to(so
, source
) < 0) {
3718 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3723 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3726 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3727 connection
*c
= CONNECTION(userdata
);
3728 uint32_t idx
= PA_INVALID_INDEX
;
3729 const char *name
= NULL
;
3732 connection_assert_ref(c
);
3735 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3736 pa_tagstruct_gets(t
, &name
) < 0 ||
3737 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3738 !pa_tagstruct_eof(t
)) {
3743 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3744 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| !*name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3746 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3748 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3750 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3751 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3755 pa_sink
*sink
= NULL
;
3757 if (idx
!= PA_INVALID_INDEX
)
3758 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3760 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3762 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3764 if (pa_sink_suspend(sink
, b
) < 0) {
3765 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3771 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3773 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3775 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3776 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3783 if (idx
!= PA_INVALID_INDEX
)
3784 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3786 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3788 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3790 if (pa_source_suspend(source
, b
) < 0) {
3791 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3797 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3800 /*** pstream callbacks ***/
3802 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
3803 connection
*c
= CONNECTION(userdata
);
3807 connection_assert_ref(c
);
3809 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
3810 pa_log("invalid packet.");
3811 connection_unlink(c
);
3815 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
) {
3816 connection
*c
= CONNECTION(userdata
);
3817 output_stream
*stream
;
3821 connection_assert_ref(c
);
3823 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
3824 pa_log("client sent block for invalid stream.");
3829 if (playback_stream_isinstance(stream
)) {
3830 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
3832 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
3833 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
);
3835 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
3838 upload_stream
*u
= UPLOAD_STREAM(stream
);
3841 if (!u
->memchunk
.memblock
) {
3842 if (u
->length
== chunk
->length
) {
3843 u
->memchunk
= *chunk
;
3844 pa_memblock_ref(u
->memchunk
.memblock
);
3847 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
3848 u
->memchunk
.index
= u
->memchunk
.length
= 0;
3852 pa_assert(u
->memchunk
.memblock
);
3855 if (l
> chunk
->length
)
3861 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
3862 src
= pa_memblock_acquire(chunk
->memblock
);
3864 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
3865 (uint8_t*) src
+chunk
->index
, l
);
3867 pa_memblock_release(u
->memchunk
.memblock
);
3868 pa_memblock_release(chunk
->memblock
);
3870 u
->memchunk
.length
+= l
;
3876 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
3877 connection
*c
= CONNECTION(userdata
);
3880 connection_assert_ref(c
);
3882 connection_unlink(c
);
3883 pa_log_info("connection died.");
3886 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
3887 connection
*c
= CONNECTION(userdata
);
3890 connection_assert_ref(c
);
3895 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
3898 if (!(q
= pa_thread_mq_get()))
3899 pa_pstream_send_revoke(p
, block_id
);
3901 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
3904 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
3907 if (!(q
= pa_thread_mq_get()))
3908 pa_pstream_send_release(p
, block_id
);
3910 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
3913 /*** client callbacks ***/
3915 static void client_kill_cb(pa_client
*c
) {
3918 connection_unlink(CONNECTION(c
->userdata
));
3921 /*** socket server callbacks ***/
3923 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
3924 connection
*c
= CONNECTION(userdata
);
3928 connection_assert_ref(c
);
3929 pa_assert(c
->auth_timeout_event
== e
);
3932 connection_unlink(c
);
3935 static void on_connection(PA_GCC_UNUSED pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
3936 pa_protocol_native
*p
= userdata
;
3938 char cname
[256], pname
[128];
3943 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
3944 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
3945 pa_iochannel_free(io
);
3949 c
= pa_msgobject_new(connection
);
3950 c
->parent
.parent
.free
= connection_free
;
3951 c
->parent
.process_msg
= connection_process_msg
;
3953 c
->authorized
= p
->public;
3955 if (!c
->authorized
&& p
->auth_ip_acl
&& pa_ip_acl_check(p
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
3956 pa_log_info("Client authenticated by IP ACL.");
3957 c
->authorized
= TRUE
;
3960 if (!c
->authorized
) {
3962 pa_gettimeofday(&tv
);
3963 tv
.tv_sec
+= AUTH_TIMEOUT
;
3964 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
3966 c
->auth_timeout_event
= NULL
;
3968 c
->is_local
= pa_iochannel_socket_is_local(io
);
3971 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
3972 pa_snprintf(cname
, sizeof(cname
), "Native client (%s)", pname
);
3973 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
3974 pa_proplist_sets(c
->client
->proplist
, "native-protocol.peer", pname
);
3975 c
->client
->kill
= client_kill_cb
;
3976 c
->client
->userdata
= c
;
3977 c
->client
->module
= p
->module
;
3979 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
3981 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
3982 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
3983 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
3984 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
3985 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
3986 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
3988 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
3990 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
3991 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
3993 c
->rrobin_index
= PA_IDXSET_INVALID
;
3994 c
->subscription
= NULL
;
3996 pa_idxset_put(p
->connections
, c
, NULL
);
3999 if (pa_iochannel_creds_supported(io
))
4000 pa_iochannel_creds_enable(io
);
4004 /*** module entry points ***/
4006 static int load_key(pa_protocol_native
*p
, const char*fn
) {
4009 p
->auth_cookie_in_property
= FALSE
;
4011 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
4012 pa_log_info("using already loaded auth cookie.");
4013 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
4014 p
->auth_cookie_in_property
= TRUE
;
4019 fn
= PA_NATIVE_COOKIE_FILE
;
4021 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
4024 pa_log_info("loading cookie from disk.");
4026 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
4027 p
->auth_cookie_in_property
= TRUE
;
4032 static pa_protocol_native
* protocol_new_internal(pa_core
*c
, pa_module
*m
, pa_modargs
*ma
) {
4033 pa_protocol_native
*p
;
4034 pa_bool_t
public = FALSE
;
4040 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
4041 pa_log("auth-anonymous= expects a boolean argument.");
4045 p
= pa_xnew(pa_protocol_native
, 1);
4050 p
->auth_ip_acl
= NULL
;
4055 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &a
) < 0) {
4056 pa_log("auth-group-enabled= expects a boolean argument.");
4059 p
->auth_group
= a
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4062 pa_log_info("Allowing access to group '%s'.", p
->auth_group
);
4067 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4069 if (!(p
->auth_ip_acl
= pa_ip_acl_new(acl
))) {
4070 pa_log("Failed to parse IP ACL '%s'", acl
);
4075 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0)
4078 p
->connections
= pa_idxset_new(NULL
, NULL
);
4084 pa_xfree(p
->auth_group
);
4087 pa_ip_acl_free(p
->auth_ip_acl
);
4092 pa_protocol_native
* pa_protocol_native_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
4094 pa_protocol_native
*p
;
4096 if (!(p
= protocol_new_internal(core
, m
, ma
)))
4099 p
->server
= pa_socket_server_ref(server
);
4100 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
4102 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
4104 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
4105 l
= pa_strlist_prepend(l
, t
);
4106 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
4112 void pa_protocol_native_free(pa_protocol_native
*p
) {
4116 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4117 connection_unlink(c
);
4118 pa_idxset_free(p
->connections
, NULL
, NULL
);
4123 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
4125 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
4126 l
= pa_strlist_remove(l
, t
);
4129 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
4131 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
4134 pa_socket_server_unref(p
->server
);
4137 if (p
->auth_cookie_in_property
)
4138 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
4141 pa_ip_acl_free(p
->auth_ip_acl
);
4144 pa_xfree(p
->auth_group
);
4149 pa_protocol_native
* pa_protocol_native_new_iochannel(
4155 pa_protocol_native
*p
;
4157 if (!(p
= protocol_new_internal(core
, m
, ma
)))
4160 on_connection(NULL
, io
, p
);