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
,
598 pa_source_output
*source_output
= NULL
;
600 pa_source_output_new_data data
;
604 pa_assert(maxlength
);
608 pa_source_output_new_data_init(&data
);
610 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
611 data
.driver
= __FILE__
;
612 data
.module
= c
->options
->module
;
613 data
.client
= c
->client
;
614 data
.source
= source
;
615 data
.direct_on_input
= direct_on_input
;
616 pa_source_output_new_data_set_sample_spec(&data
, ss
);
617 pa_source_output_new_data_set_channel_map(&data
, map
);
619 data
.resample_method
= PA_RESAMPLER_PEAKS
;
621 *ret
= -pa_source_output_new(&source_output
, c
->protocol
->core
, &data
, flags
);
623 pa_source_output_new_data_done(&data
);
628 s
= pa_msgobject_new(record_stream
);
629 s
->parent
.parent
.free
= record_stream_free
;
630 s
->parent
.process_msg
= record_stream_process_msg
;
632 s
->source_output
= source_output
;
634 s
->source_output
->push
= source_output_push_cb
;
635 s
->source_output
->kill
= source_output_kill_cb
;
636 s
->source_output
->get_latency
= source_output_get_latency_cb
;
637 s
->source_output
->moved
= source_output_moved_cb
;
638 s
->source_output
->suspend
= source_output_suspend_cb
;
639 s
->source_output
->userdata
= s
;
641 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, fragsize
);
643 s
->memblockq
= pa_memblockq_new(
647 base
= pa_frame_size(&source_output
->sample_spec
),
653 fix_record_buffer_attr_post(s
, maxlength
, fragsize
);
655 *ss
= s
->source_output
->sample_spec
;
656 *map
= s
->source_output
->channel_map
;
658 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
660 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
661 ((double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) + (double) s
->source_latency
) / PA_USEC_PER_MSEC
,
662 (double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) / PA_USEC_PER_MSEC
,
663 (double) s
->source_latency
/ PA_USEC_PER_MSEC
);
665 pa_source_output_put(s
->source_output
);
669 static void record_stream_send_killed(record_stream
*r
) {
671 record_stream_assert_ref(r
);
673 t
= pa_tagstruct_new(NULL
, 0);
674 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
675 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
676 pa_tagstruct_putu32(t
, r
->index
);
677 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
680 static void playback_stream_unlink(playback_stream
*s
) {
687 pa_sink_input_unlink(s
->sink_input
);
688 pa_sink_input_unref(s
->sink_input
);
689 s
->sink_input
= NULL
;
692 if (s
->drain_request
)
693 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
695 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
696 s
->connection
= NULL
;
697 playback_stream_unref(s
);
700 static void playback_stream_free(pa_object
* o
) {
701 playback_stream
*s
= PLAYBACK_STREAM(o
);
704 playback_stream_unlink(s
);
706 pa_memblockq_free(s
->memblockq
);
710 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
711 playback_stream
*s
= PLAYBACK_STREAM(o
);
712 playback_stream_assert_ref(s
);
718 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
723 if ((l
= (uint32_t) pa_atomic_load(&s
->missing
)) <= 0)
726 if (pa_atomic_cmpxchg(&s
->missing
, (int) l
, 0))
733 t
= pa_tagstruct_new(NULL
, 0);
734 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
735 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
736 pa_tagstruct_putu32(t
, s
->index
);
737 pa_tagstruct_putu32(t
, l
);
738 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
740 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
744 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
747 /* pa_log("signalling underflow"); */
749 /* Report that we're empty */
750 t
= pa_tagstruct_new(NULL
, 0);
751 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
752 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
753 pa_tagstruct_putu32(t
, s
->index
);
754 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
758 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
761 /* Notify the user we're overflowed*/
762 t
= pa_tagstruct_new(NULL
, 0);
763 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
764 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
765 pa_tagstruct_putu32(t
, s
->index
);
766 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
770 case PLAYBACK_STREAM_MESSAGE_STARTED
:
772 if (s
->connection
->version
>= 13) {
775 /* Notify the user we're overflowed*/
776 t
= pa_tagstruct_new(NULL
, 0);
777 pa_tagstruct_putu32(t
, PA_COMMAND_STARTED
);
778 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
779 pa_tagstruct_putu32(t
, s
->index
);
780 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
785 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
786 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
793 static void fix_playback_buffer_attr_pre(
795 pa_bool_t adjust_latency
,
796 pa_bool_t early_requests
,
803 pa_usec_t orig_tlength_usec
, tlength_usec
, orig_minreq_usec
, minreq_usec
, sink_usec
;
806 pa_assert(maxlength
);
811 frame_size
= pa_frame_size(&s
->sink_input
->sample_spec
);
813 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
814 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
816 *maxlength
= (uint32_t) frame_size
;
818 if (*tlength
== (uint32_t) -1)
819 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_TLENGTH_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
821 *tlength
= (uint32_t) frame_size
;
823 if (*minreq
== (uint32_t) -1)
824 *minreq
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_PROCESS_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
826 *minreq
= (uint32_t) frame_size
;
828 if (*tlength
< *minreq
+frame_size
)
829 *tlength
= *minreq
+(uint32_t) frame_size
;
831 orig_tlength_usec
= tlength_usec
= pa_bytes_to_usec(*tlength
, &s
->sink_input
->sample_spec
);
832 orig_minreq_usec
= minreq_usec
= pa_bytes_to_usec(*minreq
, &s
->sink_input
->sample_spec
);
834 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
835 (double) tlength_usec
/ PA_USEC_PER_MSEC
,
836 (double) minreq_usec
/ PA_USEC_PER_MSEC
);
838 if (early_requests
) {
840 /* In early request mode we need to emulate the classic
841 * fragment-based playback model. We do this setting the sink
842 * latency to the fragment size. */
844 sink_usec
= minreq_usec
;
846 pa_log_debug("Early requests mode enabled, configuring sink latency to minreq.");
848 } else if (adjust_latency
) {
850 /* So, the user asked us to adjust the latency of the stream
851 * buffer according to the what the sink can provide. The
852 * tlength passed in shall be the overall latency. Roughly
853 * half the latency will be spent on the hw buffer, the other
854 * half of it in the async buffer queue we maintain for each
855 * client. In between we'll have a safety space of size
856 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
857 * empty and needs to be filled, then our buffer must have
858 * enough data to fulfill this request immediatly and thus
859 * have at least the same tlength as the size of the hw
860 * buffer. It additionally needs space for 2 times minreq
861 * because if the buffer ran empty and a partial fillup
862 * happens immediately on the next iteration we need to be
863 * able to fulfill it and give the application also minreq
864 * time to fill it up again for the next request Makes 2 times
865 * minreq in plus.. */
867 if (tlength_usec
> minreq_usec
*2)
868 sink_usec
= (tlength_usec
- minreq_usec
*2)/2;
872 pa_log_debug("Adjust latency mode enabled, configuring sink latency to half of overall latency.");
876 /* Ok, the user didn't ask us to adjust the latency, but we
877 * still need to make sure that the parameters from the user
880 if (tlength_usec
> minreq_usec
*2)
881 sink_usec
= (tlength_usec
- minreq_usec
*2);
885 pa_log_debug("Traditional mode enabled, modifying sink usec only for compat with minreq.");
888 s
->sink_latency
= pa_sink_input_set_requested_latency(s
->sink_input
, sink_usec
);
890 if (early_requests
) {
892 /* Ok, we didn't necessarily get what we were asking for, so
893 * let's tell the user */
895 minreq_usec
= s
->sink_latency
;
897 } else if (adjust_latency
) {
899 /* Ok, we didn't necessarily get what we were asking for, so
900 * let's subtract from what we asked for for the remaining
903 if (tlength_usec
>= s
->sink_latency
)
904 tlength_usec
-= s
->sink_latency
;
907 /* FIXME: This is actually larger than necessary, since not all of
908 * the sink latency is actually rewritable. */
909 if (tlength_usec
< s
->sink_latency
+ 2*minreq_usec
)
910 tlength_usec
= s
->sink_latency
+ 2*minreq_usec
;
912 if (pa_usec_to_bytes_round_up(orig_tlength_usec
, &s
->sink_input
->sample_spec
) !=
913 pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
))
914 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
);
916 if (pa_usec_to_bytes(orig_minreq_usec
, &s
->sink_input
->sample_spec
) !=
917 pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
))
918 *minreq
= (uint32_t) pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
);
921 *minreq
= (uint32_t) frame_size
;
922 *tlength
+= (uint32_t) frame_size
*2;
925 if (*tlength
<= *minreq
)
926 *tlength
= *minreq
*2 + (uint32_t) frame_size
;
928 if (*prebuf
== (uint32_t) -1 || *prebuf
> *tlength
)
932 static void fix_playback_buffer_attr_post(
940 pa_assert(maxlength
);
945 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
946 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
947 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
948 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
953 static playback_stream
* playback_stream_new(
954 pa_native_connection
*c
,
967 pa_sink_input_flags_t flags
,
969 pa_bool_t adjust_latency
,
970 pa_bool_t early_requests
,
973 playback_stream
*s
, *ssync
;
974 pa_sink_input
*sink_input
= NULL
;
978 pa_sink_input_new_data data
;
982 pa_assert(maxlength
);
990 /* Find syncid group */
991 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
993 if (!playback_stream_isinstance(ssync
))
996 if (ssync
->syncid
== syncid
)
1000 /* Synced streams must connect to the same sink */
1004 sink
= ssync
->sink_input
->sink
;
1005 else if (sink
!= ssync
->sink_input
->sink
) {
1006 *ret
= PA_ERR_INVALID
;
1011 pa_sink_input_new_data_init(&data
);
1013 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
1014 data
.driver
= __FILE__
;
1015 data
.module
= c
->options
->module
;
1016 data
.client
= c
->client
;
1018 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
1019 pa_sink_input_new_data_set_channel_map(&data
, map
);
1021 pa_sink_input_new_data_set_volume(&data
, volume
);
1023 pa_sink_input_new_data_set_muted(&data
, muted
);
1024 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
1026 *ret
= -pa_sink_input_new(&sink_input
, c
->protocol
->core
, &data
, flags
);
1028 pa_sink_input_new_data_done(&data
);
1033 s
= pa_msgobject_new(playback_stream
);
1034 s
->parent
.parent
.parent
.free
= playback_stream_free
;
1035 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
1038 s
->sink_input
= sink_input
;
1039 s
->is_underrun
= TRUE
;
1040 s
->drain_request
= FALSE
;
1041 pa_atomic_store(&s
->missing
, 0);
1043 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
1044 s
->sink_input
->pop
= sink_input_pop_cb
;
1045 s
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1046 s
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1047 s
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1048 s
->sink_input
->kill
= sink_input_kill_cb
;
1049 s
->sink_input
->moved
= sink_input_moved_cb
;
1050 s
->sink_input
->suspend
= sink_input_suspend_cb
;
1051 s
->sink_input
->userdata
= s
;
1053 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
1055 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, tlength
, prebuf
, minreq
);
1056 pa_sink_input_get_silence(sink_input
, &silence
);
1058 s
->memblockq
= pa_memblockq_new(
1062 pa_frame_size(&sink_input
->sample_spec
),
1068 pa_memblock_unref(silence
.memblock
);
1069 fix_playback_buffer_attr_post(s
, maxlength
, tlength
, prebuf
, minreq
);
1071 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
1073 *ss
= s
->sink_input
->sample_spec
;
1074 *map
= s
->sink_input
->channel_map
;
1076 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
1078 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
1079 ((double) pa_bytes_to_usec(*tlength
, &sink_input
->sample_spec
) + (double) s
->sink_latency
) / PA_USEC_PER_MSEC
,
1080 (double) pa_bytes_to_usec(*tlength
-*minreq
*2, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1081 (double) pa_bytes_to_usec(*minreq
, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1082 (double) s
->sink_latency
/ PA_USEC_PER_MSEC
);
1084 pa_sink_input_put(s
->sink_input
);
1088 /* Called from thread context */
1089 static void playback_stream_request_bytes(playback_stream
*s
) {
1090 size_t m
, previous_missing
;
1092 playback_stream_assert_ref(s
);
1094 m
= pa_memblockq_pop_missing(s
->memblockq
);
1099 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
1101 previous_missing
= (size_t) pa_atomic_add(&s
->missing
, (int) m
);
1103 if (pa_memblockq_prebuf_active(s
->memblockq
) ||
1104 (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
))
1105 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1109 static void playback_stream_send_killed(playback_stream
*p
) {
1111 playback_stream_assert_ref(p
);
1113 t
= pa_tagstruct_new(NULL
, 0);
1114 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
1115 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1116 pa_tagstruct_putu32(t
, p
->index
);
1117 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
1120 static int native_connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1121 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1122 pa_native_connection_assert_ref(c
);
1129 case CONNECTION_MESSAGE_REVOKE
:
1130 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1133 case CONNECTION_MESSAGE_RELEASE
:
1134 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1141 static void native_connection_unlink(pa_native_connection
*c
) {
1150 pa_hook_fire(&c
->protocol
->hooks
[PA_NATIVE_HOOK_CONNECTION_UNLINK
], c
);
1153 pa_native_options_unref(c
->options
);
1155 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
1156 record_stream_unlink(r
);
1158 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
1159 if (playback_stream_isinstance(o
))
1160 playback_stream_unlink(PLAYBACK_STREAM(o
));
1162 upload_stream_unlink(UPLOAD_STREAM(o
));
1164 if (c
->subscription
)
1165 pa_subscription_free(c
->subscription
);
1168 pa_pstream_unlink(c
->pstream
);
1170 if (c
->auth_timeout_event
) {
1171 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1172 c
->auth_timeout_event
= NULL
;
1175 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
1177 pa_native_connection_unref(c
);
1180 static void native_connection_free(pa_object
*o
) {
1181 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1185 native_connection_unlink(c
);
1187 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
1188 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
1190 pa_pdispatch_unref(c
->pdispatch
);
1191 pa_pstream_unref(c
->pstream
);
1192 pa_client_free(c
->client
);
1197 static void native_connection_send_memblock(pa_native_connection
*c
) {
1201 start
= PA_IDXSET_INVALID
;
1205 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
1208 if (start
== PA_IDXSET_INVALID
)
1209 start
= c
->rrobin_index
;
1210 else if (start
== c
->rrobin_index
)
1213 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
1214 pa_memchunk schunk
= chunk
;
1216 if (schunk
.length
> r
->fragment_size
)
1217 schunk
.length
= r
->fragment_size
;
1219 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
1221 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
1222 pa_memblock_unref(schunk
.memblock
);
1229 /*** sink input callbacks ***/
1231 static void handle_seek(playback_stream
*s
, int64_t indexw
) {
1232 playback_stream_assert_ref(s
);
1234 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1236 if (s
->sink_input
->thread_info
.underrun_for
> 0) {
1238 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1240 if (pa_memblockq_is_readable(s
->memblockq
)) {
1242 /* We just ended an underrun, let's ask the sink
1243 * for a complete rewind rewrite */
1245 pa_log_debug("Requesting rewind due to end of underrun.");
1246 pa_sink_input_request_rewind(s
->sink_input
,
1247 (size_t) (s
->sink_input
->thread_info
.underrun_for
== (size_t) -1 ? 0 : s
->sink_input
->thread_info
.underrun_for
),
1248 FALSE
, TRUE
, FALSE
);
1254 indexr
= pa_memblockq_get_read_index(s
->memblockq
);
1256 if (indexw
< indexr
) {
1257 /* OK, the sink already asked for this data, so
1258 * let's have it usk us again */
1260 pa_log_debug("Requesting rewind due to rewrite.");
1261 pa_sink_input_request_rewind(s
->sink_input
, (size_t) (indexr
- indexw
), TRUE
, FALSE
, FALSE
);
1265 playback_stream_request_bytes(s
);
1268 /* Called from thread context */
1269 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1270 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1273 pa_sink_input_assert_ref(i
);
1274 s
= PLAYBACK_STREAM(i
->userdata
);
1275 playback_stream_assert_ref(s
);
1279 case SINK_INPUT_MESSAGE_SEEK
: {
1282 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1283 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
1285 handle_seek(s
, windex
);
1289 case SINK_INPUT_MESSAGE_POST_DATA
: {
1294 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1296 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1298 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
1299 pa_log_warn("Failed to push data into queue");
1300 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
1301 pa_memblockq_seek(s
->memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
1304 handle_seek(s
, windex
);
1306 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1311 case SINK_INPUT_MESSAGE_DRAIN
:
1312 case SINK_INPUT_MESSAGE_FLUSH
:
1313 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1314 case SINK_INPUT_MESSAGE_TRIGGER
: {
1317 pa_sink_input
*isync
;
1318 void (*func
)(pa_memblockq
*bq
);
1321 case SINK_INPUT_MESSAGE_FLUSH
:
1322 func
= pa_memblockq_flush_write
;
1325 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1326 func
= pa_memblockq_prebuf_force
;
1329 case SINK_INPUT_MESSAGE_DRAIN
:
1330 case SINK_INPUT_MESSAGE_TRIGGER
:
1331 func
= pa_memblockq_prebuf_disable
;
1335 pa_assert_not_reached();
1338 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1340 handle_seek(s
, windex
);
1342 /* Do the same for all other members in the sync group */
1343 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
1344 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1345 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1346 func(ssync
->memblockq
);
1347 handle_seek(ssync
, windex
);
1350 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1351 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1352 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1353 func(ssync
->memblockq
);
1354 handle_seek(ssync
, windex
);
1357 if (code
== SINK_INPUT_MESSAGE_DRAIN
) {
1358 if (!pa_memblockq_is_readable(s
->memblockq
))
1359 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
1361 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
1362 s
->drain_request
= TRUE
;
1369 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1371 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1372 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1373 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1376 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1379 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1381 pa_memblockq_prebuf_force(s
->memblockq
);
1383 handle_seek(s
, windex
);
1385 /* Fall through to the default handler */
1389 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1390 pa_usec_t
*r
= userdata
;
1392 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1394 /* Fall through, the default handler will add in the extra
1395 * latency added by the resampler */
1400 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1403 /* Called from thread context */
1404 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
1407 pa_sink_input_assert_ref(i
);
1408 s
= PLAYBACK_STREAM(i
->userdata
);
1409 playback_stream_assert_ref(s
);
1412 /* pa_log("%s, pop(): %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1414 if (pa_memblockq_is_readable(s
->memblockq
))
1415 s
->is_underrun
= FALSE
;
1417 /* pa_log("%s, UNDERRUN: %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1419 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1420 s
->drain_request
= FALSE
;
1421 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
);
1422 } else if (!s
->is_underrun
)
1423 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1425 s
->is_underrun
= TRUE
;
1427 playback_stream_request_bytes(s
);
1430 /* This call will not fail with prebuf=0, hence we check for
1431 underrun explicitly above */
1432 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
1435 chunk
->length
= PA_MIN(nbytes
, chunk
->length
);
1437 if (i
->thread_info
.underrun_for
> 0)
1438 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_STARTED
, NULL
, 0, NULL
, NULL
);
1440 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1441 playback_stream_request_bytes(s
);
1446 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1449 pa_sink_input_assert_ref(i
);
1450 s
= PLAYBACK_STREAM(i
->userdata
);
1451 playback_stream_assert_ref(s
);
1453 /* If we are in an underrun, then we don't rewind */
1454 if (i
->thread_info
.underrun_for
> 0)
1457 pa_memblockq_rewind(s
->memblockq
, nbytes
);
1460 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1463 pa_sink_input_assert_ref(i
);
1464 s
= PLAYBACK_STREAM(i
->userdata
);
1465 playback_stream_assert_ref(s
);
1467 pa_memblockq_set_maxrewind(s
->memblockq
, nbytes
);
1470 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
1474 pa_sink_input_assert_ref(i
);
1475 s
= PLAYBACK_STREAM(i
->userdata
);
1476 playback_stream_assert_ref(s
);
1478 tlength
= nbytes
+2*pa_memblockq_get_minreq(s
->memblockq
);
1480 if (pa_memblockq_get_tlength(s
->memblockq
) < tlength
)
1481 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1484 /* Called from main context */
1485 static void sink_input_kill_cb(pa_sink_input
*i
) {
1488 pa_sink_input_assert_ref(i
);
1489 s
= PLAYBACK_STREAM(i
->userdata
);
1490 playback_stream_assert_ref(s
);
1492 playback_stream_send_killed(s
);
1493 playback_stream_unlink(s
);
1496 /* Called from main context */
1497 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1501 pa_sink_input_assert_ref(i
);
1502 s
= PLAYBACK_STREAM(i
->userdata
);
1503 playback_stream_assert_ref(s
);
1505 if (s
->connection
->version
< 12)
1508 t
= pa_tagstruct_new(NULL
, 0);
1509 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1510 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1511 pa_tagstruct_putu32(t
, s
->index
);
1512 pa_tagstruct_put_boolean(t
, suspend
);
1513 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1516 /* Called from main context */
1517 static void sink_input_moved_cb(pa_sink_input
*i
) {
1520 uint32_t maxlength
, tlength
, prebuf
, minreq
;
1522 pa_sink_input_assert_ref(i
);
1523 s
= PLAYBACK_STREAM(i
->userdata
);
1524 playback_stream_assert_ref(s
);
1526 maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
1527 tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
1528 prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
1529 minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
1531 fix_playback_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1532 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1533 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1534 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
1535 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
1536 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1538 if (s
->connection
->version
< 12)
1541 t
= pa_tagstruct_new(NULL
, 0);
1542 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1543 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1544 pa_tagstruct_putu32(t
, s
->index
);
1545 pa_tagstruct_putu32(t
, i
->sink
->index
);
1546 pa_tagstruct_puts(t
, i
->sink
->name
);
1547 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1549 if (s
->connection
->version
>= 13) {
1550 pa_tagstruct_putu32(t
, maxlength
);
1551 pa_tagstruct_putu32(t
, tlength
);
1552 pa_tagstruct_putu32(t
, prebuf
);
1553 pa_tagstruct_putu32(t
, minreq
);
1554 pa_tagstruct_put_usec(t
, s
->sink_latency
);
1557 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1560 /*** source_output callbacks ***/
1562 /* Called from thread context */
1563 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1566 pa_source_output_assert_ref(o
);
1567 s
= RECORD_STREAM(o
->userdata
);
1568 record_stream_assert_ref(s
);
1571 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1574 static void source_output_kill_cb(pa_source_output
*o
) {
1577 pa_source_output_assert_ref(o
);
1578 s
= RECORD_STREAM(o
->userdata
);
1579 record_stream_assert_ref(s
);
1581 record_stream_send_killed(s
);
1582 record_stream_unlink(s
);
1585 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1588 pa_source_output_assert_ref(o
);
1589 s
= RECORD_STREAM(o
->userdata
);
1590 record_stream_assert_ref(s
);
1592 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1594 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1597 /* Called from main context */
1598 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1602 pa_source_output_assert_ref(o
);
1603 s
= RECORD_STREAM(o
->userdata
);
1604 record_stream_assert_ref(s
);
1606 if (s
->connection
->version
< 12)
1609 t
= pa_tagstruct_new(NULL
, 0);
1610 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1611 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1612 pa_tagstruct_putu32(t
, s
->index
);
1613 pa_tagstruct_put_boolean(t
, suspend
);
1614 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1617 /* Called from main context */
1618 static void source_output_moved_cb(pa_source_output
*o
) {
1621 uint32_t maxlength
, fragsize
;
1623 pa_source_output_assert_ref(o
);
1624 s
= RECORD_STREAM(o
->userdata
);
1625 record_stream_assert_ref(s
);
1627 fragsize
= (uint32_t) s
->fragment_size
;
1628 maxlength
= (uint32_t) pa_memblockq_get_length(s
->memblockq
);
1630 fix_record_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &fragsize
);
1631 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1632 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
1634 if (s
->connection
->version
< 12)
1637 t
= pa_tagstruct_new(NULL
, 0);
1638 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1639 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1640 pa_tagstruct_putu32(t
, s
->index
);
1641 pa_tagstruct_putu32(t
, o
->source
->index
);
1642 pa_tagstruct_puts(t
, o
->source
->name
);
1643 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1645 if (s
->connection
->version
>= 13) {
1646 pa_tagstruct_putu32(t
, maxlength
);
1647 pa_tagstruct_putu32(t
, fragsize
);
1648 pa_tagstruct_put_usec(t
, s
->source_latency
);
1651 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1654 /*** pdispatch callbacks ***/
1656 static void protocol_error(pa_native_connection
*c
) {
1657 pa_log("protocol error, kicking client");
1658 native_connection_unlink(c
);
1661 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1662 if (!(expression)) { \
1663 pa_pstream_send_error((pstream), (tag), (error)); \
1668 static pa_tagstruct
*reply_new(uint32_t tag
) {
1669 pa_tagstruct
*reply
;
1671 reply
= pa_tagstruct_new(NULL
, 0);
1672 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1673 pa_tagstruct_putu32(reply
, tag
);
1677 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1678 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1680 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1681 const char *name
= NULL
, *sink_name
;
1684 pa_tagstruct
*reply
;
1685 pa_sink
*sink
= NULL
;
1693 fix_channels
= FALSE
,
1695 variable_rate
= FALSE
,
1697 adjust_latency
= FALSE
,
1698 early_requests
= FALSE
,
1699 dont_inhibit_auto_suspend
= FALSE
,
1701 fail_on_suspend
= FALSE
;
1702 pa_sink_input_flags_t flags
= 0;
1704 pa_bool_t volume_set
= TRUE
;
1705 int ret
= PA_ERR_INVALID
;
1707 pa_native_connection_assert_ref(c
);
1710 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1713 PA_TAG_SAMPLE_SPEC
, &ss
,
1714 PA_TAG_CHANNEL_MAP
, &map
,
1715 PA_TAG_U32
, &sink_index
,
1716 PA_TAG_STRING
, &sink_name
,
1717 PA_TAG_U32
, &maxlength
,
1718 PA_TAG_BOOLEAN
, &corked
,
1719 PA_TAG_U32
, &tlength
,
1720 PA_TAG_U32
, &prebuf
,
1721 PA_TAG_U32
, &minreq
,
1722 PA_TAG_U32
, &syncid
,
1723 PA_TAG_CVOLUME
, &volume
,
1724 PA_TAG_INVALID
) < 0) {
1730 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1731 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
1732 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
1733 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1734 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1735 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1736 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1737 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1739 p
= pa_proplist_new();
1742 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1744 if (c
->version
>= 12) {
1745 /* Since 0.9.8 the user can ask for a couple of additional flags */
1747 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1748 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1749 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1750 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1751 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1752 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1753 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1756 pa_proplist_free(p
);
1761 if (c
->version
>= 13) {
1763 if (pa_tagstruct_get_boolean(t
, &muted
) < 0 ||
1764 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1765 pa_tagstruct_get_proplist(t
, p
) < 0) {
1767 pa_proplist_free(p
);
1772 if (c
->version
>= 14) {
1774 if (pa_tagstruct_get_boolean(t
, &volume_set
) < 0 ||
1775 pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
1777 pa_proplist_free(p
);
1782 if (c
->version
>= 15) {
1784 if (pa_tagstruct_get_boolean(t
, &muted_set
) < 0 ||
1785 pa_tagstruct_get_boolean(t
, &dont_inhibit_auto_suspend
) < 0 ||
1786 pa_tagstruct_get_boolean(t
, &fail_on_suspend
) < 0) {
1788 pa_proplist_free(p
);
1793 if (!pa_tagstruct_eof(t
)) {
1795 pa_proplist_free(p
);
1799 if (sink_index
!= PA_INVALID_INDEX
) {
1801 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1802 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1803 pa_proplist_free(p
);
1807 } else if (sink_name
) {
1809 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
))) {
1810 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1811 pa_proplist_free(p
);
1817 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1818 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1819 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1820 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1821 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1822 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1823 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1824 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0) |
1825 (dont_inhibit_auto_suspend
? PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
: 0) |
1826 (fail_on_suspend
? PA_SINK_INPUT_FAIL_ON_SUSPEND
: 0);
1828 /* Only since protocol version 15 there's a seperate muted_set
1829 * flag. For older versions we synthesize it here */
1830 muted_set
= muted_set
|| muted
;
1832 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
, &ret
);
1833 pa_proplist_free(p
);
1835 CHECK_VALIDITY(c
->pstream
, s
, tag
, ret
);
1837 reply
= reply_new(tag
);
1838 pa_tagstruct_putu32(reply
, s
->index
);
1839 pa_assert(s
->sink_input
);
1840 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1841 pa_tagstruct_putu32(reply
, missing
);
1843 /* pa_log("initial request is %u", missing); */
1845 if (c
->version
>= 9) {
1846 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1848 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1849 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1850 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1851 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1854 if (c
->version
>= 12) {
1855 /* Since 0.9.8 we support sending the chosen sample
1856 * spec/channel map/device/suspend status back to the
1859 pa_tagstruct_put_sample_spec(reply
, &ss
);
1860 pa_tagstruct_put_channel_map(reply
, &map
);
1862 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1863 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1865 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1868 if (c
->version
>= 13)
1869 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
1871 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1874 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1875 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1878 pa_native_connection_assert_ref(c
);
1881 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1882 !pa_tagstruct_eof(t
)) {
1887 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1891 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1893 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1894 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1898 playback_stream_unlink(s
);
1902 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1904 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1905 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1909 record_stream_unlink(s
);
1913 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1916 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1917 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1921 upload_stream_unlink(s
);
1926 pa_assert_not_reached();
1929 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1932 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1933 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1935 uint32_t maxlength
, fragment_size
;
1936 uint32_t source_index
;
1937 const char *name
= NULL
, *source_name
;
1940 pa_tagstruct
*reply
;
1941 pa_source
*source
= NULL
;
1948 fix_channels
= FALSE
,
1950 variable_rate
= FALSE
,
1951 adjust_latency
= FALSE
,
1952 peak_detect
= FALSE
,
1953 early_requests
= FALSE
,
1954 dont_inhibit_auto_suspend
= FALSE
,
1955 fail_on_suspend
= FALSE
;
1956 pa_source_output_flags_t flags
= 0;
1958 uint32_t direct_on_input_idx
= PA_INVALID_INDEX
;
1959 pa_sink_input
*direct_on_input
= NULL
;
1960 int ret
= PA_ERR_INVALID
;
1962 pa_native_connection_assert_ref(c
);
1965 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1966 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1967 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1968 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1969 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1970 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1971 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1972 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1977 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1978 CHECK_VALIDITY(c
->pstream
, !source_name
|| pa_namereg_is_valid_name(source_name
), tag
, PA_ERR_INVALID
);
1979 CHECK_VALIDITY(c
->pstream
, source_index
== PA_INVALID_INDEX
|| !source_name
, tag
, PA_ERR_INVALID
);
1980 CHECK_VALIDITY(c
->pstream
, !source_name
|| source_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1981 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1982 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1983 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1985 p
= pa_proplist_new();
1988 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1990 if (c
->version
>= 12) {
1991 /* Since 0.9.8 the user can ask for a couple of additional flags */
1993 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1994 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1995 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1996 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1997 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1998 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1999 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
2002 pa_proplist_free(p
);
2007 if (c
->version
>= 13) {
2009 if (pa_tagstruct_get_boolean(t
, &peak_detect
) < 0 ||
2010 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
2011 pa_tagstruct_get_proplist(t
, p
) < 0 ||
2012 pa_tagstruct_getu32(t
, &direct_on_input_idx
) < 0) {
2014 pa_proplist_free(p
);
2019 if (c
->version
>= 14) {
2021 if (pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
2023 pa_proplist_free(p
);
2028 if (c
->version
>= 15) {
2030 if (pa_tagstruct_get_boolean(t
, &dont_inhibit_auto_suspend
) < 0 ||
2031 pa_tagstruct_get_boolean(t
, &fail_on_suspend
) < 0) {
2033 pa_proplist_free(p
);
2038 if (!pa_tagstruct_eof(t
)) {
2040 pa_proplist_free(p
);
2044 if (source_index
!= PA_INVALID_INDEX
) {
2046 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
2047 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2048 pa_proplist_free(p
);
2052 } else if (source_name
) {
2054 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
))) {
2055 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2056 pa_proplist_free(p
);
2061 if (direct_on_input_idx
!= PA_INVALID_INDEX
) {
2063 if (!(direct_on_input
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, direct_on_input_idx
))) {
2064 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2065 pa_proplist_free(p
);
2071 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
2072 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
2073 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
2074 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
2075 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
2076 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
2077 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
2078 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0) |
2079 (dont_inhibit_auto_suspend
? PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND
: 0) |
2080 (fail_on_suspend
? PA_SOURCE_OUTPUT_FAIL_ON_SUSPEND
: 0);
2082 s
= record_stream_new(c
, source
, &ss
, &map
, peak_detect
, &maxlength
, &fragment_size
, flags
, p
, adjust_latency
, direct_on_input
, early_requests
, &ret
);
2083 pa_proplist_free(p
);
2085 CHECK_VALIDITY(c
->pstream
, s
, tag
, ret
);
2087 reply
= reply_new(tag
);
2088 pa_tagstruct_putu32(reply
, s
->index
);
2089 pa_assert(s
->source_output
);
2090 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
2092 if (c
->version
>= 9) {
2093 /* Since 0.9 we support sending the buffer metrics back to the client */
2095 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
2096 pa_tagstruct_putu32(reply
, (uint32_t) fragment_size
);
2099 if (c
->version
>= 12) {
2100 /* Since 0.9.8 we support sending the chosen sample
2101 * spec/channel map/device/suspend status back to the
2104 pa_tagstruct_put_sample_spec(reply
, &ss
);
2105 pa_tagstruct_put_channel_map(reply
, &map
);
2107 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
2108 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
2110 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
2113 if (c
->version
>= 13)
2114 pa_tagstruct_put_usec(reply
, s
->source_latency
);
2116 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2119 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2120 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2123 pa_native_connection_assert_ref(c
);
2126 if (!pa_tagstruct_eof(t
)) {
2131 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2132 ret
= pa_core_exit(c
->protocol
->core
, FALSE
, 0);
2133 CHECK_VALIDITY(c
->pstream
, ret
>= 0, tag
, PA_ERR_ACCESS
);
2135 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
2138 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2139 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2141 pa_tagstruct
*reply
;
2142 pa_bool_t shm_on_remote
= FALSE
, do_shm
;
2144 pa_native_connection_assert_ref(c
);
2147 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
2148 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
2149 !pa_tagstruct_eof(t
)) {
2154 /* Minimum supported version */
2155 if (c
->version
< 8) {
2156 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
2160 /* Starting with protocol version 13 the MSB of the version tag
2161 reflects if shm is available for this pa_native_connection or
2163 if (c
->version
>= 13) {
2164 shm_on_remote
= !!(c
->version
& 0x80000000U
);
2165 c
->version
&= 0x7FFFFFFFU
;
2168 pa_log_debug("Protocol version: remote %u, local %u", c
->version
, PA_PROTOCOL_VERSION
);
2170 pa_proplist_setf(c
->client
->proplist
, "native-protocol.version", "%u", c
->version
);
2172 if (!c
->authorized
) {
2173 pa_bool_t success
= FALSE
;
2176 const pa_creds
*creds
;
2178 if ((creds
= pa_pdispatch_creds(pd
))) {
2179 if (creds
->uid
== getuid())
2181 else if (c
->options
->auth_group
) {
2185 if ((gid
= pa_get_gid_of_group(c
->options
->auth_group
)) == (gid_t
) -1)
2186 pa_log_warn("Failed to get GID of group '%s'", c
->options
->auth_group
);
2187 else if (gid
== creds
->gid
)
2191 if ((r
= pa_uid_in_group(creds
->uid
, c
->options
->auth_group
)) < 0)
2192 pa_log_warn("Failed to check group membership.");
2198 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
2199 (unsigned long) creds
->uid
,
2200 (unsigned long) creds
->gid
,
2205 if (!success
&& c
->options
->auth_cookie
) {
2208 if ((ac
= pa_auth_cookie_read(c
->options
->auth_cookie
, PA_NATIVE_COOKIE_LENGTH
)))
2209 if (memcmp(ac
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
2214 pa_log_warn("Denied access to client with invalid authorization data.");
2215 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2219 c
->authorized
= TRUE
;
2220 if (c
->auth_timeout_event
) {
2221 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
2222 c
->auth_timeout_event
= NULL
;
2226 /* Enable shared memory support if possible */
2228 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
2231 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm
));
2234 if (c
->version
< 10 || (c
->version
>= 13 && !shm_on_remote
))
2239 /* Only enable SHM if both sides are owned by the same
2240 * user. This is a security measure because otherwise data
2241 * private to the user might leak. */
2243 const pa_creds
*creds
;
2244 if (!(creds
= pa_pdispatch_creds(pd
)) || getuid() != creds
->uid
)
2249 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm
));
2250 pa_pstream_enable_shm(c
->pstream
, do_shm
);
2252 reply
= reply_new(tag
);
2253 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
| (do_shm
? 0x80000000 : 0));
2257 /* SHM support is only enabled after both sides made sure they are the same user. */
2261 ucred
.uid
= getuid();
2262 ucred
.gid
= getgid();
2264 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
2267 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2271 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2272 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2273 const char *name
= NULL
;
2275 pa_tagstruct
*reply
;
2277 pa_native_connection_assert_ref(c
);
2280 p
= pa_proplist_new();
2282 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
2283 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2284 !pa_tagstruct_eof(t
)) {
2287 pa_proplist_free(p
);
2292 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
2293 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2294 pa_proplist_free(p
);
2298 pa_client_update_proplist(c
->client
, PA_UPDATE_REPLACE
, p
);
2299 pa_proplist_free(p
);
2301 reply
= reply_new(tag
);
2303 if (c
->version
>= 13)
2304 pa_tagstruct_putu32(reply
, c
->client
->index
);
2306 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2309 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2310 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2312 uint32_t idx
= PA_IDXSET_INVALID
;
2314 pa_native_connection_assert_ref(c
);
2317 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2318 !pa_tagstruct_eof(t
)) {
2323 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2324 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2326 if (command
== PA_COMMAND_LOOKUP_SINK
) {
2328 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
)))
2332 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
2333 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
)))
2334 idx
= source
->index
;
2337 if (idx
== PA_IDXSET_INVALID
)
2338 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2340 pa_tagstruct
*reply
;
2341 reply
= reply_new(tag
);
2342 pa_tagstruct_putu32(reply
, idx
);
2343 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2347 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2348 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2352 pa_native_connection_assert_ref(c
);
2355 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2356 !pa_tagstruct_eof(t
)) {
2361 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2362 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2363 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2364 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2366 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
);
2369 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2370 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2371 pa_tagstruct
*reply
;
2372 const pa_mempool_stat
*stat
;
2374 pa_native_connection_assert_ref(c
);
2377 if (!pa_tagstruct_eof(t
)) {
2382 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2384 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
2386 reply
= reply_new(tag
);
2387 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
2388 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
2389 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
2390 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
2391 pa_tagstruct_putu32(reply
, (uint32_t) pa_scache_total_size(c
->protocol
->core
));
2392 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2395 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2396 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2397 pa_tagstruct
*reply
;
2399 struct timeval tv
, now
;
2403 pa_native_connection_assert_ref(c
);
2406 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2407 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2408 !pa_tagstruct_eof(t
)) {
2413 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2414 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2415 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2416 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2417 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
)
2419 reply
= reply_new(tag
);
2421 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
2422 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
2424 pa_tagstruct_put_usec(reply
, latency
);
2426 pa_tagstruct_put_usec(reply
, 0);
2427 pa_tagstruct_put_boolean(reply
, s
->sink_input
->thread_info
.playing_for
> 0);
2428 pa_tagstruct_put_timeval(reply
, &tv
);
2429 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2430 pa_tagstruct_puts64(reply
, s
->write_index
);
2431 pa_tagstruct_puts64(reply
, s
->read_index
);
2433 if (c
->version
>= 13) {
2434 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.underrun_for
);
2435 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.playing_for
);
2438 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2441 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2442 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2443 pa_tagstruct
*reply
;
2445 struct timeval tv
, now
;
2448 pa_native_connection_assert_ref(c
);
2451 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2452 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2453 !pa_tagstruct_eof(t
)) {
2458 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2459 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2460 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2462 reply
= reply_new(tag
);
2463 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
2464 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
2465 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_RUNNING
);
2466 pa_tagstruct_put_timeval(reply
, &tv
);
2467 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2468 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
2469 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
2470 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2473 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2474 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2477 const char *name
= NULL
;
2480 pa_tagstruct
*reply
;
2483 pa_native_connection_assert_ref(c
);
2486 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2487 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
2488 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
2489 pa_tagstruct_getu32(t
, &length
) < 0) {
2494 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2495 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
2496 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
2497 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
2498 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
2499 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
2501 p
= pa_proplist_new();
2503 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2504 !pa_tagstruct_eof(t
)) {
2507 pa_proplist_free(p
);
2511 if (c
->version
< 13)
2512 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2514 if (!(name
= pa_proplist_gets(p
, PA_PROP_EVENT_ID
)))
2515 name
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
);
2517 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2519 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
2520 pa_proplist_free(p
);
2522 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2524 reply
= reply_new(tag
);
2525 pa_tagstruct_putu32(reply
, s
->index
);
2526 pa_tagstruct_putu32(reply
, length
);
2527 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2530 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2531 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2536 pa_native_connection_assert_ref(c
);
2539 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
2540 !pa_tagstruct_eof(t
)) {
2545 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2547 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
2548 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2549 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2551 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
2552 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
2554 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2556 upload_stream_unlink(s
);
2559 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2560 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2561 uint32_t sink_index
;
2564 const char *name
, *sink_name
;
2567 pa_tagstruct
*reply
;
2569 pa_native_connection_assert_ref(c
);
2572 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2574 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
2575 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
2576 pa_tagstruct_getu32(t
, &volume
) < 0 ||
2577 pa_tagstruct_gets(t
, &name
) < 0) {
2582 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
2583 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
2584 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2585 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2587 if (sink_index
!= PA_INVALID_INDEX
)
2588 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
2590 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
);
2592 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2594 p
= pa_proplist_new();
2596 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2597 !pa_tagstruct_eof(t
)) {
2599 pa_proplist_free(p
);
2603 pa_proplist_update(p
, PA_UPDATE_MERGE
, c
->client
->proplist
);
2605 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2606 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2607 pa_proplist_free(p
);
2611 pa_proplist_free(p
);
2613 reply
= reply_new(tag
);
2615 if (c
->version
>= 13)
2616 pa_tagstruct_putu32(reply
, idx
);
2618 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2621 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2622 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2625 pa_native_connection_assert_ref(c
);
2628 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2629 !pa_tagstruct_eof(t
)) {
2634 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2635 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2637 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2638 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2642 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2645 static void fixup_sample_spec(pa_native_connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2648 pa_assert(original
);
2652 if (c
->version
< 12) {
2653 /* Before protocol version 12 we didn't support S32 samples,
2654 * so we need to lie about this to the client */
2656 if (fixed
->format
== PA_SAMPLE_S32LE
)
2657 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2658 if (fixed
->format
== PA_SAMPLE_S32BE
)
2659 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2662 if (c
->version
< 15) {
2663 if (fixed
->format
== PA_SAMPLE_S24LE
|| fixed
->format
== PA_SAMPLE_S24_32LE
)
2664 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2665 if (fixed
->format
== PA_SAMPLE_S24BE
|| fixed
->format
== PA_SAMPLE_S24_32BE
)
2666 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2670 static void sink_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2671 pa_sample_spec fixed_ss
;
2674 pa_sink_assert_ref(sink
);
2676 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2680 PA_TAG_U32
, sink
->index
,
2681 PA_TAG_STRING
, sink
->name
,
2682 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2683 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2684 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2685 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2686 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, FALSE
),
2687 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
, FALSE
),
2688 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2689 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2690 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2691 PA_TAG_STRING
, sink
->driver
,
2692 PA_TAG_U32
, sink
->flags
,
2695 if (c
->version
>= 13) {
2696 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2697 pa_tagstruct_put_usec(t
, pa_sink_get_requested_latency(sink
));
2700 if (c
->version
>= 15) {
2701 pa_tagstruct_put_volume(t
, sink
->base_volume
);
2702 if (PA_UNLIKELY(pa_sink_get_state(sink
) == PA_SINK_INVALID_STATE
))
2703 pa_log_error("Internal sink state is invalid.");
2704 pa_tagstruct_putu32(t
, pa_sink_get_state(sink
));
2705 pa_tagstruct_putu32(t
, sink
->n_volume_steps
);
2709 static void source_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2710 pa_sample_spec fixed_ss
;
2713 pa_source_assert_ref(source
);
2715 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2719 PA_TAG_U32
, source
->index
,
2720 PA_TAG_STRING
, source
->name
,
2721 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2722 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2723 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2724 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2725 PA_TAG_CVOLUME
, pa_source_get_volume(source
, FALSE
),
2726 PA_TAG_BOOLEAN
, pa_source_get_mute(source
, FALSE
),
2727 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2728 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2729 PA_TAG_USEC
, pa_source_get_latency(source
),
2730 PA_TAG_STRING
, source
->driver
,
2731 PA_TAG_U32
, source
->flags
,
2734 if (c
->version
>= 13) {
2735 pa_tagstruct_put_proplist(t
, source
->proplist
);
2736 pa_tagstruct_put_usec(t
, pa_source_get_requested_latency(source
));
2739 if (c
->version
>= 15) {
2740 pa_tagstruct_put_volume(t
, source
->base_volume
);
2741 if (PA_UNLIKELY(pa_source_get_state(source
) == PA_SOURCE_INVALID_STATE
))
2742 pa_log_error("Internal source state is invalid.");
2743 pa_tagstruct_putu32(t
, pa_source_get_state(source
));
2744 pa_tagstruct_putu32(t
, source
->n_volume_steps
);
2748 static void client_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2752 pa_tagstruct_putu32(t
, client
->index
);
2753 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2754 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2755 pa_tagstruct_puts(t
, client
->driver
);
2757 if (c
->version
>= 13)
2758 pa_tagstruct_put_proplist(t
, client
->proplist
);
2761 static void card_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_card
*card
) {
2768 pa_tagstruct_putu32(t
, card
->index
);
2769 pa_tagstruct_puts(t
, card
->name
);
2770 pa_tagstruct_putu32(t
, card
->module
? card
->module
->index
: PA_INVALID_INDEX
);
2771 pa_tagstruct_puts(t
, card
->driver
);
2773 pa_tagstruct_putu32(t
, card
->profiles
? pa_hashmap_size(card
->profiles
) : 0);
2775 if (card
->profiles
) {
2776 while ((p
= pa_hashmap_iterate(card
->profiles
, &state
, NULL
))) {
2777 pa_tagstruct_puts(t
, p
->name
);
2778 pa_tagstruct_puts(t
, p
->description
);
2782 pa_tagstruct_puts(t
, card
->active_profile
? card
->active_profile
->name
: NULL
);
2783 pa_tagstruct_put_proplist(t
, card
->proplist
);
2786 static void module_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_module
*module
) {
2790 pa_tagstruct_putu32(t
, module
->index
);
2791 pa_tagstruct_puts(t
, module
->name
);
2792 pa_tagstruct_puts(t
, module
->argument
);
2793 pa_tagstruct_putu32(t
, (uint32_t) pa_module_get_n_used(module
));
2795 if (c
->version
< 15)
2796 pa_tagstruct_put_boolean(t
, FALSE
); /* autoload is obsolete */
2798 if (c
->version
>= 15)
2799 pa_tagstruct_put_proplist(t
, module
->proplist
);
2802 static void sink_input_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2803 pa_sample_spec fixed_ss
;
2804 pa_usec_t sink_latency
;
2807 pa_sink_input_assert_ref(s
);
2809 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2811 pa_tagstruct_putu32(t
, s
->index
);
2812 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2813 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2814 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2815 pa_tagstruct_putu32(t
, s
->sink
->index
);
2816 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2817 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2818 pa_tagstruct_put_cvolume(t
, pa_sink_input_get_volume(s
));
2819 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
, &sink_latency
));
2820 pa_tagstruct_put_usec(t
, sink_latency
);
2821 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2822 pa_tagstruct_puts(t
, s
->driver
);
2823 if (c
->version
>= 11)
2824 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2825 if (c
->version
>= 13)
2826 pa_tagstruct_put_proplist(t
, s
->proplist
);
2829 static void source_output_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2830 pa_sample_spec fixed_ss
;
2831 pa_usec_t source_latency
;
2834 pa_source_output_assert_ref(s
);
2836 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2838 pa_tagstruct_putu32(t
, s
->index
);
2839 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2840 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2841 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2842 pa_tagstruct_putu32(t
, s
->source
->index
);
2843 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2844 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2845 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
, &source_latency
));
2846 pa_tagstruct_put_usec(t
, source_latency
);
2847 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2848 pa_tagstruct_puts(t
, s
->driver
);
2850 if (c
->version
>= 13)
2851 pa_tagstruct_put_proplist(t
, s
->proplist
);
2854 static void scache_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2855 pa_sample_spec fixed_ss
;
2861 if (e
->memchunk
.memblock
)
2862 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2864 memset(&fixed_ss
, 0, sizeof(fixed_ss
));
2866 pa_tagstruct_putu32(t
, e
->index
);
2867 pa_tagstruct_puts(t
, e
->name
);
2869 if (e
->volume_is_set
)
2872 pa_cvolume_init(&v
);
2874 pa_tagstruct_put_cvolume(t
, &v
);
2875 pa_tagstruct_put_usec(t
, e
->memchunk
.memblock
? pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
) : 0);
2876 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2877 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2878 pa_tagstruct_putu32(t
, (uint32_t) e
->memchunk
.length
);
2879 pa_tagstruct_put_boolean(t
, e
->lazy
);
2880 pa_tagstruct_puts(t
, e
->filename
);
2882 if (c
->version
>= 13)
2883 pa_tagstruct_put_proplist(t
, e
->proplist
);
2886 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2887 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2889 pa_sink
*sink
= NULL
;
2890 pa_source
*source
= NULL
;
2891 pa_client
*client
= NULL
;
2892 pa_card
*card
= NULL
;
2893 pa_module
*module
= NULL
;
2894 pa_sink_input
*si
= NULL
;
2895 pa_source_output
*so
= NULL
;
2896 pa_scache_entry
*sce
= NULL
;
2897 const char *name
= NULL
;
2898 pa_tagstruct
*reply
;
2900 pa_native_connection_assert_ref(c
);
2903 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2904 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2905 command
!= PA_COMMAND_GET_CARD_INFO
&&
2906 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2907 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2908 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2909 pa_tagstruct_gets(t
, &name
) < 0) ||
2910 !pa_tagstruct_eof(t
)) {
2915 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2916 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2917 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2918 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2919 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2921 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2922 if (idx
!= PA_INVALID_INDEX
)
2923 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2925 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
2926 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2927 if (idx
!= PA_INVALID_INDEX
)
2928 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2930 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
2931 } else if (command
== PA_COMMAND_GET_CARD_INFO
) {
2932 if (idx
!= PA_INVALID_INDEX
)
2933 card
= pa_idxset_get_by_index(c
->protocol
->core
->cards
, idx
);
2935 card
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_CARD
);
2936 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2937 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2938 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2939 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2940 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2941 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2942 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2943 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2945 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2946 if (idx
!= PA_INVALID_INDEX
)
2947 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2949 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
);
2952 if (!sink
&& !source
&& !client
&& !card
&& !module
&& !si
&& !so
&& !sce
) {
2953 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2957 reply
= reply_new(tag
);
2959 sink_fill_tagstruct(c
, reply
, sink
);
2961 source_fill_tagstruct(c
, reply
, source
);
2963 client_fill_tagstruct(c
, reply
, client
);
2965 card_fill_tagstruct(c
, reply
, card
);
2967 module_fill_tagstruct(c
, reply
, module
);
2969 sink_input_fill_tagstruct(c
, reply
, si
);
2971 source_output_fill_tagstruct(c
, reply
, so
);
2973 scache_fill_tagstruct(c
, reply
, sce
);
2974 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2977 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2978 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2982 pa_tagstruct
*reply
;
2984 pa_native_connection_assert_ref(c
);
2987 if (!pa_tagstruct_eof(t
)) {
2992 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2994 reply
= reply_new(tag
);
2996 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2997 i
= c
->protocol
->core
->sinks
;
2998 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2999 i
= c
->protocol
->core
->sources
;
3000 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
3001 i
= c
->protocol
->core
->clients
;
3002 else if (command
== PA_COMMAND_GET_CARD_INFO_LIST
)
3003 i
= c
->protocol
->core
->cards
;
3004 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
3005 i
= c
->protocol
->core
->modules
;
3006 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
3007 i
= c
->protocol
->core
->sink_inputs
;
3008 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
3009 i
= c
->protocol
->core
->source_outputs
;
3011 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
3012 i
= c
->protocol
->core
->scache
;
3016 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
3017 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
3018 sink_fill_tagstruct(c
, reply
, p
);
3019 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
3020 source_fill_tagstruct(c
, reply
, p
);
3021 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
3022 client_fill_tagstruct(c
, reply
, p
);
3023 else if (command
== PA_COMMAND_GET_CARD_INFO_LIST
)
3024 card_fill_tagstruct(c
, reply
, p
);
3025 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
3026 module_fill_tagstruct(c
, reply
, p
);
3027 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
3028 sink_input_fill_tagstruct(c
, reply
, p
);
3029 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
3030 source_output_fill_tagstruct(c
, reply
, p
);
3032 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
3033 scache_fill_tagstruct(c
, reply
, p
);
3038 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3041 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3042 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3043 pa_tagstruct
*reply
;
3046 pa_source
*def_source
;
3047 pa_sample_spec fixed_ss
;
3049 pa_native_connection_assert_ref(c
);
3052 if (!pa_tagstruct_eof(t
)) {
3057 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3059 reply
= reply_new(tag
);
3060 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
3061 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
3062 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
3063 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
3065 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
3066 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
3068 def_sink
= pa_namereg_get_default_sink(c
->protocol
->core
);
3069 pa_tagstruct_puts(reply
, def_sink
? def_sink
->name
: NULL
);
3070 def_source
= pa_namereg_get_default_source(c
->protocol
->core
);
3071 pa_tagstruct_puts(reply
, def_source
? def_source
->name
: NULL
);
3073 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
3075 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3078 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
3080 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3082 pa_native_connection_assert_ref(c
);
3084 t
= pa_tagstruct_new(NULL
, 0);
3085 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
3086 pa_tagstruct_putu32(t
, (uint32_t) -1);
3087 pa_tagstruct_putu32(t
, e
);
3088 pa_tagstruct_putu32(t
, idx
);
3089 pa_pstream_send_tagstruct(c
->pstream
, t
);
3092 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3093 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3094 pa_subscription_mask_t m
;
3096 pa_native_connection_assert_ref(c
);
3099 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
3100 !pa_tagstruct_eof(t
)) {
3105 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3106 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
3108 if (c
->subscription
)
3109 pa_subscription_free(c
->subscription
);
3112 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
3113 pa_assert(c
->subscription
);
3115 c
->subscription
= NULL
;
3117 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3120 static void command_set_volume(
3127 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3130 pa_sink
*sink
= NULL
;
3131 pa_source
*source
= NULL
;
3132 pa_sink_input
*si
= NULL
;
3133 const char *name
= NULL
;
3135 pa_native_connection_assert_ref(c
);
3138 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3139 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3140 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3141 pa_tagstruct_get_cvolume(t
, &volume
) ||
3142 !pa_tagstruct_eof(t
)) {
3147 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3148 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3149 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3150 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3151 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3152 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
3156 case PA_COMMAND_SET_SINK_VOLUME
:
3157 if (idx
!= PA_INVALID_INDEX
)
3158 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3160 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3163 case PA_COMMAND_SET_SOURCE_VOLUME
:
3164 if (idx
!= PA_INVALID_INDEX
)
3165 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3167 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3170 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
3171 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3175 pa_assert_not_reached();
3178 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3181 pa_sink_set_volume(sink
, &volume
, TRUE
, TRUE
);
3183 pa_source_set_volume(source
, &volume
);
3185 pa_sink_input_set_volume(si
, &volume
, TRUE
);
3187 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3190 static void command_set_mute(
3197 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3200 pa_sink
*sink
= NULL
;
3201 pa_source
*source
= NULL
;
3202 pa_sink_input
*si
= NULL
;
3203 const char *name
= NULL
;
3205 pa_native_connection_assert_ref(c
);
3208 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3209 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3210 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3211 pa_tagstruct_get_boolean(t
, &mute
) ||
3212 !pa_tagstruct_eof(t
)) {
3217 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3218 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3219 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3220 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3221 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3225 case PA_COMMAND_SET_SINK_MUTE
:
3227 if (idx
!= PA_INVALID_INDEX
)
3228 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3230 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3234 case PA_COMMAND_SET_SOURCE_MUTE
:
3235 if (idx
!= PA_INVALID_INDEX
)
3236 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3238 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3242 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
3243 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3247 pa_assert_not_reached();
3250 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3253 pa_sink_set_mute(sink
, mute
);
3255 pa_source_set_mute(source
, mute
);
3257 pa_sink_input_set_mute(si
, mute
, TRUE
);
3259 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3262 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3263 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3268 pa_native_connection_assert_ref(c
);
3271 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3272 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3273 !pa_tagstruct_eof(t
)) {
3278 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3279 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3280 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3281 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3282 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3284 pa_sink_input_cork(s
->sink_input
, b
);
3287 s
->is_underrun
= TRUE
;
3289 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3292 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3293 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3297 pa_native_connection_assert_ref(c
);
3300 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3301 !pa_tagstruct_eof(t
)) {
3306 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3307 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3308 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3309 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3310 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3313 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3314 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3317 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3318 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3321 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3322 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3326 pa_assert_not_reached();
3329 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3332 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3333 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3338 pa_native_connection_assert_ref(c
);
3341 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3342 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3343 !pa_tagstruct_eof(t
)) {
3348 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3349 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3350 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3352 pa_source_output_cork(s
->source_output
, b
);
3353 pa_memblockq_prebuf_force(s
->memblockq
);
3354 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3357 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3358 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3362 pa_native_connection_assert_ref(c
);
3365 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3366 !pa_tagstruct_eof(t
)) {
3371 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3372 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3373 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3375 pa_memblockq_flush_read(s
->memblockq
);
3376 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3379 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3380 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3382 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3383 pa_tagstruct
*reply
;
3385 pa_native_connection_assert_ref(c
);
3388 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3393 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3395 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3397 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3399 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3400 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3401 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3403 if (pa_tagstruct_get(
3405 PA_TAG_U32
, &maxlength
,
3406 PA_TAG_U32
, &tlength
,
3407 PA_TAG_U32
, &prebuf
,
3408 PA_TAG_U32
, &minreq
,
3409 PA_TAG_INVALID
) < 0 ||
3410 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3411 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3412 !pa_tagstruct_eof(t
)) {
3417 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3418 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3419 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3420 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3421 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3422 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3424 reply
= reply_new(tag
);
3425 pa_tagstruct_putu32(reply
, maxlength
);
3426 pa_tagstruct_putu32(reply
, tlength
);
3427 pa_tagstruct_putu32(reply
, prebuf
);
3428 pa_tagstruct_putu32(reply
, minreq
);
3430 if (c
->version
>= 13)
3431 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3435 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3436 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3438 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3439 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3441 if (pa_tagstruct_get(
3443 PA_TAG_U32
, &maxlength
,
3444 PA_TAG_U32
, &fragsize
,
3445 PA_TAG_INVALID
) < 0 ||
3446 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3447 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3448 !pa_tagstruct_eof(t
)) {
3453 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &fragsize
);
3454 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3455 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3457 reply
= reply_new(tag
);
3458 pa_tagstruct_putu32(reply
, maxlength
);
3459 pa_tagstruct_putu32(reply
, fragsize
);
3461 if (c
->version
>= 13)
3462 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3465 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3468 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3469 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3473 pa_native_connection_assert_ref(c
);
3476 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3477 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3478 !pa_tagstruct_eof(t
)) {
3483 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3484 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3486 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3489 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3490 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3491 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3493 pa_sink_input_set_rate(s
->sink_input
, rate
);
3497 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3499 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3500 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3502 pa_source_output_set_rate(s
->source_output
, rate
);
3505 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3508 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3509 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3514 pa_native_connection_assert_ref(c
);
3517 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3519 p
= pa_proplist_new();
3521 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3523 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3524 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3525 !pa_tagstruct_eof(t
)) {
3527 pa_proplist_free(p
);
3533 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3534 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3535 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3536 !pa_tagstruct_eof(t
)) {
3538 pa_proplist_free(p
);
3543 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3545 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3548 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3549 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3550 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3552 pa_sink_input_update_proplist(s
->sink_input
, mode
, p
);
3554 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3557 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3558 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3560 pa_source_output_update_proplist(s
->source_output
, mode
, p
);
3562 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3564 pa_client_update_proplist(c
->client
, mode
, p
);
3567 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3570 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3571 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3573 unsigned changed
= 0;
3575 pa_strlist
*l
= NULL
;
3577 pa_native_connection_assert_ref(c
);
3580 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3582 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3584 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3590 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3593 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3594 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3595 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3597 p
= s
->sink_input
->proplist
;
3599 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3602 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3603 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3605 p
= s
->source_output
->proplist
;
3607 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3609 p
= c
->client
->proplist
;
3615 if (pa_tagstruct_gets(t
, &k
) < 0) {
3624 l
= pa_strlist_prepend(l
, k
);
3627 if (!pa_tagstruct_eof(t
)) {
3636 l
= pa_strlist_pop(l
, &z
);
3641 changed
+= (unsigned) (pa_proplist_unset(p
, z
) >= 0);
3645 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3648 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3651 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3652 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3654 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3657 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3658 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3661 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3662 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3667 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3668 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3671 pa_native_connection_assert_ref(c
);
3674 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3675 !pa_tagstruct_eof(t
)) {
3680 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3681 CHECK_VALIDITY(c
->pstream
, !s
|| pa_namereg_is_valid_name(s
), tag
, PA_ERR_INVALID
);
3683 if (command
== PA_COMMAND_SET_DEFAULT_SOURCE
) {
3686 source
= pa_namereg_get(c
->protocol
->core
, s
, PA_NAMEREG_SOURCE
);
3687 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3689 pa_namereg_set_default_source(c
->protocol
->core
, source
);
3692 pa_assert(command
== PA_COMMAND_SET_DEFAULT_SINK
);
3694 sink
= pa_namereg_get(c
->protocol
->core
, s
, PA_NAMEREG_SINK
);
3695 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3697 pa_namereg_set_default_sink(c
->protocol
->core
, sink
);
3700 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3703 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3704 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3708 pa_native_connection_assert_ref(c
);
3711 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3712 pa_tagstruct_gets(t
, &name
) < 0 ||
3713 !pa_tagstruct_eof(t
)) {
3718 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3719 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3721 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3724 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3725 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3726 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3728 pa_sink_input_set_name(s
->sink_input
, name
);
3732 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3734 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3735 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3737 pa_source_output_set_name(s
->source_output
, name
);
3740 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3743 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3744 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3747 pa_native_connection_assert_ref(c
);
3750 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3751 !pa_tagstruct_eof(t
)) {
3756 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3758 if (command
== PA_COMMAND_KILL_CLIENT
) {
3761 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3762 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3764 pa_native_connection_ref(c
);
3765 pa_client_kill(client
);
3767 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3770 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3771 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3773 pa_native_connection_ref(c
);
3774 pa_sink_input_kill(s
);
3776 pa_source_output
*s
;
3778 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3780 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3781 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3783 pa_native_connection_ref(c
);
3784 pa_source_output_kill(s
);
3787 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3788 pa_native_connection_unref(c
);
3791 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3792 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3794 const char *name
, *argument
;
3795 pa_tagstruct
*reply
;
3797 pa_native_connection_assert_ref(c
);
3800 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3801 pa_tagstruct_gets(t
, &argument
) < 0 ||
3802 !pa_tagstruct_eof(t
)) {
3807 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3808 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3809 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3811 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3812 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3816 reply
= reply_new(tag
);
3817 pa_tagstruct_putu32(reply
, m
->index
);
3818 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3821 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3822 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3826 pa_native_connection_assert_ref(c
);
3829 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3830 !pa_tagstruct_eof(t
)) {
3835 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3836 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3837 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3839 pa_module_unload_request(m
, FALSE
);
3840 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3843 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3844 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3845 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3846 const char *name_device
= NULL
;
3848 pa_native_connection_assert_ref(c
);
3851 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3852 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3853 pa_tagstruct_gets(t
, &name_device
) < 0 ||
3854 !pa_tagstruct_eof(t
)) {
3859 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3860 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3862 CHECK_VALIDITY(c
->pstream
, !name_device
|| pa_namereg_is_valid_name(name_device
), tag
, PA_ERR_INVALID
);
3863 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| name_device
, tag
, PA_ERR_INVALID
);
3864 CHECK_VALIDITY(c
->pstream
, idx_device
== PA_INVALID_INDEX
|| !name_device
, tag
, PA_ERR_INVALID
);
3865 CHECK_VALIDITY(c
->pstream
, !name_device
|| idx_device
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3867 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3868 pa_sink_input
*si
= NULL
;
3869 pa_sink
*sink
= NULL
;
3871 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3873 if (idx_device
!= PA_INVALID_INDEX
)
3874 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3876 sink
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SINK
);
3878 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3880 if (pa_sink_input_move_to(si
, sink
, TRUE
) < 0) {
3881 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3885 pa_source_output
*so
= NULL
;
3888 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3890 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3892 if (idx_device
!= PA_INVALID_INDEX
)
3893 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3895 source
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SOURCE
);
3897 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3899 if (pa_source_output_move_to(so
, source
, TRUE
) < 0) {
3900 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3905 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3908 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3909 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3910 uint32_t idx
= PA_INVALID_INDEX
;
3911 const char *name
= NULL
;
3914 pa_native_connection_assert_ref(c
);
3917 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3918 pa_tagstruct_gets(t
, &name
) < 0 ||
3919 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3920 !pa_tagstruct_eof(t
)) {
3925 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3926 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
) || *name
== 0, tag
, PA_ERR_INVALID
);
3927 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3928 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3929 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3931 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3933 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3935 pa_log_debug("%s all sinks", b
? "Suspending" : "Resuming");
3937 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3938 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3942 pa_sink
*sink
= NULL
;
3944 if (idx
!= PA_INVALID_INDEX
)
3945 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3947 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3949 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3951 if (pa_sink_suspend(sink
, b
) < 0) {
3952 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3958 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3960 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3962 pa_log_debug("%s all sources", b
? "Suspending" : "Resuming");
3964 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3965 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3972 if (idx
!= PA_INVALID_INDEX
)
3973 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3975 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3977 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3979 if (pa_source_suspend(source
, b
) < 0) {
3980 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3986 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3989 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3990 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3991 uint32_t idx
= PA_INVALID_INDEX
;
3992 const char *name
= NULL
;
3994 pa_native_protocol_ext_cb_t cb
;
3996 pa_native_connection_assert_ref(c
);
3999 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
4000 pa_tagstruct_gets(t
, &name
) < 0) {
4005 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
4006 CHECK_VALIDITY(c
->pstream
, !name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
4007 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
4008 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
4009 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
4011 if (idx
!= PA_INVALID_INDEX
)
4012 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
4014 for (m
= pa_idxset_first(c
->protocol
->core
->modules
, &idx
); m
; m
= pa_idxset_next(c
->protocol
->core
->modules
, &idx
))
4015 if (strcmp(name
, m
->name
) == 0)
4019 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
4020 CHECK_VALIDITY(c
->pstream
, m
->load_once
|| idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
4022 cb
= (pa_native_protocol_ext_cb_t
) (unsigned long) pa_hashmap_get(c
->protocol
->extensions
, m
);
4023 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
4025 if (cb(c
->protocol
, m
, c
, tag
, t
) < 0)
4029 static void command_set_card_profile(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
4030 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4031 uint32_t idx
= PA_INVALID_INDEX
;
4032 const char *name
= NULL
, *profile
= NULL
;
4033 pa_card
*card
= NULL
;
4035 pa_native_connection_assert_ref(c
);
4038 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
4039 pa_tagstruct_gets(t
, &name
) < 0 ||
4040 pa_tagstruct_gets(t
, &profile
) < 0 ||
4041 !pa_tagstruct_eof(t
)) {
4046 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
4047 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
4048 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
4049 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
4050 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
4052 if (idx
!= PA_INVALID_INDEX
)
4053 card
= pa_idxset_get_by_index(c
->protocol
->core
->cards
, idx
);
4055 card
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_CARD
);
4057 CHECK_VALIDITY(c
->pstream
, card
, tag
, PA_ERR_NOENTITY
);
4059 if (pa_card_set_profile(card
, profile
) < 0) {
4060 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
4064 pa_pstream_send_simple_ack(c
->pstream
, tag
);
4067 /*** pstream callbacks ***/
4069 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
4070 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4074 pa_native_connection_assert_ref(c
);
4076 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
4077 pa_log("invalid packet.");
4078 native_connection_unlink(c
);
4082 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
) {
4083 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4084 output_stream
*stream
;
4088 pa_native_connection_assert_ref(c
);
4090 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
4091 pa_log("client sent block for invalid stream.");
4096 /* pa_log("got %lu bytes", (unsigned long) chunk->length); */
4098 if (playback_stream_isinstance(stream
)) {
4099 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
4101 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
4102 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
);
4104 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
4107 upload_stream
*u
= UPLOAD_STREAM(stream
);
4110 if (!u
->memchunk
.memblock
) {
4111 if (u
->length
== chunk
->length
) {
4112 u
->memchunk
= *chunk
;
4113 pa_memblock_ref(u
->memchunk
.memblock
);
4116 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
4117 u
->memchunk
.index
= u
->memchunk
.length
= 0;
4121 pa_assert(u
->memchunk
.memblock
);
4124 if (l
> chunk
->length
)
4130 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
4131 src
= pa_memblock_acquire(chunk
->memblock
);
4133 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
4134 (uint8_t*) src
+chunk
->index
, l
);
4136 pa_memblock_release(u
->memchunk
.memblock
);
4137 pa_memblock_release(chunk
->memblock
);
4139 u
->memchunk
.length
+= l
;
4145 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
4146 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4149 pa_native_connection_assert_ref(c
);
4151 native_connection_unlink(c
);
4152 pa_log_info("Connection died.");
4155 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
4156 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4159 pa_native_connection_assert_ref(c
);
4161 native_connection_send_memblock(c
);
4164 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4167 if (!(q
= pa_thread_mq_get()))
4168 pa_pstream_send_revoke(p
, block_id
);
4170 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4173 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4176 if (!(q
= pa_thread_mq_get()))
4177 pa_pstream_send_release(p
, block_id
);
4179 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4182 /*** client callbacks ***/
4184 static void client_kill_cb(pa_client
*c
) {
4187 native_connection_unlink(PA_NATIVE_CONNECTION(c
->userdata
));
4188 pa_log_info("Connection killed.");
4191 /*** module entry points ***/
4193 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
4194 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4198 pa_native_connection_assert_ref(c
);
4199 pa_assert(c
->auth_timeout_event
== e
);
4201 if (!c
->authorized
) {
4202 native_connection_unlink(c
);
4203 pa_log_info("Connection terminated due to authentication timeout.");
4207 void pa_native_protocol_connect(pa_native_protocol
*p
, pa_iochannel
*io
, pa_native_options
*o
) {
4208 pa_native_connection
*c
;
4211 pa_client_new_data data
;
4217 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
4218 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
4219 pa_iochannel_free(io
);
4223 pa_client_new_data_init(&data
);
4224 data
.module
= o
->module
;
4225 data
.driver
= __FILE__
;
4226 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
4227 pa_proplist_setf(data
.proplist
, PA_PROP_APPLICATION_NAME
, "Native client (%s)", pname
);
4228 pa_proplist_sets(data
.proplist
, "native-protocol.peer", pname
);
4229 client
= pa_client_new(p
->core
, &data
);
4230 pa_client_new_data_done(&data
);
4235 c
= pa_msgobject_new(pa_native_connection
);
4236 c
->parent
.parent
.free
= native_connection_free
;
4237 c
->parent
.process_msg
= native_connection_process_msg
;
4239 c
->options
= pa_native_options_ref(o
);
4240 c
->authorized
= FALSE
;
4242 if (o
->auth_anonymous
) {
4243 pa_log_info("Client authenticated anonymously.");
4244 c
->authorized
= TRUE
;
4247 if (!c
->authorized
&&
4249 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
4251 pa_log_info("Client authenticated by IP ACL.");
4252 c
->authorized
= TRUE
;
4255 if (!c
->authorized
) {
4257 pa_gettimeofday(&tv
);
4258 tv
.tv_sec
+= AUTH_TIMEOUT
;
4259 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
4261 c
->auth_timeout_event
= NULL
;
4263 c
->is_local
= pa_iochannel_socket_is_local(io
);
4267 c
->client
->kill
= client_kill_cb
;
4268 c
->client
->userdata
= c
;
4270 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
4271 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
4272 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
4273 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
4274 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
4275 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
4276 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
4278 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
4280 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
4281 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
4283 c
->rrobin_index
= PA_IDXSET_INVALID
;
4284 c
->subscription
= NULL
;
4286 pa_idxset_put(p
->connections
, c
, NULL
);
4289 if (pa_iochannel_creds_supported(io
))
4290 pa_iochannel_creds_enable(io
);
4293 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_CONNECTION_PUT
], c
);
4296 void pa_native_protocol_disconnect(pa_native_protocol
*p
, pa_module
*m
) {
4297 pa_native_connection
*c
;
4303 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
4304 if (c
->options
->module
== m
)
4305 native_connection_unlink(c
);
4308 static pa_native_protocol
* native_protocol_new(pa_core
*c
) {
4309 pa_native_protocol
*p
;
4314 p
= pa_xnew(pa_native_protocol
, 1);
4317 p
->connections
= pa_idxset_new(NULL
, NULL
);
4321 p
->extensions
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
4323 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4324 pa_hook_init(&p
->hooks
[h
], p
);
4326 pa_assert_se(pa_shared_set(c
, "native-protocol", p
) >= 0);
4331 pa_native_protocol
* pa_native_protocol_get(pa_core
*c
) {
4332 pa_native_protocol
*p
;
4334 if ((p
= pa_shared_get(c
, "native-protocol")))
4335 return pa_native_protocol_ref(p
);
4337 return native_protocol_new(c
);
4340 pa_native_protocol
* pa_native_protocol_ref(pa_native_protocol
*p
) {
4342 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4349 void pa_native_protocol_unref(pa_native_protocol
*p
) {
4350 pa_native_connection
*c
;
4354 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4356 if (PA_REFCNT_DEC(p
) > 0)
4359 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4360 native_connection_unlink(c
);
4362 pa_idxset_free(p
->connections
, NULL
, NULL
);
4364 pa_strlist_free(p
->servers
);
4366 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4367 pa_hook_done(&p
->hooks
[h
]);
4369 pa_hashmap_free(p
->extensions
, NULL
, NULL
);
4371 pa_assert_se(pa_shared_remove(p
->core
, "native-protocol") >= 0);
4376 void pa_native_protocol_add_server_string(pa_native_protocol
*p
, const char *name
) {
4378 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4381 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
4383 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4386 void pa_native_protocol_remove_server_string(pa_native_protocol
*p
, const char *name
) {
4388 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4391 p
->servers
= pa_strlist_remove(p
->servers
, name
);
4393 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4396 pa_hook
*pa_native_protocol_hooks(pa_native_protocol
*p
) {
4398 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4403 pa_strlist
*pa_native_protocol_servers(pa_native_protocol
*p
) {
4405 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4410 int pa_native_protocol_install_ext(pa_native_protocol
*p
, pa_module
*m
, pa_native_protocol_ext_cb_t cb
) {
4412 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4415 pa_assert(!pa_hashmap_get(p
->extensions
, m
));
4417 pa_assert_se(pa_hashmap_put(p
->extensions
, m
, (void*) (unsigned long) cb
) == 0);
4421 void pa_native_protocol_remove_ext(pa_native_protocol
*p
, pa_module
*m
) {
4423 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4426 pa_assert_se(pa_hashmap_remove(p
->extensions
, m
));
4429 pa_native_options
* pa_native_options_new(void) {
4430 pa_native_options
*o
;
4432 o
= pa_xnew0(pa_native_options
, 1);
4438 pa_native_options
* pa_native_options_ref(pa_native_options
*o
) {
4440 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4447 void pa_native_options_unref(pa_native_options
*o
) {
4449 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4451 if (PA_REFCNT_DEC(o
) > 0)
4454 pa_xfree(o
->auth_group
);
4457 pa_ip_acl_free(o
->auth_ip_acl
);
4460 pa_auth_cookie_unref(o
->auth_cookie
);
4465 int pa_native_options_parse(pa_native_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
4470 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4473 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
4474 pa_log("auth-anonymous= expects a boolean argument.");
4479 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &enabled
) < 0) {
4480 pa_log("auth-group-enabled= expects a boolean argument.");
4484 pa_xfree(o
->auth_group
);
4485 o
->auth_group
= enabled
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4489 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4492 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4495 if (!(ipa
= pa_ip_acl_new(acl
))) {
4496 pa_log("Failed to parse IP ACL '%s'", acl
);
4501 pa_ip_acl_free(o
->auth_ip_acl
);
4503 o
->auth_ip_acl
= ipa
;
4507 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
4508 pa_log("auth-cookie-enabled= expects a boolean argument.");
4513 pa_auth_cookie_unref(o
->auth_cookie
);
4518 /* The new name for this is 'auth-cookie', for compat reasons
4519 * we check the old name too */
4520 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
4521 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
4522 cn
= PA_NATIVE_COOKIE_FILE
;
4524 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, PA_NATIVE_COOKIE_LENGTH
)))
4528 o
->auth_cookie
= NULL
;
4533 pa_pstream
* pa_native_connection_get_pstream(pa_native_connection
*c
) {
4534 pa_native_connection_assert_ref(c
);