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
= (uint32_t) pa_frame_size(&s
->source_output
->sample_spec
);
482 if (*fragsize
== (uint32_t) -1)
483 *fragsize
= (uint32_t) pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC
*PA_USEC_PER_MSEC
, &s
->source_output
->sample_spec
);
485 *fragsize
= (uint32_t) pa_frame_size(&s
->source_output
->sample_spec
);
487 if (adjust_latency
) {
488 pa_usec_t orig_fragsize_usec
, 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 orig_fragsize_usec
= 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 if (pa_usec_to_bytes(orig_fragsize_usec
, &s
->source_output
->sample_spec
) !=
505 pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
))
507 *fragsize
= (uint32_t) pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
);
509 s
->source_latency
= 0;
512 static void fix_record_buffer_attr_post(record_stream
*s
, uint32_t *maxlength
, uint32_t *fragsize
) {
516 pa_assert(maxlength
);
519 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
521 base
= pa_frame_size(&s
->source_output
->sample_spec
);
523 s
->fragment_size
= (*fragsize
/base
)*base
;
524 if (s
->fragment_size
<= 0)
525 s
->fragment_size
= base
;
527 if (s
->fragment_size
> *maxlength
)
528 s
->fragment_size
= *maxlength
;
530 *fragsize
= (uint32_t) s
->fragment_size
;
533 static record_stream
* record_stream_new(
534 pa_native_connection
*c
,
538 pa_bool_t peak_detect
,
541 pa_source_output_flags_t flags
,
543 pa_bool_t adjust_latency
,
544 pa_sink_input
*direct_on_input
) {
547 pa_source_output
*source_output
;
549 pa_source_output_new_data data
;
553 pa_assert(maxlength
);
556 pa_source_output_new_data_init(&data
);
558 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
559 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
560 data
.driver
= __FILE__
;
561 data
.module
= c
->options
->module
;
562 data
.client
= c
->client
;
563 data
.source
= source
;
564 data
.direct_on_input
= direct_on_input
;
565 pa_source_output_new_data_set_sample_spec(&data
, ss
);
566 pa_source_output_new_data_set_channel_map(&data
, map
);
568 data
.resample_method
= PA_RESAMPLER_PEAKS
;
570 source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
);
572 pa_source_output_new_data_done(&data
);
577 s
= pa_msgobject_new(record_stream
);
578 s
->parent
.parent
.free
= record_stream_free
;
579 s
->parent
.process_msg
= record_stream_process_msg
;
581 s
->source_output
= source_output
;
583 s
->source_output
->push
= source_output_push_cb
;
584 s
->source_output
->kill
= source_output_kill_cb
;
585 s
->source_output
->get_latency
= source_output_get_latency_cb
;
586 s
->source_output
->moved
= source_output_moved_cb
;
587 s
->source_output
->suspend
= source_output_suspend_cb
;
588 s
->source_output
->userdata
= s
;
590 fix_record_buffer_attr_pre(s
, adjust_latency
, maxlength
, fragsize
);
592 s
->memblockq
= pa_memblockq_new(
596 base
= pa_frame_size(&source_output
->sample_spec
),
602 fix_record_buffer_attr_post(s
, maxlength
, fragsize
);
604 *ss
= s
->source_output
->sample_spec
;
605 *map
= s
->source_output
->channel_map
;
607 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
609 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
610 ((double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) + (double) s
->source_latency
) / PA_USEC_PER_MSEC
,
611 (double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) / PA_USEC_PER_MSEC
,
612 (double) s
->source_latency
/ PA_USEC_PER_MSEC
);
614 pa_source_output_put(s
->source_output
);
618 static void record_stream_send_killed(record_stream
*r
) {
620 record_stream_assert_ref(r
);
622 t
= pa_tagstruct_new(NULL
, 0);
623 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
624 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
625 pa_tagstruct_putu32(t
, r
->index
);
626 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
629 static void playback_stream_unlink(playback_stream
*s
) {
636 pa_sink_input_unlink(s
->sink_input
);
637 pa_sink_input_unref(s
->sink_input
);
638 s
->sink_input
= NULL
;
641 if (s
->drain_request
)
642 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
644 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
645 s
->connection
= NULL
;
646 playback_stream_unref(s
);
649 static void playback_stream_free(pa_object
* o
) {
650 playback_stream
*s
= PLAYBACK_STREAM(o
);
653 playback_stream_unlink(s
);
655 pa_memblockq_free(s
->memblockq
);
659 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
660 playback_stream
*s
= PLAYBACK_STREAM(o
);
661 playback_stream_assert_ref(s
);
667 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
672 if ((l
= (uint32_t) pa_atomic_load(&s
->missing
)) <= 0)
675 if (pa_atomic_cmpxchg(&s
->missing
, (int) l
, 0))
682 t
= pa_tagstruct_new(NULL
, 0);
683 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
684 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
685 pa_tagstruct_putu32(t
, s
->index
);
686 pa_tagstruct_putu32(t
, l
);
687 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
689 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
693 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
696 /* Report that we're empty */
697 t
= pa_tagstruct_new(NULL
, 0);
698 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
699 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
700 pa_tagstruct_putu32(t
, s
->index
);
701 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
705 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
708 /* Notify the user we're overflowed*/
709 t
= pa_tagstruct_new(NULL
, 0);
710 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
711 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
712 pa_tagstruct_putu32(t
, s
->index
);
713 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
717 case PLAYBACK_STREAM_MESSAGE_STARTED
:
719 if (s
->connection
->version
>= 13) {
722 /* Notify the user we're overflowed*/
723 t
= pa_tagstruct_new(NULL
, 0);
724 pa_tagstruct_putu32(t
, PA_COMMAND_STARTED
);
725 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
726 pa_tagstruct_putu32(t
, s
->index
);
727 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
732 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
733 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
740 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
) {
742 pa_usec_t orig_tlength_usec
, tlength_usec
, orig_minreq_usec
, minreq_usec
, sink_usec
;
745 pa_assert(maxlength
);
750 frame_size
= pa_frame_size(&s
->sink_input
->sample_spec
);
752 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
753 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
755 *maxlength
= (uint32_t) frame_size
;
757 if (*tlength
== (uint32_t) -1)
758 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_TLENGTH_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
760 *tlength
= (uint32_t) frame_size
;
762 if (*minreq
== (uint32_t) -1)
763 *minreq
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_PROCESS_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
765 *minreq
= (uint32_t) frame_size
;
767 if (*tlength
< *minreq
+frame_size
)
768 *tlength
= *minreq
+(uint32_t) frame_size
;
770 orig_tlength_usec
= tlength_usec
= pa_bytes_to_usec(*tlength
, &s
->sink_input
->sample_spec
);
771 orig_minreq_usec
= minreq_usec
= pa_bytes_to_usec(*minreq
, &s
->sink_input
->sample_spec
);
773 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
774 (double) tlength_usec
/ PA_USEC_PER_MSEC
,
775 (double) minreq_usec
/ PA_USEC_PER_MSEC
);
777 if (adjust_latency
) {
779 /* So, the user asked us to adjust the latency of the stream
780 * buffer according to the what the sink can provide. The
781 * tlength passed in shall be the overall latency. Roughly
782 * half the latency will be spent on the hw buffer, the other
783 * half of it in the async buffer queue we maintain for each
784 * client. In between we'll have a safety space of size
785 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
786 * empty and needs to be filled, then our buffer must have
787 * enough data to fulfill this request immediatly and thus
788 * have at least the same tlength as the size of the hw
789 * buffer. It additionally needs space for 2 times minreq
790 * because if the buffer ran empty and a partial fillup
791 * happens immediately on the next iteration we need to be
792 * able to fulfill it and give the application also minreq
793 * time to fill it up again for the next request Makes 2 times
794 * minreq in plus.. */
796 if (tlength_usec
> minreq_usec
*2)
797 sink_usec
= (tlength_usec
- minreq_usec
*2)/2;
803 /* Ok, the user didn't ask us to adjust the latency, but we
804 * still need to make sure that the parameters from the user
807 if (tlength_usec
> minreq_usec
*2)
808 sink_usec
= (tlength_usec
- minreq_usec
*2);
813 s
->sink_latency
= pa_sink_input_set_requested_latency(s
->sink_input
, sink_usec
);
815 if (adjust_latency
) {
816 /* Ok, we didn't necessarily get what we were asking for, so
817 * let's subtract from what we asked for for the remaining
820 if (tlength_usec
>= s
->sink_latency
)
821 tlength_usec
-= s
->sink_latency
;
824 /* FIXME: This is actually larger than necessary, since not all of
825 * the sink latency is actually rewritable. */
826 if (tlength_usec
< s
->sink_latency
+ 2*minreq_usec
)
827 tlength_usec
= s
->sink_latency
+ 2*minreq_usec
;
829 if (pa_usec_to_bytes_round_up(orig_tlength_usec
, &s
->sink_input
->sample_spec
) !=
830 pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
))
831 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
);
833 if (pa_usec_to_bytes(orig_minreq_usec
, &s
->sink_input
->sample_spec
) !=
834 pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
))
835 *minreq
= (uint32_t) pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
);
838 *minreq
+= (uint32_t) frame_size
;
839 *tlength
+= (uint32_t) frame_size
*2;
842 if (*tlength
<= *minreq
)
843 *tlength
= *minreq
*2 + (uint32_t) frame_size
;
845 if (*prebuf
== (uint32_t) -1 || *prebuf
> *tlength
)
849 static void fix_playback_buffer_attr_post(playback_stream
*s
, uint32_t *maxlength
, uint32_t *tlength
, uint32_t* prebuf
, uint32_t* minreq
) {
851 pa_assert(maxlength
);
856 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
857 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
858 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
859 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
864 static playback_stream
* playback_stream_new(
865 pa_native_connection
*c
,
877 pa_sink_input_flags_t flags
,
879 pa_bool_t adjust_latency
) {
881 playback_stream
*s
, *ssync
;
882 pa_sink_input
*sink_input
;
886 pa_sink_input_new_data data
;
890 pa_assert(maxlength
);
897 /* Find syncid group */
898 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
900 if (!playback_stream_isinstance(ssync
))
903 if (ssync
->syncid
== syncid
)
907 /* Synced streams must connect to the same sink */
911 sink
= ssync
->sink_input
->sink
;
912 else if (sink
!= ssync
->sink_input
->sink
)
916 pa_sink_input_new_data_init(&data
);
918 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
919 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
920 data
.driver
= __FILE__
;
921 data
.module
= c
->options
->module
;
922 data
.client
= c
->client
;
924 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
925 pa_sink_input_new_data_set_channel_map(&data
, map
);
927 pa_sink_input_new_data_set_volume(&data
, volume
);
928 pa_sink_input_new_data_set_muted(&data
, muted
);
929 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
931 sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
);
933 pa_sink_input_new_data_done(&data
);
938 s
= pa_msgobject_new(playback_stream
);
939 s
->parent
.parent
.parent
.free
= playback_stream_free
;
940 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
943 s
->sink_input
= sink_input
;
944 s
->is_underrun
= TRUE
;
945 s
->drain_request
= FALSE
;
946 pa_atomic_store(&s
->missing
, 0);
948 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
949 s
->sink_input
->pop
= sink_input_pop_cb
;
950 s
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
951 s
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
952 s
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
953 s
->sink_input
->kill
= sink_input_kill_cb
;
954 s
->sink_input
->moved
= sink_input_moved_cb
;
955 s
->sink_input
->suspend
= sink_input_suspend_cb
;
956 s
->sink_input
->userdata
= s
;
958 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
960 fix_playback_buffer_attr_pre(s
, adjust_latency
, maxlength
, tlength
, prebuf
, minreq
);
961 pa_sink_input_get_silence(sink_input
, &silence
);
963 s
->memblockq
= pa_memblockq_new(
967 pa_frame_size(&sink_input
->sample_spec
),
973 pa_memblock_unref(silence
.memblock
);
974 fix_playback_buffer_attr_post(s
, maxlength
, tlength
, prebuf
, minreq
);
976 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
978 *ss
= s
->sink_input
->sample_spec
;
979 *map
= s
->sink_input
->channel_map
;
981 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
983 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
984 ((double) pa_bytes_to_usec(*tlength
, &sink_input
->sample_spec
) + (double) s
->sink_latency
) / PA_USEC_PER_MSEC
,
985 (double) pa_bytes_to_usec(*tlength
-*minreq
*2, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
986 (double) pa_bytes_to_usec(*minreq
, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
987 (double) s
->sink_latency
/ PA_USEC_PER_MSEC
);
989 pa_sink_input_put(s
->sink_input
);
993 /* Called from thread context */
994 static void playback_stream_request_bytes(playback_stream
*s
) {
995 size_t m
, previous_missing
;
997 playback_stream_assert_ref(s
);
999 m
= pa_memblockq_pop_missing(s
->memblockq
);
1004 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
1006 previous_missing
= (size_t) pa_atomic_add(&s
->missing
, (int) m
);
1008 if (pa_memblockq_prebuf_active(s
->memblockq
) ||
1009 (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
))
1010 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1014 static void playback_stream_send_killed(playback_stream
*p
) {
1016 playback_stream_assert_ref(p
);
1018 t
= pa_tagstruct_new(NULL
, 0);
1019 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
1020 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1021 pa_tagstruct_putu32(t
, p
->index
);
1022 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
1025 static int native_connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1026 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1027 pa_native_connection_assert_ref(c
);
1034 case CONNECTION_MESSAGE_REVOKE
:
1035 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1038 case CONNECTION_MESSAGE_RELEASE
:
1039 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1046 static void native_connection_unlink(pa_native_connection
*c
) {
1055 pa_hook_fire(&c
->protocol
->hooks
[PA_NATIVE_HOOK_CONNECTION_UNLINK
], c
);
1058 pa_native_options_unref(c
->options
);
1060 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
1061 record_stream_unlink(r
);
1063 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
1064 if (playback_stream_isinstance(o
))
1065 playback_stream_unlink(PLAYBACK_STREAM(o
));
1067 upload_stream_unlink(UPLOAD_STREAM(o
));
1069 if (c
->subscription
)
1070 pa_subscription_free(c
->subscription
);
1073 pa_pstream_unlink(c
->pstream
);
1075 if (c
->auth_timeout_event
) {
1076 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1077 c
->auth_timeout_event
= NULL
;
1080 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
1082 pa_native_connection_unref(c
);
1085 static void native_connection_free(pa_object
*o
) {
1086 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1090 native_connection_unlink(c
);
1092 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
1093 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
1095 pa_pdispatch_unref(c
->pdispatch
);
1096 pa_pstream_unref(c
->pstream
);
1097 pa_client_free(c
->client
);
1102 static void native_connection_send_memblock(pa_native_connection
*c
) {
1106 start
= PA_IDXSET_INVALID
;
1110 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
1113 if (start
== PA_IDXSET_INVALID
)
1114 start
= c
->rrobin_index
;
1115 else if (start
== c
->rrobin_index
)
1118 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
1119 pa_memchunk schunk
= chunk
;
1121 if (schunk
.length
> r
->fragment_size
)
1122 schunk
.length
= r
->fragment_size
;
1124 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
1126 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
1127 pa_memblock_unref(schunk
.memblock
);
1134 /*** sink input callbacks ***/
1136 static void handle_seek(playback_stream
*s
, int64_t indexw
) {
1137 playback_stream_assert_ref(s
);
1139 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1141 if (s
->sink_input
->thread_info
.underrun_for
> 0) {
1143 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1145 if (pa_memblockq_is_readable(s
->memblockq
)) {
1147 /* We just ended an underrun, let's ask the sink
1148 * for a complete rewind rewrite */
1150 pa_log_debug("Requesting rewind due to end of underrun.");
1151 pa_sink_input_request_rewind(s
->sink_input
,
1152 (size_t) (s
->sink_input
->thread_info
.underrun_for
== (size_t) -1 ? 0 : s
->sink_input
->thread_info
.underrun_for
),
1159 indexr
= pa_memblockq_get_read_index(s
->memblockq
);
1161 if (indexw
< indexr
) {
1162 /* OK, the sink already asked for this data, so
1163 * let's have it usk us again */
1165 pa_log_debug("Requesting rewind due to rewrite.");
1166 pa_sink_input_request_rewind(s
->sink_input
, (size_t) (indexr
- indexw
), TRUE
, FALSE
);
1170 playback_stream_request_bytes(s
);
1173 /* Called from thread context */
1174 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1175 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1178 pa_sink_input_assert_ref(i
);
1179 s
= PLAYBACK_STREAM(i
->userdata
);
1180 playback_stream_assert_ref(s
);
1184 case SINK_INPUT_MESSAGE_SEEK
: {
1187 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1188 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
1190 handle_seek(s
, windex
);
1194 case SINK_INPUT_MESSAGE_POST_DATA
: {
1199 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1201 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1203 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
1204 pa_log_warn("Failed to push data into queue");
1205 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
1206 pa_memblockq_seek(s
->memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
1209 handle_seek(s
, windex
);
1211 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1216 case SINK_INPUT_MESSAGE_DRAIN
:
1217 case SINK_INPUT_MESSAGE_FLUSH
:
1218 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1219 case SINK_INPUT_MESSAGE_TRIGGER
: {
1222 pa_sink_input
*isync
;
1223 void (*func
)(pa_memblockq
*bq
);
1226 case SINK_INPUT_MESSAGE_FLUSH
:
1227 func
= pa_memblockq_flush_write
;
1230 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1231 func
= pa_memblockq_prebuf_force
;
1234 case SINK_INPUT_MESSAGE_DRAIN
:
1235 case SINK_INPUT_MESSAGE_TRIGGER
:
1236 func
= pa_memblockq_prebuf_disable
;
1240 pa_assert_not_reached();
1243 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1245 handle_seek(s
, windex
);
1247 /* Do the same for all other members in the sync group */
1248 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
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 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1256 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1257 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1258 func(ssync
->memblockq
);
1259 handle_seek(ssync
, windex
);
1262 if (code
== SINK_INPUT_MESSAGE_DRAIN
) {
1263 if (!pa_memblockq_is_readable(s
->memblockq
))
1264 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
1266 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
1267 s
->drain_request
= TRUE
;
1274 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1276 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1277 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1278 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1281 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1284 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1286 pa_memblockq_prebuf_force(s
->memblockq
);
1288 handle_seek(s
, windex
);
1290 /* Fall through to the default handler */
1294 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1295 pa_usec_t
*r
= userdata
;
1297 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1299 /* Fall through, the default handler will add in the extra
1300 * latency added by the resampler */
1305 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1308 /* Called from thread context */
1309 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
1312 pa_sink_input_assert_ref(i
);
1313 s
= PLAYBACK_STREAM(i
->userdata
);
1314 playback_stream_assert_ref(s
);
1317 /* pa_log("%s, pop(): %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1319 if (pa_memblockq_is_readable(s
->memblockq
))
1320 s
->is_underrun
= FALSE
;
1322 /* pa_log("%s, UNDERRUN: %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1324 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1325 s
->drain_request
= FALSE
;
1326 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
);
1327 } else if (!s
->is_underrun
)
1328 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1330 s
->is_underrun
= TRUE
;
1332 playback_stream_request_bytes(s
);
1335 /* This call will not fail with prebuf=0, hence we check for
1336 underrun explicitly above */
1337 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
1340 chunk
->length
= PA_MIN(nbytes
, chunk
->length
);
1342 if (i
->thread_info
.underrun_for
> 0)
1343 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_STARTED
, NULL
, 0, NULL
, NULL
);
1345 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1346 playback_stream_request_bytes(s
);
1351 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1354 pa_sink_input_assert_ref(i
);
1355 s
= PLAYBACK_STREAM(i
->userdata
);
1356 playback_stream_assert_ref(s
);
1358 /* If we are in an underrun, then we don't rewind */
1359 if (i
->thread_info
.underrun_for
> 0)
1362 pa_memblockq_rewind(s
->memblockq
, nbytes
);
1365 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1368 pa_sink_input_assert_ref(i
);
1369 s
= PLAYBACK_STREAM(i
->userdata
);
1370 playback_stream_assert_ref(s
);
1372 pa_memblockq_set_maxrewind(s
->memblockq
, nbytes
);
1375 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
1379 pa_sink_input_assert_ref(i
);
1380 s
= PLAYBACK_STREAM(i
->userdata
);
1381 playback_stream_assert_ref(s
);
1383 tlength
= nbytes
+2*pa_memblockq_get_minreq(s
->memblockq
);
1385 if (pa_memblockq_get_tlength(s
->memblockq
) < tlength
)
1386 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1389 /* Called from main context */
1390 static void sink_input_kill_cb(pa_sink_input
*i
) {
1393 pa_sink_input_assert_ref(i
);
1394 s
= PLAYBACK_STREAM(i
->userdata
);
1395 playback_stream_assert_ref(s
);
1397 playback_stream_send_killed(s
);
1398 playback_stream_unlink(s
);
1401 /* Called from main context */
1402 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1406 pa_sink_input_assert_ref(i
);
1407 s
= PLAYBACK_STREAM(i
->userdata
);
1408 playback_stream_assert_ref(s
);
1410 if (s
->connection
->version
< 12)
1413 t
= pa_tagstruct_new(NULL
, 0);
1414 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1415 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1416 pa_tagstruct_putu32(t
, s
->index
);
1417 pa_tagstruct_put_boolean(t
, suspend
);
1418 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1421 /* Called from main context */
1422 static void sink_input_moved_cb(pa_sink_input
*i
) {
1425 uint32_t maxlength
, tlength
, prebuf
, minreq
;
1427 pa_sink_input_assert_ref(i
);
1428 s
= PLAYBACK_STREAM(i
->userdata
);
1429 playback_stream_assert_ref(s
);
1431 maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
1432 tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
1433 prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
1434 minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
1436 fix_playback_buffer_attr_pre(s
, TRUE
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1437 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1438 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1439 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
1440 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
1441 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1443 if (s
->connection
->version
< 12)
1446 t
= pa_tagstruct_new(NULL
, 0);
1447 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1448 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1449 pa_tagstruct_putu32(t
, s
->index
);
1450 pa_tagstruct_putu32(t
, i
->sink
->index
);
1451 pa_tagstruct_puts(t
, i
->sink
->name
);
1452 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1454 if (s
->connection
->version
>= 13) {
1455 pa_tagstruct_putu32(t
, maxlength
);
1456 pa_tagstruct_putu32(t
, tlength
);
1457 pa_tagstruct_putu32(t
, prebuf
);
1458 pa_tagstruct_putu32(t
, minreq
);
1459 pa_tagstruct_put_usec(t
, s
->sink_latency
);
1462 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1465 /*** source_output callbacks ***/
1467 /* Called from thread context */
1468 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1471 pa_source_output_assert_ref(o
);
1472 s
= RECORD_STREAM(o
->userdata
);
1473 record_stream_assert_ref(s
);
1476 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1479 static void source_output_kill_cb(pa_source_output
*o
) {
1482 pa_source_output_assert_ref(o
);
1483 s
= RECORD_STREAM(o
->userdata
);
1484 record_stream_assert_ref(s
);
1486 record_stream_send_killed(s
);
1487 record_stream_unlink(s
);
1490 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1493 pa_source_output_assert_ref(o
);
1494 s
= RECORD_STREAM(o
->userdata
);
1495 record_stream_assert_ref(s
);
1497 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1499 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1502 /* Called from main context */
1503 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1507 pa_source_output_assert_ref(o
);
1508 s
= RECORD_STREAM(o
->userdata
);
1509 record_stream_assert_ref(s
);
1511 if (s
->connection
->version
< 12)
1514 t
= pa_tagstruct_new(NULL
, 0);
1515 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1516 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1517 pa_tagstruct_putu32(t
, s
->index
);
1518 pa_tagstruct_put_boolean(t
, suspend
);
1519 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1522 /* Called from main context */
1523 static void source_output_moved_cb(pa_source_output
*o
) {
1526 uint32_t maxlength
, fragsize
;
1528 pa_source_output_assert_ref(o
);
1529 s
= RECORD_STREAM(o
->userdata
);
1530 record_stream_assert_ref(s
);
1532 fragsize
= (uint32_t) s
->fragment_size
;
1533 maxlength
= (uint32_t) pa_memblockq_get_length(s
->memblockq
);
1535 fix_record_buffer_attr_pre(s
, TRUE
, &maxlength
, &fragsize
);
1536 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1537 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
1539 if (s
->connection
->version
< 12)
1542 t
= pa_tagstruct_new(NULL
, 0);
1543 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1544 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1545 pa_tagstruct_putu32(t
, s
->index
);
1546 pa_tagstruct_putu32(t
, o
->source
->index
);
1547 pa_tagstruct_puts(t
, o
->source
->name
);
1548 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1550 if (s
->connection
->version
>= 13) {
1551 pa_tagstruct_putu32(t
, maxlength
);
1552 pa_tagstruct_putu32(t
, fragsize
);
1553 pa_tagstruct_put_usec(t
, s
->source_latency
);
1556 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1559 /*** pdispatch callbacks ***/
1561 static void protocol_error(pa_native_connection
*c
) {
1562 pa_log("protocol error, kicking client");
1563 native_connection_unlink(c
);
1566 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1567 if (!(expression)) { \
1568 pa_pstream_send_error((pstream), (tag), (error)); \
1573 static pa_tagstruct
*reply_new(uint32_t tag
) {
1574 pa_tagstruct
*reply
;
1576 reply
= pa_tagstruct_new(NULL
, 0);
1577 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1578 pa_tagstruct_putu32(reply
, tag
);
1582 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1583 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1585 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1586 const char *name
= NULL
, *sink_name
;
1589 pa_tagstruct
*reply
;
1590 pa_sink
*sink
= NULL
;
1598 fix_channels
= FALSE
,
1600 variable_rate
= FALSE
,
1602 adjust_latency
= FALSE
;
1604 pa_sink_input_flags_t flags
= 0;
1606 pa_bool_t volume_set
= TRUE
;
1608 pa_native_connection_assert_ref(c
);
1611 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1614 PA_TAG_SAMPLE_SPEC
, &ss
,
1615 PA_TAG_CHANNEL_MAP
, &map
,
1616 PA_TAG_U32
, &sink_index
,
1617 PA_TAG_STRING
, &sink_name
,
1618 PA_TAG_U32
, &maxlength
,
1619 PA_TAG_BOOLEAN
, &corked
,
1620 PA_TAG_U32
, &tlength
,
1621 PA_TAG_U32
, &prebuf
,
1622 PA_TAG_U32
, &minreq
,
1623 PA_TAG_U32
, &syncid
,
1624 PA_TAG_CVOLUME
, &volume
,
1625 PA_TAG_INVALID
) < 0) {
1631 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1632 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
1633 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
1634 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1635 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1636 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1637 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1638 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1640 p
= pa_proplist_new();
1643 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1645 if (c
->version
>= 12) {
1646 /* Since 0.9.8 the user can ask for a couple of additional flags */
1648 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1649 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1650 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1651 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1652 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1653 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1654 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1657 pa_proplist_free(p
);
1662 if (c
->version
>= 13) {
1664 if (pa_tagstruct_get_boolean(t
, &muted
) < 0 ||
1665 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1666 pa_tagstruct_get_proplist(t
, p
) < 0) {
1668 pa_proplist_free(p
);
1673 if (c
->version
>= 14) {
1675 if (pa_tagstruct_get_boolean(t
, &volume_set
) < 0) {
1677 pa_proplist_free(p
);
1682 if (!pa_tagstruct_eof(t
)) {
1684 pa_proplist_free(p
);
1688 if (sink_index
!= PA_INVALID_INDEX
) {
1690 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1691 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1692 pa_proplist_free(p
);
1696 } else if (sink_name
) {
1698 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1))) {
1699 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1700 pa_proplist_free(p
);
1706 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1707 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1708 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1709 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1710 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1711 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1712 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1713 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0);
1715 s
= playback_stream_new(c
, sink
, &ss
, &map
, &maxlength
, &tlength
, &prebuf
, &minreq
, volume_set
? &volume
: NULL
, muted
, syncid
, &missing
, flags
, p
, adjust_latency
);
1716 pa_proplist_free(p
);
1718 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1720 reply
= reply_new(tag
);
1721 pa_tagstruct_putu32(reply
, s
->index
);
1722 pa_assert(s
->sink_input
);
1723 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1724 pa_tagstruct_putu32(reply
, missing
);
1726 /* pa_log("initial request is %u", missing); */
1728 if (c
->version
>= 9) {
1729 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1731 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1732 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1733 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1734 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1737 if (c
->version
>= 12) {
1738 /* Since 0.9.8 we support sending the chosen sample
1739 * spec/channel map/device/suspend status back to the
1742 pa_tagstruct_put_sample_spec(reply
, &ss
);
1743 pa_tagstruct_put_channel_map(reply
, &map
);
1745 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1746 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1748 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1751 if (c
->version
>= 13)
1752 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
1754 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1757 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1758 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1761 pa_native_connection_assert_ref(c
);
1764 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1765 !pa_tagstruct_eof(t
)) {
1770 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1774 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1776 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1777 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1781 playback_stream_unlink(s
);
1785 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1787 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1788 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1792 record_stream_unlink(s
);
1796 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1799 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1800 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1804 upload_stream_unlink(s
);
1809 pa_assert_not_reached();
1812 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1815 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1816 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1818 uint32_t maxlength
, fragment_size
;
1819 uint32_t source_index
;
1820 const char *name
= NULL
, *source_name
;
1823 pa_tagstruct
*reply
;
1824 pa_source
*source
= NULL
;
1831 fix_channels
= FALSE
,
1833 variable_rate
= FALSE
,
1834 adjust_latency
= FALSE
,
1835 peak_detect
= FALSE
;
1836 pa_source_output_flags_t flags
= 0;
1838 uint32_t direct_on_input_idx
= PA_INVALID_INDEX
;
1839 pa_sink_input
*direct_on_input
= NULL
;
1841 pa_native_connection_assert_ref(c
);
1844 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1845 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1846 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1847 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1848 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1849 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1850 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1851 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1856 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1857 CHECK_VALIDITY(c
->pstream
, !source_name
|| pa_namereg_is_valid_name(source_name
), tag
, PA_ERR_INVALID
);
1858 CHECK_VALIDITY(c
->pstream
, source_index
== PA_INVALID_INDEX
|| !source_name
, tag
, PA_ERR_INVALID
);
1859 CHECK_VALIDITY(c
->pstream
, !source_name
|| source_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1860 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1861 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1862 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1864 p
= pa_proplist_new();
1867 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1869 if (c
->version
>= 12) {
1870 /* Since 0.9.8 the user can ask for a couple of additional flags */
1872 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1873 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1874 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1875 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1876 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1877 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1878 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1881 pa_proplist_free(p
);
1886 if (c
->version
>= 13) {
1888 if (pa_tagstruct_get_boolean(t
, &peak_detect
) < 0 ||
1889 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1890 pa_tagstruct_get_proplist(t
, p
) < 0 ||
1891 pa_tagstruct_getu32(t
, &direct_on_input_idx
) < 0) {
1893 pa_proplist_free(p
);
1898 if (!pa_tagstruct_eof(t
)) {
1900 pa_proplist_free(p
);
1904 if (source_index
!= PA_INVALID_INDEX
) {
1906 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
1907 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1908 pa_proplist_free(p
);
1912 } else if (source_name
) {
1914 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1))) {
1915 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1916 pa_proplist_free(p
);
1921 if (direct_on_input_idx
!= PA_INVALID_INDEX
) {
1923 if (!(direct_on_input
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, direct_on_input_idx
))) {
1924 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1925 pa_proplist_free(p
);
1931 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
1932 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
1933 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
1934 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
1935 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
1936 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
1937 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
1938 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0);
1940 s
= record_stream_new(c
, source
, &ss
, &map
, peak_detect
, &maxlength
, &fragment_size
, flags
, p
, adjust_latency
, direct_on_input
);
1941 pa_proplist_free(p
);
1943 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1945 reply
= reply_new(tag
);
1946 pa_tagstruct_putu32(reply
, s
->index
);
1947 pa_assert(s
->source_output
);
1948 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
1950 if (c
->version
>= 9) {
1951 /* Since 0.9 we support sending the buffer metrics back to the client */
1953 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1954 pa_tagstruct_putu32(reply
, (uint32_t) fragment_size
);
1957 if (c
->version
>= 12) {
1958 /* Since 0.9.8 we support sending the chosen sample
1959 * spec/channel map/device/suspend status back to the
1962 pa_tagstruct_put_sample_spec(reply
, &ss
);
1963 pa_tagstruct_put_channel_map(reply
, &map
);
1965 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
1966 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
1968 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
1971 if (c
->version
>= 13)
1972 pa_tagstruct_put_usec(reply
, s
->source_latency
);
1974 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1977 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1978 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1981 pa_native_connection_assert_ref(c
);
1984 if (!pa_tagstruct_eof(t
)) {
1989 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1990 ret
= pa_core_exit(c
->protocol
->core
, FALSE
, 0);
1991 CHECK_VALIDITY(c
->pstream
, ret
>= 0, tag
, PA_ERR_ACCESS
);
1993 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
1996 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1997 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1999 pa_tagstruct
*reply
;
2000 pa_bool_t shm_on_remote
, do_shm
;
2002 pa_native_connection_assert_ref(c
);
2005 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
2006 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
2007 !pa_tagstruct_eof(t
)) {
2012 /* Minimum supported version */
2013 if (c
->version
< 8) {
2014 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
2018 /* Starting with protocol version 13 the MSB of the version tag
2019 reflects if shm is available for this pa_native_connection or
2021 if (c
->version
>= 13) {
2022 shm_on_remote
= !!(c
->version
& 0x80000000U
);
2023 c
->version
&= 0x7FFFFFFFU
;
2026 pa_log_debug("Protocol version: remote %u, local %u", c
->version
, PA_PROTOCOL_VERSION
);
2028 pa_proplist_setf(c
->client
->proplist
, "native-protocol.version", "%u", c
->version
);
2030 if (!c
->authorized
) {
2031 pa_bool_t success
= FALSE
;
2034 const pa_creds
*creds
;
2036 if ((creds
= pa_pdispatch_creds(pd
))) {
2037 if (creds
->uid
== getuid())
2039 else if (c
->options
->auth_group
) {
2043 if ((gid
= pa_get_gid_of_group(c
->options
->auth_group
)) == (gid_t
) -1)
2044 pa_log_warn("Failed to get GID of group '%s'", c
->options
->auth_group
);
2045 else if (gid
== creds
->gid
)
2049 if ((r
= pa_uid_in_group(creds
->uid
, c
->options
->auth_group
)) < 0)
2050 pa_log_warn("Failed to check group membership.");
2056 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
2057 (unsigned long) creds
->uid
,
2058 (unsigned long) creds
->gid
,
2063 if (!success
&& c
->options
->auth_cookie
) {
2066 if ((ac
= pa_auth_cookie_read(c
->options
->auth_cookie
, PA_NATIVE_COOKIE_LENGTH
)))
2067 if (memcmp(ac
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
2072 pa_log_warn("Denied access to client with invalid authorization data.");
2073 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2077 c
->authorized
= TRUE
;
2078 if (c
->auth_timeout_event
) {
2079 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
2080 c
->auth_timeout_event
= NULL
;
2084 /* Enable shared memory support if possible */
2086 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
2089 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm
));
2092 if (c
->version
< 10 || (c
->version
>= 13 && !shm_on_remote
))
2096 /* Only enable SHM if both sides are owned by the same
2097 * user. This is a security measure because otherwise data
2098 * private to the user might leak. */
2100 const pa_creds
*creds
;
2101 if (!(creds
= pa_pdispatch_creds(pd
)) || getuid() != creds
->uid
)
2105 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm
));
2106 pa_pstream_enable_shm(c
->pstream
, do_shm
);
2108 reply
= reply_new(tag
);
2109 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
| (do_shm
? 0x80000000 : 0));
2113 /* SHM support is only enabled after both sides made sure they are the same user. */
2117 ucred
.uid
= getuid();
2118 ucred
.gid
= getgid();
2120 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
2123 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2127 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2128 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2129 const char *name
= NULL
;
2131 pa_tagstruct
*reply
;
2133 pa_native_connection_assert_ref(c
);
2136 p
= pa_proplist_new();
2138 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
2139 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2140 !pa_tagstruct_eof(t
)) {
2143 pa_proplist_free(p
);
2148 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
2149 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2150 pa_proplist_free(p
);
2154 pa_proplist_update(c
->client
->proplist
, PA_UPDATE_REPLACE
, p
);
2155 pa_proplist_free(p
);
2157 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2159 reply
= reply_new(tag
);
2161 if (c
->version
>= 13)
2162 pa_tagstruct_putu32(reply
, c
->client
->index
);
2164 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2167 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2168 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2170 uint32_t idx
= PA_IDXSET_INVALID
;
2172 pa_native_connection_assert_ref(c
);
2175 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2176 !pa_tagstruct_eof(t
)) {
2181 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2182 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2184 if (command
== PA_COMMAND_LOOKUP_SINK
) {
2186 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
2190 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
2191 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
2192 idx
= source
->index
;
2195 if (idx
== PA_IDXSET_INVALID
)
2196 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2198 pa_tagstruct
*reply
;
2199 reply
= reply_new(tag
);
2200 pa_tagstruct_putu32(reply
, idx
);
2201 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2205 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2206 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2210 pa_native_connection_assert_ref(c
);
2213 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2214 !pa_tagstruct_eof(t
)) {
2219 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2220 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2221 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2222 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2224 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
);
2227 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2228 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2229 pa_tagstruct
*reply
;
2230 const pa_mempool_stat
*stat
;
2232 pa_native_connection_assert_ref(c
);
2235 if (!pa_tagstruct_eof(t
)) {
2240 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2242 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
2244 reply
= reply_new(tag
);
2245 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
2246 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
2247 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
2248 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
2249 pa_tagstruct_putu32(reply
, (uint32_t) pa_scache_total_size(c
->protocol
->core
));
2250 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2253 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2254 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2255 pa_tagstruct
*reply
;
2257 struct timeval tv
, now
;
2261 pa_native_connection_assert_ref(c
);
2264 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2265 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2266 !pa_tagstruct_eof(t
)) {
2271 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2272 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2273 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2274 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2275 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
)
2277 reply
= reply_new(tag
);
2279 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
2280 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
2282 pa_tagstruct_put_usec(reply
, latency
);
2284 pa_tagstruct_put_usec(reply
, 0);
2285 pa_tagstruct_put_boolean(reply
, s
->sink_input
->thread_info
.playing_for
> 0);
2286 pa_tagstruct_put_timeval(reply
, &tv
);
2287 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2288 pa_tagstruct_puts64(reply
, s
->write_index
);
2289 pa_tagstruct_puts64(reply
, s
->read_index
);
2291 if (c
->version
>= 13) {
2292 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.underrun_for
);
2293 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.playing_for
);
2296 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2299 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2300 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2301 pa_tagstruct
*reply
;
2303 struct timeval tv
, now
;
2306 pa_native_connection_assert_ref(c
);
2309 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2310 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2311 !pa_tagstruct_eof(t
)) {
2316 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2317 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2318 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2320 reply
= reply_new(tag
);
2321 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
2322 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
2323 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_RUNNING
);
2324 pa_tagstruct_put_timeval(reply
, &tv
);
2325 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2326 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
2327 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
2328 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2331 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2332 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2335 const char *name
= NULL
;
2338 pa_tagstruct
*reply
;
2341 pa_native_connection_assert_ref(c
);
2344 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2345 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
2346 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
2347 pa_tagstruct_getu32(t
, &length
) < 0) {
2352 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2353 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
2354 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
2355 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
2356 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
2357 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
2359 p
= pa_proplist_new();
2361 if (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) {
2363 pa_proplist_free(p
);
2367 if (c
->version
< 13)
2368 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2370 if (!(name
= pa_proplist_gets(p
, PA_PROP_EVENT_ID
)))
2371 name
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
);
2373 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2375 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
2376 pa_proplist_free(p
);
2378 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2380 reply
= reply_new(tag
);
2381 pa_tagstruct_putu32(reply
, s
->index
);
2382 pa_tagstruct_putu32(reply
, length
);
2383 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2386 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2387 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2392 pa_native_connection_assert_ref(c
);
2395 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
2396 !pa_tagstruct_eof(t
)) {
2401 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2403 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
2404 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2405 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2407 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
2408 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
2410 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2412 upload_stream_unlink(s
);
2415 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2416 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2417 uint32_t sink_index
;
2420 const char *name
, *sink_name
;
2423 pa_tagstruct
*reply
;
2425 pa_native_connection_assert_ref(c
);
2428 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2430 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
2431 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
2432 pa_tagstruct_getu32(t
, &volume
) < 0 ||
2433 pa_tagstruct_gets(t
, &name
) < 0) {
2438 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
2439 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
2440 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2441 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2443 if (sink_index
!= PA_INVALID_INDEX
)
2444 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
2446 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
2448 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2450 p
= pa_proplist_new();
2452 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2453 !pa_tagstruct_eof(t
)) {
2455 pa_proplist_free(p
);
2459 pa_proplist_update(p
, PA_UPDATE_MERGE
, c
->client
->proplist
);
2461 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2462 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2463 pa_proplist_free(p
);
2467 pa_proplist_free(p
);
2469 reply
= reply_new(tag
);
2471 if (c
->version
>= 13)
2472 pa_tagstruct_putu32(reply
, idx
);
2474 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2477 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2478 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2481 pa_native_connection_assert_ref(c
);
2484 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2485 !pa_tagstruct_eof(t
)) {
2490 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2491 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2493 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2494 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2498 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2501 static void fixup_sample_spec(pa_native_connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2504 pa_assert(original
);
2508 if (c
->version
< 12) {
2509 /* Before protocol version 12 we didn't support S32 samples,
2510 * so we need to lie about this to the client */
2512 if (fixed
->format
== PA_SAMPLE_S32LE
)
2513 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2514 if (fixed
->format
== PA_SAMPLE_S32BE
)
2515 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2519 static void sink_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2520 pa_sample_spec fixed_ss
;
2523 pa_sink_assert_ref(sink
);
2525 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2529 PA_TAG_U32
, sink
->index
,
2530 PA_TAG_STRING
, sink
->name
,
2531 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2532 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2533 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2534 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2535 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, FALSE
),
2536 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
, FALSE
),
2537 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2538 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2539 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2540 PA_TAG_STRING
, sink
->driver
,
2541 PA_TAG_U32
, sink
->flags
,
2544 if (c
->version
>= 13) {
2545 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2546 pa_tagstruct_put_usec(t
, pa_sink_get_requested_latency(sink
));
2550 static void source_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2551 pa_sample_spec fixed_ss
;
2554 pa_source_assert_ref(source
);
2556 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2560 PA_TAG_U32
, source
->index
,
2561 PA_TAG_STRING
, source
->name
,
2562 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2563 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2564 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2565 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2566 PA_TAG_CVOLUME
, pa_source_get_volume(source
, FALSE
),
2567 PA_TAG_BOOLEAN
, pa_source_get_mute(source
, FALSE
),
2568 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2569 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2570 PA_TAG_USEC
, pa_source_get_latency(source
),
2571 PA_TAG_STRING
, source
->driver
,
2572 PA_TAG_U32
, source
->flags
,
2575 if (c
->version
>= 13) {
2576 pa_tagstruct_put_proplist(t
, source
->proplist
);
2577 pa_tagstruct_put_usec(t
, pa_source_get_requested_latency(source
));
2582 static void client_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2586 pa_tagstruct_putu32(t
, client
->index
);
2587 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2588 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2589 pa_tagstruct_puts(t
, client
->driver
);
2591 if (c
->version
>= 13)
2592 pa_tagstruct_put_proplist(t
, client
->proplist
);
2596 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
2600 pa_tagstruct_putu32(t
, module
->index
);
2601 pa_tagstruct_puts(t
, module
->name
);
2602 pa_tagstruct_puts(t
, module
->argument
);
2603 pa_tagstruct_putu32(t
, (uint32_t) module
->n_used
);
2604 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
2607 static void sink_input_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2608 pa_sample_spec fixed_ss
;
2609 pa_usec_t sink_latency
;
2612 pa_sink_input_assert_ref(s
);
2614 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2616 pa_tagstruct_putu32(t
, s
->index
);
2617 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2618 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2619 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2620 pa_tagstruct_putu32(t
, s
->sink
->index
);
2621 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2622 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2623 pa_tagstruct_put_cvolume(t
, &s
->volume
);
2624 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
, &sink_latency
));
2625 pa_tagstruct_put_usec(t
, sink_latency
);
2626 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2627 pa_tagstruct_puts(t
, s
->driver
);
2628 if (c
->version
>= 11)
2629 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2630 if (c
->version
>= 13)
2631 pa_tagstruct_put_proplist(t
, s
->proplist
);
2634 static void source_output_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2635 pa_sample_spec fixed_ss
;
2636 pa_usec_t source_latency
;
2639 pa_source_output_assert_ref(s
);
2641 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2643 pa_tagstruct_putu32(t
, s
->index
);
2644 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2645 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2646 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2647 pa_tagstruct_putu32(t
, s
->source
->index
);
2648 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2649 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2650 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
, &source_latency
));
2651 pa_tagstruct_put_usec(t
, source_latency
);
2652 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2653 pa_tagstruct_puts(t
, s
->driver
);
2655 if (c
->version
>= 13)
2656 pa_tagstruct_put_proplist(t
, s
->proplist
);
2659 static void scache_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2660 pa_sample_spec fixed_ss
;
2665 if (e
->memchunk
.memblock
)
2666 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2668 memset(&fixed_ss
, 0, sizeof(fixed_ss
));
2670 pa_tagstruct_putu32(t
, e
->index
);
2671 pa_tagstruct_puts(t
, e
->name
);
2672 pa_tagstruct_put_cvolume(t
, &e
->volume
);
2673 pa_tagstruct_put_usec(t
, e
->memchunk
.memblock
? pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
) : 0);
2674 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2675 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2676 pa_tagstruct_putu32(t
, (uint32_t) e
->memchunk
.length
);
2677 pa_tagstruct_put_boolean(t
, e
->lazy
);
2678 pa_tagstruct_puts(t
, e
->filename
);
2680 if (c
->version
>= 13)
2681 pa_tagstruct_put_proplist(t
, e
->proplist
);
2684 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2685 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2687 pa_sink
*sink
= NULL
;
2688 pa_source
*source
= NULL
;
2689 pa_client
*client
= NULL
;
2690 pa_module
*module
= NULL
;
2691 pa_sink_input
*si
= NULL
;
2692 pa_source_output
*so
= NULL
;
2693 pa_scache_entry
*sce
= NULL
;
2695 pa_tagstruct
*reply
;
2697 pa_native_connection_assert_ref(c
);
2700 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2701 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2702 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2703 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2704 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2705 pa_tagstruct_gets(t
, &name
) < 0) ||
2706 !pa_tagstruct_eof(t
)) {
2711 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2712 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2713 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2714 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2715 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2717 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2718 if (idx
!= PA_INVALID_INDEX
)
2719 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2721 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2722 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2723 if (idx
!= PA_INVALID_INDEX
)
2724 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2726 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2727 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2728 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2729 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2730 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2731 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2732 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2733 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2734 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2736 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2737 if (idx
!= PA_INVALID_INDEX
)
2738 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2740 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
2743 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
2744 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2748 reply
= reply_new(tag
);
2750 sink_fill_tagstruct(c
, reply
, sink
);
2752 source_fill_tagstruct(c
, reply
, source
);
2754 client_fill_tagstruct(c
, reply
, client
);
2756 module_fill_tagstruct(reply
, module
);
2758 sink_input_fill_tagstruct(c
, reply
, si
);
2760 source_output_fill_tagstruct(c
, reply
, so
);
2762 scache_fill_tagstruct(c
, reply
, sce
);
2763 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2766 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2767 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2771 pa_tagstruct
*reply
;
2773 pa_native_connection_assert_ref(c
);
2776 if (!pa_tagstruct_eof(t
)) {
2781 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2783 reply
= reply_new(tag
);
2785 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2786 i
= c
->protocol
->core
->sinks
;
2787 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2788 i
= c
->protocol
->core
->sources
;
2789 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2790 i
= c
->protocol
->core
->clients
;
2791 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2792 i
= c
->protocol
->core
->modules
;
2793 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2794 i
= c
->protocol
->core
->sink_inputs
;
2795 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2796 i
= c
->protocol
->core
->source_outputs
;
2798 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2799 i
= c
->protocol
->core
->scache
;
2803 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2804 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2805 sink_fill_tagstruct(c
, reply
, p
);
2806 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2807 source_fill_tagstruct(c
, reply
, p
);
2808 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2809 client_fill_tagstruct(c
, reply
, p
);
2810 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2811 module_fill_tagstruct(reply
, p
);
2812 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2813 sink_input_fill_tagstruct(c
, reply
, p
);
2814 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2815 source_output_fill_tagstruct(c
, reply
, p
);
2817 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2818 scache_fill_tagstruct(c
, reply
, p
);
2823 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2826 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2827 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2828 pa_tagstruct
*reply
;
2831 pa_sample_spec fixed_ss
;
2833 pa_native_connection_assert_ref(c
);
2836 if (!pa_tagstruct_eof(t
)) {
2841 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2843 reply
= reply_new(tag
);
2844 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
2845 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
2846 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
2847 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
2849 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
2850 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
2852 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
2853 pa_tagstruct_puts(reply
, n
);
2854 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
2855 pa_tagstruct_puts(reply
, n
);
2857 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
2859 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2862 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
2864 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2866 pa_native_connection_assert_ref(c
);
2868 t
= pa_tagstruct_new(NULL
, 0);
2869 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
2870 pa_tagstruct_putu32(t
, (uint32_t) -1);
2871 pa_tagstruct_putu32(t
, e
);
2872 pa_tagstruct_putu32(t
, idx
);
2873 pa_pstream_send_tagstruct(c
->pstream
, t
);
2876 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2877 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2878 pa_subscription_mask_t m
;
2880 pa_native_connection_assert_ref(c
);
2883 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
2884 !pa_tagstruct_eof(t
)) {
2889 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2890 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
2892 if (c
->subscription
)
2893 pa_subscription_free(c
->subscription
);
2896 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
2897 pa_assert(c
->subscription
);
2899 c
->subscription
= NULL
;
2901 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2904 static void command_set_volume(
2911 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2914 pa_sink
*sink
= NULL
;
2915 pa_source
*source
= NULL
;
2916 pa_sink_input
*si
= NULL
;
2917 const char *name
= NULL
;
2919 pa_native_connection_assert_ref(c
);
2922 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2923 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2924 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2925 pa_tagstruct_get_cvolume(t
, &volume
) ||
2926 !pa_tagstruct_eof(t
)) {
2931 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2932 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2933 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2934 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2935 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2936 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
2940 case PA_COMMAND_SET_SINK_VOLUME
:
2941 if (idx
!= PA_INVALID_INDEX
)
2942 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2944 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
2947 case PA_COMMAND_SET_SOURCE_VOLUME
:
2948 if (idx
!= PA_INVALID_INDEX
)
2949 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2951 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
2954 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
2955 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2959 pa_assert_not_reached();
2962 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
2965 pa_sink_set_volume(sink
, &volume
);
2967 pa_source_set_volume(source
, &volume
);
2969 pa_sink_input_set_volume(si
, &volume
);
2971 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2974 static void command_set_mute(
2981 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2984 pa_sink
*sink
= NULL
;
2985 pa_source
*source
= NULL
;
2986 pa_sink_input
*si
= NULL
;
2987 const char *name
= NULL
;
2989 pa_native_connection_assert_ref(c
);
2992 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2993 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2994 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
2995 pa_tagstruct_get_boolean(t
, &mute
) ||
2996 !pa_tagstruct_eof(t
)) {
3001 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3002 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3003 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3004 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3005 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3009 case PA_COMMAND_SET_SINK_MUTE
:
3011 if (idx
!= PA_INVALID_INDEX
)
3012 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3014 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3018 case PA_COMMAND_SET_SOURCE_MUTE
:
3019 if (idx
!= PA_INVALID_INDEX
)
3020 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3022 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3026 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
3027 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3031 pa_assert_not_reached();
3034 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3037 pa_sink_set_mute(sink
, mute
);
3039 pa_source_set_mute(source
, mute
);
3041 pa_sink_input_set_mute(si
, mute
);
3043 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3046 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3047 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3052 pa_native_connection_assert_ref(c
);
3055 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3056 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3057 !pa_tagstruct_eof(t
)) {
3062 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3063 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3064 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3065 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3066 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3068 pa_sink_input_cork(s
->sink_input
, b
);
3069 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3072 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3073 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3077 pa_native_connection_assert_ref(c
);
3080 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3081 !pa_tagstruct_eof(t
)) {
3086 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3087 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3088 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3089 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3090 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3093 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3094 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3097 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3098 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3101 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3102 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3106 pa_assert_not_reached();
3109 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3112 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3113 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3118 pa_native_connection_assert_ref(c
);
3121 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3122 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3123 !pa_tagstruct_eof(t
)) {
3128 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3129 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3130 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3132 pa_source_output_cork(s
->source_output
, b
);
3133 pa_memblockq_prebuf_force(s
->memblockq
);
3134 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3137 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3138 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3142 pa_native_connection_assert_ref(c
);
3145 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3146 !pa_tagstruct_eof(t
)) {
3151 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3152 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3153 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3155 pa_memblockq_flush_read(s
->memblockq
);
3156 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3159 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3160 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3162 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3163 pa_tagstruct
*reply
;
3165 pa_native_connection_assert_ref(c
);
3168 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3173 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3175 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3177 pa_bool_t adjust_latency
= FALSE
;
3179 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3180 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3181 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3183 if (pa_tagstruct_get(
3185 PA_TAG_U32
, &maxlength
,
3186 PA_TAG_U32
, &tlength
,
3187 PA_TAG_U32
, &prebuf
,
3188 PA_TAG_U32
, &minreq
,
3189 PA_TAG_INVALID
) < 0 ||
3190 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3191 !pa_tagstruct_eof(t
)) {
3196 fix_playback_buffer_attr_pre(s
, adjust_latency
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3197 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3198 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3199 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3200 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3201 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3203 reply
= reply_new(tag
);
3204 pa_tagstruct_putu32(reply
, maxlength
);
3205 pa_tagstruct_putu32(reply
, tlength
);
3206 pa_tagstruct_putu32(reply
, prebuf
);
3207 pa_tagstruct_putu32(reply
, minreq
);
3209 if (c
->version
>= 13)
3210 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3214 pa_bool_t adjust_latency
= FALSE
;
3215 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3217 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3218 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3220 if (pa_tagstruct_get(
3222 PA_TAG_U32
, &maxlength
,
3223 PA_TAG_U32
, &fragsize
,
3224 PA_TAG_INVALID
) < 0 ||
3225 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3226 !pa_tagstruct_eof(t
)) {
3231 fix_record_buffer_attr_pre(s
, adjust_latency
, &maxlength
, &fragsize
);
3232 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3233 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3235 reply
= reply_new(tag
);
3236 pa_tagstruct_putu32(reply
, maxlength
);
3237 pa_tagstruct_putu32(reply
, fragsize
);
3239 if (c
->version
>= 13)
3240 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3243 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3246 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3247 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3251 pa_native_connection_assert_ref(c
);
3254 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3255 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3256 !pa_tagstruct_eof(t
)) {
3261 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3262 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3264 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3267 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3268 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3269 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3271 pa_sink_input_set_rate(s
->sink_input
, rate
);
3275 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3277 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3278 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3280 pa_source_output_set_rate(s
->source_output
, rate
);
3283 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3286 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3287 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3292 pa_native_connection_assert_ref(c
);
3295 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3297 p
= pa_proplist_new();
3299 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3301 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3302 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3303 !pa_tagstruct_eof(t
)) {
3305 pa_proplist_free(p
);
3311 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3312 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3313 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3314 !pa_tagstruct_eof(t
)) {
3316 pa_proplist_free(p
);
3321 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3323 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3326 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3327 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3328 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3330 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
3331 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3333 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3336 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3337 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3339 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
3340 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3342 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3344 pa_proplist_update(c
->client
->proplist
, mode
, p
);
3345 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3348 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3351 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3352 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3354 unsigned changed
= 0;
3356 pa_strlist
*l
= NULL
;
3358 pa_native_connection_assert_ref(c
);
3361 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3363 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3365 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3371 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3374 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3375 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3376 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3378 p
= s
->sink_input
->proplist
;
3380 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3383 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3384 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3386 p
= s
->source_output
->proplist
;
3388 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3390 p
= c
->client
->proplist
;
3396 if (pa_tagstruct_gets(t
, &k
) < 0) {
3405 l
= pa_strlist_prepend(l
, k
);
3408 if (!pa_tagstruct_eof(t
)) {
3417 l
= pa_strlist_pop(l
, &z
);
3422 changed
+= (unsigned) (pa_proplist_unset(p
, z
) >= 0);
3426 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3429 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3432 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3433 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3435 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3438 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3439 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3442 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3443 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3448 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3449 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3452 pa_native_connection_assert_ref(c
);
3455 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3456 !pa_tagstruct_eof(t
)) {
3461 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3462 CHECK_VALIDITY(c
->pstream
, !s
|| pa_namereg_is_valid_name(s
), tag
, PA_ERR_INVALID
);
3464 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
3465 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3468 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3469 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3473 pa_native_connection_assert_ref(c
);
3476 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3477 pa_tagstruct_gets(t
, &name
) < 0 ||
3478 !pa_tagstruct_eof(t
)) {
3483 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3484 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3486 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3489 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3490 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3491 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3493 pa_sink_input_set_name(s
->sink_input
, name
);
3497 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3499 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3500 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3502 pa_source_output_set_name(s
->source_output
, name
);
3505 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3508 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3509 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3512 pa_native_connection_assert_ref(c
);
3515 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3516 !pa_tagstruct_eof(t
)) {
3521 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3523 if (command
== PA_COMMAND_KILL_CLIENT
) {
3526 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3527 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3529 pa_native_connection_ref(c
);
3530 pa_client_kill(client
);
3532 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3535 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3536 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3538 pa_native_connection_ref(c
);
3539 pa_sink_input_kill(s
);
3541 pa_source_output
*s
;
3543 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3545 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3546 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3548 pa_native_connection_ref(c
);
3549 pa_source_output_kill(s
);
3552 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3553 pa_native_connection_unref(c
);
3556 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3557 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3559 const char *name
, *argument
;
3560 pa_tagstruct
*reply
;
3562 pa_native_connection_assert_ref(c
);
3565 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3566 pa_tagstruct_gets(t
, &argument
) < 0 ||
3567 !pa_tagstruct_eof(t
)) {
3572 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3573 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3574 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3576 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3577 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3581 reply
= reply_new(tag
);
3582 pa_tagstruct_putu32(reply
, m
->index
);
3583 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3586 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3587 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3591 pa_native_connection_assert_ref(c
);
3594 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3595 !pa_tagstruct_eof(t
)) {
3600 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3601 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3602 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3604 pa_module_unload_request(m
, FALSE
);
3605 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3608 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3609 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3610 const char *name
, *module
, *argument
;
3613 pa_tagstruct
*reply
;
3615 pa_native_connection_assert_ref(c
);
3618 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3619 pa_tagstruct_getu32(t
, &type
) < 0 ||
3620 pa_tagstruct_gets(t
, &module
) < 0 ||
3621 pa_tagstruct_gets(t
, &argument
) < 0 ||
3622 !pa_tagstruct_eof(t
)) {
3627 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3628 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3629 CHECK_VALIDITY(c
->pstream
, type
== 0 || type
== 1, tag
, PA_ERR_INVALID
);
3630 CHECK_VALIDITY(c
->pstream
, module
&& *module
&& pa_utf8_valid(module
), tag
, PA_ERR_INVALID
);
3631 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3633 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
3634 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
3638 reply
= reply_new(tag
);
3639 pa_tagstruct_putu32(reply
, idx
);
3640 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3643 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3644 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3645 const char *name
= NULL
;
3646 uint32_t type
, idx
= PA_IDXSET_INVALID
;
3649 pa_native_connection_assert_ref(c
);
3652 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3653 (pa_tagstruct_gets(t
, &name
) < 0 ||
3654 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3655 !pa_tagstruct_eof(t
)) {
3660 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3661 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3662 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& pa_utf8_valid(name
) && (type
== 0 || type
== 1)), tag
, PA_ERR_INVALID
);
3665 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3667 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
3669 CHECK_VALIDITY(c
->pstream
, r
>= 0, tag
, PA_ERR_NOENTITY
);
3671 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3674 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
3677 pa_tagstruct_putu32(t
, e
->index
);
3678 pa_tagstruct_puts(t
, e
->name
);
3679 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0U : 1U);
3680 pa_tagstruct_puts(t
, e
->module
);
3681 pa_tagstruct_puts(t
, e
->argument
);
3684 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3685 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3686 const pa_autoload_entry
*a
= NULL
;
3689 pa_tagstruct
*reply
;
3691 pa_native_connection_assert_ref(c
);
3694 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
3695 (pa_tagstruct_gets(t
, &name
) < 0 ||
3696 pa_tagstruct_getu32(t
, &type
) < 0)) ||
3697 !pa_tagstruct_eof(t
)) {
3702 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3703 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
3704 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& (type
== 0 || type
== 1) && pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
3707 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
3709 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
3711 CHECK_VALIDITY(c
->pstream
, a
, tag
, PA_ERR_NOENTITY
);
3713 reply
= reply_new(tag
);
3714 autoload_fill_tagstruct(reply
, a
);
3715 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3718 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3719 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3720 pa_tagstruct
*reply
;
3722 pa_native_connection_assert_ref(c
);
3725 if (!pa_tagstruct_eof(t
)) {
3730 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3732 reply
= reply_new(tag
);
3734 if (c
->protocol
->core
->autoload_hashmap
) {
3735 pa_autoload_entry
*a
;
3738 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
3739 autoload_fill_tagstruct(reply
, a
);
3742 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3745 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3746 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3747 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3748 const char *name_device
= NULL
;
3750 pa_native_connection_assert_ref(c
);
3753 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3754 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3755 pa_tagstruct_gets(t
, &name_device
) < 0 ||
3756 !pa_tagstruct_eof(t
)) {
3761 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3762 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3764 CHECK_VALIDITY(c
->pstream
, !name_device
|| pa_namereg_is_valid_name(name_device
), tag
, PA_ERR_INVALID
);
3765 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| name_device
, tag
, PA_ERR_INVALID
);
3766 CHECK_VALIDITY(c
->pstream
, idx_device
== PA_INVALID_INDEX
|| !name_device
, tag
, PA_ERR_INVALID
);
3767 CHECK_VALIDITY(c
->pstream
, !name_device
|| idx_device
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3769 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3770 pa_sink_input
*si
= NULL
;
3771 pa_sink
*sink
= NULL
;
3773 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3775 if (idx_device
!= PA_INVALID_INDEX
)
3776 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3778 sink
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SINK
, 1);
3780 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3782 if (pa_sink_input_move_to(si
, sink
) < 0) {
3783 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3787 pa_source_output
*so
= NULL
;
3790 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3792 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3794 if (idx_device
!= PA_INVALID_INDEX
)
3795 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3797 source
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SOURCE
, 1);
3799 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3801 if (pa_source_output_move_to(so
, source
) < 0) {
3802 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3807 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3810 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3811 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3812 uint32_t idx
= PA_INVALID_INDEX
;
3813 const char *name
= NULL
;
3816 pa_native_connection_assert_ref(c
);
3819 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3820 pa_tagstruct_gets(t
, &name
) < 0 ||
3821 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3822 !pa_tagstruct_eof(t
)) {
3827 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3828 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3829 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3830 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3831 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3833 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3835 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3837 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3838 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3842 pa_sink
*sink
= NULL
;
3844 if (idx
!= PA_INVALID_INDEX
)
3845 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3847 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
3849 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3851 if (pa_sink_suspend(sink
, b
) < 0) {
3852 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3858 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3860 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3862 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3863 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3870 if (idx
!= PA_INVALID_INDEX
)
3871 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3873 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
3875 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3877 if (pa_source_suspend(source
, b
) < 0) {
3878 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3884 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3887 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3888 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3889 uint32_t idx
= PA_INVALID_INDEX
;
3890 const char *name
= NULL
;
3892 pa_native_protocol_ext_cb_t cb
;
3894 pa_native_connection_assert_ref(c
);
3897 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3898 pa_tagstruct_gets(t
, &name
) < 0) {
3903 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3904 CHECK_VALIDITY(c
->pstream
, !name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3905 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3906 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3907 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3909 if (idx
!= PA_INVALID_INDEX
)
3910 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3912 for (m
= pa_idxset_first(c
->protocol
->core
->modules
, &idx
); m
; m
= pa_idxset_next(c
->protocol
->core
->modules
, &idx
))
3913 if (strcmp(name
, m
->name
) == 0)
3917 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3918 CHECK_VALIDITY(c
->pstream
, m
->load_once
|| idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3920 cb
= (pa_native_protocol_ext_cb_t
) pa_hashmap_get(c
->protocol
->extensions
, m
);
3921 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3923 if (cb(c
->protocol
, m
, c
, tag
, t
) < 0)
3928 /*** pstream callbacks ***/
3930 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
3931 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3935 pa_native_connection_assert_ref(c
);
3937 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
3938 pa_log("invalid packet.");
3939 native_connection_unlink(c
);
3943 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
) {
3944 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3945 output_stream
*stream
;
3949 pa_native_connection_assert_ref(c
);
3951 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
3952 pa_log("client sent block for invalid stream.");
3957 if (playback_stream_isinstance(stream
)) {
3958 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
3960 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
3961 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
);
3963 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
3966 upload_stream
*u
= UPLOAD_STREAM(stream
);
3969 if (!u
->memchunk
.memblock
) {
3970 if (u
->length
== chunk
->length
) {
3971 u
->memchunk
= *chunk
;
3972 pa_memblock_ref(u
->memchunk
.memblock
);
3975 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
3976 u
->memchunk
.index
= u
->memchunk
.length
= 0;
3980 pa_assert(u
->memchunk
.memblock
);
3983 if (l
> chunk
->length
)
3989 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
3990 src
= pa_memblock_acquire(chunk
->memblock
);
3992 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
3993 (uint8_t*) src
+chunk
->index
, l
);
3995 pa_memblock_release(u
->memchunk
.memblock
);
3996 pa_memblock_release(chunk
->memblock
);
3998 u
->memchunk
.length
+= l
;
4004 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
4005 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4008 pa_native_connection_assert_ref(c
);
4010 native_connection_unlink(c
);
4011 pa_log_info("Connection died.");
4014 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
4015 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4018 pa_native_connection_assert_ref(c
);
4020 native_connection_send_memblock(c
);
4023 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4026 if (!(q
= pa_thread_mq_get()))
4027 pa_pstream_send_revoke(p
, block_id
);
4029 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4032 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4035 if (!(q
= pa_thread_mq_get()))
4036 pa_pstream_send_release(p
, block_id
);
4038 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4041 /*** client callbacks ***/
4043 static void client_kill_cb(pa_client
*c
) {
4046 native_connection_unlink(PA_NATIVE_CONNECTION(c
->userdata
));
4047 pa_log_info("Connection killed.");
4050 /*** module entry points ***/
4052 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
4053 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4057 pa_native_connection_assert_ref(c
);
4058 pa_assert(c
->auth_timeout_event
== e
);
4060 if (!c
->authorized
) {
4061 native_connection_unlink(c
);
4062 pa_log_info("Connection terminated due to authentication timeout.");
4066 void pa_native_protocol_connect(pa_native_protocol
*p
, pa_iochannel
*io
, pa_native_options
*o
) {
4067 pa_native_connection
*c
;
4068 char cname
[256], pname
[128];
4074 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
4075 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
4076 pa_iochannel_free(io
);
4080 c
= pa_msgobject_new(pa_native_connection
);
4081 c
->parent
.parent
.free
= native_connection_free
;
4082 c
->parent
.process_msg
= native_connection_process_msg
;
4084 c
->options
= pa_native_options_ref(o
);
4085 c
->authorized
= FALSE
;
4087 if (o
->auth_anonymous
) {
4088 pa_log_info("Client authenticated anonymously.");
4089 c
->authorized
= TRUE
;
4092 if (!c
->authorized
&&
4094 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
4096 pa_log_info("Client authenticated by IP ACL.");
4097 c
->authorized
= TRUE
;
4100 if (!c
->authorized
) {
4102 pa_gettimeofday(&tv
);
4103 tv
.tv_sec
+= AUTH_TIMEOUT
;
4104 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
4106 c
->auth_timeout_event
= NULL
;
4108 c
->is_local
= pa_iochannel_socket_is_local(io
);
4111 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
4112 pa_snprintf(cname
, sizeof(cname
), "Native client (%s)", pname
);
4113 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
4114 pa_proplist_sets(c
->client
->proplist
, "native-protocol.peer", pname
);
4115 c
->client
->kill
= client_kill_cb
;
4116 c
->client
->userdata
= c
;
4117 c
->client
->module
= o
->module
;
4119 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
4120 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
4121 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
4122 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
4123 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
4124 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
4125 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
4127 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
4129 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
4130 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
4132 c
->rrobin_index
= PA_IDXSET_INVALID
;
4133 c
->subscription
= NULL
;
4135 pa_idxset_put(p
->connections
, c
, NULL
);
4138 if (pa_iochannel_creds_supported(io
))
4139 pa_iochannel_creds_enable(io
);
4142 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_CONNECTION_PUT
], c
);
4145 void pa_native_protocol_disconnect(pa_native_protocol
*p
, pa_module
*m
) {
4146 pa_native_connection
*c
;
4152 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
4153 if (c
->options
->module
== m
)
4154 native_connection_unlink(c
);
4157 static pa_native_protocol
* native_protocol_new(pa_core
*c
) {
4158 pa_native_protocol
*p
;
4163 p
= pa_xnew(pa_native_protocol
, 1);
4166 p
->connections
= pa_idxset_new(NULL
, NULL
);
4170 p
->extensions
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
4172 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4173 pa_hook_init(&p
->hooks
[h
], p
);
4175 pa_assert_se(pa_shared_set(c
, "native-protocol", p
) >= 0);
4180 pa_native_protocol
* pa_native_protocol_get(pa_core
*c
) {
4181 pa_native_protocol
*p
;
4183 if ((p
= pa_shared_get(c
, "native-protocol")))
4184 return pa_native_protocol_ref(p
);
4186 return native_protocol_new(c
);
4189 pa_native_protocol
* pa_native_protocol_ref(pa_native_protocol
*p
) {
4191 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4198 void pa_native_protocol_unref(pa_native_protocol
*p
) {
4199 pa_native_connection
*c
;
4203 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4205 if (PA_REFCNT_DEC(p
) > 0)
4208 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4209 native_connection_unlink(c
);
4211 pa_idxset_free(p
->connections
, NULL
, NULL
);
4213 pa_strlist_free(p
->servers
);
4215 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4216 pa_hook_done(&p
->hooks
[h
]);
4218 pa_hashmap_free(p
->extensions
, NULL
, NULL
);
4220 pa_assert_se(pa_shared_remove(p
->core
, "native-protocol") >= 0);
4225 void pa_native_protocol_add_server_string(pa_native_protocol
*p
, const char *name
) {
4227 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4230 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
4232 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4235 void pa_native_protocol_remove_server_string(pa_native_protocol
*p
, const char *name
) {
4237 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4240 p
->servers
= pa_strlist_remove(p
->servers
, name
);
4242 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4245 pa_hook
*pa_native_protocol_hooks(pa_native_protocol
*p
) {
4247 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4252 pa_strlist
*pa_native_protocol_servers(pa_native_protocol
*p
) {
4254 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4259 int pa_native_protocol_install_ext(pa_native_protocol
*p
, pa_module
*m
, pa_native_protocol_ext_cb_t cb
) {
4261 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4264 pa_assert(!pa_hashmap_get(p
->extensions
, m
));
4266 pa_assert_se(pa_hashmap_put(p
->extensions
, m
, (void*) cb
) == 0);
4270 void pa_native_protocol_remove_ext(pa_native_protocol
*p
, pa_module
*m
) {
4272 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4275 pa_assert_se(pa_hashmap_remove(p
->extensions
, m
));
4278 pa_native_options
* pa_native_options_new(void) {
4279 pa_native_options
*o
;
4281 o
= pa_xnew0(pa_native_options
, 1);
4287 pa_native_options
* pa_native_options_ref(pa_native_options
*o
) {
4289 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4296 void pa_native_options_unref(pa_native_options
*o
) {
4298 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4300 if (PA_REFCNT_DEC(o
) > 0)
4303 pa_xfree(o
->auth_group
);
4306 pa_ip_acl_free(o
->auth_ip_acl
);
4309 pa_auth_cookie_unref(o
->auth_cookie
);
4314 int pa_native_options_parse(pa_native_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
4319 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4322 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
4323 pa_log("auth-anonymous= expects a boolean argument.");
4328 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &enabled
) < 0) {
4329 pa_log("auth-group-enabled= expects a boolean argument.");
4333 pa_xfree(o
->auth_group
);
4334 o
->auth_group
= enabled
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4338 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4341 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4344 if (!(ipa
= pa_ip_acl_new(acl
))) {
4345 pa_log("Failed to parse IP ACL '%s'", acl
);
4350 pa_ip_acl_free(o
->auth_ip_acl
);
4352 o
->auth_ip_acl
= ipa
;
4356 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
4357 pa_log("auth-cookie-enabled= expects a boolean argument.");
4362 pa_auth_cookie_unref(o
->auth_cookie
);
4367 /* The new name for this is 'auth-cookie', for compat reasons
4368 * we check the old name too */
4369 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
4370 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
4371 cn
= PA_NATIVE_COOKIE_FILE
;
4373 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, PA_NATIVE_COOKIE_LENGTH
)))
4377 o
->auth_cookie
= NULL
;
4382 pa_pstream
* pa_native_connection_get_pstream(pa_native_connection
*c
) {
4383 pa_native_connection_assert_ref(c
);