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(record_stream
*s
, pa_bool_t adjust_latency
, uint32_t *maxlength
, uint32_t *fragsize
) {
474 pa_assert(maxlength
);
477 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
478 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
480 *maxlength
= pa_frame_size(&s
->source_output
->sample_spec
);
482 if (*fragsize
== (uint32_t) -1)
483 *fragsize
= pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC
*PA_USEC_PER_MSEC
, &s
->source_output
->sample_spec
);
485 *fragsize
= pa_frame_size(&s
->source_output
->sample_spec
);
487 if (adjust_latency
) {
488 pa_usec_t fragsize_usec
;
490 /* So, the user asked us to adjust the latency according to
491 * what the source can provide. Half the latency will be
492 * spent on the hw buffer, half of it in the async buffer
493 * queue we maintain for each client. */
495 fragsize_usec
= pa_bytes_to_usec(*fragsize
, &s
->source_output
->sample_spec
);
497 s
->source_latency
= pa_source_output_set_requested_latency(s
->source_output
, fragsize_usec
/2);
499 if (fragsize_usec
>= s
->source_latency
*2)
500 fragsize_usec
-= s
->source_latency
;
502 fragsize_usec
= s
->source_latency
;
504 *fragsize
= pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
);
506 s
->source_latency
= 0;
509 static void fix_record_buffer_attr_post(record_stream
*s
, uint32_t *maxlength
, uint32_t *fragsize
) {
513 pa_assert(maxlength
);
516 *maxlength
= pa_memblockq_get_maxlength(s
->memblockq
);
518 base
= pa_frame_size(&s
->source_output
->sample_spec
);
520 s
->fragment_size
= (*fragsize
/base
)*base
;
521 if (s
->fragment_size
<= 0)
522 s
->fragment_size
= base
;
524 if (s
->fragment_size
> *maxlength
)
525 s
->fragment_size
= *maxlength
;
527 *fragsize
= s
->fragment_size
;
530 static record_stream
* record_stream_new(
531 pa_native_connection
*c
,
535 pa_bool_t peak_detect
,
538 pa_source_output_flags_t flags
,
540 pa_bool_t adjust_latency
,
541 pa_sink_input
*direct_on_input
) {
544 pa_source_output
*source_output
;
546 pa_source_output_new_data data
;
550 pa_assert(maxlength
);
553 pa_source_output_new_data_init(&data
);
555 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
556 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
557 data
.driver
= __FILE__
;
558 data
.module
= c
->options
->module
;
559 data
.client
= c
->client
;
560 data
.source
= source
;
561 data
.direct_on_input
= direct_on_input
;
562 pa_source_output_new_data_set_sample_spec(&data
, ss
);
563 pa_source_output_new_data_set_channel_map(&data
, map
);
565 data
.resample_method
= PA_RESAMPLER_PEAKS
;
567 source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
);
569 pa_source_output_new_data_done(&data
);
574 s
= pa_msgobject_new(record_stream
);
575 s
->parent
.parent
.free
= record_stream_free
;
576 s
->parent
.process_msg
= record_stream_process_msg
;
578 s
->source_output
= source_output
;
580 s
->source_output
->push
= source_output_push_cb
;
581 s
->source_output
->kill
= source_output_kill_cb
;
582 s
->source_output
->get_latency
= source_output_get_latency_cb
;
583 s
->source_output
->moved
= source_output_moved_cb
;
584 s
->source_output
->suspend
= source_output_suspend_cb
;
585 s
->source_output
->userdata
= s
;
587 fix_record_buffer_attr_pre(s
, adjust_latency
, maxlength
, fragsize
);
589 s
->memblockq
= pa_memblockq_new(
593 base
= pa_frame_size(&source_output
->sample_spec
),
599 fix_record_buffer_attr_post(s
, maxlength
, fragsize
);
601 *ss
= s
->source_output
->sample_spec
;
602 *map
= s
->source_output
->channel_map
;
604 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
606 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
607 ((double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) + (double) s
->source_latency
) / PA_USEC_PER_MSEC
,
608 (double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) / PA_USEC_PER_MSEC
,
609 (double) s
->source_latency
/ PA_USEC_PER_MSEC
);
611 pa_source_output_put(s
->source_output
);
615 static void record_stream_send_killed(record_stream
*r
) {
617 record_stream_assert_ref(r
);
619 t
= pa_tagstruct_new(NULL
, 0);
620 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
621 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
622 pa_tagstruct_putu32(t
, r
->index
);
623 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
626 static void playback_stream_unlink(playback_stream
*s
) {
633 pa_sink_input_unlink(s
->sink_input
);
634 pa_sink_input_unref(s
->sink_input
);
635 s
->sink_input
= NULL
;
638 if (s
->drain_request
)
639 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
641 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
642 s
->connection
= NULL
;
643 playback_stream_unref(s
);
646 static void playback_stream_free(pa_object
* o
) {
647 playback_stream
*s
= PLAYBACK_STREAM(o
);
650 playback_stream_unlink(s
);
652 pa_memblockq_free(s
->memblockq
);
656 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
657 playback_stream
*s
= PLAYBACK_STREAM(o
);
658 playback_stream_assert_ref(s
);
664 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
669 if ((l
= pa_atomic_load(&s
->missing
)) <= 0)
672 if (pa_atomic_cmpxchg(&s
->missing
, l
, 0))
679 t
= pa_tagstruct_new(NULL
, 0);
680 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
681 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
682 pa_tagstruct_putu32(t
, s
->index
);
683 pa_tagstruct_putu32(t
, l
);
684 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
686 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
690 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
693 /* Report that we're empty */
694 t
= pa_tagstruct_new(NULL
, 0);
695 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
696 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
697 pa_tagstruct_putu32(t
, s
->index
);
698 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
702 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
705 /* Notify the user we're overflowed*/
706 t
= pa_tagstruct_new(NULL
, 0);
707 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
708 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
709 pa_tagstruct_putu32(t
, s
->index
);
710 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
714 case PLAYBACK_STREAM_MESSAGE_STARTED
:
716 if (s
->connection
->version
>= 13) {
719 /* Notify the user we're overflowed*/
720 t
= pa_tagstruct_new(NULL
, 0);
721 pa_tagstruct_putu32(t
, PA_COMMAND_STARTED
);
722 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
723 pa_tagstruct_putu32(t
, s
->index
);
724 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
729 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
730 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
737 static void fix_playback_buffer_attr_pre(playback_stream
*s
, pa_bool_t adjust_latency
, uint32_t *maxlength
, uint32_t *tlength
, uint32_t* prebuf
, uint32_t* minreq
) {
739 pa_usec_t tlength_usec
, minreq_usec
, sink_usec
;
742 pa_assert(maxlength
);
747 frame_size
= pa_frame_size(&s
->sink_input
->sample_spec
);
749 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
750 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
752 *maxlength
= frame_size
;
754 if (*tlength
== (uint32_t) -1)
755 *tlength
= pa_usec_to_bytes(DEFAULT_TLENGTH_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
757 *tlength
= frame_size
;
759 if (*minreq
== (uint32_t) -1)
760 *minreq
= pa_usec_to_bytes(DEFAULT_PROCESS_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
762 *minreq
= frame_size
;
764 if (*tlength
< *minreq
+frame_size
)
765 *tlength
= *minreq
+frame_size
;
767 tlength_usec
= pa_bytes_to_usec(*tlength
, &s
->sink_input
->sample_spec
);
768 minreq_usec
= pa_bytes_to_usec(*minreq
, &s
->sink_input
->sample_spec
);
770 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
771 (double) tlength_usec
/ PA_USEC_PER_MSEC
,
772 (double) minreq_usec
/ PA_USEC_PER_MSEC
);
774 if (adjust_latency
) {
776 /* So, the user asked us to adjust the latency of the stream
777 * buffer according to the what the sink can provide. The
778 * tlength passed in shall be the overall latency. Roughly
779 * half the latency will be spent on the hw buffer, the other
780 * half of it in the async buffer queue we maintain for each
781 * client. In between we'll have a safety space of size
782 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
783 * empty and needs to be filled, then our buffer must have
784 * enough data to fulfill this request immediatly and thus
785 * have at least the same tlength as the size of the hw
786 * buffer. It additionally needs space for 2 times minreq
787 * because if the buffer ran empty and a partial fillup
788 * happens immediately on the next iteration we need to be
789 * able to fulfill it and give the application also minreq
790 * time to fill it up again for the next request Makes 2 times
791 * minreq in plus.. */
793 if (tlength_usec
> minreq_usec
*2)
794 sink_usec
= (tlength_usec
- minreq_usec
*2)/2;
800 /* Ok, the user didn't ask us to adjust the latency, but we
801 * still need to make sure that the parameters from the user
804 if (tlength_usec
> minreq_usec
*2)
805 sink_usec
= (tlength_usec
- minreq_usec
*2);
810 s
->sink_latency
= pa_sink_input_set_requested_latency(s
->sink_input
, sink_usec
);
812 if (adjust_latency
) {
813 /* Ok, we didn't necessarily get what we were asking for, so
814 * let's subtract from what we asked for for the remaining
817 if (tlength_usec
>= s
->sink_latency
)
818 tlength_usec
-= s
->sink_latency
;
821 /* FIXME: This is actually larger than necessary, since not all of
822 * the sink latency is actually rewritable. */
823 if (tlength_usec
< s
->sink_latency
+ 2*minreq_usec
)
824 tlength_usec
= s
->sink_latency
+ 2*minreq_usec
;
826 *tlength
= pa_usec_to_bytes(tlength_usec
, &s
->sink_input
->sample_spec
);
827 *minreq
= pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
);
830 *minreq
+= frame_size
;
831 *tlength
+= frame_size
*2;
834 if (*tlength
<= *minreq
)
835 *tlength
= *minreq
*2 + frame_size
;
837 if (*prebuf
== (uint32_t) -1 || *prebuf
> *tlength
)
841 static void fix_playback_buffer_attr_post(playback_stream
*s
, uint32_t *maxlength
, uint32_t *tlength
, uint32_t* prebuf
, uint32_t* minreq
) {
843 pa_assert(maxlength
);
848 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
849 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
850 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
851 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
856 static playback_stream
* playback_stream_new(
857 pa_native_connection
*c
,
869 pa_sink_input_flags_t flags
,
871 pa_bool_t adjust_latency
) {
873 playback_stream
*s
, *ssync
;
874 pa_sink_input
*sink_input
;
878 pa_sink_input_new_data data
;
882 pa_assert(maxlength
);
889 /* Find syncid group */
890 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
892 if (!playback_stream_isinstance(ssync
))
895 if (ssync
->syncid
== syncid
)
899 /* Synced streams must connect to the same sink */
903 sink
= ssync
->sink_input
->sink
;
904 else if (sink
!= ssync
->sink_input
->sink
)
908 pa_sink_input_new_data_init(&data
);
910 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
911 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
912 data
.driver
= __FILE__
;
913 data
.module
= c
->options
->module
;
914 data
.client
= c
->client
;
916 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
917 pa_sink_input_new_data_set_channel_map(&data
, map
);
919 pa_sink_input_new_data_set_volume(&data
, volume
);
920 pa_sink_input_new_data_set_muted(&data
, muted
);
921 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
923 sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
);
925 pa_sink_input_new_data_done(&data
);
930 s
= pa_msgobject_new(playback_stream
);
931 s
->parent
.parent
.parent
.free
= playback_stream_free
;
932 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
935 s
->sink_input
= sink_input
;
936 s
->is_underrun
= TRUE
;
937 s
->drain_request
= FALSE
;
938 pa_atomic_store(&s
->missing
, 0);
940 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
941 s
->sink_input
->pop
= sink_input_pop_cb
;
942 s
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
943 s
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
944 s
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
945 s
->sink_input
->kill
= sink_input_kill_cb
;
946 s
->sink_input
->moved
= sink_input_moved_cb
;
947 s
->sink_input
->suspend
= sink_input_suspend_cb
;
948 s
->sink_input
->userdata
= s
;
950 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
952 fix_playback_buffer_attr_pre(s
, adjust_latency
, maxlength
, tlength
, prebuf
, minreq
);
953 pa_sink_input_get_silence(sink_input
, &silence
);
955 s
->memblockq
= pa_memblockq_new(
959 pa_frame_size(&sink_input
->sample_spec
),
965 pa_memblock_unref(silence
.memblock
);
966 fix_playback_buffer_attr_post(s
, maxlength
, tlength
, prebuf
, minreq
);
968 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
970 *ss
= s
->sink_input
->sample_spec
;
971 *map
= s
->sink_input
->channel_map
;
973 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
975 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
976 ((double) pa_bytes_to_usec(*tlength
, &sink_input
->sample_spec
) + (double) s
->sink_latency
) / PA_USEC_PER_MSEC
,
977 (double) pa_bytes_to_usec(*tlength
-*minreq
*2, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
978 (double) pa_bytes_to_usec(*minreq
, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
979 (double) s
->sink_latency
/ PA_USEC_PER_MSEC
);
981 pa_sink_input_put(s
->sink_input
);
986 /* Called from thread context */
987 static void playback_stream_request_bytes(playback_stream
*s
) {
988 size_t m
, previous_missing
;
990 playback_stream_assert_ref(s
);
992 m
= pa_memblockq_pop_missing(s
->memblockq
);
997 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
999 previous_missing
= pa_atomic_add(&s
->missing
, m
);
1001 if (pa_memblockq_prebuf_active(s
->memblockq
) ||
1002 (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
))
1003 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1007 static void playback_stream_send_killed(playback_stream
*p
) {
1009 playback_stream_assert_ref(p
);
1011 t
= pa_tagstruct_new(NULL
, 0);
1012 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
1013 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1014 pa_tagstruct_putu32(t
, p
->index
);
1015 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
1018 static int native_connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1019 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1020 pa_native_connection_assert_ref(c
);
1027 case CONNECTION_MESSAGE_REVOKE
:
1028 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1031 case CONNECTION_MESSAGE_RELEASE
:
1032 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1039 static void native_connection_unlink(pa_native_connection
*c
) {
1048 pa_hook_fire(&c
->protocol
->hooks
[PA_NATIVE_HOOK_CONNECTION_UNLINK
], c
);
1051 pa_native_options_unref(c
->options
);
1053 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
1054 record_stream_unlink(r
);
1056 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
1057 if (playback_stream_isinstance(o
))
1058 playback_stream_unlink(PLAYBACK_STREAM(o
));
1060 upload_stream_unlink(UPLOAD_STREAM(o
));
1062 if (c
->subscription
)
1063 pa_subscription_free(c
->subscription
);
1066 pa_pstream_unlink(c
->pstream
);
1068 if (c
->auth_timeout_event
) {
1069 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1070 c
->auth_timeout_event
= NULL
;
1073 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
1075 pa_native_connection_unref(c
);
1078 static void native_connection_free(pa_object
*o
) {
1079 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1083 native_connection_unlink(c
);
1085 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
1086 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
1088 pa_pdispatch_unref(c
->pdispatch
);
1089 pa_pstream_unref(c
->pstream
);
1090 pa_client_free(c
->client
);
1095 static void native_connection_send_memblock(pa_native_connection
*c
) {
1099 start
= PA_IDXSET_INVALID
;
1103 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
1106 if (start
== PA_IDXSET_INVALID
)
1107 start
= c
->rrobin_index
;
1108 else if (start
== c
->rrobin_index
)
1111 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
1112 pa_memchunk schunk
= chunk
;
1114 if (schunk
.length
> r
->fragment_size
)
1115 schunk
.length
= r
->fragment_size
;
1117 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
1119 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
1120 pa_memblock_unref(schunk
.memblock
);
1127 /*** sink input callbacks ***/
1129 static void handle_seek(playback_stream
*s
, int64_t indexw
) {
1130 playback_stream_assert_ref(s
);
1132 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1134 if (s
->sink_input
->thread_info
.underrun_for
> 0) {
1136 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1138 if (pa_memblockq_is_readable(s
->memblockq
)) {
1140 /* We just ended an underrun, let's ask the sink
1141 * for a complete rewind rewrite */
1143 pa_log_debug("Requesting rewind due to end of underrun.");
1144 pa_sink_input_request_rewind(s
->sink_input
,
1145 s
->sink_input
->thread_info
.underrun_for
== (size_t) -1 ? 0 : s
->sink_input
->thread_info
.underrun_for
,
1152 indexr
= pa_memblockq_get_read_index(s
->memblockq
);
1154 if (indexw
< indexr
) {
1155 /* OK, the sink already asked for this data, so
1156 * let's have it usk us again */
1158 pa_log_debug("Requesting rewind due to rewrite.");
1159 pa_sink_input_request_rewind(s
->sink_input
, indexr
- indexw
, TRUE
, FALSE
);
1163 playback_stream_request_bytes(s
);
1166 /* Called from thread context */
1167 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1168 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1171 pa_sink_input_assert_ref(i
);
1172 s
= PLAYBACK_STREAM(i
->userdata
);
1173 playback_stream_assert_ref(s
);
1177 case SINK_INPUT_MESSAGE_SEEK
: {
1180 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1181 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
1183 handle_seek(s
, windex
);
1187 case SINK_INPUT_MESSAGE_POST_DATA
: {
1192 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1194 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1196 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
1197 pa_log_warn("Failed to push data into queue");
1198 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
1199 pa_memblockq_seek(s
->memblockq
, chunk
->length
, PA_SEEK_RELATIVE
);
1202 handle_seek(s
, windex
);
1204 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1209 case SINK_INPUT_MESSAGE_DRAIN
:
1210 case SINK_INPUT_MESSAGE_FLUSH
:
1211 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1212 case SINK_INPUT_MESSAGE_TRIGGER
: {
1215 pa_sink_input
*isync
;
1216 void (*func
)(pa_memblockq
*bq
);
1219 case SINK_INPUT_MESSAGE_FLUSH
:
1220 func
= pa_memblockq_flush_write
;
1223 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1224 func
= pa_memblockq_prebuf_force
;
1227 case SINK_INPUT_MESSAGE_DRAIN
:
1228 case SINK_INPUT_MESSAGE_TRIGGER
:
1229 func
= pa_memblockq_prebuf_disable
;
1233 pa_assert_not_reached();
1236 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1238 handle_seek(s
, windex
);
1240 /* Do the same for all other members in the sync group */
1241 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
1242 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1243 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1244 func(ssync
->memblockq
);
1245 handle_seek(ssync
, windex
);
1248 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1249 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1250 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1251 func(ssync
->memblockq
);
1252 handle_seek(ssync
, windex
);
1255 if (code
== SINK_INPUT_MESSAGE_DRAIN
) {
1256 if (!pa_memblockq_is_readable(s
->memblockq
))
1257 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
1259 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
1260 s
->drain_request
= TRUE
;
1267 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1269 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1270 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1271 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1274 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1277 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1279 pa_memblockq_prebuf_force(s
->memblockq
);
1281 handle_seek(s
, windex
);
1283 /* Fall through to the default handler */
1287 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1288 pa_usec_t
*r
= userdata
;
1290 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1292 /* Fall through, the default handler will add in the extra
1293 * latency added by the resampler */
1298 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1301 /* Called from thread context */
1302 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
1305 pa_sink_input_assert_ref(i
);
1306 s
= PLAYBACK_STREAM(i
->userdata
);
1307 playback_stream_assert_ref(s
);
1310 /* pa_log("%s, pop(): %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1312 if (pa_memblockq_is_readable(s
->memblockq
))
1313 s
->is_underrun
= FALSE
;
1315 /* pa_log("%s, UNDERRUN: %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1317 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1318 s
->drain_request
= FALSE
;
1319 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
);
1320 } else if (!s
->is_underrun
)
1321 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1323 s
->is_underrun
= TRUE
;
1325 playback_stream_request_bytes(s
);
1328 /* This call will not fail with prebuf=0, hence we check for
1329 underrun explicitly above */
1330 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
1333 chunk
->length
= PA_MIN(nbytes
, chunk
->length
);
1335 if (i
->thread_info
.underrun_for
> 0)
1336 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_STARTED
, NULL
, 0, NULL
, NULL
);
1338 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1339 playback_stream_request_bytes(s
);
1344 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1347 pa_sink_input_assert_ref(i
);
1348 s
= PLAYBACK_STREAM(i
->userdata
);
1349 playback_stream_assert_ref(s
);
1351 /* If we are in an underrun, then we don't rewind */
1352 if (i
->thread_info
.underrun_for
> 0)
1355 pa_memblockq_rewind(s
->memblockq
, nbytes
);
1358 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1361 pa_sink_input_assert_ref(i
);
1362 s
= PLAYBACK_STREAM(i
->userdata
);
1363 playback_stream_assert_ref(s
);
1365 pa_memblockq_set_maxrewind(s
->memblockq
, nbytes
);
1368 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
1372 pa_sink_input_assert_ref(i
);
1373 s
= PLAYBACK_STREAM(i
->userdata
);
1374 playback_stream_assert_ref(s
);
1376 tlength
= nbytes
+2*pa_memblockq_get_minreq(s
->memblockq
);
1378 if (pa_memblockq_get_tlength(s
->memblockq
) < tlength
)
1379 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1382 /* Called from main context */
1383 static void sink_input_kill_cb(pa_sink_input
*i
) {
1386 pa_sink_input_assert_ref(i
);
1387 s
= PLAYBACK_STREAM(i
->userdata
);
1388 playback_stream_assert_ref(s
);
1390 playback_stream_send_killed(s
);
1391 playback_stream_unlink(s
);
1394 /* Called from main context */
1395 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1399 pa_sink_input_assert_ref(i
);
1400 s
= PLAYBACK_STREAM(i
->userdata
);
1401 playback_stream_assert_ref(s
);
1403 if (s
->connection
->version
< 12)
1406 t
= pa_tagstruct_new(NULL
, 0);
1407 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1408 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1409 pa_tagstruct_putu32(t
, s
->index
);
1410 pa_tagstruct_put_boolean(t
, suspend
);
1411 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1414 /* Called from main context */
1415 static void sink_input_moved_cb(pa_sink_input
*i
) {
1418 uint32_t maxlength
, tlength
, prebuf
, minreq
;
1420 pa_sink_input_assert_ref(i
);
1421 s
= PLAYBACK_STREAM(i
->userdata
);
1422 playback_stream_assert_ref(s
);
1424 maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
1425 tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
1426 prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
1427 minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
1429 fix_playback_buffer_attr_pre(s
, TRUE
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1430 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1431 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1432 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
1433 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
1434 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1436 if (s
->connection
->version
< 12)
1439 t
= pa_tagstruct_new(NULL
, 0);
1440 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1441 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1442 pa_tagstruct_putu32(t
, s
->index
);
1443 pa_tagstruct_putu32(t
, i
->sink
->index
);
1444 pa_tagstruct_puts(t
, i
->sink
->name
);
1445 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1447 if (s
->connection
->version
>= 13) {
1448 pa_tagstruct_putu32(t
, maxlength
);
1449 pa_tagstruct_putu32(t
, tlength
);
1450 pa_tagstruct_putu32(t
, prebuf
);
1451 pa_tagstruct_putu32(t
, minreq
);
1452 pa_tagstruct_put_usec(t
, s
->sink_latency
);
1455 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1458 /*** source_output callbacks ***/
1460 /* Called from thread context */
1461 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1464 pa_source_output_assert_ref(o
);
1465 s
= RECORD_STREAM(o
->userdata
);
1466 record_stream_assert_ref(s
);
1469 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1472 static void source_output_kill_cb(pa_source_output
*o
) {
1475 pa_source_output_assert_ref(o
);
1476 s
= RECORD_STREAM(o
->userdata
);
1477 record_stream_assert_ref(s
);
1479 record_stream_send_killed(s
);
1480 record_stream_unlink(s
);
1483 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1486 pa_source_output_assert_ref(o
);
1487 s
= RECORD_STREAM(o
->userdata
);
1488 record_stream_assert_ref(s
);
1490 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1492 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1495 /* Called from main context */
1496 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1500 pa_source_output_assert_ref(o
);
1501 s
= RECORD_STREAM(o
->userdata
);
1502 record_stream_assert_ref(s
);
1504 if (s
->connection
->version
< 12)
1507 t
= pa_tagstruct_new(NULL
, 0);
1508 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1509 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1510 pa_tagstruct_putu32(t
, s
->index
);
1511 pa_tagstruct_put_boolean(t
, suspend
);
1512 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1515 /* Called from main context */
1516 static void source_output_moved_cb(pa_source_output
*o
) {
1519 uint32_t maxlength
, fragsize
;
1521 pa_source_output_assert_ref(o
);
1522 s
= RECORD_STREAM(o
->userdata
);
1523 record_stream_assert_ref(s
);
1525 fragsize
= (uint32_t) s
->fragment_size
;
1526 maxlength
= (uint32_t) pa_memblockq_get_length(s
->memblockq
);
1528 fix_record_buffer_attr_pre(s
, TRUE
, &maxlength
, &fragsize
);
1529 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1530 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
1532 if (s
->connection
->version
< 12)
1535 t
= pa_tagstruct_new(NULL
, 0);
1536 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1537 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1538 pa_tagstruct_putu32(t
, s
->index
);
1539 pa_tagstruct_putu32(t
, o
->source
->index
);
1540 pa_tagstruct_puts(t
, o
->source
->name
);
1541 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1543 if (s
->connection
->version
>= 13) {
1544 pa_tagstruct_putu32(t
, maxlength
);
1545 pa_tagstruct_putu32(t
, fragsize
);
1546 pa_tagstruct_put_usec(t
, s
->source_latency
);
1549 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1552 /*** pdispatch callbacks ***/
1554 static void protocol_error(pa_native_connection
*c
) {
1555 pa_log("protocol error, kicking client");
1556 native_connection_unlink(c
);
1559 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1560 if (!(expression)) { \
1561 pa_pstream_send_error((pstream), (tag), (error)); \
1566 static pa_tagstruct
*reply_new(uint32_t tag
) {
1567 pa_tagstruct
*reply
;
1569 reply
= pa_tagstruct_new(NULL
, 0);
1570 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1571 pa_tagstruct_putu32(reply
, tag
);
1575 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1576 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1578 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1579 const char *name
= NULL
, *sink_name
;
1582 pa_tagstruct
*reply
;
1583 pa_sink
*sink
= NULL
;
1591 fix_channels
= FALSE
,
1593 variable_rate
= FALSE
,
1595 adjust_latency
= FALSE
;
1597 pa_sink_input_flags_t flags
= 0;
1599 pa_bool_t volume_set
= TRUE
;
1601 pa_native_connection_assert_ref(c
);
1604 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1607 PA_TAG_SAMPLE_SPEC
, &ss
,
1608 PA_TAG_CHANNEL_MAP
, &map
,
1609 PA_TAG_U32
, &sink_index
,
1610 PA_TAG_STRING
, &sink_name
,
1611 PA_TAG_U32
, &maxlength
,
1612 PA_TAG_BOOLEAN
, &corked
,
1613 PA_TAG_U32
, &tlength
,
1614 PA_TAG_U32
, &prebuf
,
1615 PA_TAG_U32
, &minreq
,
1616 PA_TAG_U32
, &syncid
,
1617 PA_TAG_CVOLUME
, &volume
,
1618 PA_TAG_INVALID
) < 0) {
1624 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1625 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
1626 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
1627 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1628 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1629 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1630 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1631 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1633 p
= pa_proplist_new();
1636 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1638 if (c
->version
>= 12) {
1639 /* Since 0.9.8 the user can ask for a couple of additional flags */
1641 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1642 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1643 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1644 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1645 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1646 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1647 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1650 pa_proplist_free(p
);
1655 if (c
->version
>= 13) {
1657 if (pa_tagstruct_get_boolean(t
, &muted
) < 0 ||
1658 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1659 pa_tagstruct_get_proplist(t
, p
) < 0) {
1661 pa_proplist_free(p
);
1666 if (c
->version
>= 14) {
1668 if (pa_tagstruct_get_boolean(t
, &volume_set
) < 0) {
1670 pa_proplist_free(p
);
1675 if (!pa_tagstruct_eof(t
)) {
1677 pa_proplist_free(p
);
1681 if (sink_index
!= PA_INVALID_INDEX
) {
1683 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1684 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1685 pa_proplist_free(p
);
1689 } else if (sink_name
) {
1691 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1))) {
1692 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1693 pa_proplist_free(p
);
1699 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1700 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1701 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1702 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1703 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1704 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1705 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1706 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0);
1708 s
= playback_stream_new(c
, sink
, &ss
, &map
, &maxlength
, &tlength
, &prebuf
, &minreq
, volume_set
? &volume
: NULL
, muted
, syncid
, &missing
, flags
, p
, adjust_latency
);
1709 pa_proplist_free(p
);
1711 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1713 reply
= reply_new(tag
);
1714 pa_tagstruct_putu32(reply
, s
->index
);
1715 pa_assert(s
->sink_input
);
1716 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1717 pa_tagstruct_putu32(reply
, missing
);
1719 /* pa_log("initial request is %u", missing); */
1721 if (c
->version
>= 9) {
1722 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1724 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1725 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1726 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1727 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1730 if (c
->version
>= 12) {
1731 /* Since 0.9.8 we support sending the chosen sample
1732 * spec/channel map/device/suspend status back to the
1735 pa_tagstruct_put_sample_spec(reply
, &ss
);
1736 pa_tagstruct_put_channel_map(reply
, &map
);
1738 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1739 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1741 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1744 if (c
->version
>= 13)
1745 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
1747 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1750 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1751 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1754 pa_native_connection_assert_ref(c
);
1757 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1758 !pa_tagstruct_eof(t
)) {
1763 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1767 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1769 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1770 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1774 playback_stream_unlink(s
);
1778 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1780 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1781 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1785 record_stream_unlink(s
);
1789 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1792 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1793 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1797 upload_stream_unlink(s
);
1802 pa_assert_not_reached();
1805 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1808 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1809 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1811 uint32_t maxlength
, fragment_size
;
1812 uint32_t source_index
;
1813 const char *name
= NULL
, *source_name
;
1816 pa_tagstruct
*reply
;
1817 pa_source
*source
= NULL
;
1824 fix_channels
= FALSE
,
1826 variable_rate
= FALSE
,
1827 adjust_latency
= FALSE
,
1828 peak_detect
= FALSE
;
1829 pa_source_output_flags_t flags
= 0;
1831 uint32_t direct_on_input_idx
= PA_INVALID_INDEX
;
1832 pa_sink_input
*direct_on_input
= NULL
;
1834 pa_native_connection_assert_ref(c
);
1837 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1838 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1839 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1840 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1841 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1842 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1843 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1844 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1849 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1850 CHECK_VALIDITY(c
->pstream
, !source_name
|| pa_namereg_is_valid_name(source_name
), tag
, PA_ERR_INVALID
);
1851 CHECK_VALIDITY(c
->pstream
, source_index
== PA_INVALID_INDEX
|| !source_name
, tag
, PA_ERR_INVALID
);
1852 CHECK_VALIDITY(c
->pstream
, !source_name
|| source_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1853 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1854 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1855 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1857 p
= pa_proplist_new();
1860 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1862 if (c
->version
>= 12) {
1863 /* Since 0.9.8 the user can ask for a couple of additional flags */
1865 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1866 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1867 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1868 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1869 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1870 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1871 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1874 pa_proplist_free(p
);
1879 if (c
->version
>= 13) {
1881 if (pa_tagstruct_get_boolean(t
, &peak_detect
) < 0 ||
1882 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1883 pa_tagstruct_get_proplist(t
, p
) < 0 ||
1884 pa_tagstruct_getu32(t
, &direct_on_input_idx
) < 0) {
1886 pa_proplist_free(p
);
1891 if (!pa_tagstruct_eof(t
)) {
1893 pa_proplist_free(p
);
1897 if (source_index
!= PA_INVALID_INDEX
) {
1899 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
1900 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1901 pa_proplist_free(p
);
1905 } else if (source_name
) {
1907 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1))) {
1908 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1909 pa_proplist_free(p
);
1914 if (direct_on_input_idx
!= PA_INVALID_INDEX
) {
1916 if (!(direct_on_input
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, direct_on_input_idx
))) {
1917 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1918 pa_proplist_free(p
);
1924 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
1925 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
1926 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
1927 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
1928 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
1929 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
1930 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
1931 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0);
1933 s
= record_stream_new(c
, source
, &ss
, &map
, peak_detect
, &maxlength
, &fragment_size
, flags
, p
, adjust_latency
, direct_on_input
);
1934 pa_proplist_free(p
);
1936 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1938 reply
= reply_new(tag
);
1939 pa_tagstruct_putu32(reply
, s
->index
);
1940 pa_assert(s
->source_output
);
1941 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
1943 if (c
->version
>= 9) {
1944 /* Since 0.9 we support sending the buffer metrics back to the client */
1946 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1947 pa_tagstruct_putu32(reply
, (uint32_t) fragment_size
);
1950 if (c
->version
>= 12) {
1951 /* Since 0.9.8 we support sending the chosen sample
1952 * spec/channel map/device/suspend status back to the
1955 pa_tagstruct_put_sample_spec(reply
, &ss
);
1956 pa_tagstruct_put_channel_map(reply
, &map
);
1958 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
1959 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
1961 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
1964 if (c
->version
>= 13)
1965 pa_tagstruct_put_usec(reply
, s
->source_latency
);
1967 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1970 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1971 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1974 pa_native_connection_assert_ref(c
);
1977 if (!pa_tagstruct_eof(t
)) {
1982 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1983 ret
= pa_core_exit(c
->protocol
->core
, FALSE
, 0);
1984 CHECK_VALIDITY(c
->pstream
, ret
>= 0, tag
, PA_ERR_ACCESS
);
1986 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
1989 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1990 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1992 pa_tagstruct
*reply
;
1993 pa_bool_t shm_on_remote
, do_shm
;
1995 pa_native_connection_assert_ref(c
);
1998 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
1999 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
2000 !pa_tagstruct_eof(t
)) {
2005 /* Minimum supported version */
2006 if (c
->version
< 8) {
2007 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
2011 /* Starting with protocol version 13 the MSB of the version tag
2012 reflects if shm is available for this pa_native_connection or
2014 if (c
->version
>= 13) {
2015 shm_on_remote
= !!(c
->version
& 0x80000000U
);
2016 c
->version
&= 0x7FFFFFFFU
;
2019 pa_log_debug("Protocol version: remote %u, local %u", c
->version
, PA_PROTOCOL_VERSION
);
2021 pa_proplist_setf(c
->client
->proplist
, "native-protocol.version", "%u", c
->version
);
2023 if (!c
->authorized
) {
2024 pa_bool_t success
= FALSE
;
2027 const pa_creds
*creds
;
2029 if ((creds
= pa_pdispatch_creds(pd
))) {
2030 if (creds
->uid
== getuid())
2032 else if (c
->options
->auth_group
) {
2036 if ((gid
= pa_get_gid_of_group(c
->options
->auth_group
)) == (gid_t
) -1)
2037 pa_log_warn("Failed to get GID of group '%s'", c
->options
->auth_group
);
2038 else if (gid
== creds
->gid
)
2042 if ((r
= pa_uid_in_group(creds
->uid
, c
->options
->auth_group
)) < 0)
2043 pa_log_warn("Failed to check group membership.");
2049 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
2050 (unsigned long) creds
->uid
,
2051 (unsigned long) creds
->gid
,
2056 if (!success
&& c
->options
->auth_cookie
) {
2059 if ((ac
= pa_auth_cookie_read(c
->options
->auth_cookie
, PA_NATIVE_COOKIE_LENGTH
)))
2060 if (memcmp(ac
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
2065 pa_log_warn("Denied access to client with invalid authorization data.");
2066 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2070 c
->authorized
= TRUE
;
2071 if (c
->auth_timeout_event
) {
2072 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
2073 c
->auth_timeout_event
= NULL
;
2077 /* Enable shared memory support if possible */
2079 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
2082 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm
));
2085 if (c
->version
< 10 || (c
->version
>= 13 && !shm_on_remote
))
2089 /* Only enable SHM if both sides are owned by the same
2090 * user. This is a security measure because otherwise data
2091 * private to the user might leak. */
2093 const pa_creds
*creds
;
2094 if (!(creds
= pa_pdispatch_creds(pd
)) || getuid() != creds
->uid
)
2098 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm
));
2099 pa_pstream_enable_shm(c
->pstream
, do_shm
);
2101 reply
= reply_new(tag
);
2102 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
| (do_shm
? 0x80000000 : 0));
2106 /* SHM support is only enabled after both sides made sure they are the same user. */
2110 ucred
.uid
= getuid();
2111 ucred
.gid
= getgid();
2113 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
2116 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2120 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2121 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2122 const char *name
= NULL
;
2124 pa_tagstruct
*reply
;
2126 pa_native_connection_assert_ref(c
);
2129 p
= pa_proplist_new();
2131 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
2132 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2133 !pa_tagstruct_eof(t
)) {
2136 pa_proplist_free(p
);
2141 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
2142 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2143 pa_proplist_free(p
);
2147 pa_proplist_update(c
->client
->proplist
, PA_UPDATE_REPLACE
, p
);
2148 pa_proplist_free(p
);
2150 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2152 reply
= reply_new(tag
);
2154 if (c
->version
>= 13)
2155 pa_tagstruct_putu32(reply
, c
->client
->index
);
2157 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2160 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2161 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2163 uint32_t idx
= PA_IDXSET_INVALID
;
2165 pa_native_connection_assert_ref(c
);
2168 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2169 !pa_tagstruct_eof(t
)) {
2174 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2175 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2177 if (command
== PA_COMMAND_LOOKUP_SINK
) {
2179 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
2183 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
2184 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
2185 idx
= source
->index
;
2188 if (idx
== PA_IDXSET_INVALID
)
2189 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2191 pa_tagstruct
*reply
;
2192 reply
= reply_new(tag
);
2193 pa_tagstruct_putu32(reply
, idx
);
2194 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2198 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2199 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2203 pa_native_connection_assert_ref(c
);
2206 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2207 !pa_tagstruct_eof(t
)) {
2212 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2213 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2214 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2215 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2217 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
);
2220 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2221 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2222 pa_tagstruct
*reply
;
2223 const pa_mempool_stat
*stat
;
2225 pa_native_connection_assert_ref(c
);
2228 if (!pa_tagstruct_eof(t
)) {
2233 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2235 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
2237 reply
= reply_new(tag
);
2238 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
2239 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
2240 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
2241 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
2242 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
2243 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2246 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2247 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2248 pa_tagstruct
*reply
;
2250 struct timeval tv
, now
;
2254 pa_native_connection_assert_ref(c
);
2257 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2258 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2259 !pa_tagstruct_eof(t
)) {
2264 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2265 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2266 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2267 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2268 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
)
2270 reply
= reply_new(tag
);
2272 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
2273 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
2275 pa_tagstruct_put_usec(reply
, latency
);
2277 pa_tagstruct_put_usec(reply
, 0);
2278 pa_tagstruct_put_boolean(reply
, s
->sink_input
->thread_info
.playing_for
> 0);
2279 pa_tagstruct_put_timeval(reply
, &tv
);
2280 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2281 pa_tagstruct_puts64(reply
, s
->write_index
);
2282 pa_tagstruct_puts64(reply
, s
->read_index
);
2284 if (c
->version
>= 13) {
2285 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.underrun_for
);
2286 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.playing_for
);
2289 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2292 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2293 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2294 pa_tagstruct
*reply
;
2296 struct timeval tv
, now
;
2299 pa_native_connection_assert_ref(c
);
2302 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2303 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2304 !pa_tagstruct_eof(t
)) {
2309 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2310 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2311 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2313 reply
= reply_new(tag
);
2314 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
2315 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
2316 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_RUNNING
);
2317 pa_tagstruct_put_timeval(reply
, &tv
);
2318 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2319 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
2320 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
2321 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2324 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2325 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2328 const char *name
= NULL
;
2331 pa_tagstruct
*reply
;
2334 pa_native_connection_assert_ref(c
);
2337 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2338 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
2339 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
2340 pa_tagstruct_getu32(t
, &length
) < 0) {
2345 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2346 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
2347 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
2348 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
2349 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
2350 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
2352 p
= pa_proplist_new();
2354 if (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) {
2356 pa_proplist_free(p
);
2360 if (c
->version
< 13)
2361 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2363 if (!(name
= pa_proplist_gets(p
, PA_PROP_EVENT_ID
)))
2364 name
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
);
2366 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2368 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
2369 pa_proplist_free(p
);
2371 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2373 reply
= reply_new(tag
);
2374 pa_tagstruct_putu32(reply
, s
->index
);
2375 pa_tagstruct_putu32(reply
, length
);
2376 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2379 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2380 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2385 pa_native_connection_assert_ref(c
);
2388 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
2389 !pa_tagstruct_eof(t
)) {
2394 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2396 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
2397 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2398 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2400 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
2401 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
2403 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2405 upload_stream_unlink(s
);
2408 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2409 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2410 uint32_t sink_index
;
2413 const char *name
, *sink_name
;
2416 pa_tagstruct
*reply
;
2418 pa_native_connection_assert_ref(c
);
2421 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2423 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
2424 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
2425 pa_tagstruct_getu32(t
, &volume
) < 0 ||
2426 pa_tagstruct_gets(t
, &name
) < 0) {
2431 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
2432 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
2433 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2434 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2436 if (sink_index
!= PA_INVALID_INDEX
)
2437 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
2439 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
2441 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2443 p
= pa_proplist_new();
2445 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2446 !pa_tagstruct_eof(t
)) {
2448 pa_proplist_free(p
);
2452 pa_proplist_update(p
, PA_UPDATE_MERGE
, c
->client
->proplist
);
2454 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2455 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2456 pa_proplist_free(p
);
2460 pa_proplist_free(p
);
2462 reply
= reply_new(tag
);
2464 if (c
->version
>= 13)
2465 pa_tagstruct_putu32(reply
, idx
);
2467 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2470 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2471 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2474 pa_native_connection_assert_ref(c
);
2477 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2478 !pa_tagstruct_eof(t
)) {
2483 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2484 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2486 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2487 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2491 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2494 static void fixup_sample_spec(pa_native_connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2497 pa_assert(original
);
2501 if (c
->version
< 12) {
2502 /* Before protocol version 12 we didn't support S32 samples,
2503 * so we need to lie about this to the client */
2505 if (fixed
->format
== PA_SAMPLE_S32LE
)
2506 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2507 if (fixed
->format
== PA_SAMPLE_S32BE
)
2508 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2512 static void sink_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2513 pa_sample_spec fixed_ss
;
2516 pa_sink_assert_ref(sink
);
2518 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2522 PA_TAG_U32
, sink
->index
,
2523 PA_TAG_STRING
, sink
->name
,
2524 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2525 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2526 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2527 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2528 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, FALSE
),
2529 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
, FALSE
),
2530 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2531 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2532 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2533 PA_TAG_STRING
, sink
->driver
,
2534 PA_TAG_U32
, sink
->flags
,
2537 if (c
->version
>= 13) {
2538 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2539 pa_tagstruct_put_usec(t
, pa_sink_get_requested_latency(sink
));
2543 static void source_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2544 pa_sample_spec fixed_ss
;
2547 pa_source_assert_ref(source
);
2549 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2553 PA_TAG_U32
, source
->index
,
2554 PA_TAG_STRING
, source
->name
,
2555 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2556 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2557 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2558 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2559 PA_TAG_CVOLUME
, pa_source_get_volume(source
, FALSE
),
2560 PA_TAG_BOOLEAN
, pa_source_get_mute(source
, FALSE
),
2561 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2562 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2563 PA_TAG_USEC
, pa_source_get_latency(source
),
2564 PA_TAG_STRING
, source
->driver
,
2565 PA_TAG_U32
, source
->flags
,
2568 if (c
->version
>= 13) {
2569 pa_tagstruct_put_proplist(t
, source
->proplist
);
2570 pa_tagstruct_put_usec(t
, pa_source_get_requested_latency(source
));
2575 static void client_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2579 pa_tagstruct_putu32(t
, client
->index
);
2580 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2581 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2582 pa_tagstruct_puts(t
, client
->driver
);
2584 if (c
->version
>= 13)
2585 pa_tagstruct_put_proplist(t
, client
->proplist
);
2589 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
2593 pa_tagstruct_putu32(t
, module
->index
);
2594 pa_tagstruct_puts(t
, module
->name
);
2595 pa_tagstruct_puts(t
, module
->argument
);
2596 pa_tagstruct_putu32(t
, module
->n_used
);
2597 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
2600 static void sink_input_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2601 pa_sample_spec fixed_ss
;
2602 pa_usec_t sink_latency
;
2605 pa_sink_input_assert_ref(s
);
2607 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2609 pa_tagstruct_putu32(t
, s
->index
);
2610 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2611 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2612 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2613 pa_tagstruct_putu32(t
, s
->sink
->index
);
2614 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2615 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2616 pa_tagstruct_put_cvolume(t
, &s
->volume
);
2617 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
, &sink_latency
));
2618 pa_tagstruct_put_usec(t
, sink_latency
);
2619 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2620 pa_tagstruct_puts(t
, s
->driver
);
2621 if (c
->version
>= 11)
2622 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2623 if (c
->version
>= 13)
2624 pa_tagstruct_put_proplist(t
, s
->proplist
);
2627 static void source_output_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2628 pa_sample_spec fixed_ss
;
2629 pa_usec_t source_latency
;
2632 pa_source_output_assert_ref(s
);
2634 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2636 pa_tagstruct_putu32(t
, s
->index
);
2637 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2638 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2639 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2640 pa_tagstruct_putu32(t
, s
->source
->index
);
2641 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2642 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2643 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
, &source_latency
));
2644 pa_tagstruct_put_usec(t
, source_latency
);
2645 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2646 pa_tagstruct_puts(t
, s
->driver
);
2648 if (c
->version
>= 13)
2649 pa_tagstruct_put_proplist(t
, s
->proplist
);
2652 static void scache_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2653 pa_sample_spec fixed_ss
;
2658 if (e
->memchunk
.memblock
)
2659 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2661 memset(&fixed_ss
, 0, sizeof(fixed_ss
));
2663 pa_tagstruct_putu32(t
, e
->index
);
2664 pa_tagstruct_puts(t
, e
->name
);
2665 pa_tagstruct_put_cvolume(t
, &e
->volume
);
2666 pa_tagstruct_put_usec(t
, e
->memchunk
.memblock
? pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
) : 0);
2667 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2668 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2669 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
2670 pa_tagstruct_put_boolean(t
, e
->lazy
);
2671 pa_tagstruct_puts(t
, e
->filename
);
2673 if (c
->version
>= 13)
2674 pa_tagstruct_put_proplist(t
, e
->proplist
);
2677 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2678 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2680 pa_sink
*sink
= NULL
;
2681 pa_source
*source
= NULL
;
2682 pa_client
*client
= NULL
;
2683 pa_module
*module
= NULL
;
2684 pa_sink_input
*si
= NULL
;
2685 pa_source_output
*so
= NULL
;
2686 pa_scache_entry
*sce
= NULL
;
2688 pa_tagstruct
*reply
;
2690 pa_native_connection_assert_ref(c
);
2693 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2694 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2695 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2696 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2697 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2698 pa_tagstruct_gets(t
, &name
) < 0) ||
2699 !pa_tagstruct_eof(t
)) {
2704 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2705 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2706 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2707 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2708 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2710 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2711 if (idx
!= PA_INVALID_INDEX
)
2712 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2714 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2715 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2716 if (idx
!= PA_INVALID_INDEX
)
2717 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2719 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2720 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2721 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2722 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2723 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2724 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2725 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2726 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2727 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2729 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2730 if (idx
!= PA_INVALID_INDEX
)
2731 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2733 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
2736 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
2737 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2741 reply
= reply_new(tag
);
2743 sink_fill_tagstruct(c
, reply
, sink
);
2745 source_fill_tagstruct(c
, reply
, source
);
2747 client_fill_tagstruct(c
, reply
, client
);
2749 module_fill_tagstruct(reply
, module
);
2751 sink_input_fill_tagstruct(c
, reply
, si
);
2753 source_output_fill_tagstruct(c
, reply
, so
);
2755 scache_fill_tagstruct(c
, reply
, sce
);
2756 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2759 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2760 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2764 pa_tagstruct
*reply
;
2766 pa_native_connection_assert_ref(c
);
2769 if (!pa_tagstruct_eof(t
)) {
2774 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2776 reply
= reply_new(tag
);
2778 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2779 i
= c
->protocol
->core
->sinks
;
2780 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2781 i
= c
->protocol
->core
->sources
;
2782 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2783 i
= c
->protocol
->core
->clients
;
2784 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2785 i
= c
->protocol
->core
->modules
;
2786 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2787 i
= c
->protocol
->core
->sink_inputs
;
2788 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2789 i
= c
->protocol
->core
->source_outputs
;
2791 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2792 i
= c
->protocol
->core
->scache
;
2796 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2797 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2798 sink_fill_tagstruct(c
, reply
, p
);
2799 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2800 source_fill_tagstruct(c
, reply
, p
);
2801 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2802 client_fill_tagstruct(c
, reply
, p
);
2803 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2804 module_fill_tagstruct(reply
, p
);
2805 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2806 sink_input_fill_tagstruct(c
, reply
, p
);
2807 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2808 source_output_fill_tagstruct(c
, reply
, p
);
2810 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2811 scache_fill_tagstruct(c
, reply
, p
);
2816 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2819 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2820 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2821 pa_tagstruct
*reply
;
2824 pa_sample_spec fixed_ss
;
2826 pa_native_connection_assert_ref(c
);
2829 if (!pa_tagstruct_eof(t
)) {
2834 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2836 reply
= reply_new(tag
);
2837 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
2838 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
2839 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
2840 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
2842 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
2843 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
2845 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
2846 pa_tagstruct_puts(reply
, n
);
2847 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
2848 pa_tagstruct_puts(reply
, n
);
2850 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
2852 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2855 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
2857 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2859 pa_native_connection_assert_ref(c
);
2861 t
= pa_tagstruct_new(NULL
, 0);
2862 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
2863 pa_tagstruct_putu32(t
, (uint32_t) -1);
2864 pa_tagstruct_putu32(t
, e
);
2865 pa_tagstruct_putu32(t
, idx
);
2866 pa_pstream_send_tagstruct(c
->pstream
, t
);
2869 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2870 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2871 pa_subscription_mask_t m
;
2873 pa_native_connection_assert_ref(c
);
2876 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
2877 !pa_tagstruct_eof(t
)) {
2882 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2883 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
2885 if (c
->subscription
)
2886 pa_subscription_free(c
->subscription
);
2889 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
2890 pa_assert(c
->subscription
);
2892 c
->subscription
= NULL
;
2894 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2897 static void command_set_volume(
2904 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2907 pa_sink
*sink
= NULL
;
2908 pa_source
*source
= NULL
;
2909 pa_sink_input
*si
= NULL
;
2910 const char *name
= NULL
;
2912 pa_native_connection_assert_ref(c
);
2915 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2916 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2917 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2918 pa_tagstruct_get_cvolume(t
, &volume
) ||
2919 !pa_tagstruct_eof(t
)) {
2924 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2925 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2926 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2927 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2928 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2929 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
2933 case PA_COMMAND_SET_SINK_VOLUME
:
2934 if (idx
!= PA_INVALID_INDEX
)
2935 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2937 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2940 case PA_COMMAND_SET_SOURCE_VOLUME
:
2941 if (idx
!= PA_INVALID_INDEX
)
2942 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2944 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2947 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
2948 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2952 pa_assert_not_reached();
2955 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2958 pa_sink_set_volume(sink
, &volume
);
2960 pa_source_set_volume(source
, &volume
);
2962 pa_sink_input_set_volume(si
, &volume
);
2964 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2967 static void command_set_mute(
2974 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2977 pa_sink
*sink
= NULL
;
2978 pa_source
*source
= NULL
;
2979 pa_sink_input
*si
= NULL
;
2980 const char *name
= NULL
;
2982 pa_native_connection_assert_ref(c
);
2985 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2986 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2987 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2988 pa_tagstruct_get_boolean(t
, &mute
) ||
2989 !pa_tagstruct_eof(t
)) {
2994 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2995 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2996 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2997 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2998 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3002 case PA_COMMAND_SET_SINK_MUTE
:
3004 if (idx
!= PA_INVALID_INDEX
)
3005 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3007 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3011 case PA_COMMAND_SET_SOURCE_MUTE
:
3012 if (idx
!= PA_INVALID_INDEX
)
3013 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3015 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3019 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
3020 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3024 pa_assert_not_reached();
3027 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3030 pa_sink_set_mute(sink
, mute
);
3032 pa_source_set_mute(source
, mute
);
3034 pa_sink_input_set_mute(si
, mute
);
3036 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3039 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3040 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3045 pa_native_connection_assert_ref(c
);
3048 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3049 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3050 !pa_tagstruct_eof(t
)) {
3055 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3056 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3057 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3058 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3059 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3061 pa_sink_input_cork(s
->sink_input
, b
);
3062 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3065 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3066 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3070 pa_native_connection_assert_ref(c
);
3073 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3074 !pa_tagstruct_eof(t
)) {
3079 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3080 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3081 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3082 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3083 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3086 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3087 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3090 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3091 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3094 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3095 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3099 pa_assert_not_reached();
3102 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3105 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3106 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3111 pa_native_connection_assert_ref(c
);
3114 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3115 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3116 !pa_tagstruct_eof(t
)) {
3121 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3122 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3123 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3125 pa_source_output_cork(s
->source_output
, b
);
3126 pa_memblockq_prebuf_force(s
->memblockq
);
3127 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3130 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3131 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3135 pa_native_connection_assert_ref(c
);
3138 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3139 !pa_tagstruct_eof(t
)) {
3144 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3145 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3146 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3148 pa_memblockq_flush_read(s
->memblockq
);
3149 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3152 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3153 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3155 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3156 pa_tagstruct
*reply
;
3158 pa_native_connection_assert_ref(c
);
3161 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3166 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3168 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3170 pa_bool_t adjust_latency
= FALSE
;
3172 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3173 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3174 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3176 if (pa_tagstruct_get(
3178 PA_TAG_U32
, &maxlength
,
3179 PA_TAG_U32
, &tlength
,
3180 PA_TAG_U32
, &prebuf
,
3181 PA_TAG_U32
, &minreq
,
3182 PA_TAG_INVALID
) < 0 ||
3183 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3184 !pa_tagstruct_eof(t
)) {
3189 fix_playback_buffer_attr_pre(s
, adjust_latency
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3190 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3191 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3192 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3193 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3194 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3196 reply
= reply_new(tag
);
3197 pa_tagstruct_putu32(reply
, maxlength
);
3198 pa_tagstruct_putu32(reply
, tlength
);
3199 pa_tagstruct_putu32(reply
, prebuf
);
3200 pa_tagstruct_putu32(reply
, minreq
);
3202 if (c
->version
>= 13)
3203 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3207 pa_bool_t adjust_latency
= FALSE
;
3208 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3210 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3211 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3213 if (pa_tagstruct_get(
3215 PA_TAG_U32
, &maxlength
,
3216 PA_TAG_U32
, &fragsize
,
3217 PA_TAG_INVALID
) < 0 ||
3218 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3219 !pa_tagstruct_eof(t
)) {
3224 fix_record_buffer_attr_pre(s
, adjust_latency
, &maxlength
, &fragsize
);
3225 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3226 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3228 reply
= reply_new(tag
);
3229 pa_tagstruct_putu32(reply
, maxlength
);
3230 pa_tagstruct_putu32(reply
, fragsize
);
3232 if (c
->version
>= 13)
3233 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3236 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3239 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3240 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3244 pa_native_connection_assert_ref(c
);
3247 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3248 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3249 !pa_tagstruct_eof(t
)) {
3254 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3255 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3257 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3260 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3261 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3262 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3264 pa_sink_input_set_rate(s
->sink_input
, rate
);
3268 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3270 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3271 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3273 pa_source_output_set_rate(s
->source_output
, rate
);
3276 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3279 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3280 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3285 pa_native_connection_assert_ref(c
);
3288 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3290 p
= pa_proplist_new();
3292 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3294 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3295 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3296 !pa_tagstruct_eof(t
)) {
3298 pa_proplist_free(p
);
3304 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3305 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3306 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3307 !pa_tagstruct_eof(t
)) {
3309 pa_proplist_free(p
);
3314 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3316 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3319 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3320 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3321 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3323 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
3324 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3326 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3329 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3330 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3332 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
3333 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3335 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3337 pa_proplist_update(c
->client
->proplist
, mode
, p
);
3338 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3341 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3344 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3345 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3347 unsigned changed
= 0;
3349 pa_strlist
*l
= NULL
;
3351 pa_native_connection_assert_ref(c
);
3354 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3356 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3358 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3364 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3367 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3368 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3369 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3371 p
= s
->sink_input
->proplist
;
3373 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3376 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3377 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3379 p
= s
->source_output
->proplist
;
3381 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3383 p
= c
->client
->proplist
;
3389 if (pa_tagstruct_gets(t
, &k
) < 0) {
3398 l
= pa_strlist_prepend(l
, k
);
3401 if (!pa_tagstruct_eof(t
)) {
3410 l
= pa_strlist_pop(l
, &z
);
3415 changed
+= pa_proplist_unset(p
, z
) >= 0;
3419 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3422 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3425 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3426 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3428 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3431 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3432 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3435 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3436 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3441 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3442 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3445 pa_native_connection_assert_ref(c
);
3448 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3449 !pa_tagstruct_eof(t
)) {
3454 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3455 CHECK_VALIDITY(c
->pstream
, !s
|| pa_namereg_is_valid_name(s
), tag
, PA_ERR_INVALID
);
3457 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
3458 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3461 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3462 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3466 pa_native_connection_assert_ref(c
);
3469 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3470 pa_tagstruct_gets(t
, &name
) < 0 ||
3471 !pa_tagstruct_eof(t
)) {
3476 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3477 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3479 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
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 pa_sink_input_set_name(s
->sink_input
, name
);
3490 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3492 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3493 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3495 pa_source_output_set_name(s
->source_output
, name
);
3498 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3501 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3502 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3505 pa_native_connection_assert_ref(c
);
3508 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3509 !pa_tagstruct_eof(t
)) {
3514 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3516 if (command
== PA_COMMAND_KILL_CLIENT
) {
3519 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3520 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3522 pa_native_connection_ref(c
);
3523 pa_client_kill(client
);
3525 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3528 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3529 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3531 pa_native_connection_ref(c
);
3532 pa_sink_input_kill(s
);
3534 pa_source_output
*s
;
3536 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3538 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3539 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3541 pa_native_connection_ref(c
);
3542 pa_source_output_kill(s
);
3545 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3546 pa_native_connection_unref(c
);
3549 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3550 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3552 const char *name
, *argument
;
3553 pa_tagstruct
*reply
;
3555 pa_native_connection_assert_ref(c
);
3558 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3559 pa_tagstruct_gets(t
, &argument
) < 0 ||
3560 !pa_tagstruct_eof(t
)) {
3565 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3566 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3567 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3569 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3570 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3574 reply
= reply_new(tag
);
3575 pa_tagstruct_putu32(reply
, m
->index
);
3576 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3579 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3580 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3584 pa_native_connection_assert_ref(c
);
3587 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3588 !pa_tagstruct_eof(t
)) {
3593 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3594 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3595 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3597 pa_module_unload_request(m
, FALSE
);
3598 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3601 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3602 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3603 const char *name
, *module
, *argument
;
3606 pa_tagstruct
*reply
;
3608 pa_native_connection_assert_ref(c
);
3611 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3612 pa_tagstruct_getu32(t
, &type
) < 0 ||
3613 pa_tagstruct_gets(t
, &module
) < 0 ||
3614 pa_tagstruct_gets(t
, &argument
) < 0 ||
3615 !pa_tagstruct_eof(t
)) {
3620 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3621 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3622 CHECK_VALIDITY(c
->pstream
, type
== 0 || type
== 1, tag
, PA_ERR_INVALID
);
3623 CHECK_VALIDITY(c
->pstream
, module
&& *module
&& pa_utf8_valid(module
), tag
, PA_ERR_INVALID
);
3624 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3626 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
3627 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
3631 reply
= reply_new(tag
);
3632 pa_tagstruct_putu32(reply
, idx
);
3633 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3636 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3637 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3638 const char *name
= NULL
;
3639 uint32_t type
, idx
= PA_IDXSET_INVALID
;
3642 pa_native_connection_assert_ref(c
);
3645 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3646 (pa_tagstruct_gets(t
, &name
) < 0 ||
3647 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3648 !pa_tagstruct_eof(t
)) {
3653 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3654 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3655 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& pa_utf8_valid(name
) && (type
== 0 || type
== 1)), tag
, PA_ERR_INVALID
);
3658 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3660 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
3662 CHECK_VALIDITY(c
->pstream
, r
>= 0, tag
, PA_ERR_NOENTITY
);
3664 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3667 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
3670 pa_tagstruct_putu32(t
, e
->index
);
3671 pa_tagstruct_puts(t
, e
->name
);
3672 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
3673 pa_tagstruct_puts(t
, e
->module
);
3674 pa_tagstruct_puts(t
, e
->argument
);
3677 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3678 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3679 const pa_autoload_entry
*a
= NULL
;
3682 pa_tagstruct
*reply
;
3684 pa_native_connection_assert_ref(c
);
3687 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3688 (pa_tagstruct_gets(t
, &name
) < 0 ||
3689 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3690 !pa_tagstruct_eof(t
)) {
3695 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3696 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3697 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& (type
== 0 || type
== 1) && pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
3700 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3702 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
3704 CHECK_VALIDITY(c
->pstream
, a
, tag
, PA_ERR_NOENTITY
);
3706 reply
= reply_new(tag
);
3707 autoload_fill_tagstruct(reply
, a
);
3708 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3711 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3712 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3713 pa_tagstruct
*reply
;
3715 pa_native_connection_assert_ref(c
);
3718 if (!pa_tagstruct_eof(t
)) {
3723 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3725 reply
= reply_new(tag
);
3727 if (c
->protocol
->core
->autoload_hashmap
) {
3728 pa_autoload_entry
*a
;
3731 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
3732 autoload_fill_tagstruct(reply
, a
);
3735 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3738 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3739 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3740 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3741 const char *name_device
= NULL
;
3743 pa_native_connection_assert_ref(c
);
3746 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3747 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3748 pa_tagstruct_gets(t
, &name_device
) < 0 ||
3749 !pa_tagstruct_eof(t
)) {
3754 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3755 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3757 CHECK_VALIDITY(c
->pstream
, !name_device
|| pa_namereg_is_valid_name(name_device
), tag
, PA_ERR_INVALID
);
3758 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| name_device
, tag
, PA_ERR_INVALID
);
3759 CHECK_VALIDITY(c
->pstream
, idx_device
== PA_INVALID_INDEX
|| !name_device
, tag
, PA_ERR_INVALID
);
3760 CHECK_VALIDITY(c
->pstream
, !name_device
|| idx_device
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3762 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3763 pa_sink_input
*si
= NULL
;
3764 pa_sink
*sink
= NULL
;
3766 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3768 if (idx_device
!= PA_INVALID_INDEX
)
3769 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3771 sink
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SINK
, 1);
3773 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3775 if (pa_sink_input_move_to(si
, sink
) < 0) {
3776 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3780 pa_source_output
*so
= NULL
;
3783 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3785 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3787 if (idx_device
!= PA_INVALID_INDEX
)
3788 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3790 source
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SOURCE
, 1);
3792 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3794 if (pa_source_output_move_to(so
, source
) < 0) {
3795 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3800 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3803 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3804 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3805 uint32_t idx
= PA_INVALID_INDEX
;
3806 const char *name
= NULL
;
3809 pa_native_connection_assert_ref(c
);
3812 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3813 pa_tagstruct_gets(t
, &name
) < 0 ||
3814 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3815 !pa_tagstruct_eof(t
)) {
3820 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3821 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3822 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3823 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3824 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3826 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3828 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3830 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3831 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3835 pa_sink
*sink
= NULL
;
3837 if (idx
!= PA_INVALID_INDEX
)
3838 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3840 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3842 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3844 if (pa_sink_suspend(sink
, b
) < 0) {
3845 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3851 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3853 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3855 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3856 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3863 if (idx
!= PA_INVALID_INDEX
)
3864 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3866 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3868 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3870 if (pa_source_suspend(source
, b
) < 0) {
3871 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3877 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3880 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3881 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3882 uint32_t idx
= PA_INVALID_INDEX
;
3883 const char *name
= NULL
;
3885 pa_native_protocol_ext_cb_t cb
;
3887 pa_native_connection_assert_ref(c
);
3890 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3891 pa_tagstruct_gets(t
, &name
) < 0) {
3896 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3897 CHECK_VALIDITY(c
->pstream
, !name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3898 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3899 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3900 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3902 if (idx
!= PA_INVALID_INDEX
)
3903 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3905 for (m
= pa_idxset_first(c
->protocol
->core
->modules
, &idx
); m
; m
= pa_idxset_next(c
->protocol
->core
->modules
, &idx
))
3906 if (strcmp(name
, m
->name
) == 0)
3910 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3911 CHECK_VALIDITY(c
->pstream
, m
->load_once
|| idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3913 cb
= (pa_native_protocol_ext_cb_t
) pa_hashmap_get(c
->protocol
->extensions
, m
);
3914 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3916 if (cb(c
->protocol
, m
, c
, tag
, t
) < 0)
3921 /*** pstream callbacks ***/
3923 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
3924 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3928 pa_native_connection_assert_ref(c
);
3930 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
3931 pa_log("invalid packet.");
3932 native_connection_unlink(c
);
3936 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
) {
3937 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3938 output_stream
*stream
;
3942 pa_native_connection_assert_ref(c
);
3944 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
3945 pa_log("client sent block for invalid stream.");
3950 if (playback_stream_isinstance(stream
)) {
3951 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
3953 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
3954 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
);
3956 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
3959 upload_stream
*u
= UPLOAD_STREAM(stream
);
3962 if (!u
->memchunk
.memblock
) {
3963 if (u
->length
== chunk
->length
) {
3964 u
->memchunk
= *chunk
;
3965 pa_memblock_ref(u
->memchunk
.memblock
);
3968 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
3969 u
->memchunk
.index
= u
->memchunk
.length
= 0;
3973 pa_assert(u
->memchunk
.memblock
);
3976 if (l
> chunk
->length
)
3982 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
3983 src
= pa_memblock_acquire(chunk
->memblock
);
3985 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
3986 (uint8_t*) src
+chunk
->index
, l
);
3988 pa_memblock_release(u
->memchunk
.memblock
);
3989 pa_memblock_release(chunk
->memblock
);
3991 u
->memchunk
.length
+= l
;
3997 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
3998 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4001 pa_native_connection_assert_ref(c
);
4003 native_connection_unlink(c
);
4004 pa_log_info("Connection died.");
4007 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
4008 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4011 pa_native_connection_assert_ref(c
);
4013 native_connection_send_memblock(c
);
4016 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4019 if (!(q
= pa_thread_mq_get()))
4020 pa_pstream_send_revoke(p
, block_id
);
4022 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4025 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4028 if (!(q
= pa_thread_mq_get()))
4029 pa_pstream_send_release(p
, block_id
);
4031 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4034 /*** client callbacks ***/
4036 static void client_kill_cb(pa_client
*c
) {
4039 native_connection_unlink(PA_NATIVE_CONNECTION(c
->userdata
));
4040 pa_log_info("Connection killed.");
4043 /*** module entry points ***/
4045 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
4046 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4050 pa_native_connection_assert_ref(c
);
4051 pa_assert(c
->auth_timeout_event
== e
);
4053 if (!c
->authorized
) {
4054 native_connection_unlink(c
);
4055 pa_log_info("Connection terminated due to authentication timeout.");
4059 void pa_native_protocol_connect(pa_native_protocol
*p
, pa_iochannel
*io
, pa_native_options
*o
) {
4060 pa_native_connection
*c
;
4061 char cname
[256], pname
[128];
4067 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
4068 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
4069 pa_iochannel_free(io
);
4073 c
= pa_msgobject_new(pa_native_connection
);
4074 c
->parent
.parent
.free
= native_connection_free
;
4075 c
->parent
.process_msg
= native_connection_process_msg
;
4077 c
->options
= pa_native_options_ref(o
);
4078 c
->authorized
= FALSE
;
4080 if (o
->auth_anonymous
) {
4081 pa_log_info("Client authenticated anonymously.");
4082 c
->authorized
= TRUE
;
4085 if (!c
->authorized
&&
4087 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
4089 pa_log_info("Client authenticated by IP ACL.");
4090 c
->authorized
= TRUE
;
4093 if (!c
->authorized
) {
4095 pa_gettimeofday(&tv
);
4096 tv
.tv_sec
+= AUTH_TIMEOUT
;
4097 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
4099 c
->auth_timeout_event
= NULL
;
4101 c
->is_local
= pa_iochannel_socket_is_local(io
);
4104 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
4105 pa_snprintf(cname
, sizeof(cname
), "Native client (%s)", pname
);
4106 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
4107 pa_proplist_sets(c
->client
->proplist
, "native-protocol.peer", pname
);
4108 c
->client
->kill
= client_kill_cb
;
4109 c
->client
->userdata
= c
;
4110 c
->client
->module
= o
->module
;
4112 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
4113 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
4114 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
4115 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
4116 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
4117 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
4118 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
4120 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
4122 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
4123 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
4125 c
->rrobin_index
= PA_IDXSET_INVALID
;
4126 c
->subscription
= NULL
;
4128 pa_idxset_put(p
->connections
, c
, NULL
);
4131 if (pa_iochannel_creds_supported(io
))
4132 pa_iochannel_creds_enable(io
);
4135 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_CONNECTION_PUT
], c
);
4138 void pa_native_protocol_disconnect(pa_native_protocol
*p
, pa_module
*m
) {
4139 pa_native_connection
*c
;
4145 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
4146 if (c
->options
->module
== m
)
4147 native_connection_unlink(c
);
4150 static pa_native_protocol
* native_protocol_new(pa_core
*c
) {
4151 pa_native_protocol
*p
;
4156 p
= pa_xnew(pa_native_protocol
, 1);
4159 p
->connections
= pa_idxset_new(NULL
, NULL
);
4163 p
->extensions
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
4165 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4166 pa_hook_init(&p
->hooks
[h
], p
);
4168 pa_assert_se(pa_shared_set(c
, "native-protocol", p
) >= 0);
4173 pa_native_protocol
* pa_native_protocol_get(pa_core
*c
) {
4174 pa_native_protocol
*p
;
4176 if ((p
= pa_shared_get(c
, "native-protocol")))
4177 return pa_native_protocol_ref(p
);
4179 return native_protocol_new(c
);
4182 pa_native_protocol
* pa_native_protocol_ref(pa_native_protocol
*p
) {
4184 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4191 void pa_native_protocol_unref(pa_native_protocol
*p
) {
4192 pa_native_connection
*c
;
4196 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4198 if (PA_REFCNT_DEC(p
) > 0)
4201 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4202 native_connection_unlink(c
);
4204 pa_idxset_free(p
->connections
, NULL
, NULL
);
4206 pa_strlist_free(p
->servers
);
4208 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4209 pa_hook_done(&p
->hooks
[h
]);
4211 pa_hashmap_free(p
->extensions
, NULL
, NULL
);
4213 pa_assert_se(pa_shared_remove(p
->core
, "native-protocol") >= 0);
4218 void pa_native_protocol_add_server_string(pa_native_protocol
*p
, const char *name
) {
4220 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4223 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
4225 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4228 void pa_native_protocol_remove_server_string(pa_native_protocol
*p
, const char *name
) {
4230 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4233 p
->servers
= pa_strlist_remove(p
->servers
, name
);
4235 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4238 pa_hook
*pa_native_protocol_hooks(pa_native_protocol
*p
) {
4240 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4245 pa_strlist
*pa_native_protocol_servers(pa_native_protocol
*p
) {
4247 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4252 int pa_native_protocol_install_ext(pa_native_protocol
*p
, pa_module
*m
, pa_native_protocol_ext_cb_t cb
) {
4254 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4257 pa_assert(!pa_hashmap_get(p
->extensions
, m
));
4259 pa_assert_se(pa_hashmap_put(p
->extensions
, m
, (void*) cb
) == 0);
4263 void pa_native_protocol_remove_ext(pa_native_protocol
*p
, pa_module
*m
) {
4265 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4268 pa_assert_se(pa_hashmap_remove(p
->extensions
, m
));
4271 pa_native_options
* pa_native_options_new(void) {
4272 pa_native_options
*o
;
4274 o
= pa_xnew0(pa_native_options
, 1);
4280 pa_native_options
* pa_native_options_ref(pa_native_options
*o
) {
4282 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4289 void pa_native_options_unref(pa_native_options
*o
) {
4291 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4293 if (PA_REFCNT_DEC(o
) > 0)
4296 pa_xfree(o
->auth_group
);
4299 pa_ip_acl_free(o
->auth_ip_acl
);
4302 pa_auth_cookie_unref(o
->auth_cookie
);
4307 int pa_native_options_parse(pa_native_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
4312 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4315 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
4316 pa_log("auth-anonymous= expects a boolean argument.");
4321 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &enabled
) < 0) {
4322 pa_log("auth-group-enabled= expects a boolean argument.");
4326 pa_xfree(o
->auth_group
);
4327 o
->auth_group
= enabled
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4331 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4334 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4337 if (!(ipa
= pa_ip_acl_new(acl
))) {
4338 pa_log("Failed to parse IP ACL '%s'", acl
);
4343 pa_ip_acl_free(o
->auth_ip_acl
);
4345 o
->auth_ip_acl
= ipa
;
4349 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
4350 pa_log("auth-cookie-enabled= expects a boolean argument.");
4355 pa_auth_cookie_unref(o
->auth_cookie
);
4360 /* The new name for this is 'auth-cookie', for compat reasons
4361 * we check the old name too */
4362 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
4363 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
4364 cn
= PA_NATIVE_COOKIE_FILE
;
4366 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, PA_NATIVE_COOKIE_LENGTH
)))
4370 o
->auth_cookie
= NULL
;
4375 pa_pstream
* pa_native_connection_get_pstream(pa_native_connection
*c
) {
4376 pa_native_connection_assert_ref(c
);