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/strlist.h>
53 #include <pulsecore/shared.h>
54 #include <pulsecore/sample-util.h>
55 #include <pulsecore/llist.h>
56 #include <pulsecore/creds.h>
57 #include <pulsecore/core-util.h>
58 #include <pulsecore/ipacl.h>
59 #include <pulsecore/thread-mq.h>
61 #include "protocol-native.h"
63 /* Kick a client if it doesn't authenticate within this time */
64 #define AUTH_TIMEOUT 60
66 /* Don't accept more connection than this */
67 #define MAX_CONNECTIONS 64
69 #define MAX_MEMBLOCKQ_LENGTH (4*1024*1024) /* 4MB */
70 #define DEFAULT_TLENGTH_MSEC 2000 /* 2s */
71 #define DEFAULT_PROCESS_MSEC 20 /* 20ms */
72 #define DEFAULT_FRAGSIZE_MSEC DEFAULT_TLENGTH_MSEC
74 struct pa_native_protocol
;
76 typedef struct record_stream
{
79 pa_native_connection
*connection
;
82 pa_source_output
*source_output
;
83 pa_memblockq
*memblockq
;
85 pa_usec_t source_latency
;
88 PA_DECLARE_CLASS(record_stream
);
89 #define RECORD_STREAM(o) (record_stream_cast(o))
90 static PA_DEFINE_CHECK_TYPE(record_stream
, pa_msgobject
);
92 typedef struct output_stream
{
96 PA_DECLARE_CLASS(output_stream
);
97 #define OUTPUT_STREAM(o) (output_stream_cast(o))
98 static PA_DEFINE_CHECK_TYPE(output_stream
, pa_msgobject
);
100 typedef struct playback_stream
{
101 output_stream parent
;
103 pa_native_connection
*connection
;
106 pa_sink_input
*sink_input
;
107 pa_memblockq
*memblockq
;
108 pa_bool_t is_underrun
:1;
109 pa_bool_t drain_request
:1;
115 pa_usec_t sink_latency
;
117 /* Only updated after SINK_INPUT_MESSAGE_UPDATE_LATENCY */
118 int64_t read_index
, write_index
;
119 size_t render_memblockq_length
;
122 PA_DECLARE_CLASS(playback_stream
);
123 #define PLAYBACK_STREAM(o) (playback_stream_cast(o))
124 static PA_DEFINE_CHECK_TYPE(playback_stream
, output_stream
);
126 typedef struct upload_stream
{
127 output_stream parent
;
129 pa_native_connection
*connection
;
132 pa_memchunk memchunk
;
135 pa_sample_spec sample_spec
;
136 pa_channel_map channel_map
;
137 pa_proplist
*proplist
;
140 PA_DECLARE_CLASS(upload_stream
);
141 #define UPLOAD_STREAM(o) (upload_stream_cast(o))
142 static PA_DEFINE_CHECK_TYPE(upload_stream
, output_stream
);
144 struct pa_native_connection
{
146 pa_native_protocol
*protocol
;
147 pa_native_options
*options
;
148 pa_bool_t authorized
:1;
149 pa_bool_t is_local
:1;
153 pa_pdispatch
*pdispatch
;
154 pa_idxset
*record_streams
, *output_streams
;
155 uint32_t rrobin_index
;
156 pa_subscription
*subscription
;
157 pa_time_event
*auth_timeout_event
;
160 PA_DECLARE_CLASS(pa_native_connection
);
161 #define PA_NATIVE_CONNECTION(o) (pa_native_connection_cast(o))
162 static PA_DEFINE_CHECK_TYPE(pa_native_connection
, pa_msgobject
);
164 struct pa_native_protocol
{
168 pa_idxset
*connections
;
171 pa_hook hooks
[PA_NATIVE_HOOK_MAX
];
173 pa_hashmap
*extensions
;
177 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
178 SINK_INPUT_MESSAGE_DRAIN
, /* disabled prebuf, get playback started. */
179 SINK_INPUT_MESSAGE_FLUSH
,
180 SINK_INPUT_MESSAGE_TRIGGER
,
181 SINK_INPUT_MESSAGE_SEEK
,
182 SINK_INPUT_MESSAGE_PREBUF_FORCE
,
183 SINK_INPUT_MESSAGE_UPDATE_LATENCY
187 PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, /* data requested from sink input from the main loop */
188 PLAYBACK_STREAM_MESSAGE_UNDERFLOW
,
189 PLAYBACK_STREAM_MESSAGE_OVERFLOW
,
190 PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
,
191 PLAYBACK_STREAM_MESSAGE_STARTED
195 RECORD_STREAM_MESSAGE_POST_DATA
/* data from source output to main loop */
199 CONNECTION_MESSAGE_RELEASE
,
200 CONNECTION_MESSAGE_REVOKE
203 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
);
204 static void sink_input_kill_cb(pa_sink_input
*i
);
205 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
);
206 static void sink_input_moved_cb(pa_sink_input
*i
);
207 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
208 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
209 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
);
211 static void native_connection_send_memblock(pa_native_connection
*c
);
212 static void playback_stream_request_bytes(struct playback_stream
*s
);
214 static void source_output_kill_cb(pa_source_output
*o
);
215 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
216 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
);
217 static void source_output_moved_cb(pa_source_output
*o
);
218 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
220 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
);
222 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
223 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
224 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
225 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
226 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
227 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
228 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
229 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
230 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
231 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
232 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
233 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
234 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
235 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
236 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
237 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
238 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
239 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
240 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
241 static void command_set_volume(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
242 static void command_set_mute(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
243 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
244 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
245 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
246 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
247 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
248 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
249 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
250 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
251 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
252 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
253 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
254 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
255 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
256 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
257 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
258 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
259 static void command_set_card_profile(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
261 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
262 [PA_COMMAND_ERROR
] = NULL
,
263 [PA_COMMAND_TIMEOUT
] = NULL
,
264 [PA_COMMAND_REPLY
] = NULL
,
265 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
266 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
267 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
268 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
269 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
270 [PA_COMMAND_AUTH
] = command_auth
,
271 [PA_COMMAND_REQUEST
] = NULL
,
272 [PA_COMMAND_EXIT
] = command_exit
,
273 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
274 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
275 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
276 [PA_COMMAND_STAT
] = command_stat
,
277 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
278 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
279 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
280 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
281 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
282 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
283 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
284 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
285 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
286 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
287 [PA_COMMAND_GET_CARD_INFO
] = command_get_info
,
288 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
289 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
290 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
291 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
292 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
293 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
294 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
295 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
296 [PA_COMMAND_GET_CARD_INFO_LIST
] = command_get_info_list
,
297 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
298 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
299 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
300 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
301 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
303 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
304 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
305 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
307 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
308 [PA_COMMAND_SET_SINK_INPUT_MUTE
] = command_set_mute
,
309 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
311 [PA_COMMAND_SUSPEND_SINK
] = command_suspend
,
312 [PA_COMMAND_SUSPEND_SOURCE
] = command_suspend
,
314 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
315 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
316 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
317 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
319 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
320 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
322 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
323 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
324 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
325 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
326 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
327 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
328 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
329 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
330 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
332 [PA_COMMAND_GET_AUTOLOAD_INFO___OBSOLETE
] = NULL
,
333 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST___OBSOLETE
] = NULL
,
334 [PA_COMMAND_ADD_AUTOLOAD___OBSOLETE
] = NULL
,
335 [PA_COMMAND_REMOVE_AUTOLOAD___OBSOLETE
] = NULL
,
337 [PA_COMMAND_MOVE_SINK_INPUT
] = command_move_stream
,
338 [PA_COMMAND_MOVE_SOURCE_OUTPUT
] = command_move_stream
,
340 [PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
341 [PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
343 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
344 [PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
346 [PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
] = command_update_proplist
,
347 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
] = command_update_proplist
,
348 [PA_COMMAND_UPDATE_CLIENT_PROPLIST
] = command_update_proplist
,
350 [PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
] = command_remove_proplist
,
351 [PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
] = command_remove_proplist
,
352 [PA_COMMAND_REMOVE_CLIENT_PROPLIST
] = command_remove_proplist
,
354 [PA_COMMAND_SET_CARD_PROFILE
] = command_set_card_profile
,
356 [PA_COMMAND_EXTENSION
] = command_extension
359 /* structure management */
361 static void upload_stream_unlink(upload_stream
*s
) {
367 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
368 s
->connection
= NULL
;
369 upload_stream_unref(s
);
372 static void upload_stream_free(pa_object
*o
) {
373 upload_stream
*s
= UPLOAD_STREAM(o
);
376 upload_stream_unlink(s
);
381 pa_proplist_free(s
->proplist
);
383 if (s
->memchunk
.memblock
)
384 pa_memblock_unref(s
->memchunk
.memblock
);
389 static upload_stream
* upload_stream_new(
390 pa_native_connection
*c
,
391 const pa_sample_spec
*ss
,
392 const pa_channel_map
*map
,
402 pa_assert(length
> 0);
405 s
= pa_msgobject_new(upload_stream
);
406 s
->parent
.parent
.parent
.free
= upload_stream_free
;
408 s
->sample_spec
= *ss
;
409 s
->channel_map
= *map
;
410 s
->name
= pa_xstrdup(name
);
411 pa_memchunk_reset(&s
->memchunk
);
413 s
->proplist
= pa_proplist_copy(p
);
414 pa_proplist_update(s
->proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
416 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
421 static void record_stream_unlink(record_stream
*s
) {
427 if (s
->source_output
) {
428 pa_source_output_unlink(s
->source_output
);
429 pa_source_output_unref(s
->source_output
);
430 s
->source_output
= NULL
;
433 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->record_streams
, s
, NULL
) == s
);
434 s
->connection
= NULL
;
435 record_stream_unref(s
);
438 static void record_stream_free(pa_object
*o
) {
439 record_stream
*s
= RECORD_STREAM(o
);
442 record_stream_unlink(s
);
444 pa_memblockq_free(s
->memblockq
);
448 static int record_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
449 record_stream
*s
= RECORD_STREAM(o
);
450 record_stream_assert_ref(s
);
457 case RECORD_STREAM_MESSAGE_POST_DATA
:
459 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
460 /* pa_log_warn("Failed to push data into output queue."); */
464 if (!pa_pstream_is_pending(s
->connection
->pstream
))
465 native_connection_send_memblock(s
->connection
);
473 static void fix_record_buffer_attr_pre(
475 pa_bool_t adjust_latency
,
476 pa_bool_t early_requests
,
478 uint32_t *fragsize
) {
481 pa_usec_t orig_fragsize_usec
, fragsize_usec
, source_usec
;
484 pa_assert(maxlength
);
487 frame_size
= pa_frame_size(&s
->source_output
->sample_spec
);
489 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
490 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
492 *maxlength
= (uint32_t) frame_size
;
494 if (*fragsize
== (uint32_t) -1)
495 *fragsize
= (uint32_t) pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC
*PA_USEC_PER_MSEC
, &s
->source_output
->sample_spec
);
497 *fragsize
= (uint32_t) frame_size
;
499 orig_fragsize_usec
= fragsize_usec
= pa_bytes_to_usec(*fragsize
, &s
->source_output
->sample_spec
);
501 if (early_requests
) {
503 /* In early request mode we need to emulate the classic
504 * fragment-based playback model. We do this setting the source
505 * latency to the fragment size. */
507 source_usec
= fragsize_usec
;
509 } else if (adjust_latency
) {
511 /* So, the user asked us to adjust the latency according to
512 * what the source can provide. Half the latency will be
513 * spent on the hw buffer, half of it in the async buffer
514 * queue we maintain for each client. */
516 source_usec
= fragsize_usec
/2;
520 /* Ok, the user didn't ask us to adjust the latency, hence we
527 s
->source_latency
= pa_source_output_set_requested_latency(s
->source_output
, source_usec
);
529 s
->source_latency
= 0;
531 if (early_requests
) {
533 /* Ok, we didn't necessarily get what we were asking for, so
534 * let's tell the user */
536 fragsize_usec
= s
->source_latency
;
538 } else if (adjust_latency
) {
540 /* Now subtract what we actually got */
542 if (fragsize_usec
>= s
->source_latency
*2)
543 fragsize_usec
-= s
->source_latency
;
545 fragsize_usec
= s
->source_latency
;
548 if (pa_usec_to_bytes(orig_fragsize_usec
, &s
->source_output
->sample_spec
) !=
549 pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
))
551 *fragsize
= (uint32_t) pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
);
554 *fragsize
= (uint32_t) frame_size
;
557 static void fix_record_buffer_attr_post(
560 uint32_t *fragsize
) {
565 pa_assert(maxlength
);
568 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
570 base
= pa_frame_size(&s
->source_output
->sample_spec
);
572 s
->fragment_size
= (*fragsize
/base
)*base
;
573 if (s
->fragment_size
<= 0)
574 s
->fragment_size
= base
;
576 if (s
->fragment_size
> *maxlength
)
577 s
->fragment_size
= *maxlength
;
579 *fragsize
= (uint32_t) s
->fragment_size
;
582 static record_stream
* record_stream_new(
583 pa_native_connection
*c
,
587 pa_bool_t peak_detect
,
590 pa_source_output_flags_t flags
,
592 pa_bool_t adjust_latency
,
593 pa_sink_input
*direct_on_input
,
594 pa_bool_t early_requests
) {
597 pa_source_output
*source_output
;
599 pa_source_output_new_data data
;
603 pa_assert(maxlength
);
606 pa_source_output_new_data_init(&data
);
608 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
609 data
.driver
= __FILE__
;
610 data
.module
= c
->options
->module
;
611 data
.client
= c
->client
;
612 data
.source
= source
;
613 data
.direct_on_input
= direct_on_input
;
614 pa_source_output_new_data_set_sample_spec(&data
, ss
);
615 pa_source_output_new_data_set_channel_map(&data
, map
);
617 data
.resample_method
= PA_RESAMPLER_PEAKS
;
619 source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
);
621 pa_source_output_new_data_done(&data
);
626 s
= pa_msgobject_new(record_stream
);
627 s
->parent
.parent
.free
= record_stream_free
;
628 s
->parent
.process_msg
= record_stream_process_msg
;
630 s
->source_output
= source_output
;
632 s
->source_output
->push
= source_output_push_cb
;
633 s
->source_output
->kill
= source_output_kill_cb
;
634 s
->source_output
->get_latency
= source_output_get_latency_cb
;
635 s
->source_output
->moved
= source_output_moved_cb
;
636 s
->source_output
->suspend
= source_output_suspend_cb
;
637 s
->source_output
->userdata
= s
;
639 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, fragsize
);
641 s
->memblockq
= pa_memblockq_new(
645 base
= pa_frame_size(&source_output
->sample_spec
),
651 fix_record_buffer_attr_post(s
, maxlength
, fragsize
);
653 *ss
= s
->source_output
->sample_spec
;
654 *map
= s
->source_output
->channel_map
;
656 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
658 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
659 ((double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) + (double) s
->source_latency
) / PA_USEC_PER_MSEC
,
660 (double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) / PA_USEC_PER_MSEC
,
661 (double) s
->source_latency
/ PA_USEC_PER_MSEC
);
663 pa_source_output_put(s
->source_output
);
667 static void record_stream_send_killed(record_stream
*r
) {
669 record_stream_assert_ref(r
);
671 t
= pa_tagstruct_new(NULL
, 0);
672 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
673 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
674 pa_tagstruct_putu32(t
, r
->index
);
675 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
678 static void playback_stream_unlink(playback_stream
*s
) {
685 pa_sink_input_unlink(s
->sink_input
);
686 pa_sink_input_unref(s
->sink_input
);
687 s
->sink_input
= NULL
;
690 if (s
->drain_request
)
691 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
693 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
694 s
->connection
= NULL
;
695 playback_stream_unref(s
);
698 static void playback_stream_free(pa_object
* o
) {
699 playback_stream
*s
= PLAYBACK_STREAM(o
);
702 playback_stream_unlink(s
);
704 pa_memblockq_free(s
->memblockq
);
708 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
709 playback_stream
*s
= PLAYBACK_STREAM(o
);
710 playback_stream_assert_ref(s
);
716 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
721 if ((l
= (uint32_t) pa_atomic_load(&s
->missing
)) <= 0)
724 if (pa_atomic_cmpxchg(&s
->missing
, (int) l
, 0))
731 t
= pa_tagstruct_new(NULL
, 0);
732 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
733 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
734 pa_tagstruct_putu32(t
, s
->index
);
735 pa_tagstruct_putu32(t
, l
);
736 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
738 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
742 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
745 /* pa_log("signalling underflow"); */
747 /* Report that we're empty */
748 t
= pa_tagstruct_new(NULL
, 0);
749 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
750 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
751 pa_tagstruct_putu32(t
, s
->index
);
752 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
756 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
759 /* Notify the user we're overflowed*/
760 t
= pa_tagstruct_new(NULL
, 0);
761 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
762 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
763 pa_tagstruct_putu32(t
, s
->index
);
764 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
768 case PLAYBACK_STREAM_MESSAGE_STARTED
:
770 if (s
->connection
->version
>= 13) {
773 /* Notify the user we're overflowed*/
774 t
= pa_tagstruct_new(NULL
, 0);
775 pa_tagstruct_putu32(t
, PA_COMMAND_STARTED
);
776 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
777 pa_tagstruct_putu32(t
, s
->index
);
778 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
783 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
784 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
791 static void fix_playback_buffer_attr_pre(
793 pa_bool_t adjust_latency
,
794 pa_bool_t early_requests
,
801 pa_usec_t orig_tlength_usec
, tlength_usec
, orig_minreq_usec
, minreq_usec
, sink_usec
;
804 pa_assert(maxlength
);
809 frame_size
= pa_frame_size(&s
->sink_input
->sample_spec
);
811 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
812 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
814 *maxlength
= (uint32_t) frame_size
;
816 if (*tlength
== (uint32_t) -1)
817 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_TLENGTH_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
819 *tlength
= (uint32_t) frame_size
;
821 if (*minreq
== (uint32_t) -1)
822 *minreq
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_PROCESS_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
824 *minreq
= (uint32_t) frame_size
;
826 if (*tlength
< *minreq
+frame_size
)
827 *tlength
= *minreq
+(uint32_t) frame_size
;
829 orig_tlength_usec
= tlength_usec
= pa_bytes_to_usec(*tlength
, &s
->sink_input
->sample_spec
);
830 orig_minreq_usec
= minreq_usec
= pa_bytes_to_usec(*minreq
, &s
->sink_input
->sample_spec
);
832 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
833 (double) tlength_usec
/ PA_USEC_PER_MSEC
,
834 (double) minreq_usec
/ PA_USEC_PER_MSEC
);
836 if (early_requests
) {
838 /* In early request mode we need to emulate the classic
839 * fragment-based playback model. We do this setting the sink
840 * latency to the fragment size. */
842 sink_usec
= minreq_usec
;
844 pa_log_debug("Early requests mode enabled, configuring sink latency to minreq.");
846 } else if (adjust_latency
) {
848 /* So, the user asked us to adjust the latency of the stream
849 * buffer according to the what the sink can provide. The
850 * tlength passed in shall be the overall latency. Roughly
851 * half the latency will be spent on the hw buffer, the other
852 * half of it in the async buffer queue we maintain for each
853 * client. In between we'll have a safety space of size
854 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
855 * empty and needs to be filled, then our buffer must have
856 * enough data to fulfill this request immediatly and thus
857 * have at least the same tlength as the size of the hw
858 * buffer. It additionally needs space for 2 times minreq
859 * because if the buffer ran empty and a partial fillup
860 * happens immediately on the next iteration we need to be
861 * able to fulfill it and give the application also minreq
862 * time to fill it up again for the next request Makes 2 times
863 * minreq in plus.. */
865 if (tlength_usec
> minreq_usec
*2)
866 sink_usec
= (tlength_usec
- minreq_usec
*2)/2;
870 pa_log_debug("Adjust latency mode enabled, configuring sink latency to half of overall latency.");
874 /* Ok, the user didn't ask us to adjust the latency, but we
875 * still need to make sure that the parameters from the user
878 if (tlength_usec
> minreq_usec
*2)
879 sink_usec
= (tlength_usec
- minreq_usec
*2);
883 pa_log_debug("Traditional mode enabled, modifying sink usec only for compat with minreq.");
886 s
->sink_latency
= pa_sink_input_set_requested_latency(s
->sink_input
, sink_usec
);
888 if (early_requests
) {
890 /* Ok, we didn't necessarily get what we were asking for, so
891 * let's tell the user */
893 minreq_usec
= s
->sink_latency
;
895 } else if (adjust_latency
) {
897 /* Ok, we didn't necessarily get what we were asking for, so
898 * let's subtract from what we asked for for the remaining
901 if (tlength_usec
>= s
->sink_latency
)
902 tlength_usec
-= s
->sink_latency
;
905 /* FIXME: This is actually larger than necessary, since not all of
906 * the sink latency is actually rewritable. */
907 if (tlength_usec
< s
->sink_latency
+ 2*minreq_usec
)
908 tlength_usec
= s
->sink_latency
+ 2*minreq_usec
;
910 if (pa_usec_to_bytes_round_up(orig_tlength_usec
, &s
->sink_input
->sample_spec
) !=
911 pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
))
912 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
);
914 if (pa_usec_to_bytes(orig_minreq_usec
, &s
->sink_input
->sample_spec
) !=
915 pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
))
916 *minreq
= (uint32_t) pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
);
919 *minreq
= (uint32_t) frame_size
;
920 *tlength
+= (uint32_t) frame_size
*2;
923 if (*tlength
<= *minreq
)
924 *tlength
= *minreq
*2 + (uint32_t) frame_size
;
926 if (*prebuf
== (uint32_t) -1 || *prebuf
> *tlength
)
930 static void fix_playback_buffer_attr_post(
938 pa_assert(maxlength
);
943 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
944 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
945 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
946 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
951 static playback_stream
* playback_stream_new(
952 pa_native_connection
*c
,
965 pa_sink_input_flags_t flags
,
967 pa_bool_t adjust_latency
,
968 pa_bool_t early_requests
) {
970 playback_stream
*s
, *ssync
;
971 pa_sink_input
*sink_input
;
975 pa_sink_input_new_data data
;
979 pa_assert(maxlength
);
986 /* Find syncid group */
987 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
989 if (!playback_stream_isinstance(ssync
))
992 if (ssync
->syncid
== syncid
)
996 /* Synced streams must connect to the same sink */
1000 sink
= ssync
->sink_input
->sink
;
1001 else if (sink
!= ssync
->sink_input
->sink
)
1005 pa_sink_input_new_data_init(&data
);
1007 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
1008 data
.driver
= __FILE__
;
1009 data
.module
= c
->options
->module
;
1010 data
.client
= c
->client
;
1012 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
1013 pa_sink_input_new_data_set_channel_map(&data
, map
);
1015 pa_sink_input_new_data_set_virtual_volume(&data
, volume
);
1017 pa_sink_input_new_data_set_muted(&data
, muted
);
1018 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
1020 sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
);
1022 pa_sink_input_new_data_done(&data
);
1027 s
= pa_msgobject_new(playback_stream
);
1028 s
->parent
.parent
.parent
.free
= playback_stream_free
;
1029 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
1032 s
->sink_input
= sink_input
;
1033 s
->is_underrun
= TRUE
;
1034 s
->drain_request
= FALSE
;
1035 pa_atomic_store(&s
->missing
, 0);
1037 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
1038 s
->sink_input
->pop
= sink_input_pop_cb
;
1039 s
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1040 s
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1041 s
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1042 s
->sink_input
->kill
= sink_input_kill_cb
;
1043 s
->sink_input
->moved
= sink_input_moved_cb
;
1044 s
->sink_input
->suspend
= sink_input_suspend_cb
;
1045 s
->sink_input
->userdata
= s
;
1047 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
1049 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, tlength
, prebuf
, minreq
);
1050 pa_sink_input_get_silence(sink_input
, &silence
);
1052 s
->memblockq
= pa_memblockq_new(
1056 pa_frame_size(&sink_input
->sample_spec
),
1062 pa_memblock_unref(silence
.memblock
);
1063 fix_playback_buffer_attr_post(s
, maxlength
, tlength
, prebuf
, minreq
);
1065 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
1067 *ss
= s
->sink_input
->sample_spec
;
1068 *map
= s
->sink_input
->channel_map
;
1070 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
1072 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
1073 ((double) pa_bytes_to_usec(*tlength
, &sink_input
->sample_spec
) + (double) s
->sink_latency
) / PA_USEC_PER_MSEC
,
1074 (double) pa_bytes_to_usec(*tlength
-*minreq
*2, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1075 (double) pa_bytes_to_usec(*minreq
, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1076 (double) s
->sink_latency
/ PA_USEC_PER_MSEC
);
1078 pa_sink_input_put(s
->sink_input
);
1082 /* Called from thread context */
1083 static void playback_stream_request_bytes(playback_stream
*s
) {
1084 size_t m
, previous_missing
;
1086 playback_stream_assert_ref(s
);
1088 m
= pa_memblockq_pop_missing(s
->memblockq
);
1093 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
1095 previous_missing
= (size_t) pa_atomic_add(&s
->missing
, (int) m
);
1097 if (pa_memblockq_prebuf_active(s
->memblockq
) ||
1098 (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
))
1099 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1103 static void playback_stream_send_killed(playback_stream
*p
) {
1105 playback_stream_assert_ref(p
);
1107 t
= pa_tagstruct_new(NULL
, 0);
1108 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
1109 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1110 pa_tagstruct_putu32(t
, p
->index
);
1111 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
1114 static int native_connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1115 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1116 pa_native_connection_assert_ref(c
);
1123 case CONNECTION_MESSAGE_REVOKE
:
1124 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1127 case CONNECTION_MESSAGE_RELEASE
:
1128 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1135 static void native_connection_unlink(pa_native_connection
*c
) {
1144 pa_hook_fire(&c
->protocol
->hooks
[PA_NATIVE_HOOK_CONNECTION_UNLINK
], c
);
1147 pa_native_options_unref(c
->options
);
1149 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
1150 record_stream_unlink(r
);
1152 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
1153 if (playback_stream_isinstance(o
))
1154 playback_stream_unlink(PLAYBACK_STREAM(o
));
1156 upload_stream_unlink(UPLOAD_STREAM(o
));
1158 if (c
->subscription
)
1159 pa_subscription_free(c
->subscription
);
1162 pa_pstream_unlink(c
->pstream
);
1164 if (c
->auth_timeout_event
) {
1165 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1166 c
->auth_timeout_event
= NULL
;
1169 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
1171 pa_native_connection_unref(c
);
1174 static void native_connection_free(pa_object
*o
) {
1175 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1179 native_connection_unlink(c
);
1181 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
1182 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
1184 pa_pdispatch_unref(c
->pdispatch
);
1185 pa_pstream_unref(c
->pstream
);
1186 pa_client_free(c
->client
);
1191 static void native_connection_send_memblock(pa_native_connection
*c
) {
1195 start
= PA_IDXSET_INVALID
;
1199 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
1202 if (start
== PA_IDXSET_INVALID
)
1203 start
= c
->rrobin_index
;
1204 else if (start
== c
->rrobin_index
)
1207 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
1208 pa_memchunk schunk
= chunk
;
1210 if (schunk
.length
> r
->fragment_size
)
1211 schunk
.length
= r
->fragment_size
;
1213 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
1215 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
1216 pa_memblock_unref(schunk
.memblock
);
1223 /*** sink input callbacks ***/
1225 static void handle_seek(playback_stream
*s
, int64_t indexw
) {
1226 playback_stream_assert_ref(s
);
1228 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1230 if (s
->sink_input
->thread_info
.underrun_for
> 0) {
1232 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1234 if (pa_memblockq_is_readable(s
->memblockq
)) {
1236 /* We just ended an underrun, let's ask the sink
1237 * for a complete rewind rewrite */
1239 pa_log_debug("Requesting rewind due to end of underrun.");
1240 pa_sink_input_request_rewind(s
->sink_input
,
1241 (size_t) (s
->sink_input
->thread_info
.underrun_for
== (size_t) -1 ? 0 : s
->sink_input
->thread_info
.underrun_for
),
1242 FALSE
, TRUE
, FALSE
);
1248 indexr
= pa_memblockq_get_read_index(s
->memblockq
);
1250 if (indexw
< indexr
) {
1251 /* OK, the sink already asked for this data, so
1252 * let's have it usk us again */
1254 pa_log_debug("Requesting rewind due to rewrite.");
1255 pa_sink_input_request_rewind(s
->sink_input
, (size_t) (indexr
- indexw
), TRUE
, FALSE
, FALSE
);
1259 playback_stream_request_bytes(s
);
1262 /* Called from thread context */
1263 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1264 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1267 pa_sink_input_assert_ref(i
);
1268 s
= PLAYBACK_STREAM(i
->userdata
);
1269 playback_stream_assert_ref(s
);
1273 case SINK_INPUT_MESSAGE_SEEK
: {
1276 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1277 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
1279 handle_seek(s
, windex
);
1283 case SINK_INPUT_MESSAGE_POST_DATA
: {
1288 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1290 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1292 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
1293 pa_log_warn("Failed to push data into queue");
1294 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
1295 pa_memblockq_seek(s
->memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
1298 handle_seek(s
, windex
);
1300 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1305 case SINK_INPUT_MESSAGE_DRAIN
:
1306 case SINK_INPUT_MESSAGE_FLUSH
:
1307 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1308 case SINK_INPUT_MESSAGE_TRIGGER
: {
1311 pa_sink_input
*isync
;
1312 void (*func
)(pa_memblockq
*bq
);
1315 case SINK_INPUT_MESSAGE_FLUSH
:
1316 func
= pa_memblockq_flush_write
;
1319 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1320 func
= pa_memblockq_prebuf_force
;
1323 case SINK_INPUT_MESSAGE_DRAIN
:
1324 case SINK_INPUT_MESSAGE_TRIGGER
:
1325 func
= pa_memblockq_prebuf_disable
;
1329 pa_assert_not_reached();
1332 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1334 handle_seek(s
, windex
);
1336 /* Do the same for all other members in the sync group */
1337 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
1338 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1339 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1340 func(ssync
->memblockq
);
1341 handle_seek(ssync
, windex
);
1344 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1345 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1346 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1347 func(ssync
->memblockq
);
1348 handle_seek(ssync
, windex
);
1351 if (code
== SINK_INPUT_MESSAGE_DRAIN
) {
1352 if (!pa_memblockq_is_readable(s
->memblockq
))
1353 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
1355 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
1356 s
->drain_request
= TRUE
;
1363 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1365 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1366 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1367 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1370 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1373 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1375 pa_memblockq_prebuf_force(s
->memblockq
);
1377 handle_seek(s
, windex
);
1379 /* Fall through to the default handler */
1383 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1384 pa_usec_t
*r
= userdata
;
1386 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1388 /* Fall through, the default handler will add in the extra
1389 * latency added by the resampler */
1394 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1397 /* Called from thread context */
1398 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
1401 pa_sink_input_assert_ref(i
);
1402 s
= PLAYBACK_STREAM(i
->userdata
);
1403 playback_stream_assert_ref(s
);
1406 /* pa_log("%s, pop(): %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1408 if (pa_memblockq_is_readable(s
->memblockq
))
1409 s
->is_underrun
= FALSE
;
1411 /* pa_log("%s, UNDERRUN: %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1413 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1414 s
->drain_request
= FALSE
;
1415 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
);
1416 } else if (!s
->is_underrun
)
1417 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1419 s
->is_underrun
= TRUE
;
1421 playback_stream_request_bytes(s
);
1424 /* This call will not fail with prebuf=0, hence we check for
1425 underrun explicitly above */
1426 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
1429 chunk
->length
= PA_MIN(nbytes
, chunk
->length
);
1431 if (i
->thread_info
.underrun_for
> 0)
1432 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_STARTED
, NULL
, 0, NULL
, NULL
);
1434 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1435 playback_stream_request_bytes(s
);
1440 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1443 pa_sink_input_assert_ref(i
);
1444 s
= PLAYBACK_STREAM(i
->userdata
);
1445 playback_stream_assert_ref(s
);
1447 /* If we are in an underrun, then we don't rewind */
1448 if (i
->thread_info
.underrun_for
> 0)
1451 pa_memblockq_rewind(s
->memblockq
, nbytes
);
1454 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1457 pa_sink_input_assert_ref(i
);
1458 s
= PLAYBACK_STREAM(i
->userdata
);
1459 playback_stream_assert_ref(s
);
1461 pa_memblockq_set_maxrewind(s
->memblockq
, nbytes
);
1464 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
1468 pa_sink_input_assert_ref(i
);
1469 s
= PLAYBACK_STREAM(i
->userdata
);
1470 playback_stream_assert_ref(s
);
1472 tlength
= nbytes
+2*pa_memblockq_get_minreq(s
->memblockq
);
1474 if (pa_memblockq_get_tlength(s
->memblockq
) < tlength
)
1475 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1478 /* Called from main context */
1479 static void sink_input_kill_cb(pa_sink_input
*i
) {
1482 pa_sink_input_assert_ref(i
);
1483 s
= PLAYBACK_STREAM(i
->userdata
);
1484 playback_stream_assert_ref(s
);
1486 playback_stream_send_killed(s
);
1487 playback_stream_unlink(s
);
1490 /* Called from main context */
1491 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1495 pa_sink_input_assert_ref(i
);
1496 s
= PLAYBACK_STREAM(i
->userdata
);
1497 playback_stream_assert_ref(s
);
1499 if (s
->connection
->version
< 12)
1502 t
= pa_tagstruct_new(NULL
, 0);
1503 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1504 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1505 pa_tagstruct_putu32(t
, s
->index
);
1506 pa_tagstruct_put_boolean(t
, suspend
);
1507 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1510 /* Called from main context */
1511 static void sink_input_moved_cb(pa_sink_input
*i
) {
1514 uint32_t maxlength
, tlength
, prebuf
, minreq
;
1516 pa_sink_input_assert_ref(i
);
1517 s
= PLAYBACK_STREAM(i
->userdata
);
1518 playback_stream_assert_ref(s
);
1520 maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
1521 tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
1522 prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
1523 minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
1525 fix_playback_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1526 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1527 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1528 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
1529 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
1530 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1532 if (s
->connection
->version
< 12)
1535 t
= pa_tagstruct_new(NULL
, 0);
1536 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1537 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1538 pa_tagstruct_putu32(t
, s
->index
);
1539 pa_tagstruct_putu32(t
, i
->sink
->index
);
1540 pa_tagstruct_puts(t
, i
->sink
->name
);
1541 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1543 if (s
->connection
->version
>= 13) {
1544 pa_tagstruct_putu32(t
, maxlength
);
1545 pa_tagstruct_putu32(t
, tlength
);
1546 pa_tagstruct_putu32(t
, prebuf
);
1547 pa_tagstruct_putu32(t
, minreq
);
1548 pa_tagstruct_put_usec(t
, s
->sink_latency
);
1551 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1554 /*** source_output callbacks ***/
1556 /* Called from thread context */
1557 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1560 pa_source_output_assert_ref(o
);
1561 s
= RECORD_STREAM(o
->userdata
);
1562 record_stream_assert_ref(s
);
1565 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1568 static void source_output_kill_cb(pa_source_output
*o
) {
1571 pa_source_output_assert_ref(o
);
1572 s
= RECORD_STREAM(o
->userdata
);
1573 record_stream_assert_ref(s
);
1575 record_stream_send_killed(s
);
1576 record_stream_unlink(s
);
1579 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1582 pa_source_output_assert_ref(o
);
1583 s
= RECORD_STREAM(o
->userdata
);
1584 record_stream_assert_ref(s
);
1586 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1588 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1591 /* Called from main context */
1592 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1596 pa_source_output_assert_ref(o
);
1597 s
= RECORD_STREAM(o
->userdata
);
1598 record_stream_assert_ref(s
);
1600 if (s
->connection
->version
< 12)
1603 t
= pa_tagstruct_new(NULL
, 0);
1604 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1605 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1606 pa_tagstruct_putu32(t
, s
->index
);
1607 pa_tagstruct_put_boolean(t
, suspend
);
1608 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1611 /* Called from main context */
1612 static void source_output_moved_cb(pa_source_output
*o
) {
1615 uint32_t maxlength
, fragsize
;
1617 pa_source_output_assert_ref(o
);
1618 s
= RECORD_STREAM(o
->userdata
);
1619 record_stream_assert_ref(s
);
1621 fragsize
= (uint32_t) s
->fragment_size
;
1622 maxlength
= (uint32_t) pa_memblockq_get_length(s
->memblockq
);
1624 fix_record_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &fragsize
);
1625 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1626 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
1628 if (s
->connection
->version
< 12)
1631 t
= pa_tagstruct_new(NULL
, 0);
1632 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1633 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1634 pa_tagstruct_putu32(t
, s
->index
);
1635 pa_tagstruct_putu32(t
, o
->source
->index
);
1636 pa_tagstruct_puts(t
, o
->source
->name
);
1637 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1639 if (s
->connection
->version
>= 13) {
1640 pa_tagstruct_putu32(t
, maxlength
);
1641 pa_tagstruct_putu32(t
, fragsize
);
1642 pa_tagstruct_put_usec(t
, s
->source_latency
);
1645 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1648 /*** pdispatch callbacks ***/
1650 static void protocol_error(pa_native_connection
*c
) {
1651 pa_log("protocol error, kicking client");
1652 native_connection_unlink(c
);
1655 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1656 if (!(expression)) { \
1657 pa_pstream_send_error((pstream), (tag), (error)); \
1662 static pa_tagstruct
*reply_new(uint32_t tag
) {
1663 pa_tagstruct
*reply
;
1665 reply
= pa_tagstruct_new(NULL
, 0);
1666 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1667 pa_tagstruct_putu32(reply
, tag
);
1671 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1672 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1674 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1675 const char *name
= NULL
, *sink_name
;
1678 pa_tagstruct
*reply
;
1679 pa_sink
*sink
= NULL
;
1687 fix_channels
= FALSE
,
1689 variable_rate
= FALSE
,
1691 adjust_latency
= FALSE
,
1692 early_requests
= FALSE
,
1693 dont_inhibit_auto_suspend
= FALSE
,
1695 fail_on_suspend
= FALSE
;
1696 pa_sink_input_flags_t flags
= 0;
1698 pa_bool_t volume_set
= TRUE
;
1700 pa_native_connection_assert_ref(c
);
1703 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1706 PA_TAG_SAMPLE_SPEC
, &ss
,
1707 PA_TAG_CHANNEL_MAP
, &map
,
1708 PA_TAG_U32
, &sink_index
,
1709 PA_TAG_STRING
, &sink_name
,
1710 PA_TAG_U32
, &maxlength
,
1711 PA_TAG_BOOLEAN
, &corked
,
1712 PA_TAG_U32
, &tlength
,
1713 PA_TAG_U32
, &prebuf
,
1714 PA_TAG_U32
, &minreq
,
1715 PA_TAG_U32
, &syncid
,
1716 PA_TAG_CVOLUME
, &volume
,
1717 PA_TAG_INVALID
) < 0) {
1723 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1724 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
1725 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
1726 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1727 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1728 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1729 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1730 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1732 p
= pa_proplist_new();
1735 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1737 if (c
->version
>= 12) {
1738 /* Since 0.9.8 the user can ask for a couple of additional flags */
1740 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1741 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1742 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1743 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1744 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1745 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1746 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1749 pa_proplist_free(p
);
1754 if (c
->version
>= 13) {
1756 if (pa_tagstruct_get_boolean(t
, &muted
) < 0 ||
1757 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1758 pa_tagstruct_get_proplist(t
, p
) < 0) {
1760 pa_proplist_free(p
);
1765 if (c
->version
>= 14) {
1767 if (pa_tagstruct_get_boolean(t
, &volume_set
) < 0 ||
1768 pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
1770 pa_proplist_free(p
);
1775 if (c
->version
>= 15) {
1777 if (pa_tagstruct_get_boolean(t
, &muted_set
) < 0 ||
1778 pa_tagstruct_get_boolean(t
, &dont_inhibit_auto_suspend
) < 0 ||
1779 pa_tagstruct_get_boolean(t
, &fail_on_suspend
) < 0) {
1781 pa_proplist_free(p
);
1786 if (!pa_tagstruct_eof(t
)) {
1788 pa_proplist_free(p
);
1792 if (sink_index
!= PA_INVALID_INDEX
) {
1794 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1795 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1796 pa_proplist_free(p
);
1800 } else if (sink_name
) {
1802 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
))) {
1803 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1804 pa_proplist_free(p
);
1810 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1811 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1812 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1813 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1814 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1815 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1816 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1817 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0) |
1818 (dont_inhibit_auto_suspend
? PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
: 0) |
1819 (fail_on_suspend
? PA_SINK_INPUT_FAIL_ON_SUSPEND
: 0);
1821 /* Only since protocol version 15 there's a seperate muted_set
1822 * flag. For older versions we synthesize it here */
1823 muted_set
= muted_set
|| muted
;
1825 s
= playback_stream_new(c
, sink
, &ss
, &map
, &maxlength
, &tlength
, &prebuf
, &minreq
, volume_set
? &volume
: NULL
, muted
, muted_set
, syncid
, &missing
, flags
, p
, adjust_latency
, early_requests
);
1826 pa_proplist_free(p
);
1828 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1830 reply
= reply_new(tag
);
1831 pa_tagstruct_putu32(reply
, s
->index
);
1832 pa_assert(s
->sink_input
);
1833 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1834 pa_tagstruct_putu32(reply
, missing
);
1836 /* pa_log("initial request is %u", missing); */
1838 if (c
->version
>= 9) {
1839 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1841 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1842 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1843 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1844 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1847 if (c
->version
>= 12) {
1848 /* Since 0.9.8 we support sending the chosen sample
1849 * spec/channel map/device/suspend status back to the
1852 pa_tagstruct_put_sample_spec(reply
, &ss
);
1853 pa_tagstruct_put_channel_map(reply
, &map
);
1855 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1856 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1858 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1861 if (c
->version
>= 13)
1862 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
1864 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1867 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1868 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1871 pa_native_connection_assert_ref(c
);
1874 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1875 !pa_tagstruct_eof(t
)) {
1880 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1884 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1886 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1887 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1891 playback_stream_unlink(s
);
1895 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1897 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1898 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1902 record_stream_unlink(s
);
1906 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1909 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1910 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1914 upload_stream_unlink(s
);
1919 pa_assert_not_reached();
1922 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1925 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1926 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1928 uint32_t maxlength
, fragment_size
;
1929 uint32_t source_index
;
1930 const char *name
= NULL
, *source_name
;
1933 pa_tagstruct
*reply
;
1934 pa_source
*source
= NULL
;
1941 fix_channels
= FALSE
,
1943 variable_rate
= FALSE
,
1944 adjust_latency
= FALSE
,
1945 peak_detect
= FALSE
,
1946 early_requests
= FALSE
,
1947 dont_inhibit_auto_suspend
= FALSE
,
1948 fail_on_suspend
= FALSE
;
1949 pa_source_output_flags_t flags
= 0;
1951 uint32_t direct_on_input_idx
= PA_INVALID_INDEX
;
1952 pa_sink_input
*direct_on_input
= NULL
;
1954 pa_native_connection_assert_ref(c
);
1957 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1958 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1959 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1960 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1961 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1962 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1963 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1964 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1969 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1970 CHECK_VALIDITY(c
->pstream
, !source_name
|| pa_namereg_is_valid_name(source_name
), tag
, PA_ERR_INVALID
);
1971 CHECK_VALIDITY(c
->pstream
, source_index
== PA_INVALID_INDEX
|| !source_name
, tag
, PA_ERR_INVALID
);
1972 CHECK_VALIDITY(c
->pstream
, !source_name
|| source_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1973 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1974 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1975 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1977 p
= pa_proplist_new();
1980 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1982 if (c
->version
>= 12) {
1983 /* Since 0.9.8 the user can ask for a couple of additional flags */
1985 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1986 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1987 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1988 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1989 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1990 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1991 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1994 pa_proplist_free(p
);
1999 if (c
->version
>= 13) {
2001 if (pa_tagstruct_get_boolean(t
, &peak_detect
) < 0 ||
2002 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
2003 pa_tagstruct_get_proplist(t
, p
) < 0 ||
2004 pa_tagstruct_getu32(t
, &direct_on_input_idx
) < 0) {
2006 pa_proplist_free(p
);
2011 if (c
->version
>= 14) {
2013 if (pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
2015 pa_proplist_free(p
);
2020 if (c
->version
>= 15) {
2022 if (pa_tagstruct_get_boolean(t
, &dont_inhibit_auto_suspend
) < 0 ||
2023 pa_tagstruct_get_boolean(t
, &fail_on_suspend
) < 0) {
2025 pa_proplist_free(p
);
2030 if (!pa_tagstruct_eof(t
)) {
2032 pa_proplist_free(p
);
2036 if (source_index
!= PA_INVALID_INDEX
) {
2038 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
2039 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2040 pa_proplist_free(p
);
2044 } else if (source_name
) {
2046 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
))) {
2047 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2048 pa_proplist_free(p
);
2053 if (direct_on_input_idx
!= PA_INVALID_INDEX
) {
2055 if (!(direct_on_input
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, direct_on_input_idx
))) {
2056 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2057 pa_proplist_free(p
);
2063 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
2064 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
2065 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
2066 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
2067 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
2068 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
2069 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
2070 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0) |
2071 (dont_inhibit_auto_suspend
? PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND
: 0) |
2072 (fail_on_suspend
? PA_SOURCE_OUTPUT_FAIL_ON_SUSPEND
: 0);
2074 s
= record_stream_new(c
, source
, &ss
, &map
, peak_detect
, &maxlength
, &fragment_size
, flags
, p
, adjust_latency
, direct_on_input
, early_requests
);
2075 pa_proplist_free(p
);
2077 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2079 reply
= reply_new(tag
);
2080 pa_tagstruct_putu32(reply
, s
->index
);
2081 pa_assert(s
->source_output
);
2082 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
2084 if (c
->version
>= 9) {
2085 /* Since 0.9 we support sending the buffer metrics back to the client */
2087 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
2088 pa_tagstruct_putu32(reply
, (uint32_t) fragment_size
);
2091 if (c
->version
>= 12) {
2092 /* Since 0.9.8 we support sending the chosen sample
2093 * spec/channel map/device/suspend status back to the
2096 pa_tagstruct_put_sample_spec(reply
, &ss
);
2097 pa_tagstruct_put_channel_map(reply
, &map
);
2099 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
2100 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
2102 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
2105 if (c
->version
>= 13)
2106 pa_tagstruct_put_usec(reply
, s
->source_latency
);
2108 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2111 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2112 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2115 pa_native_connection_assert_ref(c
);
2118 if (!pa_tagstruct_eof(t
)) {
2123 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2124 ret
= pa_core_exit(c
->protocol
->core
, FALSE
, 0);
2125 CHECK_VALIDITY(c
->pstream
, ret
>= 0, tag
, PA_ERR_ACCESS
);
2127 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
2130 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2131 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2133 pa_tagstruct
*reply
;
2134 pa_bool_t shm_on_remote
= FALSE
, do_shm
;
2136 pa_native_connection_assert_ref(c
);
2139 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
2140 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
2141 !pa_tagstruct_eof(t
)) {
2146 /* Minimum supported version */
2147 if (c
->version
< 8) {
2148 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
2152 /* Starting with protocol version 13 the MSB of the version tag
2153 reflects if shm is available for this pa_native_connection or
2155 if (c
->version
>= 13) {
2156 shm_on_remote
= !!(c
->version
& 0x80000000U
);
2157 c
->version
&= 0x7FFFFFFFU
;
2160 pa_log_debug("Protocol version: remote %u, local %u", c
->version
, PA_PROTOCOL_VERSION
);
2162 pa_proplist_setf(c
->client
->proplist
, "native-protocol.version", "%u", c
->version
);
2164 if (!c
->authorized
) {
2165 pa_bool_t success
= FALSE
;
2168 const pa_creds
*creds
;
2170 if ((creds
= pa_pdispatch_creds(pd
))) {
2171 if (creds
->uid
== getuid())
2173 else if (c
->options
->auth_group
) {
2177 if ((gid
= pa_get_gid_of_group(c
->options
->auth_group
)) == (gid_t
) -1)
2178 pa_log_warn("Failed to get GID of group '%s'", c
->options
->auth_group
);
2179 else if (gid
== creds
->gid
)
2183 if ((r
= pa_uid_in_group(creds
->uid
, c
->options
->auth_group
)) < 0)
2184 pa_log_warn("Failed to check group membership.");
2190 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
2191 (unsigned long) creds
->uid
,
2192 (unsigned long) creds
->gid
,
2197 if (!success
&& c
->options
->auth_cookie
) {
2200 if ((ac
= pa_auth_cookie_read(c
->options
->auth_cookie
, PA_NATIVE_COOKIE_LENGTH
)))
2201 if (memcmp(ac
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
2206 pa_log_warn("Denied access to client with invalid authorization data.");
2207 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2211 c
->authorized
= TRUE
;
2212 if (c
->auth_timeout_event
) {
2213 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
2214 c
->auth_timeout_event
= NULL
;
2218 /* Enable shared memory support if possible */
2220 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
2223 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm
));
2226 if (c
->version
< 10 || (c
->version
>= 13 && !shm_on_remote
))
2231 /* Only enable SHM if both sides are owned by the same
2232 * user. This is a security measure because otherwise data
2233 * private to the user might leak. */
2235 const pa_creds
*creds
;
2236 if (!(creds
= pa_pdispatch_creds(pd
)) || getuid() != creds
->uid
)
2241 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm
));
2242 pa_pstream_enable_shm(c
->pstream
, do_shm
);
2244 reply
= reply_new(tag
);
2245 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
| (do_shm
? 0x80000000 : 0));
2249 /* SHM support is only enabled after both sides made sure they are the same user. */
2253 ucred
.uid
= getuid();
2254 ucred
.gid
= getgid();
2256 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
2259 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2263 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2264 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2265 const char *name
= NULL
;
2267 pa_tagstruct
*reply
;
2269 pa_native_connection_assert_ref(c
);
2272 p
= pa_proplist_new();
2274 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
2275 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2276 !pa_tagstruct_eof(t
)) {
2279 pa_proplist_free(p
);
2284 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
2285 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2286 pa_proplist_free(p
);
2290 pa_proplist_update(c
->client
->proplist
, PA_UPDATE_REPLACE
, p
);
2291 pa_proplist_free(p
);
2293 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2295 reply
= reply_new(tag
);
2297 if (c
->version
>= 13)
2298 pa_tagstruct_putu32(reply
, c
->client
->index
);
2300 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2303 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2304 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2306 uint32_t idx
= PA_IDXSET_INVALID
;
2308 pa_native_connection_assert_ref(c
);
2311 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2312 !pa_tagstruct_eof(t
)) {
2317 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2318 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2320 if (command
== PA_COMMAND_LOOKUP_SINK
) {
2322 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
)))
2326 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
2327 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
)))
2328 idx
= source
->index
;
2331 if (idx
== PA_IDXSET_INVALID
)
2332 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2334 pa_tagstruct
*reply
;
2335 reply
= reply_new(tag
);
2336 pa_tagstruct_putu32(reply
, idx
);
2337 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2341 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2342 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2346 pa_native_connection_assert_ref(c
);
2349 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2350 !pa_tagstruct_eof(t
)) {
2355 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2356 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2357 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2358 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2360 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
);
2363 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2364 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2365 pa_tagstruct
*reply
;
2366 const pa_mempool_stat
*stat
;
2368 pa_native_connection_assert_ref(c
);
2371 if (!pa_tagstruct_eof(t
)) {
2376 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2378 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
2380 reply
= reply_new(tag
);
2381 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
2382 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
2383 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
2384 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
2385 pa_tagstruct_putu32(reply
, (uint32_t) pa_scache_total_size(c
->protocol
->core
));
2386 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2389 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2390 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2391 pa_tagstruct
*reply
;
2393 struct timeval tv
, now
;
2397 pa_native_connection_assert_ref(c
);
2400 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2401 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2402 !pa_tagstruct_eof(t
)) {
2407 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2408 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2409 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2410 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2411 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
)
2413 reply
= reply_new(tag
);
2415 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
2416 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
2418 pa_tagstruct_put_usec(reply
, latency
);
2420 pa_tagstruct_put_usec(reply
, 0);
2421 pa_tagstruct_put_boolean(reply
, s
->sink_input
->thread_info
.playing_for
> 0);
2422 pa_tagstruct_put_timeval(reply
, &tv
);
2423 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2424 pa_tagstruct_puts64(reply
, s
->write_index
);
2425 pa_tagstruct_puts64(reply
, s
->read_index
);
2427 if (c
->version
>= 13) {
2428 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.underrun_for
);
2429 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.playing_for
);
2432 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2435 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2436 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2437 pa_tagstruct
*reply
;
2439 struct timeval tv
, now
;
2442 pa_native_connection_assert_ref(c
);
2445 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2446 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2447 !pa_tagstruct_eof(t
)) {
2452 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2453 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2454 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2456 reply
= reply_new(tag
);
2457 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
2458 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
2459 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_RUNNING
);
2460 pa_tagstruct_put_timeval(reply
, &tv
);
2461 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2462 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
2463 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
2464 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2467 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2468 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2471 const char *name
= NULL
;
2474 pa_tagstruct
*reply
;
2477 pa_native_connection_assert_ref(c
);
2480 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2481 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
2482 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
2483 pa_tagstruct_getu32(t
, &length
) < 0) {
2488 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2489 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
2490 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
2491 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
2492 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
2493 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
2495 p
= pa_proplist_new();
2497 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2498 !pa_tagstruct_eof(t
)) {
2501 pa_proplist_free(p
);
2505 if (c
->version
< 13)
2506 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2508 if (!(name
= pa_proplist_gets(p
, PA_PROP_EVENT_ID
)))
2509 name
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
);
2511 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2513 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
2514 pa_proplist_free(p
);
2516 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2518 reply
= reply_new(tag
);
2519 pa_tagstruct_putu32(reply
, s
->index
);
2520 pa_tagstruct_putu32(reply
, length
);
2521 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2524 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2525 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2530 pa_native_connection_assert_ref(c
);
2533 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
2534 !pa_tagstruct_eof(t
)) {
2539 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2541 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
2542 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2543 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2545 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
2546 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
2548 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2550 upload_stream_unlink(s
);
2553 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2554 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2555 uint32_t sink_index
;
2558 const char *name
, *sink_name
;
2561 pa_tagstruct
*reply
;
2563 pa_native_connection_assert_ref(c
);
2566 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2568 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
2569 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
2570 pa_tagstruct_getu32(t
, &volume
) < 0 ||
2571 pa_tagstruct_gets(t
, &name
) < 0) {
2576 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
2577 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
2578 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2579 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2581 if (sink_index
!= PA_INVALID_INDEX
)
2582 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
2584 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
);
2586 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2588 p
= pa_proplist_new();
2590 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2591 !pa_tagstruct_eof(t
)) {
2593 pa_proplist_free(p
);
2597 pa_proplist_update(p
, PA_UPDATE_MERGE
, c
->client
->proplist
);
2599 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2600 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2601 pa_proplist_free(p
);
2605 pa_proplist_free(p
);
2607 reply
= reply_new(tag
);
2609 if (c
->version
>= 13)
2610 pa_tagstruct_putu32(reply
, idx
);
2612 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2615 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2616 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2619 pa_native_connection_assert_ref(c
);
2622 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2623 !pa_tagstruct_eof(t
)) {
2628 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2629 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2631 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2632 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2636 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2639 static void fixup_sample_spec(pa_native_connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2642 pa_assert(original
);
2646 if (c
->version
< 12) {
2647 /* Before protocol version 12 we didn't support S32 samples,
2648 * so we need to lie about this to the client */
2650 if (fixed
->format
== PA_SAMPLE_S32LE
)
2651 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2652 if (fixed
->format
== PA_SAMPLE_S32BE
)
2653 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2656 if (c
->version
< 15) {
2657 if (fixed
->format
== PA_SAMPLE_S24LE
|| fixed
->format
== PA_SAMPLE_S24_32LE
)
2658 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2659 if (fixed
->format
== PA_SAMPLE_S24BE
|| fixed
->format
== PA_SAMPLE_S24_32BE
)
2660 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2664 static void sink_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2665 pa_sample_spec fixed_ss
;
2668 pa_sink_assert_ref(sink
);
2670 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2674 PA_TAG_U32
, sink
->index
,
2675 PA_TAG_STRING
, sink
->name
,
2676 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2677 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2678 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2679 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2680 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, FALSE
),
2681 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
, FALSE
),
2682 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2683 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2684 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2685 PA_TAG_STRING
, sink
->driver
,
2686 PA_TAG_U32
, sink
->flags
,
2689 if (c
->version
>= 13) {
2690 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2691 pa_tagstruct_put_usec(t
, pa_sink_get_requested_latency(sink
));
2694 if (c
->version
>= 15) {
2695 pa_tagstruct_put_volume(t
, sink
->base_volume
);
2696 if (PA_UNLIKELY(pa_sink_get_state(sink
) == PA_SINK_INVALID_STATE
))
2697 pa_log_error("Internal sink state is invalid.");
2698 pa_tagstruct_putu32(t
, pa_sink_get_state(sink
));
2699 pa_tagstruct_putu32(t
, sink
->n_volume_steps
);
2703 static void source_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2704 pa_sample_spec fixed_ss
;
2707 pa_source_assert_ref(source
);
2709 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2713 PA_TAG_U32
, source
->index
,
2714 PA_TAG_STRING
, source
->name
,
2715 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2716 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2717 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2718 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2719 PA_TAG_CVOLUME
, pa_source_get_volume(source
, FALSE
),
2720 PA_TAG_BOOLEAN
, pa_source_get_mute(source
, FALSE
),
2721 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2722 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2723 PA_TAG_USEC
, pa_source_get_latency(source
),
2724 PA_TAG_STRING
, source
->driver
,
2725 PA_TAG_U32
, source
->flags
,
2728 if (c
->version
>= 13) {
2729 pa_tagstruct_put_proplist(t
, source
->proplist
);
2730 pa_tagstruct_put_usec(t
, pa_source_get_requested_latency(source
));
2733 if (c
->version
>= 15) {
2734 pa_tagstruct_put_volume(t
, source
->base_volume
);
2735 if (PA_UNLIKELY(pa_source_get_state(source
) == PA_SOURCE_INVALID_STATE
))
2736 pa_log_error("Internal source state is invalid.");
2737 pa_tagstruct_putu32(t
, pa_source_get_state(source
));
2738 pa_tagstruct_putu32(t
, source
->n_volume_steps
);
2742 static void client_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2746 pa_tagstruct_putu32(t
, client
->index
);
2747 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2748 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2749 pa_tagstruct_puts(t
, client
->driver
);
2751 if (c
->version
>= 13)
2752 pa_tagstruct_put_proplist(t
, client
->proplist
);
2755 static void card_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_card
*card
) {
2762 pa_tagstruct_putu32(t
, card
->index
);
2763 pa_tagstruct_puts(t
, card
->name
);
2764 pa_tagstruct_putu32(t
, card
->module
? card
->module
->index
: PA_INVALID_INDEX
);
2765 pa_tagstruct_puts(t
, card
->driver
);
2767 pa_tagstruct_putu32(t
, card
->profiles
? pa_hashmap_size(card
->profiles
) : 0);
2769 if (card
->profiles
) {
2770 while ((p
= pa_hashmap_iterate(card
->profiles
, &state
, NULL
))) {
2771 pa_tagstruct_puts(t
, p
->name
);
2772 pa_tagstruct_puts(t
, p
->description
);
2776 pa_tagstruct_puts(t
, card
->active_profile
? card
->active_profile
->name
: NULL
);
2777 pa_tagstruct_put_proplist(t
, card
->proplist
);
2780 static void module_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_module
*module
) {
2784 pa_tagstruct_putu32(t
, module
->index
);
2785 pa_tagstruct_puts(t
, module
->name
);
2786 pa_tagstruct_puts(t
, module
->argument
);
2787 pa_tagstruct_putu32(t
, (uint32_t) pa_module_get_n_used(module
));
2789 if (c
->version
< 15)
2790 pa_tagstruct_put_boolean(t
, FALSE
); /* autoload is obsolete */
2792 if (c
->version
>= 15)
2793 pa_tagstruct_put_proplist(t
, module
->proplist
);
2796 static void sink_input_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2797 pa_sample_spec fixed_ss
;
2798 pa_usec_t sink_latency
;
2801 pa_sink_input_assert_ref(s
);
2803 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2805 pa_tagstruct_putu32(t
, s
->index
);
2806 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2807 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2808 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2809 pa_tagstruct_putu32(t
, s
->sink
->index
);
2810 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2811 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2812 pa_tagstruct_put_cvolume(t
, pa_sink_input_get_volume(s
));
2813 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
, &sink_latency
));
2814 pa_tagstruct_put_usec(t
, sink_latency
);
2815 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2816 pa_tagstruct_puts(t
, s
->driver
);
2817 if (c
->version
>= 11)
2818 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2819 if (c
->version
>= 13)
2820 pa_tagstruct_put_proplist(t
, s
->proplist
);
2823 static void source_output_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2824 pa_sample_spec fixed_ss
;
2825 pa_usec_t source_latency
;
2828 pa_source_output_assert_ref(s
);
2830 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2832 pa_tagstruct_putu32(t
, s
->index
);
2833 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2834 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2835 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2836 pa_tagstruct_putu32(t
, s
->source
->index
);
2837 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2838 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2839 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
, &source_latency
));
2840 pa_tagstruct_put_usec(t
, source_latency
);
2841 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2842 pa_tagstruct_puts(t
, s
->driver
);
2844 if (c
->version
>= 13)
2845 pa_tagstruct_put_proplist(t
, s
->proplist
);
2848 static void scache_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2849 pa_sample_spec fixed_ss
;
2855 if (e
->memchunk
.memblock
)
2856 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2858 memset(&fixed_ss
, 0, sizeof(fixed_ss
));
2860 pa_tagstruct_putu32(t
, e
->index
);
2861 pa_tagstruct_puts(t
, e
->name
);
2863 if (e
->volume_is_set
)
2866 pa_cvolume_init(&v
);
2868 pa_tagstruct_put_cvolume(t
, &v
);
2869 pa_tagstruct_put_usec(t
, e
->memchunk
.memblock
? pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
) : 0);
2870 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2871 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2872 pa_tagstruct_putu32(t
, (uint32_t) e
->memchunk
.length
);
2873 pa_tagstruct_put_boolean(t
, e
->lazy
);
2874 pa_tagstruct_puts(t
, e
->filename
);
2876 if (c
->version
>= 13)
2877 pa_tagstruct_put_proplist(t
, e
->proplist
);
2880 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2881 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2883 pa_sink
*sink
= NULL
;
2884 pa_source
*source
= NULL
;
2885 pa_client
*client
= NULL
;
2886 pa_card
*card
= NULL
;
2887 pa_module
*module
= NULL
;
2888 pa_sink_input
*si
= NULL
;
2889 pa_source_output
*so
= NULL
;
2890 pa_scache_entry
*sce
= NULL
;
2891 const char *name
= NULL
;
2892 pa_tagstruct
*reply
;
2894 pa_native_connection_assert_ref(c
);
2897 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2898 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2899 command
!= PA_COMMAND_GET_CARD_INFO
&&
2900 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2901 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2902 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2903 pa_tagstruct_gets(t
, &name
) < 0) ||
2904 !pa_tagstruct_eof(t
)) {
2909 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2910 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2911 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2912 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2913 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2915 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2916 if (idx
!= PA_INVALID_INDEX
)
2917 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2919 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
2920 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2921 if (idx
!= PA_INVALID_INDEX
)
2922 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2924 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
2925 } else if (command
== PA_COMMAND_GET_CARD_INFO
) {
2926 if (idx
!= PA_INVALID_INDEX
)
2927 card
= pa_idxset_get_by_index(c
->protocol
->core
->cards
, idx
);
2929 card
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_CARD
);
2930 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2931 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2932 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2933 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2934 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2935 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2936 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2937 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2939 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2940 if (idx
!= PA_INVALID_INDEX
)
2941 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2943 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
);
2946 if (!sink
&& !source
&& !client
&& !card
&& !module
&& !si
&& !so
&& !sce
) {
2947 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2951 reply
= reply_new(tag
);
2953 sink_fill_tagstruct(c
, reply
, sink
);
2955 source_fill_tagstruct(c
, reply
, source
);
2957 client_fill_tagstruct(c
, reply
, client
);
2959 card_fill_tagstruct(c
, reply
, card
);
2961 module_fill_tagstruct(c
, reply
, module
);
2963 sink_input_fill_tagstruct(c
, reply
, si
);
2965 source_output_fill_tagstruct(c
, reply
, so
);
2967 scache_fill_tagstruct(c
, reply
, sce
);
2968 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2971 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2972 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2976 pa_tagstruct
*reply
;
2978 pa_native_connection_assert_ref(c
);
2981 if (!pa_tagstruct_eof(t
)) {
2986 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2988 reply
= reply_new(tag
);
2990 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2991 i
= c
->protocol
->core
->sinks
;
2992 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2993 i
= c
->protocol
->core
->sources
;
2994 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2995 i
= c
->protocol
->core
->clients
;
2996 else if (command
== PA_COMMAND_GET_CARD_INFO_LIST
)
2997 i
= c
->protocol
->core
->cards
;
2998 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2999 i
= c
->protocol
->core
->modules
;
3000 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
3001 i
= c
->protocol
->core
->sink_inputs
;
3002 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
3003 i
= c
->protocol
->core
->source_outputs
;
3005 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
3006 i
= c
->protocol
->core
->scache
;
3010 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
3011 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
3012 sink_fill_tagstruct(c
, reply
, p
);
3013 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
3014 source_fill_tagstruct(c
, reply
, p
);
3015 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
3016 client_fill_tagstruct(c
, reply
, p
);
3017 else if (command
== PA_COMMAND_GET_CARD_INFO_LIST
)
3018 card_fill_tagstruct(c
, reply
, p
);
3019 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
3020 module_fill_tagstruct(c
, reply
, p
);
3021 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
3022 sink_input_fill_tagstruct(c
, reply
, p
);
3023 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
3024 source_output_fill_tagstruct(c
, reply
, p
);
3026 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
3027 scache_fill_tagstruct(c
, reply
, p
);
3032 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3035 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3036 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3037 pa_tagstruct
*reply
;
3040 pa_source
*def_source
;
3041 pa_sample_spec fixed_ss
;
3043 pa_native_connection_assert_ref(c
);
3046 if (!pa_tagstruct_eof(t
)) {
3051 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3053 reply
= reply_new(tag
);
3054 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
3055 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
3056 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
3057 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
3059 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
3060 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
3062 def_sink
= pa_namereg_get_default_sink(c
->protocol
->core
);
3063 pa_tagstruct_puts(reply
, def_sink
? def_sink
->name
: NULL
);
3064 def_source
= pa_namereg_get_default_source(c
->protocol
->core
);
3065 pa_tagstruct_puts(reply
, def_source
? def_source
->name
: NULL
);
3067 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
3069 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3072 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
3074 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3076 pa_native_connection_assert_ref(c
);
3078 t
= pa_tagstruct_new(NULL
, 0);
3079 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
3080 pa_tagstruct_putu32(t
, (uint32_t) -1);
3081 pa_tagstruct_putu32(t
, e
);
3082 pa_tagstruct_putu32(t
, idx
);
3083 pa_pstream_send_tagstruct(c
->pstream
, t
);
3086 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3087 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3088 pa_subscription_mask_t m
;
3090 pa_native_connection_assert_ref(c
);
3093 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
3094 !pa_tagstruct_eof(t
)) {
3099 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3100 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
3102 if (c
->subscription
)
3103 pa_subscription_free(c
->subscription
);
3106 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
3107 pa_assert(c
->subscription
);
3109 c
->subscription
= NULL
;
3111 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3114 static void command_set_volume(
3121 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3124 pa_sink
*sink
= NULL
;
3125 pa_source
*source
= NULL
;
3126 pa_sink_input
*si
= NULL
;
3127 const char *name
= NULL
;
3129 pa_native_connection_assert_ref(c
);
3132 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3133 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3134 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3135 pa_tagstruct_get_cvolume(t
, &volume
) ||
3136 !pa_tagstruct_eof(t
)) {
3141 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3142 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3143 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3144 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3145 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3146 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
3150 case PA_COMMAND_SET_SINK_VOLUME
:
3151 if (idx
!= PA_INVALID_INDEX
)
3152 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3154 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3157 case PA_COMMAND_SET_SOURCE_VOLUME
:
3158 if (idx
!= PA_INVALID_INDEX
)
3159 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3161 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3164 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
3165 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3169 pa_assert_not_reached();
3172 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3175 pa_sink_set_volume(sink
, &volume
, TRUE
, TRUE
);
3177 pa_source_set_volume(source
, &volume
);
3179 pa_sink_input_set_volume(si
, &volume
, TRUE
);
3181 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3184 static void command_set_mute(
3191 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3194 pa_sink
*sink
= NULL
;
3195 pa_source
*source
= NULL
;
3196 pa_sink_input
*si
= NULL
;
3197 const char *name
= NULL
;
3199 pa_native_connection_assert_ref(c
);
3202 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3203 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3204 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3205 pa_tagstruct_get_boolean(t
, &mute
) ||
3206 !pa_tagstruct_eof(t
)) {
3211 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3212 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3213 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3214 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3215 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3219 case PA_COMMAND_SET_SINK_MUTE
:
3221 if (idx
!= PA_INVALID_INDEX
)
3222 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3224 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3228 case PA_COMMAND_SET_SOURCE_MUTE
:
3229 if (idx
!= PA_INVALID_INDEX
)
3230 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3232 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3236 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
3237 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3241 pa_assert_not_reached();
3244 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3247 pa_sink_set_mute(sink
, mute
);
3249 pa_source_set_mute(source
, mute
);
3251 pa_sink_input_set_mute(si
, mute
, TRUE
);
3253 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3256 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3257 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3262 pa_native_connection_assert_ref(c
);
3265 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3266 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3267 !pa_tagstruct_eof(t
)) {
3272 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3273 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3274 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3275 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3276 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3278 pa_sink_input_cork(s
->sink_input
, b
);
3281 s
->is_underrun
= TRUE
;
3283 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3286 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3287 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3291 pa_native_connection_assert_ref(c
);
3294 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3295 !pa_tagstruct_eof(t
)) {
3300 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3301 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3302 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3303 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3304 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3307 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3308 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3311 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3312 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3315 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3316 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3320 pa_assert_not_reached();
3323 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3326 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3327 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3332 pa_native_connection_assert_ref(c
);
3335 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3336 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3337 !pa_tagstruct_eof(t
)) {
3342 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3343 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3344 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3346 pa_source_output_cork(s
->source_output
, b
);
3347 pa_memblockq_prebuf_force(s
->memblockq
);
3348 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3351 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3352 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3356 pa_native_connection_assert_ref(c
);
3359 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3360 !pa_tagstruct_eof(t
)) {
3365 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3366 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3367 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3369 pa_memblockq_flush_read(s
->memblockq
);
3370 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3373 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3374 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3376 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3377 pa_tagstruct
*reply
;
3379 pa_native_connection_assert_ref(c
);
3382 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3387 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3389 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3391 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3393 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3394 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3395 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3397 if (pa_tagstruct_get(
3399 PA_TAG_U32
, &maxlength
,
3400 PA_TAG_U32
, &tlength
,
3401 PA_TAG_U32
, &prebuf
,
3402 PA_TAG_U32
, &minreq
,
3403 PA_TAG_INVALID
) < 0 ||
3404 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3405 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3406 !pa_tagstruct_eof(t
)) {
3411 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3412 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3413 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3414 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3415 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3416 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3418 reply
= reply_new(tag
);
3419 pa_tagstruct_putu32(reply
, maxlength
);
3420 pa_tagstruct_putu32(reply
, tlength
);
3421 pa_tagstruct_putu32(reply
, prebuf
);
3422 pa_tagstruct_putu32(reply
, minreq
);
3424 if (c
->version
>= 13)
3425 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3429 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3430 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3432 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3433 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3435 if (pa_tagstruct_get(
3437 PA_TAG_U32
, &maxlength
,
3438 PA_TAG_U32
, &fragsize
,
3439 PA_TAG_INVALID
) < 0 ||
3440 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3441 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3442 !pa_tagstruct_eof(t
)) {
3447 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &fragsize
);
3448 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3449 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3451 reply
= reply_new(tag
);
3452 pa_tagstruct_putu32(reply
, maxlength
);
3453 pa_tagstruct_putu32(reply
, fragsize
);
3455 if (c
->version
>= 13)
3456 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3459 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3462 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3463 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3467 pa_native_connection_assert_ref(c
);
3470 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3471 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3472 !pa_tagstruct_eof(t
)) {
3477 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3478 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3480 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3483 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3484 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3485 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3487 pa_sink_input_set_rate(s
->sink_input
, rate
);
3491 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3493 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3494 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3496 pa_source_output_set_rate(s
->source_output
, rate
);
3499 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3502 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3503 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3508 pa_native_connection_assert_ref(c
);
3511 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3513 p
= pa_proplist_new();
3515 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3517 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3518 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3519 !pa_tagstruct_eof(t
)) {
3521 pa_proplist_free(p
);
3527 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3528 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3529 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3530 !pa_tagstruct_eof(t
)) {
3532 pa_proplist_free(p
);
3537 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3539 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3542 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3543 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3544 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3546 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
3547 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3549 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3552 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3553 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3555 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
3556 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3558 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3560 pa_proplist_update(c
->client
->proplist
, mode
, p
);
3561 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3564 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3567 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3568 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3570 unsigned changed
= 0;
3572 pa_strlist
*l
= NULL
;
3574 pa_native_connection_assert_ref(c
);
3577 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3579 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3581 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3587 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3590 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3591 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3592 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3594 p
= s
->sink_input
->proplist
;
3596 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3599 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3600 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3602 p
= s
->source_output
->proplist
;
3604 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3606 p
= c
->client
->proplist
;
3612 if (pa_tagstruct_gets(t
, &k
) < 0) {
3621 l
= pa_strlist_prepend(l
, k
);
3624 if (!pa_tagstruct_eof(t
)) {
3633 l
= pa_strlist_pop(l
, &z
);
3638 changed
+= (unsigned) (pa_proplist_unset(p
, z
) >= 0);
3642 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3645 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3648 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3649 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3651 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3654 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3655 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3658 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3659 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3664 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3665 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3668 pa_native_connection_assert_ref(c
);
3671 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3672 !pa_tagstruct_eof(t
)) {
3677 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3678 CHECK_VALIDITY(c
->pstream
, !s
|| pa_namereg_is_valid_name(s
), tag
, PA_ERR_INVALID
);
3680 if (command
== PA_COMMAND_SET_DEFAULT_SOURCE
) {
3683 source
= pa_namereg_get(c
->protocol
->core
, s
, PA_NAMEREG_SOURCE
);
3684 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3686 pa_namereg_set_default_source(c
->protocol
->core
, source
);
3689 pa_assert(command
== PA_COMMAND_SET_DEFAULT_SINK
);
3691 sink
= pa_namereg_get(c
->protocol
->core
, s
, PA_NAMEREG_SINK
);
3692 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3694 pa_namereg_set_default_sink(c
->protocol
->core
, sink
);
3697 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3700 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3701 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3705 pa_native_connection_assert_ref(c
);
3708 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3709 pa_tagstruct_gets(t
, &name
) < 0 ||
3710 !pa_tagstruct_eof(t
)) {
3715 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3716 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3718 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3721 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3722 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3723 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3725 pa_sink_input_set_name(s
->sink_input
, name
);
3729 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3731 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3732 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3734 pa_source_output_set_name(s
->source_output
, name
);
3737 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3740 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3741 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3744 pa_native_connection_assert_ref(c
);
3747 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3748 !pa_tagstruct_eof(t
)) {
3753 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3755 if (command
== PA_COMMAND_KILL_CLIENT
) {
3758 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3759 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3761 pa_native_connection_ref(c
);
3762 pa_client_kill(client
);
3764 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3767 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3768 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3770 pa_native_connection_ref(c
);
3771 pa_sink_input_kill(s
);
3773 pa_source_output
*s
;
3775 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3777 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3778 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3780 pa_native_connection_ref(c
);
3781 pa_source_output_kill(s
);
3784 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3785 pa_native_connection_unref(c
);
3788 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3789 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3791 const char *name
, *argument
;
3792 pa_tagstruct
*reply
;
3794 pa_native_connection_assert_ref(c
);
3797 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3798 pa_tagstruct_gets(t
, &argument
) < 0 ||
3799 !pa_tagstruct_eof(t
)) {
3804 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3805 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3806 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3808 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3809 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3813 reply
= reply_new(tag
);
3814 pa_tagstruct_putu32(reply
, m
->index
);
3815 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3818 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3819 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3823 pa_native_connection_assert_ref(c
);
3826 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3827 !pa_tagstruct_eof(t
)) {
3832 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3833 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3834 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3836 pa_module_unload_request(m
, FALSE
);
3837 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3840 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3841 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3842 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3843 const char *name_device
= NULL
;
3845 pa_native_connection_assert_ref(c
);
3848 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3849 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3850 pa_tagstruct_gets(t
, &name_device
) < 0 ||
3851 !pa_tagstruct_eof(t
)) {
3856 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3857 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3859 CHECK_VALIDITY(c
->pstream
, !name_device
|| pa_namereg_is_valid_name(name_device
), tag
, PA_ERR_INVALID
);
3860 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| name_device
, tag
, PA_ERR_INVALID
);
3861 CHECK_VALIDITY(c
->pstream
, idx_device
== PA_INVALID_INDEX
|| !name_device
, tag
, PA_ERR_INVALID
);
3862 CHECK_VALIDITY(c
->pstream
, !name_device
|| idx_device
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3864 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3865 pa_sink_input
*si
= NULL
;
3866 pa_sink
*sink
= NULL
;
3868 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3870 if (idx_device
!= PA_INVALID_INDEX
)
3871 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3873 sink
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SINK
);
3875 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3877 if (pa_sink_input_move_to(si
, sink
, TRUE
) < 0) {
3878 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3882 pa_source_output
*so
= NULL
;
3885 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3887 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3889 if (idx_device
!= PA_INVALID_INDEX
)
3890 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3892 source
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SOURCE
);
3894 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3896 if (pa_source_output_move_to(so
, source
, TRUE
) < 0) {
3897 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3902 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3905 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3906 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3907 uint32_t idx
= PA_INVALID_INDEX
;
3908 const char *name
= NULL
;
3911 pa_native_connection_assert_ref(c
);
3914 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3915 pa_tagstruct_gets(t
, &name
) < 0 ||
3916 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3917 !pa_tagstruct_eof(t
)) {
3922 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3923 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
) || *name
== 0, tag
, PA_ERR_INVALID
);
3924 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3925 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3926 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3928 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3930 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3932 pa_log_debug("%s all sinks", b
? "Suspending" : "Resuming");
3934 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3935 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3939 pa_sink
*sink
= NULL
;
3941 if (idx
!= PA_INVALID_INDEX
)
3942 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3944 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3946 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3948 if (pa_sink_suspend(sink
, b
) < 0) {
3949 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3955 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3957 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3959 pa_log_debug("%s all sources", b
? "Suspending" : "Resuming");
3961 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3962 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3969 if (idx
!= PA_INVALID_INDEX
)
3970 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3972 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3974 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3976 if (pa_source_suspend(source
, b
) < 0) {
3977 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3983 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3986 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3987 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3988 uint32_t idx
= PA_INVALID_INDEX
;
3989 const char *name
= NULL
;
3991 pa_native_protocol_ext_cb_t cb
;
3993 pa_native_connection_assert_ref(c
);
3996 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3997 pa_tagstruct_gets(t
, &name
) < 0) {
4002 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
4003 CHECK_VALIDITY(c
->pstream
, !name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
4004 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
4005 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
4006 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
4008 if (idx
!= PA_INVALID_INDEX
)
4009 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
4011 for (m
= pa_idxset_first(c
->protocol
->core
->modules
, &idx
); m
; m
= pa_idxset_next(c
->protocol
->core
->modules
, &idx
))
4012 if (strcmp(name
, m
->name
) == 0)
4016 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
4017 CHECK_VALIDITY(c
->pstream
, m
->load_once
|| idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
4019 cb
= (pa_native_protocol_ext_cb_t
) (unsigned long) pa_hashmap_get(c
->protocol
->extensions
, m
);
4020 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
4022 if (cb(c
->protocol
, m
, c
, tag
, t
) < 0)
4026 static void command_set_card_profile(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
4027 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4028 uint32_t idx
= PA_INVALID_INDEX
;
4029 const char *name
= NULL
, *profile
= NULL
;
4030 pa_card
*card
= NULL
;
4032 pa_native_connection_assert_ref(c
);
4035 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
4036 pa_tagstruct_gets(t
, &name
) < 0 ||
4037 pa_tagstruct_gets(t
, &profile
) < 0 ||
4038 !pa_tagstruct_eof(t
)) {
4043 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
4044 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
4045 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
4046 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
4047 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
4049 if (idx
!= PA_INVALID_INDEX
)
4050 card
= pa_idxset_get_by_index(c
->protocol
->core
->cards
, idx
);
4052 card
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_CARD
);
4054 CHECK_VALIDITY(c
->pstream
, card
, tag
, PA_ERR_NOENTITY
);
4056 if (pa_card_set_profile(card
, profile
) < 0) {
4057 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
4061 pa_pstream_send_simple_ack(c
->pstream
, tag
);
4064 /*** pstream callbacks ***/
4066 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
4067 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4071 pa_native_connection_assert_ref(c
);
4073 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
4074 pa_log("invalid packet.");
4075 native_connection_unlink(c
);
4079 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
) {
4080 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4081 output_stream
*stream
;
4085 pa_native_connection_assert_ref(c
);
4087 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
4088 pa_log("client sent block for invalid stream.");
4093 /* pa_log("got %lu bytes", (unsigned long) chunk->length); */
4095 if (playback_stream_isinstance(stream
)) {
4096 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
4098 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
4099 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
);
4101 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
4104 upload_stream
*u
= UPLOAD_STREAM(stream
);
4107 if (!u
->memchunk
.memblock
) {
4108 if (u
->length
== chunk
->length
) {
4109 u
->memchunk
= *chunk
;
4110 pa_memblock_ref(u
->memchunk
.memblock
);
4113 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
4114 u
->memchunk
.index
= u
->memchunk
.length
= 0;
4118 pa_assert(u
->memchunk
.memblock
);
4121 if (l
> chunk
->length
)
4127 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
4128 src
= pa_memblock_acquire(chunk
->memblock
);
4130 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
4131 (uint8_t*) src
+chunk
->index
, l
);
4133 pa_memblock_release(u
->memchunk
.memblock
);
4134 pa_memblock_release(chunk
->memblock
);
4136 u
->memchunk
.length
+= l
;
4142 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
4143 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4146 pa_native_connection_assert_ref(c
);
4148 native_connection_unlink(c
);
4149 pa_log_info("Connection died.");
4152 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
4153 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4156 pa_native_connection_assert_ref(c
);
4158 native_connection_send_memblock(c
);
4161 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4164 if (!(q
= pa_thread_mq_get()))
4165 pa_pstream_send_revoke(p
, block_id
);
4167 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4170 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4173 if (!(q
= pa_thread_mq_get()))
4174 pa_pstream_send_release(p
, block_id
);
4176 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4179 /*** client callbacks ***/
4181 static void client_kill_cb(pa_client
*c
) {
4184 native_connection_unlink(PA_NATIVE_CONNECTION(c
->userdata
));
4185 pa_log_info("Connection killed.");
4188 /*** module entry points ***/
4190 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
4191 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4195 pa_native_connection_assert_ref(c
);
4196 pa_assert(c
->auth_timeout_event
== e
);
4198 if (!c
->authorized
) {
4199 native_connection_unlink(c
);
4200 pa_log_info("Connection terminated due to authentication timeout.");
4204 void pa_native_protocol_connect(pa_native_protocol
*p
, pa_iochannel
*io
, pa_native_options
*o
) {
4205 pa_native_connection
*c
;
4208 pa_client_new_data data
;
4214 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
4215 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
4216 pa_iochannel_free(io
);
4220 pa_client_new_data_init(&data
);
4221 data
.module
= o
->module
;
4222 data
.driver
= __FILE__
;
4223 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
4224 pa_proplist_setf(data
.proplist
, PA_PROP_APPLICATION_NAME
, "Native client (%s)", pname
);
4225 pa_proplist_sets(data
.proplist
, "native-protocol.peer", pname
);
4226 client
= pa_client_new(p
->core
, &data
);
4227 pa_client_new_data_done(&data
);
4232 c
= pa_msgobject_new(pa_native_connection
);
4233 c
->parent
.parent
.free
= native_connection_free
;
4234 c
->parent
.process_msg
= native_connection_process_msg
;
4236 c
->options
= pa_native_options_ref(o
);
4237 c
->authorized
= FALSE
;
4239 if (o
->auth_anonymous
) {
4240 pa_log_info("Client authenticated anonymously.");
4241 c
->authorized
= TRUE
;
4244 if (!c
->authorized
&&
4246 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
4248 pa_log_info("Client authenticated by IP ACL.");
4249 c
->authorized
= TRUE
;
4252 if (!c
->authorized
) {
4254 pa_gettimeofday(&tv
);
4255 tv
.tv_sec
+= AUTH_TIMEOUT
;
4256 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
4258 c
->auth_timeout_event
= NULL
;
4260 c
->is_local
= pa_iochannel_socket_is_local(io
);
4264 c
->client
->kill
= client_kill_cb
;
4265 c
->client
->userdata
= c
;
4267 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
4268 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
4269 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
4270 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
4271 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
4272 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
4273 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
4275 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
4277 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
4278 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
4280 c
->rrobin_index
= PA_IDXSET_INVALID
;
4281 c
->subscription
= NULL
;
4283 pa_idxset_put(p
->connections
, c
, NULL
);
4286 if (pa_iochannel_creds_supported(io
))
4287 pa_iochannel_creds_enable(io
);
4290 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_CONNECTION_PUT
], c
);
4293 void pa_native_protocol_disconnect(pa_native_protocol
*p
, pa_module
*m
) {
4294 pa_native_connection
*c
;
4300 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
4301 if (c
->options
->module
== m
)
4302 native_connection_unlink(c
);
4305 static pa_native_protocol
* native_protocol_new(pa_core
*c
) {
4306 pa_native_protocol
*p
;
4311 p
= pa_xnew(pa_native_protocol
, 1);
4314 p
->connections
= pa_idxset_new(NULL
, NULL
);
4318 p
->extensions
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
4320 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4321 pa_hook_init(&p
->hooks
[h
], p
);
4323 pa_assert_se(pa_shared_set(c
, "native-protocol", p
) >= 0);
4328 pa_native_protocol
* pa_native_protocol_get(pa_core
*c
) {
4329 pa_native_protocol
*p
;
4331 if ((p
= pa_shared_get(c
, "native-protocol")))
4332 return pa_native_protocol_ref(p
);
4334 return native_protocol_new(c
);
4337 pa_native_protocol
* pa_native_protocol_ref(pa_native_protocol
*p
) {
4339 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4346 void pa_native_protocol_unref(pa_native_protocol
*p
) {
4347 pa_native_connection
*c
;
4351 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4353 if (PA_REFCNT_DEC(p
) > 0)
4356 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4357 native_connection_unlink(c
);
4359 pa_idxset_free(p
->connections
, NULL
, NULL
);
4361 pa_strlist_free(p
->servers
);
4363 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4364 pa_hook_done(&p
->hooks
[h
]);
4366 pa_hashmap_free(p
->extensions
, NULL
, NULL
);
4368 pa_assert_se(pa_shared_remove(p
->core
, "native-protocol") >= 0);
4373 void pa_native_protocol_add_server_string(pa_native_protocol
*p
, const char *name
) {
4375 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4378 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
4380 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4383 void pa_native_protocol_remove_server_string(pa_native_protocol
*p
, const char *name
) {
4385 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4388 p
->servers
= pa_strlist_remove(p
->servers
, name
);
4390 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4393 pa_hook
*pa_native_protocol_hooks(pa_native_protocol
*p
) {
4395 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4400 pa_strlist
*pa_native_protocol_servers(pa_native_protocol
*p
) {
4402 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4407 int pa_native_protocol_install_ext(pa_native_protocol
*p
, pa_module
*m
, pa_native_protocol_ext_cb_t cb
) {
4409 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4412 pa_assert(!pa_hashmap_get(p
->extensions
, m
));
4414 pa_assert_se(pa_hashmap_put(p
->extensions
, m
, (void*) (unsigned long) cb
) == 0);
4418 void pa_native_protocol_remove_ext(pa_native_protocol
*p
, pa_module
*m
) {
4420 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4423 pa_assert_se(pa_hashmap_remove(p
->extensions
, m
));
4426 pa_native_options
* pa_native_options_new(void) {
4427 pa_native_options
*o
;
4429 o
= pa_xnew0(pa_native_options
, 1);
4435 pa_native_options
* pa_native_options_ref(pa_native_options
*o
) {
4437 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4444 void pa_native_options_unref(pa_native_options
*o
) {
4446 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4448 if (PA_REFCNT_DEC(o
) > 0)
4451 pa_xfree(o
->auth_group
);
4454 pa_ip_acl_free(o
->auth_ip_acl
);
4457 pa_auth_cookie_unref(o
->auth_cookie
);
4462 int pa_native_options_parse(pa_native_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
4467 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4470 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
4471 pa_log("auth-anonymous= expects a boolean argument.");
4476 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &enabled
) < 0) {
4477 pa_log("auth-group-enabled= expects a boolean argument.");
4481 pa_xfree(o
->auth_group
);
4482 o
->auth_group
= enabled
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4486 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4489 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4492 if (!(ipa
= pa_ip_acl_new(acl
))) {
4493 pa_log("Failed to parse IP ACL '%s'", acl
);
4498 pa_ip_acl_free(o
->auth_ip_acl
);
4500 o
->auth_ip_acl
= ipa
;
4504 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
4505 pa_log("auth-cookie-enabled= expects a boolean argument.");
4510 pa_auth_cookie_unref(o
->auth_cookie
);
4515 /* The new name for this is 'auth-cookie', for compat reasons
4516 * we check the old name too */
4517 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
4518 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
4519 cn
= PA_NATIVE_COOKIE_FILE
;
4521 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, PA_NATIVE_COOKIE_LENGTH
)))
4525 o
->auth_cookie
= NULL
;
4530 pa_pstream
* pa_native_connection_get_pstream(pa_native_connection
*c
) {
4531 pa_native_connection_assert_ref(c
);