2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/timeval.h>
33 #include <pulse/version.h>
34 #include <pulse/utf8.h>
35 #include <pulse/util.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/native-common.h>
39 #include <pulsecore/packet.h>
40 #include <pulsecore/client.h>
41 #include <pulsecore/source-output.h>
42 #include <pulsecore/sink-input.h>
43 #include <pulsecore/pstream.h>
44 #include <pulsecore/tagstruct.h>
45 #include <pulsecore/pdispatch.h>
46 #include <pulsecore/pstream-util.h>
47 #include <pulsecore/authkey.h>
48 #include <pulsecore/namereg.h>
49 #include <pulsecore/core-scache.h>
50 #include <pulsecore/core-subscribe.h>
51 #include <pulsecore/log.h>
52 #include <pulsecore/autoload.h>
53 #include <pulsecore/strlist.h>
54 #include <pulsecore/shared.h>
55 #include <pulsecore/sample-util.h>
56 #include <pulsecore/llist.h>
57 #include <pulsecore/creds.h>
58 #include <pulsecore/core-util.h>
59 #include <pulsecore/ipacl.h>
60 #include <pulsecore/thread-mq.h>
62 #include "protocol-native.h"
64 /* Kick a client if it doesn't authenticate within this time */
65 #define AUTH_TIMEOUT 60
67 /* Don't accept more connection than this */
68 #define MAX_CONNECTIONS 64
70 #define MAX_MEMBLOCKQ_LENGTH (4*1024*1024) /* 4MB */
71 #define DEFAULT_TLENGTH_MSEC 2000 /* 2s */
72 #define DEFAULT_PROCESS_MSEC 20 /* 20ms */
73 #define DEFAULT_FRAGSIZE_MSEC DEFAULT_TLENGTH_MSEC
75 struct pa_native_protocol
;
77 typedef struct record_stream
{
80 pa_native_connection
*connection
;
83 pa_source_output
*source_output
;
84 pa_memblockq
*memblockq
;
86 pa_usec_t source_latency
;
89 PA_DECLARE_CLASS(record_stream
);
90 #define RECORD_STREAM(o) (record_stream_cast(o))
91 static PA_DEFINE_CHECK_TYPE(record_stream
, pa_msgobject
);
93 typedef struct output_stream
{
97 PA_DECLARE_CLASS(output_stream
);
98 #define OUTPUT_STREAM(o) (output_stream_cast(o))
99 static PA_DEFINE_CHECK_TYPE(output_stream
, pa_msgobject
);
101 typedef struct playback_stream
{
102 output_stream parent
;
104 pa_native_connection
*connection
;
107 pa_sink_input
*sink_input
;
108 pa_memblockq
*memblockq
;
109 pa_bool_t is_underrun
:1;
110 pa_bool_t drain_request
:1;
116 pa_usec_t sink_latency
;
118 /* Only updated after SINK_INPUT_MESSAGE_UPDATE_LATENCY */
119 int64_t read_index
, write_index
;
120 size_t render_memblockq_length
;
123 PA_DECLARE_CLASS(playback_stream
);
124 #define PLAYBACK_STREAM(o) (playback_stream_cast(o))
125 static PA_DEFINE_CHECK_TYPE(playback_stream
, output_stream
);
127 typedef struct upload_stream
{
128 output_stream parent
;
130 pa_native_connection
*connection
;
133 pa_memchunk memchunk
;
136 pa_sample_spec sample_spec
;
137 pa_channel_map channel_map
;
138 pa_proplist
*proplist
;
141 PA_DECLARE_CLASS(upload_stream
);
142 #define UPLOAD_STREAM(o) (upload_stream_cast(o))
143 static PA_DEFINE_CHECK_TYPE(upload_stream
, output_stream
);
145 struct pa_native_connection
{
147 pa_native_protocol
*protocol
;
148 pa_native_options
*options
;
149 pa_bool_t authorized
:1;
150 pa_bool_t is_local
:1;
154 pa_pdispatch
*pdispatch
;
155 pa_idxset
*record_streams
, *output_streams
;
156 uint32_t rrobin_index
;
157 pa_subscription
*subscription
;
158 pa_time_event
*auth_timeout_event
;
161 PA_DECLARE_CLASS(pa_native_connection
);
162 #define PA_NATIVE_CONNECTION(o) (pa_native_connection_cast(o))
163 static PA_DEFINE_CHECK_TYPE(pa_native_connection
, pa_msgobject
);
165 struct pa_native_protocol
{
169 pa_idxset
*connections
;
172 pa_hook hooks
[PA_NATIVE_HOOK_MAX
];
174 pa_hashmap
*extensions
;
178 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
179 SINK_INPUT_MESSAGE_DRAIN
, /* disabled prebuf, get playback started. */
180 SINK_INPUT_MESSAGE_FLUSH
,
181 SINK_INPUT_MESSAGE_TRIGGER
,
182 SINK_INPUT_MESSAGE_SEEK
,
183 SINK_INPUT_MESSAGE_PREBUF_FORCE
,
184 SINK_INPUT_MESSAGE_UPDATE_LATENCY
188 PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, /* data requested from sink input from the main loop */
189 PLAYBACK_STREAM_MESSAGE_UNDERFLOW
,
190 PLAYBACK_STREAM_MESSAGE_OVERFLOW
,
191 PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
,
192 PLAYBACK_STREAM_MESSAGE_STARTED
196 RECORD_STREAM_MESSAGE_POST_DATA
/* data from source output to main loop */
200 CONNECTION_MESSAGE_RELEASE
,
201 CONNECTION_MESSAGE_REVOKE
204 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
);
205 static void sink_input_kill_cb(pa_sink_input
*i
);
206 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
);
207 static void sink_input_moved_cb(pa_sink_input
*i
);
208 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
209 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
210 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
);
212 static void native_connection_send_memblock(pa_native_connection
*c
);
213 static void playback_stream_request_bytes(struct playback_stream
*s
);
215 static void source_output_kill_cb(pa_source_output
*o
);
216 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
217 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
);
218 static void source_output_moved_cb(pa_source_output
*o
);
219 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
221 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
);
223 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
224 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
225 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
226 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
227 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
228 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
229 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
230 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
231 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
232 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
233 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
234 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
235 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
236 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
237 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
238 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
239 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
240 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
241 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
242 static void command_set_volume(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
243 static void command_set_mute(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
244 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
245 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
246 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
247 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
248 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
249 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
250 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
251 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
252 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
253 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
254 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
255 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
256 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
257 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
258 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
259 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
260 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
261 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
262 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
263 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
265 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
266 [PA_COMMAND_ERROR
] = NULL
,
267 [PA_COMMAND_TIMEOUT
] = NULL
,
268 [PA_COMMAND_REPLY
] = NULL
,
269 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
270 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
271 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
272 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
273 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
274 [PA_COMMAND_AUTH
] = command_auth
,
275 [PA_COMMAND_REQUEST
] = NULL
,
276 [PA_COMMAND_EXIT
] = command_exit
,
277 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
278 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
279 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
280 [PA_COMMAND_STAT
] = command_stat
,
281 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
282 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
283 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
284 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
285 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
286 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
287 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
288 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
289 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
290 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
291 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
292 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
293 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
294 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
295 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
296 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
297 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
298 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
299 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
300 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
301 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
302 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
303 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
305 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
306 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
307 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
309 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
310 [PA_COMMAND_SET_SINK_INPUT_MUTE
] = command_set_mute
,
311 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
313 [PA_COMMAND_SUSPEND_SINK
] = command_suspend
,
314 [PA_COMMAND_SUSPEND_SOURCE
] = command_suspend
,
316 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
317 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
318 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
319 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
321 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
322 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
324 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
325 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
326 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
327 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
328 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
329 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
330 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
331 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
332 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
333 [PA_COMMAND_GET_AUTOLOAD_INFO
] = command_get_autoload_info
,
334 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = command_get_autoload_info_list
,
335 [PA_COMMAND_ADD_AUTOLOAD
] = command_add_autoload
,
336 [PA_COMMAND_REMOVE_AUTOLOAD
] = command_remove_autoload
,
338 [PA_COMMAND_MOVE_SINK_INPUT
] = command_move_stream
,
339 [PA_COMMAND_MOVE_SOURCE_OUTPUT
] = command_move_stream
,
341 [PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
342 [PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
344 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
345 [PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
347 [PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
] = command_update_proplist
,
348 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
] = command_update_proplist
,
349 [PA_COMMAND_UPDATE_CLIENT_PROPLIST
] = command_update_proplist
,
351 [PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
] = command_remove_proplist
,
352 [PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
] = command_remove_proplist
,
353 [PA_COMMAND_REMOVE_CLIENT_PROPLIST
] = command_remove_proplist
,
355 [PA_COMMAND_EXTENSION
] = command_extension
358 /* structure management */
360 static void upload_stream_unlink(upload_stream
*s
) {
366 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
367 s
->connection
= NULL
;
368 upload_stream_unref(s
);
371 static void upload_stream_free(pa_object
*o
) {
372 upload_stream
*s
= UPLOAD_STREAM(o
);
375 upload_stream_unlink(s
);
380 pa_proplist_free(s
->proplist
);
382 if (s
->memchunk
.memblock
)
383 pa_memblock_unref(s
->memchunk
.memblock
);
388 static upload_stream
* upload_stream_new(
389 pa_native_connection
*c
,
390 const pa_sample_spec
*ss
,
391 const pa_channel_map
*map
,
401 pa_assert(length
> 0);
404 s
= pa_msgobject_new(upload_stream
);
405 s
->parent
.parent
.parent
.free
= upload_stream_free
;
407 s
->sample_spec
= *ss
;
408 s
->channel_map
= *map
;
409 s
->name
= pa_xstrdup(name
);
410 pa_memchunk_reset(&s
->memchunk
);
412 s
->proplist
= pa_proplist_copy(p
);
413 pa_proplist_update(s
->proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
415 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
420 static void record_stream_unlink(record_stream
*s
) {
426 if (s
->source_output
) {
427 pa_source_output_unlink(s
->source_output
);
428 pa_source_output_unref(s
->source_output
);
429 s
->source_output
= NULL
;
432 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->record_streams
, s
, NULL
) == s
);
433 s
->connection
= NULL
;
434 record_stream_unref(s
);
437 static void record_stream_free(pa_object
*o
) {
438 record_stream
*s
= RECORD_STREAM(o
);
441 record_stream_unlink(s
);
443 pa_memblockq_free(s
->memblockq
);
447 static int record_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
448 record_stream
*s
= RECORD_STREAM(o
);
449 record_stream_assert_ref(s
);
456 case RECORD_STREAM_MESSAGE_POST_DATA
:
458 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
459 /* pa_log_warn("Failed to push data into output queue."); */
463 if (!pa_pstream_is_pending(s
->connection
->pstream
))
464 native_connection_send_memblock(s
->connection
);
472 static void fix_record_buffer_attr_pre(
474 pa_bool_t adjust_latency
,
475 pa_bool_t early_requests
,
477 uint32_t *fragsize
) {
480 pa_usec_t orig_fragsize_usec
, fragsize_usec
, source_usec
;
483 pa_assert(maxlength
);
486 frame_size
= pa_frame_size(&s
->source_output
->sample_spec
);
488 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
489 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
491 *maxlength
= (uint32_t) frame_size
;
493 if (*fragsize
== (uint32_t) -1)
494 *fragsize
= (uint32_t) pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC
*PA_USEC_PER_MSEC
, &s
->source_output
->sample_spec
);
496 *fragsize
= (uint32_t) frame_size
;
498 orig_fragsize_usec
= fragsize_usec
= pa_bytes_to_usec(*fragsize
, &s
->source_output
->sample_spec
);
500 if (early_requests
) {
502 /* In early request mode we need to emulate the classic
503 * fragment-based playback model. We do this setting the source
504 * latency to the fragment size. */
506 source_usec
= fragsize_usec
;
508 } else if (adjust_latency
) {
510 /* So, the user asked us to adjust the latency according to
511 * what the source can provide. Half the latency will be
512 * spent on the hw buffer, half of it in the async buffer
513 * queue we maintain for each client. */
515 source_usec
= fragsize_usec
/2;
519 /* Ok, the user didn't ask us to adjust the latency, hence we
526 s
->source_latency
= pa_source_output_set_requested_latency(s
->source_output
, source_usec
);
528 s
->source_latency
= 0;
530 if (early_requests
) {
532 /* Ok, we didn't necessarily get what we were asking for, so
533 * let's tell the user */
535 fragsize_usec
= s
->source_latency
;
537 } else if (adjust_latency
) {
539 /* Now subtract what we actually got */
541 if (fragsize_usec
>= s
->source_latency
*2)
542 fragsize_usec
-= s
->source_latency
;
544 fragsize_usec
= s
->source_latency
;
547 if (pa_usec_to_bytes(orig_fragsize_usec
, &s
->source_output
->sample_spec
) !=
548 pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
))
550 *fragsize
= (uint32_t) pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
);
553 *fragsize
= (uint32_t) frame_size
;
556 static void fix_record_buffer_attr_post(
559 uint32_t *fragsize
) {
564 pa_assert(maxlength
);
567 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
569 base
= pa_frame_size(&s
->source_output
->sample_spec
);
571 s
->fragment_size
= (*fragsize
/base
)*base
;
572 if (s
->fragment_size
<= 0)
573 s
->fragment_size
= base
;
575 if (s
->fragment_size
> *maxlength
)
576 s
->fragment_size
= *maxlength
;
578 *fragsize
= (uint32_t) s
->fragment_size
;
581 static record_stream
* record_stream_new(
582 pa_native_connection
*c
,
586 pa_bool_t peak_detect
,
589 pa_source_output_flags_t flags
,
591 pa_bool_t adjust_latency
,
592 pa_sink_input
*direct_on_input
,
593 pa_bool_t early_requests
) {
596 pa_source_output
*source_output
;
598 pa_source_output_new_data data
;
602 pa_assert(maxlength
);
605 pa_source_output_new_data_init(&data
);
607 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
608 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
609 data
.driver
= __FILE__
;
610 data
.module
= c
->options
->module
;
611 data
.client
= c
->client
;
612 data
.source
= source
;
613 data
.direct_on_input
= direct_on_input
;
614 pa_source_output_new_data_set_sample_spec(&data
, ss
);
615 pa_source_output_new_data_set_channel_map(&data
, map
);
617 data
.resample_method
= PA_RESAMPLER_PEAKS
;
619 source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
);
621 pa_source_output_new_data_done(&data
);
626 s
= pa_msgobject_new(record_stream
);
627 s
->parent
.parent
.free
= record_stream_free
;
628 s
->parent
.process_msg
= record_stream_process_msg
;
630 s
->source_output
= source_output
;
632 s
->source_output
->push
= source_output_push_cb
;
633 s
->source_output
->kill
= source_output_kill_cb
;
634 s
->source_output
->get_latency
= source_output_get_latency_cb
;
635 s
->source_output
->moved
= source_output_moved_cb
;
636 s
->source_output
->suspend
= source_output_suspend_cb
;
637 s
->source_output
->userdata
= s
;
639 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, fragsize
);
641 s
->memblockq
= pa_memblockq_new(
645 base
= pa_frame_size(&source_output
->sample_spec
),
651 fix_record_buffer_attr_post(s
, maxlength
, fragsize
);
653 *ss
= s
->source_output
->sample_spec
;
654 *map
= s
->source_output
->channel_map
;
656 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
658 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
659 ((double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) + (double) s
->source_latency
) / PA_USEC_PER_MSEC
,
660 (double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) / PA_USEC_PER_MSEC
,
661 (double) s
->source_latency
/ PA_USEC_PER_MSEC
);
663 pa_source_output_put(s
->source_output
);
667 static void record_stream_send_killed(record_stream
*r
) {
669 record_stream_assert_ref(r
);
671 t
= pa_tagstruct_new(NULL
, 0);
672 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
673 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
674 pa_tagstruct_putu32(t
, r
->index
);
675 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
678 static void playback_stream_unlink(playback_stream
*s
) {
685 pa_sink_input_unlink(s
->sink_input
);
686 pa_sink_input_unref(s
->sink_input
);
687 s
->sink_input
= NULL
;
690 if (s
->drain_request
)
691 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
693 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
694 s
->connection
= NULL
;
695 playback_stream_unref(s
);
698 static void playback_stream_free(pa_object
* o
) {
699 playback_stream
*s
= PLAYBACK_STREAM(o
);
702 playback_stream_unlink(s
);
704 pa_memblockq_free(s
->memblockq
);
708 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
709 playback_stream
*s
= PLAYBACK_STREAM(o
);
710 playback_stream_assert_ref(s
);
716 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
721 if ((l
= (uint32_t) pa_atomic_load(&s
->missing
)) <= 0)
724 if (pa_atomic_cmpxchg(&s
->missing
, (int) l
, 0))
731 t
= pa_tagstruct_new(NULL
, 0);
732 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
733 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
734 pa_tagstruct_putu32(t
, s
->index
);
735 pa_tagstruct_putu32(t
, l
);
736 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
738 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
742 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
745 /* pa_log("signalling underflow"); */
747 /* Report that we're empty */
748 t
= pa_tagstruct_new(NULL
, 0);
749 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
750 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
751 pa_tagstruct_putu32(t
, s
->index
);
752 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
756 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
759 /* Notify the user we're overflowed*/
760 t
= pa_tagstruct_new(NULL
, 0);
761 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
762 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
763 pa_tagstruct_putu32(t
, s
->index
);
764 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
768 case PLAYBACK_STREAM_MESSAGE_STARTED
:
770 if (s
->connection
->version
>= 13) {
773 /* Notify the user we're overflowed*/
774 t
= pa_tagstruct_new(NULL
, 0);
775 pa_tagstruct_putu32(t
, PA_COMMAND_STARTED
);
776 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
777 pa_tagstruct_putu32(t
, s
->index
);
778 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
783 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
784 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
791 static void fix_playback_buffer_attr_pre(
793 pa_bool_t adjust_latency
,
794 pa_bool_t early_requests
,
801 pa_usec_t orig_tlength_usec
, tlength_usec
, orig_minreq_usec
, minreq_usec
, sink_usec
;
804 pa_assert(maxlength
);
809 frame_size
= pa_frame_size(&s
->sink_input
->sample_spec
);
811 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
812 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
814 *maxlength
= (uint32_t) frame_size
;
816 if (*tlength
== (uint32_t) -1)
817 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_TLENGTH_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
819 *tlength
= (uint32_t) frame_size
;
821 if (*minreq
== (uint32_t) -1)
822 *minreq
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_PROCESS_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
824 *minreq
= (uint32_t) frame_size
;
826 if (*tlength
< *minreq
+frame_size
)
827 *tlength
= *minreq
+(uint32_t) frame_size
;
829 orig_tlength_usec
= tlength_usec
= pa_bytes_to_usec(*tlength
, &s
->sink_input
->sample_spec
);
830 orig_minreq_usec
= minreq_usec
= pa_bytes_to_usec(*minreq
, &s
->sink_input
->sample_spec
);
832 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
833 (double) tlength_usec
/ PA_USEC_PER_MSEC
,
834 (double) minreq_usec
/ PA_USEC_PER_MSEC
);
836 if (early_requests
) {
838 /* In early request mode we need to emulate the classic
839 * fragment-based playback model. We do this setting the sink
840 * latency to the fragment size. */
842 sink_usec
= minreq_usec
;
844 pa_log_debug("Early requests mode enabled, configuring sink latency to minreq.");
846 } else if (adjust_latency
) {
848 /* So, the user asked us to adjust the latency of the stream
849 * buffer according to the what the sink can provide. The
850 * tlength passed in shall be the overall latency. Roughly
851 * half the latency will be spent on the hw buffer, the other
852 * half of it in the async buffer queue we maintain for each
853 * client. In between we'll have a safety space of size
854 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
855 * empty and needs to be filled, then our buffer must have
856 * enough data to fulfill this request immediatly and thus
857 * have at least the same tlength as the size of the hw
858 * buffer. It additionally needs space for 2 times minreq
859 * because if the buffer ran empty and a partial fillup
860 * happens immediately on the next iteration we need to be
861 * able to fulfill it and give the application also minreq
862 * time to fill it up again for the next request Makes 2 times
863 * minreq in plus.. */
865 if (tlength_usec
> minreq_usec
*2)
866 sink_usec
= (tlength_usec
- minreq_usec
*2)/2;
870 pa_log_debug("Adjust latency mode enabled, configuring sink latency to half of overall latency.");
874 /* Ok, the user didn't ask us to adjust the latency, but we
875 * still need to make sure that the parameters from the user
878 if (tlength_usec
> minreq_usec
*2)
879 sink_usec
= (tlength_usec
- minreq_usec
*2);
883 pa_log_debug("Traditional mode enabled, modifying sink usec only for compat with minreq.");
886 s
->sink_latency
= pa_sink_input_set_requested_latency(s
->sink_input
, sink_usec
);
888 if (early_requests
) {
890 /* Ok, we didn't necessarily get what we were asking for, so
891 * let's tell the user */
893 minreq_usec
= s
->sink_latency
;
895 } else if (adjust_latency
) {
897 /* Ok, we didn't necessarily get what we were asking for, so
898 * let's subtract from what we asked for for the remaining
901 if (tlength_usec
>= s
->sink_latency
)
902 tlength_usec
-= s
->sink_latency
;
905 /* FIXME: This is actually larger than necessary, since not all of
906 * the sink latency is actually rewritable. */
907 if (tlength_usec
< s
->sink_latency
+ 2*minreq_usec
)
908 tlength_usec
= s
->sink_latency
+ 2*minreq_usec
;
910 if (pa_usec_to_bytes_round_up(orig_tlength_usec
, &s
->sink_input
->sample_spec
) !=
911 pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
))
912 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
);
914 if (pa_usec_to_bytes(orig_minreq_usec
, &s
->sink_input
->sample_spec
) !=
915 pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
))
916 *minreq
= (uint32_t) pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
);
919 *minreq
= (uint32_t) frame_size
;
920 *tlength
+= (uint32_t) frame_size
*2;
923 if (*tlength
<= *minreq
)
924 *tlength
= *minreq
*2 + (uint32_t) frame_size
;
926 if (*prebuf
== (uint32_t) -1 || *prebuf
> *tlength
)
930 static void fix_playback_buffer_attr_post(
938 pa_assert(maxlength
);
943 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
944 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
945 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
946 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
951 static playback_stream
* playback_stream_new(
952 pa_native_connection
*c
,
964 pa_sink_input_flags_t flags
,
966 pa_bool_t adjust_latency
,
967 pa_bool_t early_requests
) {
969 playback_stream
*s
, *ssync
;
970 pa_sink_input
*sink_input
;
974 pa_sink_input_new_data data
;
978 pa_assert(maxlength
);
985 /* Find syncid group */
986 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
988 if (!playback_stream_isinstance(ssync
))
991 if (ssync
->syncid
== syncid
)
995 /* Synced streams must connect to the same sink */
999 sink
= ssync
->sink_input
->sink
;
1000 else if (sink
!= ssync
->sink_input
->sink
)
1004 pa_sink_input_new_data_init(&data
);
1006 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
1007 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
1008 data
.driver
= __FILE__
;
1009 data
.module
= c
->options
->module
;
1010 data
.client
= c
->client
;
1012 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
1013 pa_sink_input_new_data_set_channel_map(&data
, map
);
1015 pa_sink_input_new_data_set_volume(&data
, volume
);
1016 pa_sink_input_new_data_set_muted(&data
, muted
);
1017 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
1019 sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
);
1021 pa_sink_input_new_data_done(&data
);
1026 s
= pa_msgobject_new(playback_stream
);
1027 s
->parent
.parent
.parent
.free
= playback_stream_free
;
1028 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
1031 s
->sink_input
= sink_input
;
1032 s
->is_underrun
= TRUE
;
1033 s
->drain_request
= FALSE
;
1034 pa_atomic_store(&s
->missing
, 0);
1036 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
1037 s
->sink_input
->pop
= sink_input_pop_cb
;
1038 s
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1039 s
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1040 s
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1041 s
->sink_input
->kill
= sink_input_kill_cb
;
1042 s
->sink_input
->moved
= sink_input_moved_cb
;
1043 s
->sink_input
->suspend
= sink_input_suspend_cb
;
1044 s
->sink_input
->userdata
= s
;
1046 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
1048 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, tlength
, prebuf
, minreq
);
1049 pa_sink_input_get_silence(sink_input
, &silence
);
1051 s
->memblockq
= pa_memblockq_new(
1055 pa_frame_size(&sink_input
->sample_spec
),
1061 pa_memblock_unref(silence
.memblock
);
1062 fix_playback_buffer_attr_post(s
, maxlength
, tlength
, prebuf
, minreq
);
1064 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
1066 *ss
= s
->sink_input
->sample_spec
;
1067 *map
= s
->sink_input
->channel_map
;
1069 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
1071 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
1072 ((double) pa_bytes_to_usec(*tlength
, &sink_input
->sample_spec
) + (double) s
->sink_latency
) / PA_USEC_PER_MSEC
,
1073 (double) pa_bytes_to_usec(*tlength
-*minreq
*2, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1074 (double) pa_bytes_to_usec(*minreq
, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1075 (double) s
->sink_latency
/ PA_USEC_PER_MSEC
);
1077 pa_sink_input_put(s
->sink_input
);
1081 /* Called from thread context */
1082 static void playback_stream_request_bytes(playback_stream
*s
) {
1083 size_t m
, previous_missing
;
1085 playback_stream_assert_ref(s
);
1087 m
= pa_memblockq_pop_missing(s
->memblockq
);
1092 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
1094 previous_missing
= (size_t) pa_atomic_add(&s
->missing
, (int) m
);
1096 if (pa_memblockq_prebuf_active(s
->memblockq
) ||
1097 (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
))
1098 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1102 static void playback_stream_send_killed(playback_stream
*p
) {
1104 playback_stream_assert_ref(p
);
1106 t
= pa_tagstruct_new(NULL
, 0);
1107 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
1108 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1109 pa_tagstruct_putu32(t
, p
->index
);
1110 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
1113 static int native_connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1114 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1115 pa_native_connection_assert_ref(c
);
1122 case CONNECTION_MESSAGE_REVOKE
:
1123 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1126 case CONNECTION_MESSAGE_RELEASE
:
1127 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1134 static void native_connection_unlink(pa_native_connection
*c
) {
1143 pa_hook_fire(&c
->protocol
->hooks
[PA_NATIVE_HOOK_CONNECTION_UNLINK
], c
);
1146 pa_native_options_unref(c
->options
);
1148 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
1149 record_stream_unlink(r
);
1151 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
1152 if (playback_stream_isinstance(o
))
1153 playback_stream_unlink(PLAYBACK_STREAM(o
));
1155 upload_stream_unlink(UPLOAD_STREAM(o
));
1157 if (c
->subscription
)
1158 pa_subscription_free(c
->subscription
);
1161 pa_pstream_unlink(c
->pstream
);
1163 if (c
->auth_timeout_event
) {
1164 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1165 c
->auth_timeout_event
= NULL
;
1168 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
1170 pa_native_connection_unref(c
);
1173 static void native_connection_free(pa_object
*o
) {
1174 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1178 native_connection_unlink(c
);
1180 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
1181 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
1183 pa_pdispatch_unref(c
->pdispatch
);
1184 pa_pstream_unref(c
->pstream
);
1185 pa_client_free(c
->client
);
1190 static void native_connection_send_memblock(pa_native_connection
*c
) {
1194 start
= PA_IDXSET_INVALID
;
1198 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
1201 if (start
== PA_IDXSET_INVALID
)
1202 start
= c
->rrobin_index
;
1203 else if (start
== c
->rrobin_index
)
1206 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
1207 pa_memchunk schunk
= chunk
;
1209 if (schunk
.length
> r
->fragment_size
)
1210 schunk
.length
= r
->fragment_size
;
1212 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
1214 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
1215 pa_memblock_unref(schunk
.memblock
);
1222 /*** sink input callbacks ***/
1224 static void handle_seek(playback_stream
*s
, int64_t indexw
) {
1225 playback_stream_assert_ref(s
);
1227 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1229 if (s
->sink_input
->thread_info
.underrun_for
> 0) {
1231 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1233 if (pa_memblockq_is_readable(s
->memblockq
)) {
1235 /* We just ended an underrun, let's ask the sink
1236 * for a complete rewind rewrite */
1238 pa_log_debug("Requesting rewind due to end of underrun.");
1239 pa_sink_input_request_rewind(s
->sink_input
,
1240 (size_t) (s
->sink_input
->thread_info
.underrun_for
== (size_t) -1 ? 0 : s
->sink_input
->thread_info
.underrun_for
),
1247 indexr
= pa_memblockq_get_read_index(s
->memblockq
);
1249 if (indexw
< indexr
) {
1250 /* OK, the sink already asked for this data, so
1251 * let's have it usk us again */
1253 pa_log_debug("Requesting rewind due to rewrite.");
1254 pa_sink_input_request_rewind(s
->sink_input
, (size_t) (indexr
- indexw
), TRUE
, FALSE
);
1258 playback_stream_request_bytes(s
);
1261 /* Called from thread context */
1262 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1263 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1266 pa_sink_input_assert_ref(i
);
1267 s
= PLAYBACK_STREAM(i
->userdata
);
1268 playback_stream_assert_ref(s
);
1272 case SINK_INPUT_MESSAGE_SEEK
: {
1275 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1276 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
1278 handle_seek(s
, windex
);
1282 case SINK_INPUT_MESSAGE_POST_DATA
: {
1287 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1289 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1291 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
1292 pa_log_warn("Failed to push data into queue");
1293 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
1294 pa_memblockq_seek(s
->memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
1297 handle_seek(s
, windex
);
1299 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1304 case SINK_INPUT_MESSAGE_DRAIN
:
1305 case SINK_INPUT_MESSAGE_FLUSH
:
1306 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1307 case SINK_INPUT_MESSAGE_TRIGGER
: {
1310 pa_sink_input
*isync
;
1311 void (*func
)(pa_memblockq
*bq
);
1314 case SINK_INPUT_MESSAGE_FLUSH
:
1315 func
= pa_memblockq_flush_write
;
1318 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1319 func
= pa_memblockq_prebuf_force
;
1322 case SINK_INPUT_MESSAGE_DRAIN
:
1323 case SINK_INPUT_MESSAGE_TRIGGER
:
1324 func
= pa_memblockq_prebuf_disable
;
1328 pa_assert_not_reached();
1331 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1333 handle_seek(s
, windex
);
1335 /* Do the same for all other members in the sync group */
1336 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
1337 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1338 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1339 func(ssync
->memblockq
);
1340 handle_seek(ssync
, windex
);
1343 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1344 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1345 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1346 func(ssync
->memblockq
);
1347 handle_seek(ssync
, windex
);
1350 if (code
== SINK_INPUT_MESSAGE_DRAIN
) {
1351 if (!pa_memblockq_is_readable(s
->memblockq
))
1352 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
1354 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
1355 s
->drain_request
= TRUE
;
1362 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1364 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1365 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1366 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1369 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1372 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1374 pa_memblockq_prebuf_force(s
->memblockq
);
1376 handle_seek(s
, windex
);
1378 /* Fall through to the default handler */
1382 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1383 pa_usec_t
*r
= userdata
;
1385 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1387 /* Fall through, the default handler will add in the extra
1388 * latency added by the resampler */
1393 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1396 /* Called from thread context */
1397 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
1400 pa_sink_input_assert_ref(i
);
1401 s
= PLAYBACK_STREAM(i
->userdata
);
1402 playback_stream_assert_ref(s
);
1405 /* pa_log("%s, pop(): %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1407 if (pa_memblockq_is_readable(s
->memblockq
))
1408 s
->is_underrun
= FALSE
;
1410 /* pa_log("%s, UNDERRUN: %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1412 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1413 s
->drain_request
= FALSE
;
1414 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
);
1415 } else if (!s
->is_underrun
)
1416 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1418 s
->is_underrun
= TRUE
;
1420 playback_stream_request_bytes(s
);
1423 /* This call will not fail with prebuf=0, hence we check for
1424 underrun explicitly above */
1425 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
1428 chunk
->length
= PA_MIN(nbytes
, chunk
->length
);
1430 if (i
->thread_info
.underrun_for
> 0)
1431 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_STARTED
, NULL
, 0, NULL
, NULL
);
1433 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1434 playback_stream_request_bytes(s
);
1439 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1442 pa_sink_input_assert_ref(i
);
1443 s
= PLAYBACK_STREAM(i
->userdata
);
1444 playback_stream_assert_ref(s
);
1446 /* If we are in an underrun, then we don't rewind */
1447 if (i
->thread_info
.underrun_for
> 0)
1450 pa_memblockq_rewind(s
->memblockq
, nbytes
);
1453 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1456 pa_sink_input_assert_ref(i
);
1457 s
= PLAYBACK_STREAM(i
->userdata
);
1458 playback_stream_assert_ref(s
);
1460 pa_memblockq_set_maxrewind(s
->memblockq
, nbytes
);
1463 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
1467 pa_sink_input_assert_ref(i
);
1468 s
= PLAYBACK_STREAM(i
->userdata
);
1469 playback_stream_assert_ref(s
);
1471 tlength
= nbytes
+2*pa_memblockq_get_minreq(s
->memblockq
);
1473 if (pa_memblockq_get_tlength(s
->memblockq
) < tlength
)
1474 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1477 /* Called from main context */
1478 static void sink_input_kill_cb(pa_sink_input
*i
) {
1481 pa_sink_input_assert_ref(i
);
1482 s
= PLAYBACK_STREAM(i
->userdata
);
1483 playback_stream_assert_ref(s
);
1485 playback_stream_send_killed(s
);
1486 playback_stream_unlink(s
);
1489 /* Called from main context */
1490 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1494 pa_sink_input_assert_ref(i
);
1495 s
= PLAYBACK_STREAM(i
->userdata
);
1496 playback_stream_assert_ref(s
);
1498 if (s
->connection
->version
< 12)
1501 t
= pa_tagstruct_new(NULL
, 0);
1502 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1503 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1504 pa_tagstruct_putu32(t
, s
->index
);
1505 pa_tagstruct_put_boolean(t
, suspend
);
1506 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1509 /* Called from main context */
1510 static void sink_input_moved_cb(pa_sink_input
*i
) {
1513 uint32_t maxlength
, tlength
, prebuf
, minreq
;
1515 pa_sink_input_assert_ref(i
);
1516 s
= PLAYBACK_STREAM(i
->userdata
);
1517 playback_stream_assert_ref(s
);
1519 maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
1520 tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
1521 prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
1522 minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
1524 fix_playback_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1525 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1526 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1527 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
1528 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
1529 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1531 if (s
->connection
->version
< 12)
1534 t
= pa_tagstruct_new(NULL
, 0);
1535 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1536 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1537 pa_tagstruct_putu32(t
, s
->index
);
1538 pa_tagstruct_putu32(t
, i
->sink
->index
);
1539 pa_tagstruct_puts(t
, i
->sink
->name
);
1540 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1542 if (s
->connection
->version
>= 13) {
1543 pa_tagstruct_putu32(t
, maxlength
);
1544 pa_tagstruct_putu32(t
, tlength
);
1545 pa_tagstruct_putu32(t
, prebuf
);
1546 pa_tagstruct_putu32(t
, minreq
);
1547 pa_tagstruct_put_usec(t
, s
->sink_latency
);
1550 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1553 /*** source_output callbacks ***/
1555 /* Called from thread context */
1556 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1559 pa_source_output_assert_ref(o
);
1560 s
= RECORD_STREAM(o
->userdata
);
1561 record_stream_assert_ref(s
);
1564 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1567 static void source_output_kill_cb(pa_source_output
*o
) {
1570 pa_source_output_assert_ref(o
);
1571 s
= RECORD_STREAM(o
->userdata
);
1572 record_stream_assert_ref(s
);
1574 record_stream_send_killed(s
);
1575 record_stream_unlink(s
);
1578 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1581 pa_source_output_assert_ref(o
);
1582 s
= RECORD_STREAM(o
->userdata
);
1583 record_stream_assert_ref(s
);
1585 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1587 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1590 /* Called from main context */
1591 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1595 pa_source_output_assert_ref(o
);
1596 s
= RECORD_STREAM(o
->userdata
);
1597 record_stream_assert_ref(s
);
1599 if (s
->connection
->version
< 12)
1602 t
= pa_tagstruct_new(NULL
, 0);
1603 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1604 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1605 pa_tagstruct_putu32(t
, s
->index
);
1606 pa_tagstruct_put_boolean(t
, suspend
);
1607 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1610 /* Called from main context */
1611 static void source_output_moved_cb(pa_source_output
*o
) {
1614 uint32_t maxlength
, fragsize
;
1616 pa_source_output_assert_ref(o
);
1617 s
= RECORD_STREAM(o
->userdata
);
1618 record_stream_assert_ref(s
);
1620 fragsize
= (uint32_t) s
->fragment_size
;
1621 maxlength
= (uint32_t) pa_memblockq_get_length(s
->memblockq
);
1623 fix_record_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &fragsize
);
1624 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1625 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
1627 if (s
->connection
->version
< 12)
1630 t
= pa_tagstruct_new(NULL
, 0);
1631 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1632 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1633 pa_tagstruct_putu32(t
, s
->index
);
1634 pa_tagstruct_putu32(t
, o
->source
->index
);
1635 pa_tagstruct_puts(t
, o
->source
->name
);
1636 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1638 if (s
->connection
->version
>= 13) {
1639 pa_tagstruct_putu32(t
, maxlength
);
1640 pa_tagstruct_putu32(t
, fragsize
);
1641 pa_tagstruct_put_usec(t
, s
->source_latency
);
1644 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1647 /*** pdispatch callbacks ***/
1649 static void protocol_error(pa_native_connection
*c
) {
1650 pa_log("protocol error, kicking client");
1651 native_connection_unlink(c
);
1654 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1655 if (!(expression)) { \
1656 pa_pstream_send_error((pstream), (tag), (error)); \
1661 static pa_tagstruct
*reply_new(uint32_t tag
) {
1662 pa_tagstruct
*reply
;
1664 reply
= pa_tagstruct_new(NULL
, 0);
1665 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1666 pa_tagstruct_putu32(reply
, tag
);
1670 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1671 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1673 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1674 const char *name
= NULL
, *sink_name
;
1677 pa_tagstruct
*reply
;
1678 pa_sink
*sink
= NULL
;
1686 fix_channels
= FALSE
,
1688 variable_rate
= FALSE
,
1690 adjust_latency
= FALSE
,
1691 early_requests
= FALSE
;
1693 pa_sink_input_flags_t flags
= 0;
1695 pa_bool_t volume_set
= TRUE
;
1697 pa_native_connection_assert_ref(c
);
1700 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1703 PA_TAG_SAMPLE_SPEC
, &ss
,
1704 PA_TAG_CHANNEL_MAP
, &map
,
1705 PA_TAG_U32
, &sink_index
,
1706 PA_TAG_STRING
, &sink_name
,
1707 PA_TAG_U32
, &maxlength
,
1708 PA_TAG_BOOLEAN
, &corked
,
1709 PA_TAG_U32
, &tlength
,
1710 PA_TAG_U32
, &prebuf
,
1711 PA_TAG_U32
, &minreq
,
1712 PA_TAG_U32
, &syncid
,
1713 PA_TAG_CVOLUME
, &volume
,
1714 PA_TAG_INVALID
) < 0) {
1720 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1721 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
1722 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
1723 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1724 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1725 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1726 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1727 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1729 p
= pa_proplist_new();
1732 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1734 if (c
->version
>= 12) {
1735 /* Since 0.9.8 the user can ask for a couple of additional flags */
1737 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1738 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1739 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1740 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1741 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1742 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1743 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1746 pa_proplist_free(p
);
1751 if (c
->version
>= 13) {
1753 if (pa_tagstruct_get_boolean(t
, &muted
) < 0 ||
1754 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1755 pa_tagstruct_get_proplist(t
, p
) < 0) {
1757 pa_proplist_free(p
);
1762 if (c
->version
>= 14) {
1764 if (pa_tagstruct_get_boolean(t
, &volume_set
) < 0 ||
1765 pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
1767 pa_proplist_free(p
);
1772 if (!pa_tagstruct_eof(t
)) {
1774 pa_proplist_free(p
);
1778 if (sink_index
!= PA_INVALID_INDEX
) {
1780 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1781 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1782 pa_proplist_free(p
);
1786 } else if (sink_name
) {
1788 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1))) {
1789 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1790 pa_proplist_free(p
);
1796 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1797 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1798 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1799 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1800 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1801 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1802 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1803 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0);
1805 s
= playback_stream_new(c
, sink
, &ss
, &map
, &maxlength
, &tlength
, &prebuf
, &minreq
, volume_set
? &volume
: NULL
, muted
, syncid
, &missing
, flags
, p
, adjust_latency
, early_requests
);
1806 pa_proplist_free(p
);
1808 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1810 reply
= reply_new(tag
);
1811 pa_tagstruct_putu32(reply
, s
->index
);
1812 pa_assert(s
->sink_input
);
1813 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1814 pa_tagstruct_putu32(reply
, missing
);
1816 /* pa_log("initial request is %u", missing); */
1818 if (c
->version
>= 9) {
1819 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1821 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1822 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1823 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1824 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1827 if (c
->version
>= 12) {
1828 /* Since 0.9.8 we support sending the chosen sample
1829 * spec/channel map/device/suspend status back to the
1832 pa_tagstruct_put_sample_spec(reply
, &ss
);
1833 pa_tagstruct_put_channel_map(reply
, &map
);
1835 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1836 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1838 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1841 if (c
->version
>= 13)
1842 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
1844 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1847 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1848 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1851 pa_native_connection_assert_ref(c
);
1854 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1855 !pa_tagstruct_eof(t
)) {
1860 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1864 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1866 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1867 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1871 playback_stream_unlink(s
);
1875 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1877 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1878 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1882 record_stream_unlink(s
);
1886 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1889 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1890 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1894 upload_stream_unlink(s
);
1899 pa_assert_not_reached();
1902 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1905 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1906 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1908 uint32_t maxlength
, fragment_size
;
1909 uint32_t source_index
;
1910 const char *name
= NULL
, *source_name
;
1913 pa_tagstruct
*reply
;
1914 pa_source
*source
= NULL
;
1921 fix_channels
= FALSE
,
1923 variable_rate
= FALSE
,
1924 adjust_latency
= FALSE
,
1925 peak_detect
= FALSE
,
1926 early_requests
= FALSE
;
1927 pa_source_output_flags_t flags
= 0;
1929 uint32_t direct_on_input_idx
= PA_INVALID_INDEX
;
1930 pa_sink_input
*direct_on_input
= NULL
;
1932 pa_native_connection_assert_ref(c
);
1935 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1936 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1937 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1938 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1939 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1940 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1941 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1942 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1947 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1948 CHECK_VALIDITY(c
->pstream
, !source_name
|| pa_namereg_is_valid_name(source_name
), tag
, PA_ERR_INVALID
);
1949 CHECK_VALIDITY(c
->pstream
, source_index
== PA_INVALID_INDEX
|| !source_name
, tag
, PA_ERR_INVALID
);
1950 CHECK_VALIDITY(c
->pstream
, !source_name
|| source_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1951 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1952 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1953 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1955 p
= pa_proplist_new();
1958 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1960 if (c
->version
>= 12) {
1961 /* Since 0.9.8 the user can ask for a couple of additional flags */
1963 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1964 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1965 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1966 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1967 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1968 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1969 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1972 pa_proplist_free(p
);
1977 if (c
->version
>= 13) {
1979 if (pa_tagstruct_get_boolean(t
, &peak_detect
) < 0 ||
1980 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1981 pa_tagstruct_get_proplist(t
, p
) < 0 ||
1982 pa_tagstruct_getu32(t
, &direct_on_input_idx
) < 0) {
1984 pa_proplist_free(p
);
1989 if (c
->version
>= 14) {
1991 if (pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
1993 pa_proplist_free(p
);
1998 if (!pa_tagstruct_eof(t
)) {
2000 pa_proplist_free(p
);
2004 if (source_index
!= PA_INVALID_INDEX
) {
2006 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
2007 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2008 pa_proplist_free(p
);
2012 } else if (source_name
) {
2014 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1))) {
2015 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2016 pa_proplist_free(p
);
2021 if (direct_on_input_idx
!= PA_INVALID_INDEX
) {
2023 if (!(direct_on_input
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, direct_on_input_idx
))) {
2024 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2025 pa_proplist_free(p
);
2031 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
2032 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
2033 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
2034 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
2035 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
2036 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
2037 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
2038 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0);
2040 s
= record_stream_new(c
, source
, &ss
, &map
, peak_detect
, &maxlength
, &fragment_size
, flags
, p
, adjust_latency
, direct_on_input
, early_requests
);
2041 pa_proplist_free(p
);
2043 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2045 reply
= reply_new(tag
);
2046 pa_tagstruct_putu32(reply
, s
->index
);
2047 pa_assert(s
->source_output
);
2048 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
2050 if (c
->version
>= 9) {
2051 /* Since 0.9 we support sending the buffer metrics back to the client */
2053 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
2054 pa_tagstruct_putu32(reply
, (uint32_t) fragment_size
);
2057 if (c
->version
>= 12) {
2058 /* Since 0.9.8 we support sending the chosen sample
2059 * spec/channel map/device/suspend status back to the
2062 pa_tagstruct_put_sample_spec(reply
, &ss
);
2063 pa_tagstruct_put_channel_map(reply
, &map
);
2065 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
2066 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
2068 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
2071 if (c
->version
>= 13)
2072 pa_tagstruct_put_usec(reply
, s
->source_latency
);
2074 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2077 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2078 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2081 pa_native_connection_assert_ref(c
);
2084 if (!pa_tagstruct_eof(t
)) {
2089 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2090 ret
= pa_core_exit(c
->protocol
->core
, FALSE
, 0);
2091 CHECK_VALIDITY(c
->pstream
, ret
>= 0, tag
, PA_ERR_ACCESS
);
2093 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
2096 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2097 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2099 pa_tagstruct
*reply
;
2100 pa_bool_t shm_on_remote
= FALSE
, do_shm
;
2102 pa_native_connection_assert_ref(c
);
2105 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
2106 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
2107 !pa_tagstruct_eof(t
)) {
2112 /* Minimum supported version */
2113 if (c
->version
< 8) {
2114 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
2118 /* Starting with protocol version 13 the MSB of the version tag
2119 reflects if shm is available for this pa_native_connection or
2121 if (c
->version
>= 13) {
2122 shm_on_remote
= !!(c
->version
& 0x80000000U
);
2123 c
->version
&= 0x7FFFFFFFU
;
2126 pa_log_debug("Protocol version: remote %u, local %u", c
->version
, PA_PROTOCOL_VERSION
);
2128 pa_proplist_setf(c
->client
->proplist
, "native-protocol.version", "%u", c
->version
);
2130 if (!c
->authorized
) {
2131 pa_bool_t success
= FALSE
;
2134 const pa_creds
*creds
;
2136 if ((creds
= pa_pdispatch_creds(pd
))) {
2137 if (creds
->uid
== getuid())
2139 else if (c
->options
->auth_group
) {
2143 if ((gid
= pa_get_gid_of_group(c
->options
->auth_group
)) == (gid_t
) -1)
2144 pa_log_warn("Failed to get GID of group '%s'", c
->options
->auth_group
);
2145 else if (gid
== creds
->gid
)
2149 if ((r
= pa_uid_in_group(creds
->uid
, c
->options
->auth_group
)) < 0)
2150 pa_log_warn("Failed to check group membership.");
2156 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
2157 (unsigned long) creds
->uid
,
2158 (unsigned long) creds
->gid
,
2163 if (!success
&& c
->options
->auth_cookie
) {
2166 if ((ac
= pa_auth_cookie_read(c
->options
->auth_cookie
, PA_NATIVE_COOKIE_LENGTH
)))
2167 if (memcmp(ac
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
2172 pa_log_warn("Denied access to client with invalid authorization data.");
2173 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2177 c
->authorized
= TRUE
;
2178 if (c
->auth_timeout_event
) {
2179 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
2180 c
->auth_timeout_event
= NULL
;
2184 /* Enable shared memory support if possible */
2186 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
2189 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm
));
2192 if (c
->version
< 10 || (c
->version
>= 13 && !shm_on_remote
))
2197 /* Only enable SHM if both sides are owned by the same
2198 * user. This is a security measure because otherwise data
2199 * private to the user might leak. */
2201 const pa_creds
*creds
;
2202 if (!(creds
= pa_pdispatch_creds(pd
)) || getuid() != creds
->uid
)
2207 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm
));
2208 pa_pstream_enable_shm(c
->pstream
, do_shm
);
2210 reply
= reply_new(tag
);
2211 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
| (do_shm
? 0x80000000 : 0));
2215 /* SHM support is only enabled after both sides made sure they are the same user. */
2219 ucred
.uid
= getuid();
2220 ucred
.gid
= getgid();
2222 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
2225 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2229 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2230 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2231 const char *name
= NULL
;
2233 pa_tagstruct
*reply
;
2235 pa_native_connection_assert_ref(c
);
2238 p
= pa_proplist_new();
2240 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
2241 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2242 !pa_tagstruct_eof(t
)) {
2245 pa_proplist_free(p
);
2250 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
2251 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2252 pa_proplist_free(p
);
2256 pa_proplist_update(c
->client
->proplist
, PA_UPDATE_REPLACE
, p
);
2257 pa_proplist_free(p
);
2259 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2261 reply
= reply_new(tag
);
2263 if (c
->version
>= 13)
2264 pa_tagstruct_putu32(reply
, c
->client
->index
);
2266 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2269 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2270 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2272 uint32_t idx
= PA_IDXSET_INVALID
;
2274 pa_native_connection_assert_ref(c
);
2277 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2278 !pa_tagstruct_eof(t
)) {
2283 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2284 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2286 if (command
== PA_COMMAND_LOOKUP_SINK
) {
2288 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
2292 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
2293 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
2294 idx
= source
->index
;
2297 if (idx
== PA_IDXSET_INVALID
)
2298 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2300 pa_tagstruct
*reply
;
2301 reply
= reply_new(tag
);
2302 pa_tagstruct_putu32(reply
, idx
);
2303 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2307 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2308 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2312 pa_native_connection_assert_ref(c
);
2315 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2316 !pa_tagstruct_eof(t
)) {
2321 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2322 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2323 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2324 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2326 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
);
2329 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2330 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2331 pa_tagstruct
*reply
;
2332 const pa_mempool_stat
*stat
;
2334 pa_native_connection_assert_ref(c
);
2337 if (!pa_tagstruct_eof(t
)) {
2342 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2344 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
2346 reply
= reply_new(tag
);
2347 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
2348 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
2349 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
2350 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
2351 pa_tagstruct_putu32(reply
, (uint32_t) pa_scache_total_size(c
->protocol
->core
));
2352 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2355 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2356 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2357 pa_tagstruct
*reply
;
2359 struct timeval tv
, now
;
2363 pa_native_connection_assert_ref(c
);
2366 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2367 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2368 !pa_tagstruct_eof(t
)) {
2373 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2374 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2375 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2376 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2377 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
)
2379 reply
= reply_new(tag
);
2381 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
2382 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
2384 pa_tagstruct_put_usec(reply
, latency
);
2386 pa_tagstruct_put_usec(reply
, 0);
2387 pa_tagstruct_put_boolean(reply
, s
->sink_input
->thread_info
.playing_for
> 0);
2388 pa_tagstruct_put_timeval(reply
, &tv
);
2389 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2390 pa_tagstruct_puts64(reply
, s
->write_index
);
2391 pa_tagstruct_puts64(reply
, s
->read_index
);
2393 if (c
->version
>= 13) {
2394 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.underrun_for
);
2395 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.playing_for
);
2398 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2401 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2402 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2403 pa_tagstruct
*reply
;
2405 struct timeval tv
, now
;
2408 pa_native_connection_assert_ref(c
);
2411 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2412 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2413 !pa_tagstruct_eof(t
)) {
2418 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2419 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2420 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2422 reply
= reply_new(tag
);
2423 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
2424 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
2425 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_RUNNING
);
2426 pa_tagstruct_put_timeval(reply
, &tv
);
2427 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2428 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
2429 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
2430 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2433 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2434 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2437 const char *name
= NULL
;
2440 pa_tagstruct
*reply
;
2443 pa_native_connection_assert_ref(c
);
2446 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2447 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
2448 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
2449 pa_tagstruct_getu32(t
, &length
) < 0) {
2454 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2455 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
2456 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
2457 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
2458 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
2459 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
2461 p
= pa_proplist_new();
2463 if (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) {
2465 pa_proplist_free(p
);
2469 if (c
->version
< 13)
2470 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2472 if (!(name
= pa_proplist_gets(p
, PA_PROP_EVENT_ID
)))
2473 name
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
);
2475 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2477 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
2478 pa_proplist_free(p
);
2480 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2482 reply
= reply_new(tag
);
2483 pa_tagstruct_putu32(reply
, s
->index
);
2484 pa_tagstruct_putu32(reply
, length
);
2485 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2488 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2489 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2494 pa_native_connection_assert_ref(c
);
2497 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
2498 !pa_tagstruct_eof(t
)) {
2503 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2505 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
2506 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2507 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2509 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
2510 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
2512 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2514 upload_stream_unlink(s
);
2517 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2518 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2519 uint32_t sink_index
;
2522 const char *name
, *sink_name
;
2525 pa_tagstruct
*reply
;
2527 pa_native_connection_assert_ref(c
);
2530 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2532 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
2533 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
2534 pa_tagstruct_getu32(t
, &volume
) < 0 ||
2535 pa_tagstruct_gets(t
, &name
) < 0) {
2540 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
2541 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
2542 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2543 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2545 if (sink_index
!= PA_INVALID_INDEX
)
2546 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
2548 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
2550 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2552 p
= pa_proplist_new();
2554 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2555 !pa_tagstruct_eof(t
)) {
2557 pa_proplist_free(p
);
2561 pa_proplist_update(p
, PA_UPDATE_MERGE
, c
->client
->proplist
);
2563 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2564 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2565 pa_proplist_free(p
);
2569 pa_proplist_free(p
);
2571 reply
= reply_new(tag
);
2573 if (c
->version
>= 13)
2574 pa_tagstruct_putu32(reply
, idx
);
2576 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2579 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2580 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2583 pa_native_connection_assert_ref(c
);
2586 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2587 !pa_tagstruct_eof(t
)) {
2592 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2593 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2595 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2596 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2600 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2603 static void fixup_sample_spec(pa_native_connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2606 pa_assert(original
);
2610 if (c
->version
< 12) {
2611 /* Before protocol version 12 we didn't support S32 samples,
2612 * so we need to lie about this to the client */
2614 if (fixed
->format
== PA_SAMPLE_S32LE
)
2615 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2616 if (fixed
->format
== PA_SAMPLE_S32BE
)
2617 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2621 static void sink_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2622 pa_sample_spec fixed_ss
;
2625 pa_sink_assert_ref(sink
);
2627 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2631 PA_TAG_U32
, sink
->index
,
2632 PA_TAG_STRING
, sink
->name
,
2633 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2634 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2635 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2636 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2637 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, FALSE
),
2638 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
, FALSE
),
2639 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2640 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2641 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2642 PA_TAG_STRING
, sink
->driver
,
2643 PA_TAG_U32
, sink
->flags
,
2646 if (c
->version
>= 13) {
2647 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2648 pa_tagstruct_put_usec(t
, pa_sink_get_requested_latency(sink
));
2652 static void source_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2653 pa_sample_spec fixed_ss
;
2656 pa_source_assert_ref(source
);
2658 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2662 PA_TAG_U32
, source
->index
,
2663 PA_TAG_STRING
, source
->name
,
2664 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2665 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2666 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2667 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2668 PA_TAG_CVOLUME
, pa_source_get_volume(source
, FALSE
),
2669 PA_TAG_BOOLEAN
, pa_source_get_mute(source
, FALSE
),
2670 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2671 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2672 PA_TAG_USEC
, pa_source_get_latency(source
),
2673 PA_TAG_STRING
, source
->driver
,
2674 PA_TAG_U32
, source
->flags
,
2677 if (c
->version
>= 13) {
2678 pa_tagstruct_put_proplist(t
, source
->proplist
);
2679 pa_tagstruct_put_usec(t
, pa_source_get_requested_latency(source
));
2684 static void client_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2688 pa_tagstruct_putu32(t
, client
->index
);
2689 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2690 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2691 pa_tagstruct_puts(t
, client
->driver
);
2693 if (c
->version
>= 13)
2694 pa_tagstruct_put_proplist(t
, client
->proplist
);
2698 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
2702 pa_tagstruct_putu32(t
, module
->index
);
2703 pa_tagstruct_puts(t
, module
->name
);
2704 pa_tagstruct_puts(t
, module
->argument
);
2705 pa_tagstruct_putu32(t
, (uint32_t) module
->n_used
);
2706 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
2709 static void sink_input_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2710 pa_sample_spec fixed_ss
;
2711 pa_usec_t sink_latency
;
2714 pa_sink_input_assert_ref(s
);
2716 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2718 pa_tagstruct_putu32(t
, s
->index
);
2719 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2720 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2721 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2722 pa_tagstruct_putu32(t
, s
->sink
->index
);
2723 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2724 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2725 pa_tagstruct_put_cvolume(t
, pa_sink_input_get_volume(s
));
2726 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
, &sink_latency
));
2727 pa_tagstruct_put_usec(t
, sink_latency
);
2728 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2729 pa_tagstruct_puts(t
, s
->driver
);
2730 if (c
->version
>= 11)
2731 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2732 if (c
->version
>= 13)
2733 pa_tagstruct_put_proplist(t
, s
->proplist
);
2736 static void source_output_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2737 pa_sample_spec fixed_ss
;
2738 pa_usec_t source_latency
;
2741 pa_source_output_assert_ref(s
);
2743 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2745 pa_tagstruct_putu32(t
, s
->index
);
2746 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2747 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2748 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2749 pa_tagstruct_putu32(t
, s
->source
->index
);
2750 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2751 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2752 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
, &source_latency
));
2753 pa_tagstruct_put_usec(t
, source_latency
);
2754 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2755 pa_tagstruct_puts(t
, s
->driver
);
2757 if (c
->version
>= 13)
2758 pa_tagstruct_put_proplist(t
, s
->proplist
);
2761 static void scache_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2762 pa_sample_spec fixed_ss
;
2767 if (e
->memchunk
.memblock
)
2768 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2770 memset(&fixed_ss
, 0, sizeof(fixed_ss
));
2772 pa_tagstruct_putu32(t
, e
->index
);
2773 pa_tagstruct_puts(t
, e
->name
);
2774 pa_tagstruct_put_cvolume(t
, &e
->volume
);
2775 pa_tagstruct_put_usec(t
, e
->memchunk
.memblock
? pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
) : 0);
2776 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2777 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2778 pa_tagstruct_putu32(t
, (uint32_t) e
->memchunk
.length
);
2779 pa_tagstruct_put_boolean(t
, e
->lazy
);
2780 pa_tagstruct_puts(t
, e
->filename
);
2782 if (c
->version
>= 13)
2783 pa_tagstruct_put_proplist(t
, e
->proplist
);
2786 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2787 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2789 pa_sink
*sink
= NULL
;
2790 pa_source
*source
= NULL
;
2791 pa_client
*client
= NULL
;
2792 pa_module
*module
= NULL
;
2793 pa_sink_input
*si
= NULL
;
2794 pa_source_output
*so
= NULL
;
2795 pa_scache_entry
*sce
= NULL
;
2796 const char *name
= NULL
;
2797 pa_tagstruct
*reply
;
2799 pa_native_connection_assert_ref(c
);
2802 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2803 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2804 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2805 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2806 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2807 pa_tagstruct_gets(t
, &name
) < 0) ||
2808 !pa_tagstruct_eof(t
)) {
2813 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2814 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2815 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2816 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2817 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2819 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2820 if (idx
!= PA_INVALID_INDEX
)
2821 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2823 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2824 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2825 if (idx
!= PA_INVALID_INDEX
)
2826 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2828 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2829 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2830 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2831 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2832 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2833 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2834 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2835 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2836 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2838 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2839 if (idx
!= PA_INVALID_INDEX
)
2840 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2842 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
2845 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
2846 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2850 reply
= reply_new(tag
);
2852 sink_fill_tagstruct(c
, reply
, sink
);
2854 source_fill_tagstruct(c
, reply
, source
);
2856 client_fill_tagstruct(c
, reply
, client
);
2858 module_fill_tagstruct(reply
, module
);
2860 sink_input_fill_tagstruct(c
, reply
, si
);
2862 source_output_fill_tagstruct(c
, reply
, so
);
2864 scache_fill_tagstruct(c
, reply
, sce
);
2865 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2868 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2869 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2873 pa_tagstruct
*reply
;
2875 pa_native_connection_assert_ref(c
);
2878 if (!pa_tagstruct_eof(t
)) {
2883 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2885 reply
= reply_new(tag
);
2887 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2888 i
= c
->protocol
->core
->sinks
;
2889 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2890 i
= c
->protocol
->core
->sources
;
2891 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2892 i
= c
->protocol
->core
->clients
;
2893 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2894 i
= c
->protocol
->core
->modules
;
2895 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2896 i
= c
->protocol
->core
->sink_inputs
;
2897 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2898 i
= c
->protocol
->core
->source_outputs
;
2900 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2901 i
= c
->protocol
->core
->scache
;
2905 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2906 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2907 sink_fill_tagstruct(c
, reply
, p
);
2908 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2909 source_fill_tagstruct(c
, reply
, p
);
2910 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2911 client_fill_tagstruct(c
, reply
, p
);
2912 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2913 module_fill_tagstruct(reply
, p
);
2914 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2915 sink_input_fill_tagstruct(c
, reply
, p
);
2916 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2917 source_output_fill_tagstruct(c
, reply
, p
);
2919 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2920 scache_fill_tagstruct(c
, reply
, p
);
2925 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2928 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2929 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2930 pa_tagstruct
*reply
;
2933 pa_sample_spec fixed_ss
;
2935 pa_native_connection_assert_ref(c
);
2938 if (!pa_tagstruct_eof(t
)) {
2943 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2945 reply
= reply_new(tag
);
2946 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
2947 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
2948 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
2949 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
2951 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
2952 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
2954 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
2955 pa_tagstruct_puts(reply
, n
);
2956 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
2957 pa_tagstruct_puts(reply
, n
);
2959 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
2961 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2964 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
2966 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2968 pa_native_connection_assert_ref(c
);
2970 t
= pa_tagstruct_new(NULL
, 0);
2971 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
2972 pa_tagstruct_putu32(t
, (uint32_t) -1);
2973 pa_tagstruct_putu32(t
, e
);
2974 pa_tagstruct_putu32(t
, idx
);
2975 pa_pstream_send_tagstruct(c
->pstream
, t
);
2978 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2979 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2980 pa_subscription_mask_t m
;
2982 pa_native_connection_assert_ref(c
);
2985 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
2986 !pa_tagstruct_eof(t
)) {
2991 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2992 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
2994 if (c
->subscription
)
2995 pa_subscription_free(c
->subscription
);
2998 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
2999 pa_assert(c
->subscription
);
3001 c
->subscription
= NULL
;
3003 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3006 static void command_set_volume(
3013 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3016 pa_sink
*sink
= NULL
;
3017 pa_source
*source
= NULL
;
3018 pa_sink_input
*si
= NULL
;
3019 const char *name
= NULL
;
3021 pa_native_connection_assert_ref(c
);
3024 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3025 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3026 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3027 pa_tagstruct_get_cvolume(t
, &volume
) ||
3028 !pa_tagstruct_eof(t
)) {
3033 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3034 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3035 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3036 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3037 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3038 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
3042 case PA_COMMAND_SET_SINK_VOLUME
:
3043 if (idx
!= PA_INVALID_INDEX
)
3044 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3046 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3049 case PA_COMMAND_SET_SOURCE_VOLUME
:
3050 if (idx
!= PA_INVALID_INDEX
)
3051 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3053 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3056 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
3057 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3061 pa_assert_not_reached();
3064 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3067 pa_sink_set_volume(sink
, &volume
);
3069 pa_source_set_volume(source
, &volume
);
3071 pa_sink_input_set_volume(si
, &volume
);
3073 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3076 static void command_set_mute(
3083 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3086 pa_sink
*sink
= NULL
;
3087 pa_source
*source
= NULL
;
3088 pa_sink_input
*si
= NULL
;
3089 const char *name
= NULL
;
3091 pa_native_connection_assert_ref(c
);
3094 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3095 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3096 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3097 pa_tagstruct_get_boolean(t
, &mute
) ||
3098 !pa_tagstruct_eof(t
)) {
3103 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3104 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3105 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3106 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3107 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3111 case PA_COMMAND_SET_SINK_MUTE
:
3113 if (idx
!= PA_INVALID_INDEX
)
3114 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3116 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3120 case PA_COMMAND_SET_SOURCE_MUTE
:
3121 if (idx
!= PA_INVALID_INDEX
)
3122 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3124 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3128 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
3129 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3133 pa_assert_not_reached();
3136 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3139 pa_sink_set_mute(sink
, mute
);
3141 pa_source_set_mute(source
, mute
);
3143 pa_sink_input_set_mute(si
, mute
);
3145 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3148 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3149 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3154 pa_native_connection_assert_ref(c
);
3157 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3158 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3159 !pa_tagstruct_eof(t
)) {
3164 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3165 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3166 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3167 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3168 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3170 pa_sink_input_cork(s
->sink_input
, b
);
3173 s
->is_underrun
= TRUE
;
3175 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3178 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3179 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3183 pa_native_connection_assert_ref(c
);
3186 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3187 !pa_tagstruct_eof(t
)) {
3192 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3193 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3194 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3195 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3196 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3199 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3200 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3203 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3204 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3207 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3208 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3212 pa_assert_not_reached();
3215 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3218 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3219 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3224 pa_native_connection_assert_ref(c
);
3227 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3228 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3229 !pa_tagstruct_eof(t
)) {
3234 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3235 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3236 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3238 pa_source_output_cork(s
->source_output
, b
);
3239 pa_memblockq_prebuf_force(s
->memblockq
);
3240 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3243 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3244 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3248 pa_native_connection_assert_ref(c
);
3251 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3252 !pa_tagstruct_eof(t
)) {
3257 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3258 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3259 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3261 pa_memblockq_flush_read(s
->memblockq
);
3262 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3265 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3266 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3268 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3269 pa_tagstruct
*reply
;
3271 pa_native_connection_assert_ref(c
);
3274 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3279 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3281 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3283 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3285 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3286 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3287 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3289 if (pa_tagstruct_get(
3291 PA_TAG_U32
, &maxlength
,
3292 PA_TAG_U32
, &tlength
,
3293 PA_TAG_U32
, &prebuf
,
3294 PA_TAG_U32
, &minreq
,
3295 PA_TAG_INVALID
) < 0 ||
3296 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3297 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3298 !pa_tagstruct_eof(t
)) {
3303 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3304 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3305 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3306 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3307 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3308 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3310 reply
= reply_new(tag
);
3311 pa_tagstruct_putu32(reply
, maxlength
);
3312 pa_tagstruct_putu32(reply
, tlength
);
3313 pa_tagstruct_putu32(reply
, prebuf
);
3314 pa_tagstruct_putu32(reply
, minreq
);
3316 if (c
->version
>= 13)
3317 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3321 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3322 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3324 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3325 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3327 if (pa_tagstruct_get(
3329 PA_TAG_U32
, &maxlength
,
3330 PA_TAG_U32
, &fragsize
,
3331 PA_TAG_INVALID
) < 0 ||
3332 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3333 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3334 !pa_tagstruct_eof(t
)) {
3339 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &fragsize
);
3340 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3341 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3343 reply
= reply_new(tag
);
3344 pa_tagstruct_putu32(reply
, maxlength
);
3345 pa_tagstruct_putu32(reply
, fragsize
);
3347 if (c
->version
>= 13)
3348 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3351 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3354 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3355 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3359 pa_native_connection_assert_ref(c
);
3362 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3363 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3364 !pa_tagstruct_eof(t
)) {
3369 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3370 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3372 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3375 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3376 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3377 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3379 pa_sink_input_set_rate(s
->sink_input
, rate
);
3383 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3385 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3386 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3388 pa_source_output_set_rate(s
->source_output
, rate
);
3391 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3394 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3395 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3400 pa_native_connection_assert_ref(c
);
3403 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3405 p
= pa_proplist_new();
3407 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3409 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3410 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3411 !pa_tagstruct_eof(t
)) {
3413 pa_proplist_free(p
);
3419 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3420 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3421 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3422 !pa_tagstruct_eof(t
)) {
3424 pa_proplist_free(p
);
3429 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3431 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3434 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3435 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3436 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3438 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
3439 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3441 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3444 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3445 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3447 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
3448 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3450 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3452 pa_proplist_update(c
->client
->proplist
, mode
, p
);
3453 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3456 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3459 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3460 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3462 unsigned changed
= 0;
3464 pa_strlist
*l
= NULL
;
3466 pa_native_connection_assert_ref(c
);
3469 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3471 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3473 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3479 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3482 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3483 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3484 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3486 p
= s
->sink_input
->proplist
;
3488 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3491 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3492 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3494 p
= s
->source_output
->proplist
;
3496 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3498 p
= c
->client
->proplist
;
3504 if (pa_tagstruct_gets(t
, &k
) < 0) {
3513 l
= pa_strlist_prepend(l
, k
);
3516 if (!pa_tagstruct_eof(t
)) {
3525 l
= pa_strlist_pop(l
, &z
);
3530 changed
+= (unsigned) (pa_proplist_unset(p
, z
) >= 0);
3534 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3537 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3540 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3541 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3543 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3546 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3547 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3550 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3551 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3556 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3557 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3560 pa_native_connection_assert_ref(c
);
3563 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3564 !pa_tagstruct_eof(t
)) {
3569 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3570 CHECK_VALIDITY(c
->pstream
, !s
|| pa_namereg_is_valid_name(s
), tag
, PA_ERR_INVALID
);
3572 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
3573 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3576 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3577 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3581 pa_native_connection_assert_ref(c
);
3584 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3585 pa_tagstruct_gets(t
, &name
) < 0 ||
3586 !pa_tagstruct_eof(t
)) {
3591 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3592 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3594 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3597 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3598 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3599 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3601 pa_sink_input_set_name(s
->sink_input
, name
);
3605 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3607 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3608 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3610 pa_source_output_set_name(s
->source_output
, name
);
3613 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3616 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3617 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3620 pa_native_connection_assert_ref(c
);
3623 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3624 !pa_tagstruct_eof(t
)) {
3629 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3631 if (command
== PA_COMMAND_KILL_CLIENT
) {
3634 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3635 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3637 pa_native_connection_ref(c
);
3638 pa_client_kill(client
);
3640 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3643 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3644 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3646 pa_native_connection_ref(c
);
3647 pa_sink_input_kill(s
);
3649 pa_source_output
*s
;
3651 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3653 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3654 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3656 pa_native_connection_ref(c
);
3657 pa_source_output_kill(s
);
3660 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3661 pa_native_connection_unref(c
);
3664 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3665 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3667 const char *name
, *argument
;
3668 pa_tagstruct
*reply
;
3670 pa_native_connection_assert_ref(c
);
3673 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3674 pa_tagstruct_gets(t
, &argument
) < 0 ||
3675 !pa_tagstruct_eof(t
)) {
3680 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3681 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3682 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3684 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3685 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3689 reply
= reply_new(tag
);
3690 pa_tagstruct_putu32(reply
, m
->index
);
3691 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3694 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3695 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3699 pa_native_connection_assert_ref(c
);
3702 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3703 !pa_tagstruct_eof(t
)) {
3708 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3709 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3710 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3712 pa_module_unload_request(m
, FALSE
);
3713 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3716 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3717 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3718 const char *name
, *module
, *argument
;
3721 pa_tagstruct
*reply
;
3723 pa_native_connection_assert_ref(c
);
3726 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3727 pa_tagstruct_getu32(t
, &type
) < 0 ||
3728 pa_tagstruct_gets(t
, &module
) < 0 ||
3729 pa_tagstruct_gets(t
, &argument
) < 0 ||
3730 !pa_tagstruct_eof(t
)) {
3735 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3736 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3737 CHECK_VALIDITY(c
->pstream
, type
== 0 || type
== 1, tag
, PA_ERR_INVALID
);
3738 CHECK_VALIDITY(c
->pstream
, module
&& *module
&& pa_utf8_valid(module
), tag
, PA_ERR_INVALID
);
3739 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3741 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
3742 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
3746 reply
= reply_new(tag
);
3747 pa_tagstruct_putu32(reply
, idx
);
3748 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3751 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3752 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3753 const char *name
= NULL
;
3754 uint32_t type
, idx
= PA_IDXSET_INVALID
;
3757 pa_native_connection_assert_ref(c
);
3760 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3761 (pa_tagstruct_gets(t
, &name
) < 0 ||
3762 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3763 !pa_tagstruct_eof(t
)) {
3768 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3769 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3770 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& pa_utf8_valid(name
) && (type
== 0 || type
== 1)), tag
, PA_ERR_INVALID
);
3773 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3775 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
3777 CHECK_VALIDITY(c
->pstream
, r
>= 0, tag
, PA_ERR_NOENTITY
);
3779 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3782 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
3785 pa_tagstruct_putu32(t
, e
->index
);
3786 pa_tagstruct_puts(t
, e
->name
);
3787 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0U : 1U);
3788 pa_tagstruct_puts(t
, e
->module
);
3789 pa_tagstruct_puts(t
, e
->argument
);
3792 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3793 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3794 const pa_autoload_entry
*a
= NULL
;
3797 pa_tagstruct
*reply
;
3799 pa_native_connection_assert_ref(c
);
3802 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3803 (pa_tagstruct_gets(t
, &name
) < 0 ||
3804 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3805 !pa_tagstruct_eof(t
)) {
3810 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3811 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3812 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& (type
== 0 || type
== 1) && pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
3815 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3817 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
3819 CHECK_VALIDITY(c
->pstream
, a
, tag
, PA_ERR_NOENTITY
);
3821 reply
= reply_new(tag
);
3822 autoload_fill_tagstruct(reply
, a
);
3823 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3826 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3827 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3828 pa_tagstruct
*reply
;
3830 pa_native_connection_assert_ref(c
);
3833 if (!pa_tagstruct_eof(t
)) {
3838 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3840 reply
= reply_new(tag
);
3842 if (c
->protocol
->core
->autoload_hashmap
) {
3843 pa_autoload_entry
*a
;
3846 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
3847 autoload_fill_tagstruct(reply
, a
);
3850 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3853 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3854 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3855 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3856 const char *name_device
= NULL
;
3858 pa_native_connection_assert_ref(c
);
3861 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3862 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3863 pa_tagstruct_gets(t
, &name_device
) < 0 ||
3864 !pa_tagstruct_eof(t
)) {
3869 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3870 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3872 CHECK_VALIDITY(c
->pstream
, !name_device
|| pa_namereg_is_valid_name(name_device
), tag
, PA_ERR_INVALID
);
3873 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| name_device
, tag
, PA_ERR_INVALID
);
3874 CHECK_VALIDITY(c
->pstream
, idx_device
== PA_INVALID_INDEX
|| !name_device
, tag
, PA_ERR_INVALID
);
3875 CHECK_VALIDITY(c
->pstream
, !name_device
|| idx_device
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3877 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3878 pa_sink_input
*si
= NULL
;
3879 pa_sink
*sink
= NULL
;
3881 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3883 if (idx_device
!= PA_INVALID_INDEX
)
3884 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3886 sink
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SINK
, 1);
3888 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3890 if (pa_sink_input_move_to(si
, sink
) < 0) {
3891 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3895 pa_source_output
*so
= NULL
;
3898 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3900 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3902 if (idx_device
!= PA_INVALID_INDEX
)
3903 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3905 source
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SOURCE
, 1);
3907 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3909 if (pa_source_output_move_to(so
, source
) < 0) {
3910 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3915 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3918 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3919 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3920 uint32_t idx
= PA_INVALID_INDEX
;
3921 const char *name
= NULL
;
3924 pa_native_connection_assert_ref(c
);
3927 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3928 pa_tagstruct_gets(t
, &name
) < 0 ||
3929 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3930 !pa_tagstruct_eof(t
)) {
3935 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3936 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3937 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3938 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3939 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3941 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3943 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3945 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3946 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3950 pa_sink
*sink
= NULL
;
3952 if (idx
!= PA_INVALID_INDEX
)
3953 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3955 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3957 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3959 if (pa_sink_suspend(sink
, b
) < 0) {
3960 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3966 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3968 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3970 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3971 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3978 if (idx
!= PA_INVALID_INDEX
)
3979 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3981 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3983 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3985 if (pa_source_suspend(source
, b
) < 0) {
3986 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3992 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3995 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3996 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3997 uint32_t idx
= PA_INVALID_INDEX
;
3998 const char *name
= NULL
;
4000 pa_native_protocol_ext_cb_t cb
;
4002 pa_native_connection_assert_ref(c
);
4005 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
4006 pa_tagstruct_gets(t
, &name
) < 0) {
4011 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
4012 CHECK_VALIDITY(c
->pstream
, !name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
4013 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
4014 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
4015 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
4017 if (idx
!= PA_INVALID_INDEX
)
4018 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
4020 for (m
= pa_idxset_first(c
->protocol
->core
->modules
, &idx
); m
; m
= pa_idxset_next(c
->protocol
->core
->modules
, &idx
))
4021 if (strcmp(name
, m
->name
) == 0)
4025 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
4026 CHECK_VALIDITY(c
->pstream
, m
->load_once
|| idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
4028 cb
= (pa_native_protocol_ext_cb_t
) pa_hashmap_get(c
->protocol
->extensions
, m
);
4029 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
4031 if (cb(c
->protocol
, m
, c
, tag
, t
) < 0)
4036 /*** pstream callbacks ***/
4038 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
4039 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4043 pa_native_connection_assert_ref(c
);
4045 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
4046 pa_log("invalid packet.");
4047 native_connection_unlink(c
);
4051 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
) {
4052 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4053 output_stream
*stream
;
4057 pa_native_connection_assert_ref(c
);
4059 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
4060 pa_log("client sent block for invalid stream.");
4065 /* pa_log("got %lu bytes", (unsigned long) chunk->length); */
4067 if (playback_stream_isinstance(stream
)) {
4068 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
4070 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
4071 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
);
4073 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
4076 upload_stream
*u
= UPLOAD_STREAM(stream
);
4079 if (!u
->memchunk
.memblock
) {
4080 if (u
->length
== chunk
->length
) {
4081 u
->memchunk
= *chunk
;
4082 pa_memblock_ref(u
->memchunk
.memblock
);
4085 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
4086 u
->memchunk
.index
= u
->memchunk
.length
= 0;
4090 pa_assert(u
->memchunk
.memblock
);
4093 if (l
> chunk
->length
)
4099 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
4100 src
= pa_memblock_acquire(chunk
->memblock
);
4102 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
4103 (uint8_t*) src
+chunk
->index
, l
);
4105 pa_memblock_release(u
->memchunk
.memblock
);
4106 pa_memblock_release(chunk
->memblock
);
4108 u
->memchunk
.length
+= l
;
4114 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
4115 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4118 pa_native_connection_assert_ref(c
);
4120 native_connection_unlink(c
);
4121 pa_log_info("Connection died.");
4124 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
4125 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4128 pa_native_connection_assert_ref(c
);
4130 native_connection_send_memblock(c
);
4133 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4136 if (!(q
= pa_thread_mq_get()))
4137 pa_pstream_send_revoke(p
, block_id
);
4139 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4142 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4145 if (!(q
= pa_thread_mq_get()))
4146 pa_pstream_send_release(p
, block_id
);
4148 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4151 /*** client callbacks ***/
4153 static void client_kill_cb(pa_client
*c
) {
4156 native_connection_unlink(PA_NATIVE_CONNECTION(c
->userdata
));
4157 pa_log_info("Connection killed.");
4160 /*** module entry points ***/
4162 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
4163 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4167 pa_native_connection_assert_ref(c
);
4168 pa_assert(c
->auth_timeout_event
== e
);
4170 if (!c
->authorized
) {
4171 native_connection_unlink(c
);
4172 pa_log_info("Connection terminated due to authentication timeout.");
4176 void pa_native_protocol_connect(pa_native_protocol
*p
, pa_iochannel
*io
, pa_native_options
*o
) {
4177 pa_native_connection
*c
;
4178 char cname
[256], pname
[128];
4184 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
4185 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
4186 pa_iochannel_free(io
);
4190 c
= pa_msgobject_new(pa_native_connection
);
4191 c
->parent
.parent
.free
= native_connection_free
;
4192 c
->parent
.process_msg
= native_connection_process_msg
;
4194 c
->options
= pa_native_options_ref(o
);
4195 c
->authorized
= FALSE
;
4197 if (o
->auth_anonymous
) {
4198 pa_log_info("Client authenticated anonymously.");
4199 c
->authorized
= TRUE
;
4202 if (!c
->authorized
&&
4204 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
4206 pa_log_info("Client authenticated by IP ACL.");
4207 c
->authorized
= TRUE
;
4210 if (!c
->authorized
) {
4212 pa_gettimeofday(&tv
);
4213 tv
.tv_sec
+= AUTH_TIMEOUT
;
4214 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
4216 c
->auth_timeout_event
= NULL
;
4218 c
->is_local
= pa_iochannel_socket_is_local(io
);
4221 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
4222 pa_snprintf(cname
, sizeof(cname
), "Native client (%s)", pname
);
4223 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
4224 pa_proplist_sets(c
->client
->proplist
, "native-protocol.peer", pname
);
4225 c
->client
->kill
= client_kill_cb
;
4226 c
->client
->userdata
= c
;
4227 c
->client
->module
= o
->module
;
4229 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
4230 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
4231 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
4232 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
4233 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
4234 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
4235 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
4237 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
4239 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
4240 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
4242 c
->rrobin_index
= PA_IDXSET_INVALID
;
4243 c
->subscription
= NULL
;
4245 pa_idxset_put(p
->connections
, c
, NULL
);
4248 if (pa_iochannel_creds_supported(io
))
4249 pa_iochannel_creds_enable(io
);
4252 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_CONNECTION_PUT
], c
);
4255 void pa_native_protocol_disconnect(pa_native_protocol
*p
, pa_module
*m
) {
4256 pa_native_connection
*c
;
4262 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
4263 if (c
->options
->module
== m
)
4264 native_connection_unlink(c
);
4267 static pa_native_protocol
* native_protocol_new(pa_core
*c
) {
4268 pa_native_protocol
*p
;
4273 p
= pa_xnew(pa_native_protocol
, 1);
4276 p
->connections
= pa_idxset_new(NULL
, NULL
);
4280 p
->extensions
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
4282 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4283 pa_hook_init(&p
->hooks
[h
], p
);
4285 pa_assert_se(pa_shared_set(c
, "native-protocol", p
) >= 0);
4290 pa_native_protocol
* pa_native_protocol_get(pa_core
*c
) {
4291 pa_native_protocol
*p
;
4293 if ((p
= pa_shared_get(c
, "native-protocol")))
4294 return pa_native_protocol_ref(p
);
4296 return native_protocol_new(c
);
4299 pa_native_protocol
* pa_native_protocol_ref(pa_native_protocol
*p
) {
4301 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4308 void pa_native_protocol_unref(pa_native_protocol
*p
) {
4309 pa_native_connection
*c
;
4313 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4315 if (PA_REFCNT_DEC(p
) > 0)
4318 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4319 native_connection_unlink(c
);
4321 pa_idxset_free(p
->connections
, NULL
, NULL
);
4323 pa_strlist_free(p
->servers
);
4325 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4326 pa_hook_done(&p
->hooks
[h
]);
4328 pa_hashmap_free(p
->extensions
, NULL
, NULL
);
4330 pa_assert_se(pa_shared_remove(p
->core
, "native-protocol") >= 0);
4335 void pa_native_protocol_add_server_string(pa_native_protocol
*p
, const char *name
) {
4337 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4340 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
4342 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4345 void pa_native_protocol_remove_server_string(pa_native_protocol
*p
, const char *name
) {
4347 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4350 p
->servers
= pa_strlist_remove(p
->servers
, name
);
4352 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4355 pa_hook
*pa_native_protocol_hooks(pa_native_protocol
*p
) {
4357 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4362 pa_strlist
*pa_native_protocol_servers(pa_native_protocol
*p
) {
4364 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4369 int pa_native_protocol_install_ext(pa_native_protocol
*p
, pa_module
*m
, pa_native_protocol_ext_cb_t cb
) {
4371 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4374 pa_assert(!pa_hashmap_get(p
->extensions
, m
));
4376 pa_assert_se(pa_hashmap_put(p
->extensions
, m
, (void*) cb
) == 0);
4380 void pa_native_protocol_remove_ext(pa_native_protocol
*p
, pa_module
*m
) {
4382 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4385 pa_assert_se(pa_hashmap_remove(p
->extensions
, m
));
4388 pa_native_options
* pa_native_options_new(void) {
4389 pa_native_options
*o
;
4391 o
= pa_xnew0(pa_native_options
, 1);
4397 pa_native_options
* pa_native_options_ref(pa_native_options
*o
) {
4399 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4406 void pa_native_options_unref(pa_native_options
*o
) {
4408 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4410 if (PA_REFCNT_DEC(o
) > 0)
4413 pa_xfree(o
->auth_group
);
4416 pa_ip_acl_free(o
->auth_ip_acl
);
4419 pa_auth_cookie_unref(o
->auth_cookie
);
4424 int pa_native_options_parse(pa_native_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
4429 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4432 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
4433 pa_log("auth-anonymous= expects a boolean argument.");
4438 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &enabled
) < 0) {
4439 pa_log("auth-group-enabled= expects a boolean argument.");
4443 pa_xfree(o
->auth_group
);
4444 o
->auth_group
= enabled
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4448 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4451 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4454 if (!(ipa
= pa_ip_acl_new(acl
))) {
4455 pa_log("Failed to parse IP ACL '%s'", acl
);
4460 pa_ip_acl_free(o
->auth_ip_acl
);
4462 o
->auth_ip_acl
= ipa
;
4466 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
4467 pa_log("auth-cookie-enabled= expects a boolean argument.");
4472 pa_auth_cookie_unref(o
->auth_cookie
);
4477 /* The new name for this is 'auth-cookie', for compat reasons
4478 * we check the old name too */
4479 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
4480 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
4481 cn
= PA_NATIVE_COOKIE_FILE
;
4483 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, PA_NATIVE_COOKIE_LENGTH
)))
4487 o
->auth_cookie
= NULL
;
4492 pa_pstream
* pa_native_connection_get_pstream(pa_native_connection
*c
) {
4493 pa_native_connection_assert_ref(c
);