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
);
260 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
261 [PA_COMMAND_ERROR
] = NULL
,
262 [PA_COMMAND_TIMEOUT
] = NULL
,
263 [PA_COMMAND_REPLY
] = NULL
,
264 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
265 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
266 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
267 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
268 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
269 [PA_COMMAND_AUTH
] = command_auth
,
270 [PA_COMMAND_REQUEST
] = NULL
,
271 [PA_COMMAND_EXIT
] = command_exit
,
272 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
273 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
274 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
275 [PA_COMMAND_STAT
] = command_stat
,
276 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
277 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
278 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
279 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
280 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
281 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
282 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
283 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
284 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
285 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
286 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
287 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
288 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
289 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
290 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
291 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
292 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
293 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
294 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
295 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
296 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
297 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
298 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
300 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
301 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
302 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
304 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
305 [PA_COMMAND_SET_SINK_INPUT_MUTE
] = command_set_mute
,
306 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
308 [PA_COMMAND_SUSPEND_SINK
] = command_suspend
,
309 [PA_COMMAND_SUSPEND_SOURCE
] = command_suspend
,
311 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
312 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
313 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
314 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
316 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
317 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
319 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
320 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
321 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
322 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
323 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
324 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
325 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
326 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
327 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
329 [PA_COMMAND_GET_AUTOLOAD_INFO___OBSOLETE
] = NULL
,
330 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST___OBSOLETE
] = NULL
,
331 [PA_COMMAND_ADD_AUTOLOAD___OBSOLETE
] = NULL
,
332 [PA_COMMAND_REMOVE_AUTOLOAD___OBSOLETE
] = NULL
,
334 [PA_COMMAND_MOVE_SINK_INPUT
] = command_move_stream
,
335 [PA_COMMAND_MOVE_SOURCE_OUTPUT
] = command_move_stream
,
337 [PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
338 [PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
340 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
341 [PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
343 [PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
] = command_update_proplist
,
344 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
] = command_update_proplist
,
345 [PA_COMMAND_UPDATE_CLIENT_PROPLIST
] = command_update_proplist
,
347 [PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
] = command_remove_proplist
,
348 [PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
] = command_remove_proplist
,
349 [PA_COMMAND_REMOVE_CLIENT_PROPLIST
] = command_remove_proplist
,
351 [PA_COMMAND_EXTENSION
] = command_extension
354 /* structure management */
356 static void upload_stream_unlink(upload_stream
*s
) {
362 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
363 s
->connection
= NULL
;
364 upload_stream_unref(s
);
367 static void upload_stream_free(pa_object
*o
) {
368 upload_stream
*s
= UPLOAD_STREAM(o
);
371 upload_stream_unlink(s
);
376 pa_proplist_free(s
->proplist
);
378 if (s
->memchunk
.memblock
)
379 pa_memblock_unref(s
->memchunk
.memblock
);
384 static upload_stream
* upload_stream_new(
385 pa_native_connection
*c
,
386 const pa_sample_spec
*ss
,
387 const pa_channel_map
*map
,
397 pa_assert(length
> 0);
400 s
= pa_msgobject_new(upload_stream
);
401 s
->parent
.parent
.parent
.free
= upload_stream_free
;
403 s
->sample_spec
= *ss
;
404 s
->channel_map
= *map
;
405 s
->name
= pa_xstrdup(name
);
406 pa_memchunk_reset(&s
->memchunk
);
408 s
->proplist
= pa_proplist_copy(p
);
409 pa_proplist_update(s
->proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
411 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
416 static void record_stream_unlink(record_stream
*s
) {
422 if (s
->source_output
) {
423 pa_source_output_unlink(s
->source_output
);
424 pa_source_output_unref(s
->source_output
);
425 s
->source_output
= NULL
;
428 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->record_streams
, s
, NULL
) == s
);
429 s
->connection
= NULL
;
430 record_stream_unref(s
);
433 static void record_stream_free(pa_object
*o
) {
434 record_stream
*s
= RECORD_STREAM(o
);
437 record_stream_unlink(s
);
439 pa_memblockq_free(s
->memblockq
);
443 static int record_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
444 record_stream
*s
= RECORD_STREAM(o
);
445 record_stream_assert_ref(s
);
452 case RECORD_STREAM_MESSAGE_POST_DATA
:
454 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
455 /* pa_log_warn("Failed to push data into output queue."); */
459 if (!pa_pstream_is_pending(s
->connection
->pstream
))
460 native_connection_send_memblock(s
->connection
);
468 static void fix_record_buffer_attr_pre(
470 pa_bool_t adjust_latency
,
471 pa_bool_t early_requests
,
473 uint32_t *fragsize
) {
476 pa_usec_t orig_fragsize_usec
, fragsize_usec
, source_usec
;
479 pa_assert(maxlength
);
482 frame_size
= pa_frame_size(&s
->source_output
->sample_spec
);
484 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
485 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
487 *maxlength
= (uint32_t) frame_size
;
489 if (*fragsize
== (uint32_t) -1)
490 *fragsize
= (uint32_t) pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC
*PA_USEC_PER_MSEC
, &s
->source_output
->sample_spec
);
492 *fragsize
= (uint32_t) frame_size
;
494 orig_fragsize_usec
= fragsize_usec
= pa_bytes_to_usec(*fragsize
, &s
->source_output
->sample_spec
);
496 if (early_requests
) {
498 /* In early request mode we need to emulate the classic
499 * fragment-based playback model. We do this setting the source
500 * latency to the fragment size. */
502 source_usec
= fragsize_usec
;
504 } else if (adjust_latency
) {
506 /* So, the user asked us to adjust the latency according to
507 * what the source can provide. Half the latency will be
508 * spent on the hw buffer, half of it in the async buffer
509 * queue we maintain for each client. */
511 source_usec
= fragsize_usec
/2;
515 /* Ok, the user didn't ask us to adjust the latency, hence we
522 s
->source_latency
= pa_source_output_set_requested_latency(s
->source_output
, source_usec
);
524 s
->source_latency
= 0;
526 if (early_requests
) {
528 /* Ok, we didn't necessarily get what we were asking for, so
529 * let's tell the user */
531 fragsize_usec
= s
->source_latency
;
533 } else if (adjust_latency
) {
535 /* Now subtract what we actually got */
537 if (fragsize_usec
>= s
->source_latency
*2)
538 fragsize_usec
-= s
->source_latency
;
540 fragsize_usec
= s
->source_latency
;
543 if (pa_usec_to_bytes(orig_fragsize_usec
, &s
->source_output
->sample_spec
) !=
544 pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
))
546 *fragsize
= (uint32_t) pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
);
549 *fragsize
= (uint32_t) frame_size
;
552 static void fix_record_buffer_attr_post(
555 uint32_t *fragsize
) {
560 pa_assert(maxlength
);
563 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
565 base
= pa_frame_size(&s
->source_output
->sample_spec
);
567 s
->fragment_size
= (*fragsize
/base
)*base
;
568 if (s
->fragment_size
<= 0)
569 s
->fragment_size
= base
;
571 if (s
->fragment_size
> *maxlength
)
572 s
->fragment_size
= *maxlength
;
574 *fragsize
= (uint32_t) s
->fragment_size
;
577 static record_stream
* record_stream_new(
578 pa_native_connection
*c
,
582 pa_bool_t peak_detect
,
585 pa_source_output_flags_t flags
,
587 pa_bool_t adjust_latency
,
588 pa_sink_input
*direct_on_input
,
589 pa_bool_t early_requests
) {
592 pa_source_output
*source_output
;
594 pa_source_output_new_data data
;
598 pa_assert(maxlength
);
601 pa_source_output_new_data_init(&data
);
603 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
604 data
.driver
= __FILE__
;
605 data
.module
= c
->options
->module
;
606 data
.client
= c
->client
;
607 data
.source
= source
;
608 data
.direct_on_input
= direct_on_input
;
609 pa_source_output_new_data_set_sample_spec(&data
, ss
);
610 pa_source_output_new_data_set_channel_map(&data
, map
);
612 data
.resample_method
= PA_RESAMPLER_PEAKS
;
614 source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
);
616 pa_source_output_new_data_done(&data
);
621 s
= pa_msgobject_new(record_stream
);
622 s
->parent
.parent
.free
= record_stream_free
;
623 s
->parent
.process_msg
= record_stream_process_msg
;
625 s
->source_output
= source_output
;
627 s
->source_output
->push
= source_output_push_cb
;
628 s
->source_output
->kill
= source_output_kill_cb
;
629 s
->source_output
->get_latency
= source_output_get_latency_cb
;
630 s
->source_output
->moved
= source_output_moved_cb
;
631 s
->source_output
->suspend
= source_output_suspend_cb
;
632 s
->source_output
->userdata
= s
;
634 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, fragsize
);
636 s
->memblockq
= pa_memblockq_new(
640 base
= pa_frame_size(&source_output
->sample_spec
),
646 fix_record_buffer_attr_post(s
, maxlength
, fragsize
);
648 *ss
= s
->source_output
->sample_spec
;
649 *map
= s
->source_output
->channel_map
;
651 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
653 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
654 ((double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) + (double) s
->source_latency
) / PA_USEC_PER_MSEC
,
655 (double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) / PA_USEC_PER_MSEC
,
656 (double) s
->source_latency
/ PA_USEC_PER_MSEC
);
658 pa_source_output_put(s
->source_output
);
662 static void record_stream_send_killed(record_stream
*r
) {
664 record_stream_assert_ref(r
);
666 t
= pa_tagstruct_new(NULL
, 0);
667 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
668 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
669 pa_tagstruct_putu32(t
, r
->index
);
670 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
673 static void playback_stream_unlink(playback_stream
*s
) {
680 pa_sink_input_unlink(s
->sink_input
);
681 pa_sink_input_unref(s
->sink_input
);
682 s
->sink_input
= NULL
;
685 if (s
->drain_request
)
686 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
688 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
689 s
->connection
= NULL
;
690 playback_stream_unref(s
);
693 static void playback_stream_free(pa_object
* o
) {
694 playback_stream
*s
= PLAYBACK_STREAM(o
);
697 playback_stream_unlink(s
);
699 pa_memblockq_free(s
->memblockq
);
703 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
704 playback_stream
*s
= PLAYBACK_STREAM(o
);
705 playback_stream_assert_ref(s
);
711 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
716 if ((l
= (uint32_t) pa_atomic_load(&s
->missing
)) <= 0)
719 if (pa_atomic_cmpxchg(&s
->missing
, (int) l
, 0))
726 t
= pa_tagstruct_new(NULL
, 0);
727 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
728 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
729 pa_tagstruct_putu32(t
, s
->index
);
730 pa_tagstruct_putu32(t
, l
);
731 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
733 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
737 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
740 /* pa_log("signalling underflow"); */
742 /* Report that we're empty */
743 t
= pa_tagstruct_new(NULL
, 0);
744 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
745 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
746 pa_tagstruct_putu32(t
, s
->index
);
747 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
751 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
754 /* Notify the user we're overflowed*/
755 t
= pa_tagstruct_new(NULL
, 0);
756 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
757 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
758 pa_tagstruct_putu32(t
, s
->index
);
759 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
763 case PLAYBACK_STREAM_MESSAGE_STARTED
:
765 if (s
->connection
->version
>= 13) {
768 /* Notify the user we're overflowed*/
769 t
= pa_tagstruct_new(NULL
, 0);
770 pa_tagstruct_putu32(t
, PA_COMMAND_STARTED
);
771 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
772 pa_tagstruct_putu32(t
, s
->index
);
773 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
778 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
779 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
786 static void fix_playback_buffer_attr_pre(
788 pa_bool_t adjust_latency
,
789 pa_bool_t early_requests
,
796 pa_usec_t orig_tlength_usec
, tlength_usec
, orig_minreq_usec
, minreq_usec
, sink_usec
;
799 pa_assert(maxlength
);
804 frame_size
= pa_frame_size(&s
->sink_input
->sample_spec
);
806 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
807 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
809 *maxlength
= (uint32_t) frame_size
;
811 if (*tlength
== (uint32_t) -1)
812 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_TLENGTH_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
814 *tlength
= (uint32_t) frame_size
;
816 if (*minreq
== (uint32_t) -1)
817 *minreq
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_PROCESS_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
819 *minreq
= (uint32_t) frame_size
;
821 if (*tlength
< *minreq
+frame_size
)
822 *tlength
= *minreq
+(uint32_t) frame_size
;
824 orig_tlength_usec
= tlength_usec
= pa_bytes_to_usec(*tlength
, &s
->sink_input
->sample_spec
);
825 orig_minreq_usec
= minreq_usec
= pa_bytes_to_usec(*minreq
, &s
->sink_input
->sample_spec
);
827 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
828 (double) tlength_usec
/ PA_USEC_PER_MSEC
,
829 (double) minreq_usec
/ PA_USEC_PER_MSEC
);
831 if (early_requests
) {
833 /* In early request mode we need to emulate the classic
834 * fragment-based playback model. We do this setting the sink
835 * latency to the fragment size. */
837 sink_usec
= minreq_usec
;
839 pa_log_debug("Early requests mode enabled, configuring sink latency to minreq.");
841 } else if (adjust_latency
) {
843 /* So, the user asked us to adjust the latency of the stream
844 * buffer according to the what the sink can provide. The
845 * tlength passed in shall be the overall latency. Roughly
846 * half the latency will be spent on the hw buffer, the other
847 * half of it in the async buffer queue we maintain for each
848 * client. In between we'll have a safety space of size
849 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
850 * empty and needs to be filled, then our buffer must have
851 * enough data to fulfill this request immediatly and thus
852 * have at least the same tlength as the size of the hw
853 * buffer. It additionally needs space for 2 times minreq
854 * because if the buffer ran empty and a partial fillup
855 * happens immediately on the next iteration we need to be
856 * able to fulfill it and give the application also minreq
857 * time to fill it up again for the next request Makes 2 times
858 * minreq in plus.. */
860 if (tlength_usec
> minreq_usec
*2)
861 sink_usec
= (tlength_usec
- minreq_usec
*2)/2;
865 pa_log_debug("Adjust latency mode enabled, configuring sink latency to half of overall latency.");
869 /* Ok, the user didn't ask us to adjust the latency, but we
870 * still need to make sure that the parameters from the user
873 if (tlength_usec
> minreq_usec
*2)
874 sink_usec
= (tlength_usec
- minreq_usec
*2);
878 pa_log_debug("Traditional mode enabled, modifying sink usec only for compat with minreq.");
881 s
->sink_latency
= pa_sink_input_set_requested_latency(s
->sink_input
, sink_usec
);
883 if (early_requests
) {
885 /* Ok, we didn't necessarily get what we were asking for, so
886 * let's tell the user */
888 minreq_usec
= s
->sink_latency
;
890 } else if (adjust_latency
) {
892 /* Ok, we didn't necessarily get what we were asking for, so
893 * let's subtract from what we asked for for the remaining
896 if (tlength_usec
>= s
->sink_latency
)
897 tlength_usec
-= s
->sink_latency
;
900 /* FIXME: This is actually larger than necessary, since not all of
901 * the sink latency is actually rewritable. */
902 if (tlength_usec
< s
->sink_latency
+ 2*minreq_usec
)
903 tlength_usec
= s
->sink_latency
+ 2*minreq_usec
;
905 if (pa_usec_to_bytes_round_up(orig_tlength_usec
, &s
->sink_input
->sample_spec
) !=
906 pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
))
907 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
);
909 if (pa_usec_to_bytes(orig_minreq_usec
, &s
->sink_input
->sample_spec
) !=
910 pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
))
911 *minreq
= (uint32_t) pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
);
914 *minreq
= (uint32_t) frame_size
;
915 *tlength
+= (uint32_t) frame_size
*2;
918 if (*tlength
<= *minreq
)
919 *tlength
= *minreq
*2 + (uint32_t) frame_size
;
921 if (*prebuf
== (uint32_t) -1 || *prebuf
> *tlength
)
925 static void fix_playback_buffer_attr_post(
933 pa_assert(maxlength
);
938 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
939 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
940 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
941 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
946 static playback_stream
* playback_stream_new(
947 pa_native_connection
*c
,
960 pa_sink_input_flags_t flags
,
962 pa_bool_t adjust_latency
,
963 pa_bool_t early_requests
) {
965 playback_stream
*s
, *ssync
;
966 pa_sink_input
*sink_input
;
970 pa_sink_input_new_data data
;
974 pa_assert(maxlength
);
981 /* Find syncid group */
982 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
984 if (!playback_stream_isinstance(ssync
))
987 if (ssync
->syncid
== syncid
)
991 /* Synced streams must connect to the same sink */
995 sink
= ssync
->sink_input
->sink
;
996 else if (sink
!= ssync
->sink_input
->sink
)
1000 pa_sink_input_new_data_init(&data
);
1002 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
1003 data
.driver
= __FILE__
;
1004 data
.module
= c
->options
->module
;
1005 data
.client
= c
->client
;
1007 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
1008 pa_sink_input_new_data_set_channel_map(&data
, map
);
1010 pa_sink_input_new_data_set_volume(&data
, volume
);
1012 pa_sink_input_new_data_set_muted(&data
, muted
);
1013 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
1015 sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
);
1017 pa_sink_input_new_data_done(&data
);
1022 s
= pa_msgobject_new(playback_stream
);
1023 s
->parent
.parent
.parent
.free
= playback_stream_free
;
1024 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
1027 s
->sink_input
= sink_input
;
1028 s
->is_underrun
= TRUE
;
1029 s
->drain_request
= FALSE
;
1030 pa_atomic_store(&s
->missing
, 0);
1032 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
1033 s
->sink_input
->pop
= sink_input_pop_cb
;
1034 s
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1035 s
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1036 s
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1037 s
->sink_input
->kill
= sink_input_kill_cb
;
1038 s
->sink_input
->moved
= sink_input_moved_cb
;
1039 s
->sink_input
->suspend
= sink_input_suspend_cb
;
1040 s
->sink_input
->userdata
= s
;
1042 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
1044 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, tlength
, prebuf
, minreq
);
1045 pa_sink_input_get_silence(sink_input
, &silence
);
1047 s
->memblockq
= pa_memblockq_new(
1051 pa_frame_size(&sink_input
->sample_spec
),
1057 pa_memblock_unref(silence
.memblock
);
1058 fix_playback_buffer_attr_post(s
, maxlength
, tlength
, prebuf
, minreq
);
1060 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
1062 *ss
= s
->sink_input
->sample_spec
;
1063 *map
= s
->sink_input
->channel_map
;
1065 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
1067 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
1068 ((double) pa_bytes_to_usec(*tlength
, &sink_input
->sample_spec
) + (double) s
->sink_latency
) / PA_USEC_PER_MSEC
,
1069 (double) pa_bytes_to_usec(*tlength
-*minreq
*2, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1070 (double) pa_bytes_to_usec(*minreq
, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1071 (double) s
->sink_latency
/ PA_USEC_PER_MSEC
);
1073 pa_sink_input_put(s
->sink_input
);
1077 /* Called from thread context */
1078 static void playback_stream_request_bytes(playback_stream
*s
) {
1079 size_t m
, previous_missing
;
1081 playback_stream_assert_ref(s
);
1083 m
= pa_memblockq_pop_missing(s
->memblockq
);
1088 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
1090 previous_missing
= (size_t) pa_atomic_add(&s
->missing
, (int) m
);
1092 if (pa_memblockq_prebuf_active(s
->memblockq
) ||
1093 (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
))
1094 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1098 static void playback_stream_send_killed(playback_stream
*p
) {
1100 playback_stream_assert_ref(p
);
1102 t
= pa_tagstruct_new(NULL
, 0);
1103 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
1104 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1105 pa_tagstruct_putu32(t
, p
->index
);
1106 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
1109 static int native_connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1110 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1111 pa_native_connection_assert_ref(c
);
1118 case CONNECTION_MESSAGE_REVOKE
:
1119 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1122 case CONNECTION_MESSAGE_RELEASE
:
1123 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1130 static void native_connection_unlink(pa_native_connection
*c
) {
1139 pa_hook_fire(&c
->protocol
->hooks
[PA_NATIVE_HOOK_CONNECTION_UNLINK
], c
);
1142 pa_native_options_unref(c
->options
);
1144 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
1145 record_stream_unlink(r
);
1147 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
1148 if (playback_stream_isinstance(o
))
1149 playback_stream_unlink(PLAYBACK_STREAM(o
));
1151 upload_stream_unlink(UPLOAD_STREAM(o
));
1153 if (c
->subscription
)
1154 pa_subscription_free(c
->subscription
);
1157 pa_pstream_unlink(c
->pstream
);
1159 if (c
->auth_timeout_event
) {
1160 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1161 c
->auth_timeout_event
= NULL
;
1164 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
1166 pa_native_connection_unref(c
);
1169 static void native_connection_free(pa_object
*o
) {
1170 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1174 native_connection_unlink(c
);
1176 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
1177 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
1179 pa_pdispatch_unref(c
->pdispatch
);
1180 pa_pstream_unref(c
->pstream
);
1181 pa_client_free(c
->client
);
1186 static void native_connection_send_memblock(pa_native_connection
*c
) {
1190 start
= PA_IDXSET_INVALID
;
1194 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
1197 if (start
== PA_IDXSET_INVALID
)
1198 start
= c
->rrobin_index
;
1199 else if (start
== c
->rrobin_index
)
1202 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
1203 pa_memchunk schunk
= chunk
;
1205 if (schunk
.length
> r
->fragment_size
)
1206 schunk
.length
= r
->fragment_size
;
1208 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
1210 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
1211 pa_memblock_unref(schunk
.memblock
);
1218 /*** sink input callbacks ***/
1220 static void handle_seek(playback_stream
*s
, int64_t indexw
) {
1221 playback_stream_assert_ref(s
);
1223 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1225 if (s
->sink_input
->thread_info
.underrun_for
> 0) {
1227 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1229 if (pa_memblockq_is_readable(s
->memblockq
)) {
1231 /* We just ended an underrun, let's ask the sink
1232 * for a complete rewind rewrite */
1234 pa_log_debug("Requesting rewind due to end of underrun.");
1235 pa_sink_input_request_rewind(s
->sink_input
,
1236 (size_t) (s
->sink_input
->thread_info
.underrun_for
== (size_t) -1 ? 0 : s
->sink_input
->thread_info
.underrun_for
),
1237 FALSE
, TRUE
, FALSE
);
1243 indexr
= pa_memblockq_get_read_index(s
->memblockq
);
1245 if (indexw
< indexr
) {
1246 /* OK, the sink already asked for this data, so
1247 * let's have it usk us again */
1249 pa_log_debug("Requesting rewind due to rewrite.");
1250 pa_sink_input_request_rewind(s
->sink_input
, (size_t) (indexr
- indexw
), TRUE
, FALSE
, FALSE
);
1254 playback_stream_request_bytes(s
);
1257 /* Called from thread context */
1258 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1259 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1262 pa_sink_input_assert_ref(i
);
1263 s
= PLAYBACK_STREAM(i
->userdata
);
1264 playback_stream_assert_ref(s
);
1268 case SINK_INPUT_MESSAGE_SEEK
: {
1271 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1272 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
1274 handle_seek(s
, windex
);
1278 case SINK_INPUT_MESSAGE_POST_DATA
: {
1283 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1285 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1287 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
1288 pa_log_warn("Failed to push data into queue");
1289 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
1290 pa_memblockq_seek(s
->memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
1293 handle_seek(s
, windex
);
1295 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1300 case SINK_INPUT_MESSAGE_DRAIN
:
1301 case SINK_INPUT_MESSAGE_FLUSH
:
1302 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1303 case SINK_INPUT_MESSAGE_TRIGGER
: {
1306 pa_sink_input
*isync
;
1307 void (*func
)(pa_memblockq
*bq
);
1310 case SINK_INPUT_MESSAGE_FLUSH
:
1311 func
= pa_memblockq_flush_write
;
1314 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1315 func
= pa_memblockq_prebuf_force
;
1318 case SINK_INPUT_MESSAGE_DRAIN
:
1319 case SINK_INPUT_MESSAGE_TRIGGER
:
1320 func
= pa_memblockq_prebuf_disable
;
1324 pa_assert_not_reached();
1327 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1329 handle_seek(s
, windex
);
1331 /* Do the same for all other members in the sync group */
1332 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
1333 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1334 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1335 func(ssync
->memblockq
);
1336 handle_seek(ssync
, windex
);
1339 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1340 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1341 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1342 func(ssync
->memblockq
);
1343 handle_seek(ssync
, windex
);
1346 if (code
== SINK_INPUT_MESSAGE_DRAIN
) {
1347 if (!pa_memblockq_is_readable(s
->memblockq
))
1348 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
1350 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
1351 s
->drain_request
= TRUE
;
1358 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1360 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1361 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1362 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1365 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1368 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1370 pa_memblockq_prebuf_force(s
->memblockq
);
1372 handle_seek(s
, windex
);
1374 /* Fall through to the default handler */
1378 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1379 pa_usec_t
*r
= userdata
;
1381 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1383 /* Fall through, the default handler will add in the extra
1384 * latency added by the resampler */
1389 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1392 /* Called from thread context */
1393 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
1396 pa_sink_input_assert_ref(i
);
1397 s
= PLAYBACK_STREAM(i
->userdata
);
1398 playback_stream_assert_ref(s
);
1401 /* pa_log("%s, pop(): %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1403 if (pa_memblockq_is_readable(s
->memblockq
))
1404 s
->is_underrun
= FALSE
;
1406 /* pa_log("%s, UNDERRUN: %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1408 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1409 s
->drain_request
= FALSE
;
1410 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
);
1411 } else if (!s
->is_underrun
)
1412 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1414 s
->is_underrun
= TRUE
;
1416 playback_stream_request_bytes(s
);
1419 /* This call will not fail with prebuf=0, hence we check for
1420 underrun explicitly above */
1421 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
1424 chunk
->length
= PA_MIN(nbytes
, chunk
->length
);
1426 if (i
->thread_info
.underrun_for
> 0)
1427 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_STARTED
, NULL
, 0, NULL
, NULL
);
1429 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1430 playback_stream_request_bytes(s
);
1435 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1438 pa_sink_input_assert_ref(i
);
1439 s
= PLAYBACK_STREAM(i
->userdata
);
1440 playback_stream_assert_ref(s
);
1442 /* If we are in an underrun, then we don't rewind */
1443 if (i
->thread_info
.underrun_for
> 0)
1446 pa_memblockq_rewind(s
->memblockq
, nbytes
);
1449 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1452 pa_sink_input_assert_ref(i
);
1453 s
= PLAYBACK_STREAM(i
->userdata
);
1454 playback_stream_assert_ref(s
);
1456 pa_memblockq_set_maxrewind(s
->memblockq
, nbytes
);
1459 static void sink_input_update_max_request_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 tlength
= nbytes
+2*pa_memblockq_get_minreq(s
->memblockq
);
1469 if (pa_memblockq_get_tlength(s
->memblockq
) < tlength
)
1470 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1473 /* Called from main context */
1474 static void sink_input_kill_cb(pa_sink_input
*i
) {
1477 pa_sink_input_assert_ref(i
);
1478 s
= PLAYBACK_STREAM(i
->userdata
);
1479 playback_stream_assert_ref(s
);
1481 playback_stream_send_killed(s
);
1482 playback_stream_unlink(s
);
1485 /* Called from main context */
1486 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1490 pa_sink_input_assert_ref(i
);
1491 s
= PLAYBACK_STREAM(i
->userdata
);
1492 playback_stream_assert_ref(s
);
1494 if (s
->connection
->version
< 12)
1497 t
= pa_tagstruct_new(NULL
, 0);
1498 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1499 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1500 pa_tagstruct_putu32(t
, s
->index
);
1501 pa_tagstruct_put_boolean(t
, suspend
);
1502 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1505 /* Called from main context */
1506 static void sink_input_moved_cb(pa_sink_input
*i
) {
1509 uint32_t maxlength
, tlength
, prebuf
, minreq
;
1511 pa_sink_input_assert_ref(i
);
1512 s
= PLAYBACK_STREAM(i
->userdata
);
1513 playback_stream_assert_ref(s
);
1515 maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
1516 tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
1517 prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
1518 minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
1520 fix_playback_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1521 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1522 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1523 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
1524 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
1525 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1527 if (s
->connection
->version
< 12)
1530 t
= pa_tagstruct_new(NULL
, 0);
1531 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1532 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1533 pa_tagstruct_putu32(t
, s
->index
);
1534 pa_tagstruct_putu32(t
, i
->sink
->index
);
1535 pa_tagstruct_puts(t
, i
->sink
->name
);
1536 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1538 if (s
->connection
->version
>= 13) {
1539 pa_tagstruct_putu32(t
, maxlength
);
1540 pa_tagstruct_putu32(t
, tlength
);
1541 pa_tagstruct_putu32(t
, prebuf
);
1542 pa_tagstruct_putu32(t
, minreq
);
1543 pa_tagstruct_put_usec(t
, s
->sink_latency
);
1546 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1549 /*** source_output callbacks ***/
1551 /* Called from thread context */
1552 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1555 pa_source_output_assert_ref(o
);
1556 s
= RECORD_STREAM(o
->userdata
);
1557 record_stream_assert_ref(s
);
1560 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1563 static void source_output_kill_cb(pa_source_output
*o
) {
1566 pa_source_output_assert_ref(o
);
1567 s
= RECORD_STREAM(o
->userdata
);
1568 record_stream_assert_ref(s
);
1570 record_stream_send_killed(s
);
1571 record_stream_unlink(s
);
1574 static pa_usec_t
source_output_get_latency_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 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1583 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1586 /* Called from main context */
1587 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1591 pa_source_output_assert_ref(o
);
1592 s
= RECORD_STREAM(o
->userdata
);
1593 record_stream_assert_ref(s
);
1595 if (s
->connection
->version
< 12)
1598 t
= pa_tagstruct_new(NULL
, 0);
1599 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1600 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1601 pa_tagstruct_putu32(t
, s
->index
);
1602 pa_tagstruct_put_boolean(t
, suspend
);
1603 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1606 /* Called from main context */
1607 static void source_output_moved_cb(pa_source_output
*o
) {
1610 uint32_t maxlength
, fragsize
;
1612 pa_source_output_assert_ref(o
);
1613 s
= RECORD_STREAM(o
->userdata
);
1614 record_stream_assert_ref(s
);
1616 fragsize
= (uint32_t) s
->fragment_size
;
1617 maxlength
= (uint32_t) pa_memblockq_get_length(s
->memblockq
);
1619 fix_record_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &fragsize
);
1620 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1621 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
1623 if (s
->connection
->version
< 12)
1626 t
= pa_tagstruct_new(NULL
, 0);
1627 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1628 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1629 pa_tagstruct_putu32(t
, s
->index
);
1630 pa_tagstruct_putu32(t
, o
->source
->index
);
1631 pa_tagstruct_puts(t
, o
->source
->name
);
1632 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1634 if (s
->connection
->version
>= 13) {
1635 pa_tagstruct_putu32(t
, maxlength
);
1636 pa_tagstruct_putu32(t
, fragsize
);
1637 pa_tagstruct_put_usec(t
, s
->source_latency
);
1640 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1643 /*** pdispatch callbacks ***/
1645 static void protocol_error(pa_native_connection
*c
) {
1646 pa_log("protocol error, kicking client");
1647 native_connection_unlink(c
);
1650 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1651 if (!(expression)) { \
1652 pa_pstream_send_error((pstream), (tag), (error)); \
1657 static pa_tagstruct
*reply_new(uint32_t tag
) {
1658 pa_tagstruct
*reply
;
1660 reply
= pa_tagstruct_new(NULL
, 0);
1661 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1662 pa_tagstruct_putu32(reply
, tag
);
1666 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1667 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1669 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1670 const char *name
= NULL
, *sink_name
;
1673 pa_tagstruct
*reply
;
1674 pa_sink
*sink
= NULL
;
1682 fix_channels
= FALSE
,
1684 variable_rate
= FALSE
,
1686 adjust_latency
= FALSE
,
1687 early_requests
= FALSE
,
1688 dont_inhibit_auto_suspend
= FALSE
,
1691 pa_sink_input_flags_t flags
= 0;
1693 pa_bool_t volume_set
= TRUE
;
1695 pa_native_connection_assert_ref(c
);
1698 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1701 PA_TAG_SAMPLE_SPEC
, &ss
,
1702 PA_TAG_CHANNEL_MAP
, &map
,
1703 PA_TAG_U32
, &sink_index
,
1704 PA_TAG_STRING
, &sink_name
,
1705 PA_TAG_U32
, &maxlength
,
1706 PA_TAG_BOOLEAN
, &corked
,
1707 PA_TAG_U32
, &tlength
,
1708 PA_TAG_U32
, &prebuf
,
1709 PA_TAG_U32
, &minreq
,
1710 PA_TAG_U32
, &syncid
,
1711 PA_TAG_CVOLUME
, &volume
,
1712 PA_TAG_INVALID
) < 0) {
1718 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1719 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
1720 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
1721 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1722 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1723 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1724 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1725 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1727 p
= pa_proplist_new();
1730 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1732 if (c
->version
>= 12) {
1733 /* Since 0.9.8 the user can ask for a couple of additional flags */
1735 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1736 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1737 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1738 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1739 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1740 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1741 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1744 pa_proplist_free(p
);
1749 if (c
->version
>= 13) {
1751 if (pa_tagstruct_get_boolean(t
, &muted
) < 0 ||
1752 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1753 pa_tagstruct_get_proplist(t
, p
) < 0) {
1755 pa_proplist_free(p
);
1760 if (c
->version
>= 14) {
1762 if (pa_tagstruct_get_boolean(t
, &volume_set
) < 0 ||
1763 pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
1765 pa_proplist_free(p
);
1770 if (c
->version
>= 15) {
1772 if (pa_tagstruct_get_boolean(t
, &muted_set
) < 0 ||
1773 pa_tagstruct_get_boolean(t
, &dont_inhibit_auto_suspend
) < 0) {
1775 pa_proplist_free(p
);
1780 if (!pa_tagstruct_eof(t
)) {
1782 pa_proplist_free(p
);
1786 if (sink_index
!= PA_INVALID_INDEX
) {
1788 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1789 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1790 pa_proplist_free(p
);
1794 } else if (sink_name
) {
1796 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
))) {
1797 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1798 pa_proplist_free(p
);
1804 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1805 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1806 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1807 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1808 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1809 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1810 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1811 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0) |
1812 (dont_inhibit_auto_suspend
? PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
: 0);
1814 /* Only since protocol version 15 there's a seperate muted_set
1815 * flag. For older versions we synthesize it here */
1816 muted_set
= muted_set
|| muted
;
1818 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
);
1819 pa_proplist_free(p
);
1821 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1823 reply
= reply_new(tag
);
1824 pa_tagstruct_putu32(reply
, s
->index
);
1825 pa_assert(s
->sink_input
);
1826 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1827 pa_tagstruct_putu32(reply
, missing
);
1829 /* pa_log("initial request is %u", missing); */
1831 if (c
->version
>= 9) {
1832 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1834 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1835 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1836 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1837 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1840 if (c
->version
>= 12) {
1841 /* Since 0.9.8 we support sending the chosen sample
1842 * spec/channel map/device/suspend status back to the
1845 pa_tagstruct_put_sample_spec(reply
, &ss
);
1846 pa_tagstruct_put_channel_map(reply
, &map
);
1848 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1849 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1851 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1854 if (c
->version
>= 13)
1855 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
1857 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1860 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1861 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1864 pa_native_connection_assert_ref(c
);
1867 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1868 !pa_tagstruct_eof(t
)) {
1873 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1877 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1879 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1880 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1884 playback_stream_unlink(s
);
1888 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1890 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1891 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1895 record_stream_unlink(s
);
1899 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1902 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1903 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1907 upload_stream_unlink(s
);
1912 pa_assert_not_reached();
1915 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1918 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1919 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1921 uint32_t maxlength
, fragment_size
;
1922 uint32_t source_index
;
1923 const char *name
= NULL
, *source_name
;
1926 pa_tagstruct
*reply
;
1927 pa_source
*source
= NULL
;
1934 fix_channels
= FALSE
,
1936 variable_rate
= FALSE
,
1937 adjust_latency
= FALSE
,
1938 peak_detect
= FALSE
,
1939 early_requests
= FALSE
,
1940 dont_inhibit_auto_suspend
= FALSE
;
1941 pa_source_output_flags_t flags
= 0;
1943 uint32_t direct_on_input_idx
= PA_INVALID_INDEX
;
1944 pa_sink_input
*direct_on_input
= NULL
;
1946 pa_native_connection_assert_ref(c
);
1949 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1950 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1951 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1952 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1953 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1954 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1955 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1956 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1961 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1962 CHECK_VALIDITY(c
->pstream
, !source_name
|| pa_namereg_is_valid_name(source_name
), tag
, PA_ERR_INVALID
);
1963 CHECK_VALIDITY(c
->pstream
, source_index
== PA_INVALID_INDEX
|| !source_name
, tag
, PA_ERR_INVALID
);
1964 CHECK_VALIDITY(c
->pstream
, !source_name
|| source_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1965 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1966 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1967 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1969 p
= pa_proplist_new();
1972 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1974 if (c
->version
>= 12) {
1975 /* Since 0.9.8 the user can ask for a couple of additional flags */
1977 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1978 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1979 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1980 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1981 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1982 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1983 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1986 pa_proplist_free(p
);
1991 if (c
->version
>= 13) {
1993 if (pa_tagstruct_get_boolean(t
, &peak_detect
) < 0 ||
1994 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1995 pa_tagstruct_get_proplist(t
, p
) < 0 ||
1996 pa_tagstruct_getu32(t
, &direct_on_input_idx
) < 0) {
1998 pa_proplist_free(p
);
2003 if (c
->version
>= 14) {
2005 if (pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
2007 pa_proplist_free(p
);
2012 if (c
->version
>= 15) {
2014 if (pa_tagstruct_get_boolean(t
, &dont_inhibit_auto_suspend
) < 0) {
2016 pa_proplist_free(p
);
2021 if (!pa_tagstruct_eof(t
)) {
2023 pa_proplist_free(p
);
2027 if (source_index
!= PA_INVALID_INDEX
) {
2029 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
2030 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2031 pa_proplist_free(p
);
2035 } else if (source_name
) {
2037 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
))) {
2038 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2039 pa_proplist_free(p
);
2044 if (direct_on_input_idx
!= PA_INVALID_INDEX
) {
2046 if (!(direct_on_input
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, direct_on_input_idx
))) {
2047 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2048 pa_proplist_free(p
);
2054 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
2055 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
2056 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
2057 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
2058 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
2059 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
2060 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
2061 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0) |
2062 (dont_inhibit_auto_suspend
? PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND
: 0);
2064 s
= record_stream_new(c
, source
, &ss
, &map
, peak_detect
, &maxlength
, &fragment_size
, flags
, p
, adjust_latency
, direct_on_input
, early_requests
);
2065 pa_proplist_free(p
);
2067 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2069 reply
= reply_new(tag
);
2070 pa_tagstruct_putu32(reply
, s
->index
);
2071 pa_assert(s
->source_output
);
2072 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
2074 if (c
->version
>= 9) {
2075 /* Since 0.9 we support sending the buffer metrics back to the client */
2077 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
2078 pa_tagstruct_putu32(reply
, (uint32_t) fragment_size
);
2081 if (c
->version
>= 12) {
2082 /* Since 0.9.8 we support sending the chosen sample
2083 * spec/channel map/device/suspend status back to the
2086 pa_tagstruct_put_sample_spec(reply
, &ss
);
2087 pa_tagstruct_put_channel_map(reply
, &map
);
2089 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
2090 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
2092 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
2095 if (c
->version
>= 13)
2096 pa_tagstruct_put_usec(reply
, s
->source_latency
);
2098 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2101 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2102 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2105 pa_native_connection_assert_ref(c
);
2108 if (!pa_tagstruct_eof(t
)) {
2113 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2114 ret
= pa_core_exit(c
->protocol
->core
, FALSE
, 0);
2115 CHECK_VALIDITY(c
->pstream
, ret
>= 0, tag
, PA_ERR_ACCESS
);
2117 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
2120 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2121 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2123 pa_tagstruct
*reply
;
2124 pa_bool_t shm_on_remote
= FALSE
, do_shm
;
2126 pa_native_connection_assert_ref(c
);
2129 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
2130 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
2131 !pa_tagstruct_eof(t
)) {
2136 /* Minimum supported version */
2137 if (c
->version
< 8) {
2138 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
2142 /* Starting with protocol version 13 the MSB of the version tag
2143 reflects if shm is available for this pa_native_connection or
2145 if (c
->version
>= 13) {
2146 shm_on_remote
= !!(c
->version
& 0x80000000U
);
2147 c
->version
&= 0x7FFFFFFFU
;
2150 pa_log_debug("Protocol version: remote %u, local %u", c
->version
, PA_PROTOCOL_VERSION
);
2152 pa_proplist_setf(c
->client
->proplist
, "native-protocol.version", "%u", c
->version
);
2154 if (!c
->authorized
) {
2155 pa_bool_t success
= FALSE
;
2158 const pa_creds
*creds
;
2160 if ((creds
= pa_pdispatch_creds(pd
))) {
2161 if (creds
->uid
== getuid())
2163 else if (c
->options
->auth_group
) {
2167 if ((gid
= pa_get_gid_of_group(c
->options
->auth_group
)) == (gid_t
) -1)
2168 pa_log_warn("Failed to get GID of group '%s'", c
->options
->auth_group
);
2169 else if (gid
== creds
->gid
)
2173 if ((r
= pa_uid_in_group(creds
->uid
, c
->options
->auth_group
)) < 0)
2174 pa_log_warn("Failed to check group membership.");
2180 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
2181 (unsigned long) creds
->uid
,
2182 (unsigned long) creds
->gid
,
2187 if (!success
&& c
->options
->auth_cookie
) {
2190 if ((ac
= pa_auth_cookie_read(c
->options
->auth_cookie
, PA_NATIVE_COOKIE_LENGTH
)))
2191 if (memcmp(ac
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
2196 pa_log_warn("Denied access to client with invalid authorization data.");
2197 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2201 c
->authorized
= TRUE
;
2202 if (c
->auth_timeout_event
) {
2203 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
2204 c
->auth_timeout_event
= NULL
;
2208 /* Enable shared memory support if possible */
2210 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
2213 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm
));
2216 if (c
->version
< 10 || (c
->version
>= 13 && !shm_on_remote
))
2221 /* Only enable SHM if both sides are owned by the same
2222 * user. This is a security measure because otherwise data
2223 * private to the user might leak. */
2225 const pa_creds
*creds
;
2226 if (!(creds
= pa_pdispatch_creds(pd
)) || getuid() != creds
->uid
)
2231 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm
));
2232 pa_pstream_enable_shm(c
->pstream
, do_shm
);
2234 reply
= reply_new(tag
);
2235 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
| (do_shm
? 0x80000000 : 0));
2239 /* SHM support is only enabled after both sides made sure they are the same user. */
2243 ucred
.uid
= getuid();
2244 ucred
.gid
= getgid();
2246 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
2249 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2253 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2254 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2255 const char *name
= NULL
;
2257 pa_tagstruct
*reply
;
2259 pa_native_connection_assert_ref(c
);
2262 p
= pa_proplist_new();
2264 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
2265 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2266 !pa_tagstruct_eof(t
)) {
2269 pa_proplist_free(p
);
2274 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
2275 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2276 pa_proplist_free(p
);
2280 pa_proplist_update(c
->client
->proplist
, PA_UPDATE_REPLACE
, p
);
2281 pa_proplist_free(p
);
2283 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2285 reply
= reply_new(tag
);
2287 if (c
->version
>= 13)
2288 pa_tagstruct_putu32(reply
, c
->client
->index
);
2290 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2293 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2294 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2296 uint32_t idx
= PA_IDXSET_INVALID
;
2298 pa_native_connection_assert_ref(c
);
2301 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2302 !pa_tagstruct_eof(t
)) {
2307 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2308 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2310 if (command
== PA_COMMAND_LOOKUP_SINK
) {
2312 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
)))
2316 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
2317 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
)))
2318 idx
= source
->index
;
2321 if (idx
== PA_IDXSET_INVALID
)
2322 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2324 pa_tagstruct
*reply
;
2325 reply
= reply_new(tag
);
2326 pa_tagstruct_putu32(reply
, idx
);
2327 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2331 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2332 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2336 pa_native_connection_assert_ref(c
);
2339 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2340 !pa_tagstruct_eof(t
)) {
2345 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2346 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2347 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2348 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2350 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
);
2353 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2354 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2355 pa_tagstruct
*reply
;
2356 const pa_mempool_stat
*stat
;
2358 pa_native_connection_assert_ref(c
);
2361 if (!pa_tagstruct_eof(t
)) {
2366 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2368 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
2370 reply
= reply_new(tag
);
2371 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
2372 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
2373 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
2374 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
2375 pa_tagstruct_putu32(reply
, (uint32_t) pa_scache_total_size(c
->protocol
->core
));
2376 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2379 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2380 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2381 pa_tagstruct
*reply
;
2383 struct timeval tv
, now
;
2387 pa_native_connection_assert_ref(c
);
2390 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2391 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2392 !pa_tagstruct_eof(t
)) {
2397 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2398 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2399 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2400 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2401 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
)
2403 reply
= reply_new(tag
);
2405 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
2406 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
2408 pa_tagstruct_put_usec(reply
, latency
);
2410 pa_tagstruct_put_usec(reply
, 0);
2411 pa_tagstruct_put_boolean(reply
, s
->sink_input
->thread_info
.playing_for
> 0);
2412 pa_tagstruct_put_timeval(reply
, &tv
);
2413 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2414 pa_tagstruct_puts64(reply
, s
->write_index
);
2415 pa_tagstruct_puts64(reply
, s
->read_index
);
2417 if (c
->version
>= 13) {
2418 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.underrun_for
);
2419 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.playing_for
);
2422 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2425 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2426 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2427 pa_tagstruct
*reply
;
2429 struct timeval tv
, now
;
2432 pa_native_connection_assert_ref(c
);
2435 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2436 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2437 !pa_tagstruct_eof(t
)) {
2442 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2443 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2444 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2446 reply
= reply_new(tag
);
2447 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
2448 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
2449 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_RUNNING
);
2450 pa_tagstruct_put_timeval(reply
, &tv
);
2451 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2452 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
2453 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
2454 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2457 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2458 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2461 const char *name
= NULL
;
2464 pa_tagstruct
*reply
;
2467 pa_native_connection_assert_ref(c
);
2470 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2471 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
2472 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
2473 pa_tagstruct_getu32(t
, &length
) < 0) {
2478 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2479 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
2480 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
2481 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
2482 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
2483 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
2485 p
= pa_proplist_new();
2487 if (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) {
2489 pa_proplist_free(p
);
2493 if (c
->version
< 13)
2494 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2496 if (!(name
= pa_proplist_gets(p
, PA_PROP_EVENT_ID
)))
2497 name
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
);
2499 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2501 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
2502 pa_proplist_free(p
);
2504 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2506 reply
= reply_new(tag
);
2507 pa_tagstruct_putu32(reply
, s
->index
);
2508 pa_tagstruct_putu32(reply
, length
);
2509 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2512 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2513 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2518 pa_native_connection_assert_ref(c
);
2521 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
2522 !pa_tagstruct_eof(t
)) {
2527 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2529 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
2530 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2531 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2533 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
2534 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
2536 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2538 upload_stream_unlink(s
);
2541 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2542 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2543 uint32_t sink_index
;
2546 const char *name
, *sink_name
;
2549 pa_tagstruct
*reply
;
2551 pa_native_connection_assert_ref(c
);
2554 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2556 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
2557 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
2558 pa_tagstruct_getu32(t
, &volume
) < 0 ||
2559 pa_tagstruct_gets(t
, &name
) < 0) {
2564 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
2565 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
2566 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2567 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2569 if (sink_index
!= PA_INVALID_INDEX
)
2570 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
2572 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
);
2574 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2576 p
= pa_proplist_new();
2578 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2579 !pa_tagstruct_eof(t
)) {
2581 pa_proplist_free(p
);
2585 pa_proplist_update(p
, PA_UPDATE_MERGE
, c
->client
->proplist
);
2587 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2588 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2589 pa_proplist_free(p
);
2593 pa_proplist_free(p
);
2595 reply
= reply_new(tag
);
2597 if (c
->version
>= 13)
2598 pa_tagstruct_putu32(reply
, idx
);
2600 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2603 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2604 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2607 pa_native_connection_assert_ref(c
);
2610 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2611 !pa_tagstruct_eof(t
)) {
2616 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2617 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2619 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2620 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2624 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2627 static void fixup_sample_spec(pa_native_connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2630 pa_assert(original
);
2634 if (c
->version
< 12) {
2635 /* Before protocol version 12 we didn't support S32 samples,
2636 * so we need to lie about this to the client */
2638 if (fixed
->format
== PA_SAMPLE_S32LE
)
2639 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2640 if (fixed
->format
== PA_SAMPLE_S32BE
)
2641 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2644 if (c
->version
< 15) {
2645 if (fixed
->format
== PA_SAMPLE_S24LE
|| fixed
->format
== PA_SAMPLE_S24_32LE
)
2646 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2647 if (fixed
->format
== PA_SAMPLE_S24BE
|| fixed
->format
== PA_SAMPLE_S24_32BE
)
2648 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2652 static void sink_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2653 pa_sample_spec fixed_ss
;
2656 pa_sink_assert_ref(sink
);
2658 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2662 PA_TAG_U32
, sink
->index
,
2663 PA_TAG_STRING
, sink
->name
,
2664 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2665 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2666 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2667 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2668 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, FALSE
),
2669 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
, FALSE
),
2670 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2671 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2672 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2673 PA_TAG_STRING
, sink
->driver
,
2674 PA_TAG_U32
, sink
->flags
,
2677 if (c
->version
>= 13) {
2678 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2679 pa_tagstruct_put_usec(t
, pa_sink_get_requested_latency(sink
));
2682 if (c
->version
>= 15)
2683 pa_tagstruct_put_volume(t
, sink
->base_volume
);
2686 static void source_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2687 pa_sample_spec fixed_ss
;
2690 pa_source_assert_ref(source
);
2692 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2696 PA_TAG_U32
, source
->index
,
2697 PA_TAG_STRING
, source
->name
,
2698 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2699 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2700 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2701 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2702 PA_TAG_CVOLUME
, pa_source_get_volume(source
, FALSE
),
2703 PA_TAG_BOOLEAN
, pa_source_get_mute(source
, FALSE
),
2704 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2705 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2706 PA_TAG_USEC
, pa_source_get_latency(source
),
2707 PA_TAG_STRING
, source
->driver
,
2708 PA_TAG_U32
, source
->flags
,
2711 if (c
->version
>= 13) {
2712 pa_tagstruct_put_proplist(t
, source
->proplist
);
2713 pa_tagstruct_put_usec(t
, pa_source_get_requested_latency(source
));
2716 if (c
->version
>= 15)
2717 pa_tagstruct_put_volume(t
, source
->base_volume
);
2720 static void client_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2724 pa_tagstruct_putu32(t
, client
->index
);
2725 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2726 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2727 pa_tagstruct_puts(t
, client
->driver
);
2729 if (c
->version
>= 13)
2730 pa_tagstruct_put_proplist(t
, client
->proplist
);
2733 static void module_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_module
*module
) {
2737 pa_tagstruct_putu32(t
, module
->index
);
2738 pa_tagstruct_puts(t
, module
->name
);
2739 pa_tagstruct_puts(t
, module
->argument
);
2740 pa_tagstruct_putu32(t
, (uint32_t) pa_module_get_n_used(module
));
2742 if (c
->version
< 15)
2743 pa_tagstruct_put_boolean(t
, FALSE
); /* autoload is obsolete */
2745 if (c
->version
>= 15)
2746 pa_tagstruct_put_proplist(t
, module
->proplist
);
2749 static void sink_input_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2750 pa_sample_spec fixed_ss
;
2751 pa_usec_t sink_latency
;
2754 pa_sink_input_assert_ref(s
);
2756 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2758 pa_tagstruct_putu32(t
, s
->index
);
2759 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2760 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2761 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2762 pa_tagstruct_putu32(t
, s
->sink
->index
);
2763 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2764 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2765 pa_tagstruct_put_cvolume(t
, pa_sink_input_get_volume(s
));
2766 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
, &sink_latency
));
2767 pa_tagstruct_put_usec(t
, sink_latency
);
2768 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2769 pa_tagstruct_puts(t
, s
->driver
);
2770 if (c
->version
>= 11)
2771 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2772 if (c
->version
>= 13)
2773 pa_tagstruct_put_proplist(t
, s
->proplist
);
2776 static void source_output_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2777 pa_sample_spec fixed_ss
;
2778 pa_usec_t source_latency
;
2781 pa_source_output_assert_ref(s
);
2783 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2785 pa_tagstruct_putu32(t
, s
->index
);
2786 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2787 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2788 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2789 pa_tagstruct_putu32(t
, s
->source
->index
);
2790 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2791 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2792 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
, &source_latency
));
2793 pa_tagstruct_put_usec(t
, source_latency
);
2794 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2795 pa_tagstruct_puts(t
, s
->driver
);
2797 if (c
->version
>= 13)
2798 pa_tagstruct_put_proplist(t
, s
->proplist
);
2801 static void scache_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2802 pa_sample_spec fixed_ss
;
2807 if (e
->memchunk
.memblock
)
2808 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2810 memset(&fixed_ss
, 0, sizeof(fixed_ss
));
2812 pa_tagstruct_putu32(t
, e
->index
);
2813 pa_tagstruct_puts(t
, e
->name
);
2814 pa_tagstruct_put_cvolume(t
, &e
->volume
);
2815 pa_tagstruct_put_usec(t
, e
->memchunk
.memblock
? pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
) : 0);
2816 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2817 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2818 pa_tagstruct_putu32(t
, (uint32_t) e
->memchunk
.length
);
2819 pa_tagstruct_put_boolean(t
, e
->lazy
);
2820 pa_tagstruct_puts(t
, e
->filename
);
2822 if (c
->version
>= 13)
2823 pa_tagstruct_put_proplist(t
, e
->proplist
);
2826 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2827 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2829 pa_sink
*sink
= NULL
;
2830 pa_source
*source
= NULL
;
2831 pa_client
*client
= NULL
;
2832 pa_module
*module
= NULL
;
2833 pa_sink_input
*si
= NULL
;
2834 pa_source_output
*so
= NULL
;
2835 pa_scache_entry
*sce
= NULL
;
2836 const char *name
= NULL
;
2837 pa_tagstruct
*reply
;
2839 pa_native_connection_assert_ref(c
);
2842 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2843 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2844 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2845 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2846 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2847 pa_tagstruct_gets(t
, &name
) < 0) ||
2848 !pa_tagstruct_eof(t
)) {
2853 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2854 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2855 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2856 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2857 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2859 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2860 if (idx
!= PA_INVALID_INDEX
)
2861 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2863 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
2864 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2865 if (idx
!= PA_INVALID_INDEX
)
2866 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2868 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
2869 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2870 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2871 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2872 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2873 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2874 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2875 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2876 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2878 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2879 if (idx
!= PA_INVALID_INDEX
)
2880 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2882 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
);
2885 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
2886 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2890 reply
= reply_new(tag
);
2892 sink_fill_tagstruct(c
, reply
, sink
);
2894 source_fill_tagstruct(c
, reply
, source
);
2896 client_fill_tagstruct(c
, reply
, client
);
2898 module_fill_tagstruct(c
, reply
, module
);
2900 sink_input_fill_tagstruct(c
, reply
, si
);
2902 source_output_fill_tagstruct(c
, reply
, so
);
2904 scache_fill_tagstruct(c
, reply
, sce
);
2905 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2908 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2909 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2913 pa_tagstruct
*reply
;
2915 pa_native_connection_assert_ref(c
);
2918 if (!pa_tagstruct_eof(t
)) {
2923 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2925 reply
= reply_new(tag
);
2927 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2928 i
= c
->protocol
->core
->sinks
;
2929 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2930 i
= c
->protocol
->core
->sources
;
2931 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2932 i
= c
->protocol
->core
->clients
;
2933 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2934 i
= c
->protocol
->core
->modules
;
2935 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2936 i
= c
->protocol
->core
->sink_inputs
;
2937 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2938 i
= c
->protocol
->core
->source_outputs
;
2940 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2941 i
= c
->protocol
->core
->scache
;
2945 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2946 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2947 sink_fill_tagstruct(c
, reply
, p
);
2948 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2949 source_fill_tagstruct(c
, reply
, p
);
2950 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2951 client_fill_tagstruct(c
, reply
, p
);
2952 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2953 module_fill_tagstruct(c
, reply
, p
);
2954 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2955 sink_input_fill_tagstruct(c
, reply
, p
);
2956 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2957 source_output_fill_tagstruct(c
, reply
, p
);
2959 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2960 scache_fill_tagstruct(c
, reply
, p
);
2965 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2968 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2969 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2970 pa_tagstruct
*reply
;
2973 pa_sample_spec fixed_ss
;
2975 pa_native_connection_assert_ref(c
);
2978 if (!pa_tagstruct_eof(t
)) {
2983 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2985 reply
= reply_new(tag
);
2986 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
2987 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
2988 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
2989 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
2991 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
2992 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
2994 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
2995 pa_tagstruct_puts(reply
, n
);
2996 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
2997 pa_tagstruct_puts(reply
, n
);
2999 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
3001 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3004 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
3006 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3008 pa_native_connection_assert_ref(c
);
3010 t
= pa_tagstruct_new(NULL
, 0);
3011 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
3012 pa_tagstruct_putu32(t
, (uint32_t) -1);
3013 pa_tagstruct_putu32(t
, e
);
3014 pa_tagstruct_putu32(t
, idx
);
3015 pa_pstream_send_tagstruct(c
->pstream
, t
);
3018 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3019 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3020 pa_subscription_mask_t m
;
3022 pa_native_connection_assert_ref(c
);
3025 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
3026 !pa_tagstruct_eof(t
)) {
3031 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3032 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
3034 if (c
->subscription
)
3035 pa_subscription_free(c
->subscription
);
3038 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
3039 pa_assert(c
->subscription
);
3041 c
->subscription
= NULL
;
3043 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3046 static void command_set_volume(
3053 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3056 pa_sink
*sink
= NULL
;
3057 pa_source
*source
= NULL
;
3058 pa_sink_input
*si
= NULL
;
3059 const char *name
= NULL
;
3061 pa_native_connection_assert_ref(c
);
3064 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3065 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3066 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3067 pa_tagstruct_get_cvolume(t
, &volume
) ||
3068 !pa_tagstruct_eof(t
)) {
3073 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3074 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3075 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3076 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3077 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3078 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
3082 case PA_COMMAND_SET_SINK_VOLUME
:
3083 if (idx
!= PA_INVALID_INDEX
)
3084 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3086 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3089 case PA_COMMAND_SET_SOURCE_VOLUME
:
3090 if (idx
!= PA_INVALID_INDEX
)
3091 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3093 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3096 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
3097 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3101 pa_assert_not_reached();
3104 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3107 pa_sink_set_volume(sink
, &volume
);
3109 pa_source_set_volume(source
, &volume
);
3111 pa_sink_input_set_volume(si
, &volume
);
3113 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3116 static void command_set_mute(
3123 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3126 pa_sink
*sink
= NULL
;
3127 pa_source
*source
= NULL
;
3128 pa_sink_input
*si
= NULL
;
3129 const char *name
= NULL
;
3131 pa_native_connection_assert_ref(c
);
3134 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3135 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3136 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3137 pa_tagstruct_get_boolean(t
, &mute
) ||
3138 !pa_tagstruct_eof(t
)) {
3143 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3144 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3145 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3146 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3147 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3151 case PA_COMMAND_SET_SINK_MUTE
:
3153 if (idx
!= PA_INVALID_INDEX
)
3154 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3156 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3160 case PA_COMMAND_SET_SOURCE_MUTE
:
3161 if (idx
!= PA_INVALID_INDEX
)
3162 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3164 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3168 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
3169 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3173 pa_assert_not_reached();
3176 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3179 pa_sink_set_mute(sink
, mute
);
3181 pa_source_set_mute(source
, mute
);
3183 pa_sink_input_set_mute(si
, mute
);
3185 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3188 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3189 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3194 pa_native_connection_assert_ref(c
);
3197 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3198 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3199 !pa_tagstruct_eof(t
)) {
3204 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3205 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3206 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3207 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3208 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3210 pa_sink_input_cork(s
->sink_input
, b
);
3213 s
->is_underrun
= TRUE
;
3215 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3218 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3219 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3223 pa_native_connection_assert_ref(c
);
3226 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3227 !pa_tagstruct_eof(t
)) {
3232 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3233 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3234 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3235 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3236 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3239 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3240 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3243 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3244 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3247 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3248 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3252 pa_assert_not_reached();
3255 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3258 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3259 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3264 pa_native_connection_assert_ref(c
);
3267 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3268 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3269 !pa_tagstruct_eof(t
)) {
3274 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3275 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3276 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3278 pa_source_output_cork(s
->source_output
, b
);
3279 pa_memblockq_prebuf_force(s
->memblockq
);
3280 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3283 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3284 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3288 pa_native_connection_assert_ref(c
);
3291 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3292 !pa_tagstruct_eof(t
)) {
3297 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3298 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3299 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3301 pa_memblockq_flush_read(s
->memblockq
);
3302 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3305 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3306 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3308 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3309 pa_tagstruct
*reply
;
3311 pa_native_connection_assert_ref(c
);
3314 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3319 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3321 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3323 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3325 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3326 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3327 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3329 if (pa_tagstruct_get(
3331 PA_TAG_U32
, &maxlength
,
3332 PA_TAG_U32
, &tlength
,
3333 PA_TAG_U32
, &prebuf
,
3334 PA_TAG_U32
, &minreq
,
3335 PA_TAG_INVALID
) < 0 ||
3336 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3337 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3338 !pa_tagstruct_eof(t
)) {
3343 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3344 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3345 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3346 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3347 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3348 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3350 reply
= reply_new(tag
);
3351 pa_tagstruct_putu32(reply
, maxlength
);
3352 pa_tagstruct_putu32(reply
, tlength
);
3353 pa_tagstruct_putu32(reply
, prebuf
);
3354 pa_tagstruct_putu32(reply
, minreq
);
3356 if (c
->version
>= 13)
3357 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3361 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3362 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3364 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3365 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3367 if (pa_tagstruct_get(
3369 PA_TAG_U32
, &maxlength
,
3370 PA_TAG_U32
, &fragsize
,
3371 PA_TAG_INVALID
) < 0 ||
3372 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3373 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3374 !pa_tagstruct_eof(t
)) {
3379 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &fragsize
);
3380 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3381 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3383 reply
= reply_new(tag
);
3384 pa_tagstruct_putu32(reply
, maxlength
);
3385 pa_tagstruct_putu32(reply
, fragsize
);
3387 if (c
->version
>= 13)
3388 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3391 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3394 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3395 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3399 pa_native_connection_assert_ref(c
);
3402 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3403 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3404 !pa_tagstruct_eof(t
)) {
3409 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3410 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3412 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3415 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3416 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3417 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3419 pa_sink_input_set_rate(s
->sink_input
, rate
);
3423 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3425 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3426 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3428 pa_source_output_set_rate(s
->source_output
, rate
);
3431 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3434 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3435 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3440 pa_native_connection_assert_ref(c
);
3443 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3445 p
= pa_proplist_new();
3447 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3449 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3450 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3451 !pa_tagstruct_eof(t
)) {
3453 pa_proplist_free(p
);
3459 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3460 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3461 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3462 !pa_tagstruct_eof(t
)) {
3464 pa_proplist_free(p
);
3469 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3471 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3474 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3475 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3476 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3478 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
3479 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3481 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3484 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3485 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3487 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
3488 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3490 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3492 pa_proplist_update(c
->client
->proplist
, mode
, p
);
3493 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3496 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3499 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3500 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3502 unsigned changed
= 0;
3504 pa_strlist
*l
= NULL
;
3506 pa_native_connection_assert_ref(c
);
3509 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3511 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3513 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3519 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3522 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3523 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3524 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3526 p
= s
->sink_input
->proplist
;
3528 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3531 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3532 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3534 p
= s
->source_output
->proplist
;
3536 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3538 p
= c
->client
->proplist
;
3544 if (pa_tagstruct_gets(t
, &k
) < 0) {
3553 l
= pa_strlist_prepend(l
, k
);
3556 if (!pa_tagstruct_eof(t
)) {
3565 l
= pa_strlist_pop(l
, &z
);
3570 changed
+= (unsigned) (pa_proplist_unset(p
, z
) >= 0);
3574 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3577 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3580 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3581 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3583 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3586 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3587 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3590 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3591 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3596 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3597 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3600 pa_native_connection_assert_ref(c
);
3603 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3604 !pa_tagstruct_eof(t
)) {
3609 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3610 CHECK_VALIDITY(c
->pstream
, !s
|| pa_namereg_is_valid_name(s
), tag
, PA_ERR_INVALID
);
3612 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
3613 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3616 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3617 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3621 pa_native_connection_assert_ref(c
);
3624 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3625 pa_tagstruct_gets(t
, &name
) < 0 ||
3626 !pa_tagstruct_eof(t
)) {
3631 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3632 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3634 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3637 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3638 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3639 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3641 pa_sink_input_set_name(s
->sink_input
, name
);
3645 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3647 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3648 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3650 pa_source_output_set_name(s
->source_output
, name
);
3653 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3656 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3657 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3660 pa_native_connection_assert_ref(c
);
3663 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3664 !pa_tagstruct_eof(t
)) {
3669 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3671 if (command
== PA_COMMAND_KILL_CLIENT
) {
3674 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3675 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3677 pa_native_connection_ref(c
);
3678 pa_client_kill(client
);
3680 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3683 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3684 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3686 pa_native_connection_ref(c
);
3687 pa_sink_input_kill(s
);
3689 pa_source_output
*s
;
3691 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3693 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3694 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3696 pa_native_connection_ref(c
);
3697 pa_source_output_kill(s
);
3700 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3701 pa_native_connection_unref(c
);
3704 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3705 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3707 const char *name
, *argument
;
3708 pa_tagstruct
*reply
;
3710 pa_native_connection_assert_ref(c
);
3713 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3714 pa_tagstruct_gets(t
, &argument
) < 0 ||
3715 !pa_tagstruct_eof(t
)) {
3720 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3721 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3722 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3724 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3725 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3729 reply
= reply_new(tag
);
3730 pa_tagstruct_putu32(reply
, m
->index
);
3731 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3734 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3735 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3739 pa_native_connection_assert_ref(c
);
3742 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3743 !pa_tagstruct_eof(t
)) {
3748 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3749 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3750 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3752 pa_module_unload_request(m
, FALSE
);
3753 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3756 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3757 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3758 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3759 const char *name_device
= NULL
;
3761 pa_native_connection_assert_ref(c
);
3764 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3765 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3766 pa_tagstruct_gets(t
, &name_device
) < 0 ||
3767 !pa_tagstruct_eof(t
)) {
3772 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3773 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3775 CHECK_VALIDITY(c
->pstream
, !name_device
|| pa_namereg_is_valid_name(name_device
), tag
, PA_ERR_INVALID
);
3776 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| name_device
, tag
, PA_ERR_INVALID
);
3777 CHECK_VALIDITY(c
->pstream
, idx_device
== PA_INVALID_INDEX
|| !name_device
, tag
, PA_ERR_INVALID
);
3778 CHECK_VALIDITY(c
->pstream
, !name_device
|| idx_device
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3780 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3781 pa_sink_input
*si
= NULL
;
3782 pa_sink
*sink
= NULL
;
3784 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3786 if (idx_device
!= PA_INVALID_INDEX
)
3787 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3789 sink
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SINK
);
3791 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3793 if (pa_sink_input_move_to(si
, sink
) < 0) {
3794 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3798 pa_source_output
*so
= NULL
;
3801 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3803 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3805 if (idx_device
!= PA_INVALID_INDEX
)
3806 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3808 source
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SOURCE
);
3810 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3812 if (pa_source_output_move_to(so
, source
) < 0) {
3813 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3818 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3821 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3822 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3823 uint32_t idx
= PA_INVALID_INDEX
;
3824 const char *name
= NULL
;
3827 pa_native_connection_assert_ref(c
);
3830 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3831 pa_tagstruct_gets(t
, &name
) < 0 ||
3832 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3833 !pa_tagstruct_eof(t
)) {
3838 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3839 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
) || *name
== 0, tag
, PA_ERR_INVALID
);
3840 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3841 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3842 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3844 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3846 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3848 pa_log_debug("%s all sinks", b
? "Suspending" : "Resuming");
3850 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3851 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3855 pa_sink
*sink
= NULL
;
3857 if (idx
!= PA_INVALID_INDEX
)
3858 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3860 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3862 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3864 if (pa_sink_suspend(sink
, b
) < 0) {
3865 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3871 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3873 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3875 pa_log_debug("%s all sources", b
? "Suspending" : "Resuming");
3877 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3878 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3885 if (idx
!= PA_INVALID_INDEX
)
3886 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3888 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3890 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3892 if (pa_source_suspend(source
, b
) < 0) {
3893 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3899 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3902 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3903 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3904 uint32_t idx
= PA_INVALID_INDEX
;
3905 const char *name
= NULL
;
3907 pa_native_protocol_ext_cb_t cb
;
3909 pa_native_connection_assert_ref(c
);
3912 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3913 pa_tagstruct_gets(t
, &name
) < 0) {
3918 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3919 CHECK_VALIDITY(c
->pstream
, !name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3920 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3921 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3922 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3924 if (idx
!= PA_INVALID_INDEX
)
3925 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3927 for (m
= pa_idxset_first(c
->protocol
->core
->modules
, &idx
); m
; m
= pa_idxset_next(c
->protocol
->core
->modules
, &idx
))
3928 if (strcmp(name
, m
->name
) == 0)
3932 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3933 CHECK_VALIDITY(c
->pstream
, m
->load_once
|| idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3935 cb
= (pa_native_protocol_ext_cb_t
) pa_hashmap_get(c
->protocol
->extensions
, m
);
3936 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3938 if (cb(c
->protocol
, m
, c
, tag
, t
) < 0)
3943 /*** pstream callbacks ***/
3945 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
3946 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3950 pa_native_connection_assert_ref(c
);
3952 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
3953 pa_log("invalid packet.");
3954 native_connection_unlink(c
);
3958 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
) {
3959 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3960 output_stream
*stream
;
3964 pa_native_connection_assert_ref(c
);
3966 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
3967 pa_log("client sent block for invalid stream.");
3972 /* pa_log("got %lu bytes", (unsigned long) chunk->length); */
3974 if (playback_stream_isinstance(stream
)) {
3975 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
3977 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
3978 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
);
3980 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
3983 upload_stream
*u
= UPLOAD_STREAM(stream
);
3986 if (!u
->memchunk
.memblock
) {
3987 if (u
->length
== chunk
->length
) {
3988 u
->memchunk
= *chunk
;
3989 pa_memblock_ref(u
->memchunk
.memblock
);
3992 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
3993 u
->memchunk
.index
= u
->memchunk
.length
= 0;
3997 pa_assert(u
->memchunk
.memblock
);
4000 if (l
> chunk
->length
)
4006 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
4007 src
= pa_memblock_acquire(chunk
->memblock
);
4009 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
4010 (uint8_t*) src
+chunk
->index
, l
);
4012 pa_memblock_release(u
->memchunk
.memblock
);
4013 pa_memblock_release(chunk
->memblock
);
4015 u
->memchunk
.length
+= l
;
4021 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
4022 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4025 pa_native_connection_assert_ref(c
);
4027 native_connection_unlink(c
);
4028 pa_log_info("Connection died.");
4031 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
4032 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4035 pa_native_connection_assert_ref(c
);
4037 native_connection_send_memblock(c
);
4040 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4043 if (!(q
= pa_thread_mq_get()))
4044 pa_pstream_send_revoke(p
, block_id
);
4046 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4049 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4052 if (!(q
= pa_thread_mq_get()))
4053 pa_pstream_send_release(p
, block_id
);
4055 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4058 /*** client callbacks ***/
4060 static void client_kill_cb(pa_client
*c
) {
4063 native_connection_unlink(PA_NATIVE_CONNECTION(c
->userdata
));
4064 pa_log_info("Connection killed.");
4067 /*** module entry points ***/
4069 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
4070 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4074 pa_native_connection_assert_ref(c
);
4075 pa_assert(c
->auth_timeout_event
== e
);
4077 if (!c
->authorized
) {
4078 native_connection_unlink(c
);
4079 pa_log_info("Connection terminated due to authentication timeout.");
4083 void pa_native_protocol_connect(pa_native_protocol
*p
, pa_iochannel
*io
, pa_native_options
*o
) {
4084 pa_native_connection
*c
;
4087 pa_client_new_data data
;
4093 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
4094 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
4095 pa_iochannel_free(io
);
4099 pa_client_new_data_init(&data
);
4100 data
.module
= o
->module
;
4101 data
.driver
= __FILE__
;
4102 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
4103 pa_proplist_setf(data
.proplist
, PA_PROP_APPLICATION_NAME
, "Native client (%s)", pname
);
4104 pa_proplist_sets(data
.proplist
, "native-protocol.peer", pname
);
4105 client
= pa_client_new(p
->core
, &data
);
4106 pa_client_new_data_done(&data
);
4111 c
= pa_msgobject_new(pa_native_connection
);
4112 c
->parent
.parent
.free
= native_connection_free
;
4113 c
->parent
.process_msg
= native_connection_process_msg
;
4115 c
->options
= pa_native_options_ref(o
);
4116 c
->authorized
= FALSE
;
4118 if (o
->auth_anonymous
) {
4119 pa_log_info("Client authenticated anonymously.");
4120 c
->authorized
= TRUE
;
4123 if (!c
->authorized
&&
4125 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
4127 pa_log_info("Client authenticated by IP ACL.");
4128 c
->authorized
= TRUE
;
4131 if (!c
->authorized
) {
4133 pa_gettimeofday(&tv
);
4134 tv
.tv_sec
+= AUTH_TIMEOUT
;
4135 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
4137 c
->auth_timeout_event
= NULL
;
4139 c
->is_local
= pa_iochannel_socket_is_local(io
);
4143 c
->client
->kill
= client_kill_cb
;
4144 c
->client
->userdata
= c
;
4146 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
4147 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
4148 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
4149 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
4150 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
4151 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
4152 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
4154 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
4156 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
4157 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
4159 c
->rrobin_index
= PA_IDXSET_INVALID
;
4160 c
->subscription
= NULL
;
4162 pa_idxset_put(p
->connections
, c
, NULL
);
4165 if (pa_iochannel_creds_supported(io
))
4166 pa_iochannel_creds_enable(io
);
4169 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_CONNECTION_PUT
], c
);
4172 void pa_native_protocol_disconnect(pa_native_protocol
*p
, pa_module
*m
) {
4173 pa_native_connection
*c
;
4179 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
4180 if (c
->options
->module
== m
)
4181 native_connection_unlink(c
);
4184 static pa_native_protocol
* native_protocol_new(pa_core
*c
) {
4185 pa_native_protocol
*p
;
4190 p
= pa_xnew(pa_native_protocol
, 1);
4193 p
->connections
= pa_idxset_new(NULL
, NULL
);
4197 p
->extensions
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
4199 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4200 pa_hook_init(&p
->hooks
[h
], p
);
4202 pa_assert_se(pa_shared_set(c
, "native-protocol", p
) >= 0);
4207 pa_native_protocol
* pa_native_protocol_get(pa_core
*c
) {
4208 pa_native_protocol
*p
;
4210 if ((p
= pa_shared_get(c
, "native-protocol")))
4211 return pa_native_protocol_ref(p
);
4213 return native_protocol_new(c
);
4216 pa_native_protocol
* pa_native_protocol_ref(pa_native_protocol
*p
) {
4218 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4225 void pa_native_protocol_unref(pa_native_protocol
*p
) {
4226 pa_native_connection
*c
;
4230 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4232 if (PA_REFCNT_DEC(p
) > 0)
4235 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4236 native_connection_unlink(c
);
4238 pa_idxset_free(p
->connections
, NULL
, NULL
);
4240 pa_strlist_free(p
->servers
);
4242 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4243 pa_hook_done(&p
->hooks
[h
]);
4245 pa_hashmap_free(p
->extensions
, NULL
, NULL
);
4247 pa_assert_se(pa_shared_remove(p
->core
, "native-protocol") >= 0);
4252 void pa_native_protocol_add_server_string(pa_native_protocol
*p
, const char *name
) {
4254 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4257 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
4259 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4262 void pa_native_protocol_remove_server_string(pa_native_protocol
*p
, const char *name
) {
4264 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4267 p
->servers
= pa_strlist_remove(p
->servers
, name
);
4269 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4272 pa_hook
*pa_native_protocol_hooks(pa_native_protocol
*p
) {
4274 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4279 pa_strlist
*pa_native_protocol_servers(pa_native_protocol
*p
) {
4281 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4286 int pa_native_protocol_install_ext(pa_native_protocol
*p
, pa_module
*m
, pa_native_protocol_ext_cb_t cb
) {
4288 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4291 pa_assert(!pa_hashmap_get(p
->extensions
, m
));
4293 pa_assert_se(pa_hashmap_put(p
->extensions
, m
, (void*) cb
) == 0);
4297 void pa_native_protocol_remove_ext(pa_native_protocol
*p
, pa_module
*m
) {
4299 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4302 pa_assert_se(pa_hashmap_remove(p
->extensions
, m
));
4305 pa_native_options
* pa_native_options_new(void) {
4306 pa_native_options
*o
;
4308 o
= pa_xnew0(pa_native_options
, 1);
4314 pa_native_options
* pa_native_options_ref(pa_native_options
*o
) {
4316 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4323 void pa_native_options_unref(pa_native_options
*o
) {
4325 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4327 if (PA_REFCNT_DEC(o
) > 0)
4330 pa_xfree(o
->auth_group
);
4333 pa_ip_acl_free(o
->auth_ip_acl
);
4336 pa_auth_cookie_unref(o
->auth_cookie
);
4341 int pa_native_options_parse(pa_native_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
4346 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4349 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
4350 pa_log("auth-anonymous= expects a boolean argument.");
4355 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &enabled
) < 0) {
4356 pa_log("auth-group-enabled= expects a boolean argument.");
4360 pa_xfree(o
->auth_group
);
4361 o
->auth_group
= enabled
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4365 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4368 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4371 if (!(ipa
= pa_ip_acl_new(acl
))) {
4372 pa_log("Failed to parse IP ACL '%s'", acl
);
4377 pa_ip_acl_free(o
->auth_ip_acl
);
4379 o
->auth_ip_acl
= ipa
;
4383 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
4384 pa_log("auth-cookie-enabled= expects a boolean argument.");
4389 pa_auth_cookie_unref(o
->auth_cookie
);
4394 /* The new name for this is 'auth-cookie', for compat reasons
4395 * we check the old name too */
4396 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
4397 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
4398 cn
= PA_NATIVE_COOKIE_FILE
;
4400 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, PA_NATIVE_COOKIE_LENGTH
)))
4404 o
->auth_cookie
= NULL
;
4409 pa_pstream
* pa_native_connection_get_pstream(pa_native_connection
*c
) {
4410 pa_native_connection_assert_ref(c
);