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_CARD_INFO
] = command_get_info
,
287 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
288 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
289 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
290 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
291 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
292 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
293 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
294 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
295 [PA_COMMAND_GET_CARD_INFO_LIST
] = command_get_info_list
,
296 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
297 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
298 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
299 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
300 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
302 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
303 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
304 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
306 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
307 [PA_COMMAND_SET_SINK_INPUT_MUTE
] = command_set_mute
,
308 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
310 [PA_COMMAND_SUSPEND_SINK
] = command_suspend
,
311 [PA_COMMAND_SUSPEND_SOURCE
] = command_suspend
,
313 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
314 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
315 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
316 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_flush_or_prebuf_playback_stream
,
318 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
319 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
321 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
322 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
323 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
324 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
325 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
326 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
327 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
328 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
329 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
331 [PA_COMMAND_GET_AUTOLOAD_INFO___OBSOLETE
] = NULL
,
332 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST___OBSOLETE
] = NULL
,
333 [PA_COMMAND_ADD_AUTOLOAD___OBSOLETE
] = NULL
,
334 [PA_COMMAND_REMOVE_AUTOLOAD___OBSOLETE
] = NULL
,
336 [PA_COMMAND_MOVE_SINK_INPUT
] = command_move_stream
,
337 [PA_COMMAND_MOVE_SOURCE_OUTPUT
] = command_move_stream
,
339 [PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
340 [PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
] = command_set_stream_buffer_attr
,
342 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
343 [PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
] = command_update_stream_sample_rate
,
345 [PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
] = command_update_proplist
,
346 [PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
] = command_update_proplist
,
347 [PA_COMMAND_UPDATE_CLIENT_PROPLIST
] = command_update_proplist
,
349 [PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
] = command_remove_proplist
,
350 [PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
] = command_remove_proplist
,
351 [PA_COMMAND_REMOVE_CLIENT_PROPLIST
] = command_remove_proplist
,
353 [PA_COMMAND_EXTENSION
] = command_extension
356 /* structure management */
358 static void upload_stream_unlink(upload_stream
*s
) {
364 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
365 s
->connection
= NULL
;
366 upload_stream_unref(s
);
369 static void upload_stream_free(pa_object
*o
) {
370 upload_stream
*s
= UPLOAD_STREAM(o
);
373 upload_stream_unlink(s
);
378 pa_proplist_free(s
->proplist
);
380 if (s
->memchunk
.memblock
)
381 pa_memblock_unref(s
->memchunk
.memblock
);
386 static upload_stream
* upload_stream_new(
387 pa_native_connection
*c
,
388 const pa_sample_spec
*ss
,
389 const pa_channel_map
*map
,
399 pa_assert(length
> 0);
402 s
= pa_msgobject_new(upload_stream
);
403 s
->parent
.parent
.parent
.free
= upload_stream_free
;
405 s
->sample_spec
= *ss
;
406 s
->channel_map
= *map
;
407 s
->name
= pa_xstrdup(name
);
408 pa_memchunk_reset(&s
->memchunk
);
410 s
->proplist
= pa_proplist_copy(p
);
411 pa_proplist_update(s
->proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
413 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
418 static void record_stream_unlink(record_stream
*s
) {
424 if (s
->source_output
) {
425 pa_source_output_unlink(s
->source_output
);
426 pa_source_output_unref(s
->source_output
);
427 s
->source_output
= NULL
;
430 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->record_streams
, s
, NULL
) == s
);
431 s
->connection
= NULL
;
432 record_stream_unref(s
);
435 static void record_stream_free(pa_object
*o
) {
436 record_stream
*s
= RECORD_STREAM(o
);
439 record_stream_unlink(s
);
441 pa_memblockq_free(s
->memblockq
);
445 static int record_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
446 record_stream
*s
= RECORD_STREAM(o
);
447 record_stream_assert_ref(s
);
454 case RECORD_STREAM_MESSAGE_POST_DATA
:
456 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
457 /* pa_log_warn("Failed to push data into output queue."); */
461 if (!pa_pstream_is_pending(s
->connection
->pstream
))
462 native_connection_send_memblock(s
->connection
);
470 static void fix_record_buffer_attr_pre(
472 pa_bool_t adjust_latency
,
473 pa_bool_t early_requests
,
475 uint32_t *fragsize
) {
478 pa_usec_t orig_fragsize_usec
, fragsize_usec
, source_usec
;
481 pa_assert(maxlength
);
484 frame_size
= pa_frame_size(&s
->source_output
->sample_spec
);
486 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
487 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
489 *maxlength
= (uint32_t) frame_size
;
491 if (*fragsize
== (uint32_t) -1)
492 *fragsize
= (uint32_t) pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC
*PA_USEC_PER_MSEC
, &s
->source_output
->sample_spec
);
494 *fragsize
= (uint32_t) frame_size
;
496 orig_fragsize_usec
= fragsize_usec
= pa_bytes_to_usec(*fragsize
, &s
->source_output
->sample_spec
);
498 if (early_requests
) {
500 /* In early request mode we need to emulate the classic
501 * fragment-based playback model. We do this setting the source
502 * latency to the fragment size. */
504 source_usec
= fragsize_usec
;
506 } else if (adjust_latency
) {
508 /* So, the user asked us to adjust the latency according to
509 * what the source can provide. Half the latency will be
510 * spent on the hw buffer, half of it in the async buffer
511 * queue we maintain for each client. */
513 source_usec
= fragsize_usec
/2;
517 /* Ok, the user didn't ask us to adjust the latency, hence we
524 s
->source_latency
= pa_source_output_set_requested_latency(s
->source_output
, source_usec
);
526 s
->source_latency
= 0;
528 if (early_requests
) {
530 /* Ok, we didn't necessarily get what we were asking for, so
531 * let's tell the user */
533 fragsize_usec
= s
->source_latency
;
535 } else if (adjust_latency
) {
537 /* Now subtract what we actually got */
539 if (fragsize_usec
>= s
->source_latency
*2)
540 fragsize_usec
-= s
->source_latency
;
542 fragsize_usec
= s
->source_latency
;
545 if (pa_usec_to_bytes(orig_fragsize_usec
, &s
->source_output
->sample_spec
) !=
546 pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
))
548 *fragsize
= (uint32_t) pa_usec_to_bytes(fragsize_usec
, &s
->source_output
->sample_spec
);
551 *fragsize
= (uint32_t) frame_size
;
554 static void fix_record_buffer_attr_post(
557 uint32_t *fragsize
) {
562 pa_assert(maxlength
);
565 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
567 base
= pa_frame_size(&s
->source_output
->sample_spec
);
569 s
->fragment_size
= (*fragsize
/base
)*base
;
570 if (s
->fragment_size
<= 0)
571 s
->fragment_size
= base
;
573 if (s
->fragment_size
> *maxlength
)
574 s
->fragment_size
= *maxlength
;
576 *fragsize
= (uint32_t) s
->fragment_size
;
579 static record_stream
* record_stream_new(
580 pa_native_connection
*c
,
584 pa_bool_t peak_detect
,
587 pa_source_output_flags_t flags
,
589 pa_bool_t adjust_latency
,
590 pa_sink_input
*direct_on_input
,
591 pa_bool_t early_requests
) {
594 pa_source_output
*source_output
;
596 pa_source_output_new_data data
;
600 pa_assert(maxlength
);
603 pa_source_output_new_data_init(&data
);
605 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
606 data
.driver
= __FILE__
;
607 data
.module
= c
->options
->module
;
608 data
.client
= c
->client
;
609 data
.source
= source
;
610 data
.direct_on_input
= direct_on_input
;
611 pa_source_output_new_data_set_sample_spec(&data
, ss
);
612 pa_source_output_new_data_set_channel_map(&data
, map
);
614 data
.resample_method
= PA_RESAMPLER_PEAKS
;
616 source_output
= pa_source_output_new(c
->protocol
->core
, &data
, flags
);
618 pa_source_output_new_data_done(&data
);
623 s
= pa_msgobject_new(record_stream
);
624 s
->parent
.parent
.free
= record_stream_free
;
625 s
->parent
.process_msg
= record_stream_process_msg
;
627 s
->source_output
= source_output
;
629 s
->source_output
->push
= source_output_push_cb
;
630 s
->source_output
->kill
= source_output_kill_cb
;
631 s
->source_output
->get_latency
= source_output_get_latency_cb
;
632 s
->source_output
->moved
= source_output_moved_cb
;
633 s
->source_output
->suspend
= source_output_suspend_cb
;
634 s
->source_output
->userdata
= s
;
636 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, fragsize
);
638 s
->memblockq
= pa_memblockq_new(
642 base
= pa_frame_size(&source_output
->sample_spec
),
648 fix_record_buffer_attr_post(s
, maxlength
, fragsize
);
650 *ss
= s
->source_output
->sample_spec
;
651 *map
= s
->source_output
->channel_map
;
653 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
655 pa_log_info("Final latency %0.2f ms = %0.2f ms + %0.2f ms",
656 ((double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) + (double) s
->source_latency
) / PA_USEC_PER_MSEC
,
657 (double) pa_bytes_to_usec(s
->fragment_size
, &source_output
->sample_spec
) / PA_USEC_PER_MSEC
,
658 (double) s
->source_latency
/ PA_USEC_PER_MSEC
);
660 pa_source_output_put(s
->source_output
);
664 static void record_stream_send_killed(record_stream
*r
) {
666 record_stream_assert_ref(r
);
668 t
= pa_tagstruct_new(NULL
, 0);
669 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
670 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
671 pa_tagstruct_putu32(t
, r
->index
);
672 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
675 static void playback_stream_unlink(playback_stream
*s
) {
682 pa_sink_input_unlink(s
->sink_input
);
683 pa_sink_input_unref(s
->sink_input
);
684 s
->sink_input
= NULL
;
687 if (s
->drain_request
)
688 pa_pstream_send_error(s
->connection
->pstream
, s
->drain_tag
, PA_ERR_NOENTITY
);
690 pa_assert_se(pa_idxset_remove_by_data(s
->connection
->output_streams
, s
, NULL
) == s
);
691 s
->connection
= NULL
;
692 playback_stream_unref(s
);
695 static void playback_stream_free(pa_object
* o
) {
696 playback_stream
*s
= PLAYBACK_STREAM(o
);
699 playback_stream_unlink(s
);
701 pa_memblockq_free(s
->memblockq
);
705 static int playback_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
706 playback_stream
*s
= PLAYBACK_STREAM(o
);
707 playback_stream_assert_ref(s
);
713 case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
: {
718 if ((l
= (uint32_t) pa_atomic_load(&s
->missing
)) <= 0)
721 if (pa_atomic_cmpxchg(&s
->missing
, (int) l
, 0))
728 t
= pa_tagstruct_new(NULL
, 0);
729 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
730 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
731 pa_tagstruct_putu32(t
, s
->index
);
732 pa_tagstruct_putu32(t
, l
);
733 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
735 /* pa_log("Requesting %lu bytes", (unsigned long) l); */
739 case PLAYBACK_STREAM_MESSAGE_UNDERFLOW
: {
742 /* pa_log("signalling underflow"); */
744 /* Report that we're empty */
745 t
= pa_tagstruct_new(NULL
, 0);
746 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
747 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
748 pa_tagstruct_putu32(t
, s
->index
);
749 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
753 case PLAYBACK_STREAM_MESSAGE_OVERFLOW
: {
756 /* Notify the user we're overflowed*/
757 t
= pa_tagstruct_new(NULL
, 0);
758 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
759 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
760 pa_tagstruct_putu32(t
, s
->index
);
761 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
765 case PLAYBACK_STREAM_MESSAGE_STARTED
:
767 if (s
->connection
->version
>= 13) {
770 /* Notify the user we're overflowed*/
771 t
= pa_tagstruct_new(NULL
, 0);
772 pa_tagstruct_putu32(t
, PA_COMMAND_STARTED
);
773 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
774 pa_tagstruct_putu32(t
, s
->index
);
775 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
780 case PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
:
781 pa_pstream_send_simple_ack(s
->connection
->pstream
, PA_PTR_TO_UINT(userdata
));
788 static void fix_playback_buffer_attr_pre(
790 pa_bool_t adjust_latency
,
791 pa_bool_t early_requests
,
798 pa_usec_t orig_tlength_usec
, tlength_usec
, orig_minreq_usec
, minreq_usec
, sink_usec
;
801 pa_assert(maxlength
);
806 frame_size
= pa_frame_size(&s
->sink_input
->sample_spec
);
808 if (*maxlength
== (uint32_t) -1 || *maxlength
> MAX_MEMBLOCKQ_LENGTH
)
809 *maxlength
= MAX_MEMBLOCKQ_LENGTH
;
811 *maxlength
= (uint32_t) frame_size
;
813 if (*tlength
== (uint32_t) -1)
814 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_TLENGTH_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
816 *tlength
= (uint32_t) frame_size
;
818 if (*minreq
== (uint32_t) -1)
819 *minreq
= (uint32_t) pa_usec_to_bytes_round_up(DEFAULT_PROCESS_MSEC
*PA_USEC_PER_MSEC
, &s
->sink_input
->sample_spec
);
821 *minreq
= (uint32_t) frame_size
;
823 if (*tlength
< *minreq
+frame_size
)
824 *tlength
= *minreq
+(uint32_t) frame_size
;
826 orig_tlength_usec
= tlength_usec
= pa_bytes_to_usec(*tlength
, &s
->sink_input
->sample_spec
);
827 orig_minreq_usec
= minreq_usec
= pa_bytes_to_usec(*minreq
, &s
->sink_input
->sample_spec
);
829 pa_log_info("Requested tlength=%0.2f ms, minreq=%0.2f ms",
830 (double) tlength_usec
/ PA_USEC_PER_MSEC
,
831 (double) minreq_usec
/ PA_USEC_PER_MSEC
);
833 if (early_requests
) {
835 /* In early request mode we need to emulate the classic
836 * fragment-based playback model. We do this setting the sink
837 * latency to the fragment size. */
839 sink_usec
= minreq_usec
;
841 pa_log_debug("Early requests mode enabled, configuring sink latency to minreq.");
843 } else if (adjust_latency
) {
845 /* So, the user asked us to adjust the latency of the stream
846 * buffer according to the what the sink can provide. The
847 * tlength passed in shall be the overall latency. Roughly
848 * half the latency will be spent on the hw buffer, the other
849 * half of it in the async buffer queue we maintain for each
850 * client. In between we'll have a safety space of size
851 * 2*minreq. Why the 2*minreq? When the hw buffer is completey
852 * empty and needs to be filled, then our buffer must have
853 * enough data to fulfill this request immediatly and thus
854 * have at least the same tlength as the size of the hw
855 * buffer. It additionally needs space for 2 times minreq
856 * because if the buffer ran empty and a partial fillup
857 * happens immediately on the next iteration we need to be
858 * able to fulfill it and give the application also minreq
859 * time to fill it up again for the next request Makes 2 times
860 * minreq in plus.. */
862 if (tlength_usec
> minreq_usec
*2)
863 sink_usec
= (tlength_usec
- minreq_usec
*2)/2;
867 pa_log_debug("Adjust latency mode enabled, configuring sink latency to half of overall latency.");
871 /* Ok, the user didn't ask us to adjust the latency, but we
872 * still need to make sure that the parameters from the user
875 if (tlength_usec
> minreq_usec
*2)
876 sink_usec
= (tlength_usec
- minreq_usec
*2);
880 pa_log_debug("Traditional mode enabled, modifying sink usec only for compat with minreq.");
883 s
->sink_latency
= pa_sink_input_set_requested_latency(s
->sink_input
, sink_usec
);
885 if (early_requests
) {
887 /* Ok, we didn't necessarily get what we were asking for, so
888 * let's tell the user */
890 minreq_usec
= s
->sink_latency
;
892 } else if (adjust_latency
) {
894 /* Ok, we didn't necessarily get what we were asking for, so
895 * let's subtract from what we asked for for the remaining
898 if (tlength_usec
>= s
->sink_latency
)
899 tlength_usec
-= s
->sink_latency
;
902 /* FIXME: This is actually larger than necessary, since not all of
903 * the sink latency is actually rewritable. */
904 if (tlength_usec
< s
->sink_latency
+ 2*minreq_usec
)
905 tlength_usec
= s
->sink_latency
+ 2*minreq_usec
;
907 if (pa_usec_to_bytes_round_up(orig_tlength_usec
, &s
->sink_input
->sample_spec
) !=
908 pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
))
909 *tlength
= (uint32_t) pa_usec_to_bytes_round_up(tlength_usec
, &s
->sink_input
->sample_spec
);
911 if (pa_usec_to_bytes(orig_minreq_usec
, &s
->sink_input
->sample_spec
) !=
912 pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
))
913 *minreq
= (uint32_t) pa_usec_to_bytes(minreq_usec
, &s
->sink_input
->sample_spec
);
916 *minreq
= (uint32_t) frame_size
;
917 *tlength
+= (uint32_t) frame_size
*2;
920 if (*tlength
<= *minreq
)
921 *tlength
= *minreq
*2 + (uint32_t) frame_size
;
923 if (*prebuf
== (uint32_t) -1 || *prebuf
> *tlength
)
927 static void fix_playback_buffer_attr_post(
935 pa_assert(maxlength
);
940 *maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
941 *tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
942 *prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
943 *minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
948 static playback_stream
* playback_stream_new(
949 pa_native_connection
*c
,
962 pa_sink_input_flags_t flags
,
964 pa_bool_t adjust_latency
,
965 pa_bool_t early_requests
) {
967 playback_stream
*s
, *ssync
;
968 pa_sink_input
*sink_input
;
972 pa_sink_input_new_data data
;
976 pa_assert(maxlength
);
983 /* Find syncid group */
984 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
986 if (!playback_stream_isinstance(ssync
))
989 if (ssync
->syncid
== syncid
)
993 /* Synced streams must connect to the same sink */
997 sink
= ssync
->sink_input
->sink
;
998 else if (sink
!= ssync
->sink_input
->sink
)
1002 pa_sink_input_new_data_init(&data
);
1004 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
1005 data
.driver
= __FILE__
;
1006 data
.module
= c
->options
->module
;
1007 data
.client
= c
->client
;
1009 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
1010 pa_sink_input_new_data_set_channel_map(&data
, map
);
1012 pa_sink_input_new_data_set_volume(&data
, volume
);
1014 pa_sink_input_new_data_set_muted(&data
, muted
);
1015 data
.sync_base
= ssync
? ssync
->sink_input
: NULL
;
1017 sink_input
= pa_sink_input_new(c
->protocol
->core
, &data
, flags
);
1019 pa_sink_input_new_data_done(&data
);
1024 s
= pa_msgobject_new(playback_stream
);
1025 s
->parent
.parent
.parent
.free
= playback_stream_free
;
1026 s
->parent
.parent
.process_msg
= playback_stream_process_msg
;
1029 s
->sink_input
= sink_input
;
1030 s
->is_underrun
= TRUE
;
1031 s
->drain_request
= FALSE
;
1032 pa_atomic_store(&s
->missing
, 0);
1034 s
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
1035 s
->sink_input
->pop
= sink_input_pop_cb
;
1036 s
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1037 s
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1038 s
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1039 s
->sink_input
->kill
= sink_input_kill_cb
;
1040 s
->sink_input
->moved
= sink_input_moved_cb
;
1041 s
->sink_input
->suspend
= sink_input_suspend_cb
;
1042 s
->sink_input
->userdata
= s
;
1044 start_index
= ssync
? pa_memblockq_get_read_index(ssync
->memblockq
) : 0;
1046 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, maxlength
, tlength
, prebuf
, minreq
);
1047 pa_sink_input_get_silence(sink_input
, &silence
);
1049 s
->memblockq
= pa_memblockq_new(
1053 pa_frame_size(&sink_input
->sample_spec
),
1059 pa_memblock_unref(silence
.memblock
);
1060 fix_playback_buffer_attr_post(s
, maxlength
, tlength
, prebuf
, minreq
);
1062 *missing
= (uint32_t) pa_memblockq_pop_missing(s
->memblockq
);
1064 *ss
= s
->sink_input
->sample_spec
;
1065 *map
= s
->sink_input
->channel_map
;
1067 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
1069 pa_log_info("Final latency %0.2f ms = %0.2f ms + 2*%0.2f ms + %0.2f ms",
1070 ((double) pa_bytes_to_usec(*tlength
, &sink_input
->sample_spec
) + (double) s
->sink_latency
) / PA_USEC_PER_MSEC
,
1071 (double) pa_bytes_to_usec(*tlength
-*minreq
*2, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1072 (double) pa_bytes_to_usec(*minreq
, &sink_input
->sample_spec
) / PA_USEC_PER_MSEC
,
1073 (double) s
->sink_latency
/ PA_USEC_PER_MSEC
);
1075 pa_sink_input_put(s
->sink_input
);
1079 /* Called from thread context */
1080 static void playback_stream_request_bytes(playback_stream
*s
) {
1081 size_t m
, previous_missing
;
1083 playback_stream_assert_ref(s
);
1085 m
= pa_memblockq_pop_missing(s
->memblockq
);
1090 /* pa_log("request_bytes(%lu)", (unsigned long) m); */
1092 previous_missing
= (size_t) pa_atomic_add(&s
->missing
, (int) m
);
1094 if (pa_memblockq_prebuf_active(s
->memblockq
) ||
1095 (previous_missing
< s
->minreq
&& previous_missing
+m
>= s
->minreq
))
1096 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1100 static void playback_stream_send_killed(playback_stream
*p
) {
1102 playback_stream_assert_ref(p
);
1104 t
= pa_tagstruct_new(NULL
, 0);
1105 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
1106 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1107 pa_tagstruct_putu32(t
, p
->index
);
1108 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
1111 static int native_connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1112 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1113 pa_native_connection_assert_ref(c
);
1120 case CONNECTION_MESSAGE_REVOKE
:
1121 pa_pstream_send_revoke(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1124 case CONNECTION_MESSAGE_RELEASE
:
1125 pa_pstream_send_release(c
->pstream
, PA_PTR_TO_UINT(userdata
));
1132 static void native_connection_unlink(pa_native_connection
*c
) {
1141 pa_hook_fire(&c
->protocol
->hooks
[PA_NATIVE_HOOK_CONNECTION_UNLINK
], c
);
1144 pa_native_options_unref(c
->options
);
1146 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
1147 record_stream_unlink(r
);
1149 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
1150 if (playback_stream_isinstance(o
))
1151 playback_stream_unlink(PLAYBACK_STREAM(o
));
1153 upload_stream_unlink(UPLOAD_STREAM(o
));
1155 if (c
->subscription
)
1156 pa_subscription_free(c
->subscription
);
1159 pa_pstream_unlink(c
->pstream
);
1161 if (c
->auth_timeout_event
) {
1162 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
1163 c
->auth_timeout_event
= NULL
;
1166 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
1168 pa_native_connection_unref(c
);
1171 static void native_connection_free(pa_object
*o
) {
1172 pa_native_connection
*c
= PA_NATIVE_CONNECTION(o
);
1176 native_connection_unlink(c
);
1178 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
1179 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
1181 pa_pdispatch_unref(c
->pdispatch
);
1182 pa_pstream_unref(c
->pstream
);
1183 pa_client_free(c
->client
);
1188 static void native_connection_send_memblock(pa_native_connection
*c
) {
1192 start
= PA_IDXSET_INVALID
;
1196 if (!(r
= RECORD_STREAM(pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
))))
1199 if (start
== PA_IDXSET_INVALID
)
1200 start
= c
->rrobin_index
;
1201 else if (start
== c
->rrobin_index
)
1204 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
1205 pa_memchunk schunk
= chunk
;
1207 if (schunk
.length
> r
->fragment_size
)
1208 schunk
.length
= r
->fragment_size
;
1210 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
1212 pa_memblockq_drop(r
->memblockq
, schunk
.length
);
1213 pa_memblock_unref(schunk
.memblock
);
1220 /*** sink input callbacks ***/
1222 static void handle_seek(playback_stream
*s
, int64_t indexw
) {
1223 playback_stream_assert_ref(s
);
1225 /* pa_log("handle_seek: %llu -- %i", (unsigned long long) s->sink_input->thread_info.underrun_for, pa_memblockq_is_readable(s->memblockq)); */
1227 if (s
->sink_input
->thread_info
.underrun_for
> 0) {
1229 /* pa_log("%lu vs. %lu", (unsigned long) pa_memblockq_get_length(s->memblockq), (unsigned long) pa_memblockq_get_prebuf(s->memblockq)); */
1231 if (pa_memblockq_is_readable(s
->memblockq
)) {
1233 /* We just ended an underrun, let's ask the sink
1234 * for a complete rewind rewrite */
1236 pa_log_debug("Requesting rewind due to end of underrun.");
1237 pa_sink_input_request_rewind(s
->sink_input
,
1238 (size_t) (s
->sink_input
->thread_info
.underrun_for
== (size_t) -1 ? 0 : s
->sink_input
->thread_info
.underrun_for
),
1239 FALSE
, TRUE
, FALSE
);
1245 indexr
= pa_memblockq_get_read_index(s
->memblockq
);
1247 if (indexw
< indexr
) {
1248 /* OK, the sink already asked for this data, so
1249 * let's have it usk us again */
1251 pa_log_debug("Requesting rewind due to rewrite.");
1252 pa_sink_input_request_rewind(s
->sink_input
, (size_t) (indexr
- indexw
), TRUE
, FALSE
, FALSE
);
1256 playback_stream_request_bytes(s
);
1259 /* Called from thread context */
1260 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1261 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1264 pa_sink_input_assert_ref(i
);
1265 s
= PLAYBACK_STREAM(i
->userdata
);
1266 playback_stream_assert_ref(s
);
1270 case SINK_INPUT_MESSAGE_SEEK
: {
1273 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1274 pa_memblockq_seek(s
->memblockq
, offset
, PA_PTR_TO_UINT(userdata
));
1276 handle_seek(s
, windex
);
1280 case SINK_INPUT_MESSAGE_POST_DATA
: {
1285 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1287 /* pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
1289 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
1290 pa_log_warn("Failed to push data into queue");
1291 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_OVERFLOW
, NULL
, 0, NULL
, NULL
);
1292 pa_memblockq_seek(s
->memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
1295 handle_seek(s
, windex
);
1297 /* pa_log("sink input post2: %lu", (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1302 case SINK_INPUT_MESSAGE_DRAIN
:
1303 case SINK_INPUT_MESSAGE_FLUSH
:
1304 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1305 case SINK_INPUT_MESSAGE_TRIGGER
: {
1308 pa_sink_input
*isync
;
1309 void (*func
)(pa_memblockq
*bq
);
1312 case SINK_INPUT_MESSAGE_FLUSH
:
1313 func
= pa_memblockq_flush_write
;
1316 case SINK_INPUT_MESSAGE_PREBUF_FORCE
:
1317 func
= pa_memblockq_prebuf_force
;
1320 case SINK_INPUT_MESSAGE_DRAIN
:
1321 case SINK_INPUT_MESSAGE_TRIGGER
:
1322 func
= pa_memblockq_prebuf_disable
;
1326 pa_assert_not_reached();
1329 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1331 handle_seek(s
, windex
);
1333 /* Do the same for all other members in the sync group */
1334 for (isync
= i
->sync_prev
; isync
; isync
= isync
->sync_prev
) {
1335 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1336 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1337 func(ssync
->memblockq
);
1338 handle_seek(ssync
, windex
);
1341 for (isync
= i
->sync_next
; isync
; isync
= isync
->sync_next
) {
1342 playback_stream
*ssync
= PLAYBACK_STREAM(isync
->userdata
);
1343 windex
= pa_memblockq_get_write_index(ssync
->memblockq
);
1344 func(ssync
->memblockq
);
1345 handle_seek(ssync
, windex
);
1348 if (code
== SINK_INPUT_MESSAGE_DRAIN
) {
1349 if (!pa_memblockq_is_readable(s
->memblockq
))
1350 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_DRAIN_ACK
, userdata
, 0, NULL
, NULL
);
1352 s
->drain_tag
= PA_PTR_TO_UINT(userdata
);
1353 s
->drain_request
= TRUE
;
1360 case SINK_INPUT_MESSAGE_UPDATE_LATENCY
:
1362 s
->read_index
= pa_memblockq_get_read_index(s
->memblockq
);
1363 s
->write_index
= pa_memblockq_get_write_index(s
->memblockq
);
1364 s
->render_memblockq_length
= pa_memblockq_get_length(s
->sink_input
->thread_info
.render_memblockq
);
1367 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1370 windex
= pa_memblockq_get_write_index(s
->memblockq
);
1372 pa_memblockq_prebuf_force(s
->memblockq
);
1374 handle_seek(s
, windex
);
1376 /* Fall through to the default handler */
1380 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1381 pa_usec_t
*r
= userdata
;
1383 *r
= pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
1385 /* Fall through, the default handler will add in the extra
1386 * latency added by the resampler */
1391 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1394 /* Called from thread context */
1395 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
1398 pa_sink_input_assert_ref(i
);
1399 s
= PLAYBACK_STREAM(i
->userdata
);
1400 playback_stream_assert_ref(s
);
1403 /* pa_log("%s, pop(): %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1405 if (pa_memblockq_is_readable(s
->memblockq
))
1406 s
->is_underrun
= FALSE
;
1408 /* pa_log("%s, UNDERRUN: %lu", pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME), (unsigned long) pa_memblockq_get_length(s->memblockq)); */
1410 if (s
->drain_request
&& pa_sink_input_safe_to_remove(i
)) {
1411 s
->drain_request
= FALSE
;
1412 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
);
1413 } else if (!s
->is_underrun
)
1414 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_UNDERFLOW
, NULL
, 0, NULL
, NULL
);
1416 s
->is_underrun
= TRUE
;
1418 playback_stream_request_bytes(s
);
1421 /* This call will not fail with prebuf=0, hence we check for
1422 underrun explicitly above */
1423 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
1426 chunk
->length
= PA_MIN(nbytes
, chunk
->length
);
1428 if (i
->thread_info
.underrun_for
> 0)
1429 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PLAYBACK_STREAM_MESSAGE_STARTED
, NULL
, 0, NULL
, NULL
);
1431 pa_memblockq_drop(s
->memblockq
, chunk
->length
);
1432 playback_stream_request_bytes(s
);
1437 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1440 pa_sink_input_assert_ref(i
);
1441 s
= PLAYBACK_STREAM(i
->userdata
);
1442 playback_stream_assert_ref(s
);
1444 /* If we are in an underrun, then we don't rewind */
1445 if (i
->thread_info
.underrun_for
> 0)
1448 pa_memblockq_rewind(s
->memblockq
, nbytes
);
1451 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1454 pa_sink_input_assert_ref(i
);
1455 s
= PLAYBACK_STREAM(i
->userdata
);
1456 playback_stream_assert_ref(s
);
1458 pa_memblockq_set_maxrewind(s
->memblockq
, nbytes
);
1461 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
1465 pa_sink_input_assert_ref(i
);
1466 s
= PLAYBACK_STREAM(i
->userdata
);
1467 playback_stream_assert_ref(s
);
1469 tlength
= nbytes
+2*pa_memblockq_get_minreq(s
->memblockq
);
1471 if (pa_memblockq_get_tlength(s
->memblockq
) < tlength
)
1472 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1475 /* Called from main context */
1476 static void sink_input_kill_cb(pa_sink_input
*i
) {
1479 pa_sink_input_assert_ref(i
);
1480 s
= PLAYBACK_STREAM(i
->userdata
);
1481 playback_stream_assert_ref(s
);
1483 playback_stream_send_killed(s
);
1484 playback_stream_unlink(s
);
1487 /* Called from main context */
1488 static void sink_input_suspend_cb(pa_sink_input
*i
, pa_bool_t suspend
) {
1492 pa_sink_input_assert_ref(i
);
1493 s
= PLAYBACK_STREAM(i
->userdata
);
1494 playback_stream_assert_ref(s
);
1496 if (s
->connection
->version
< 12)
1499 t
= pa_tagstruct_new(NULL
, 0);
1500 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
);
1501 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1502 pa_tagstruct_putu32(t
, s
->index
);
1503 pa_tagstruct_put_boolean(t
, suspend
);
1504 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1507 /* Called from main context */
1508 static void sink_input_moved_cb(pa_sink_input
*i
) {
1511 uint32_t maxlength
, tlength
, prebuf
, minreq
;
1513 pa_sink_input_assert_ref(i
);
1514 s
= PLAYBACK_STREAM(i
->userdata
);
1515 playback_stream_assert_ref(s
);
1517 maxlength
= (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
);
1518 tlength
= (uint32_t) pa_memblockq_get_tlength(s
->memblockq
);
1519 prebuf
= (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
);
1520 minreq
= (uint32_t) pa_memblockq_get_minreq(s
->memblockq
);
1522 fix_playback_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1523 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1524 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
1525 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
1526 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
1527 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
1529 if (s
->connection
->version
< 12)
1532 t
= pa_tagstruct_new(NULL
, 0);
1533 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_MOVED
);
1534 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1535 pa_tagstruct_putu32(t
, s
->index
);
1536 pa_tagstruct_putu32(t
, i
->sink
->index
);
1537 pa_tagstruct_puts(t
, i
->sink
->name
);
1538 pa_tagstruct_put_boolean(t
, pa_sink_get_state(i
->sink
) == PA_SINK_SUSPENDED
);
1540 if (s
->connection
->version
>= 13) {
1541 pa_tagstruct_putu32(t
, maxlength
);
1542 pa_tagstruct_putu32(t
, tlength
);
1543 pa_tagstruct_putu32(t
, prebuf
);
1544 pa_tagstruct_putu32(t
, minreq
);
1545 pa_tagstruct_put_usec(t
, s
->sink_latency
);
1548 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1551 /*** source_output callbacks ***/
1553 /* Called from thread context */
1554 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1557 pa_source_output_assert_ref(o
);
1558 s
= RECORD_STREAM(o
->userdata
);
1559 record_stream_assert_ref(s
);
1562 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), RECORD_STREAM_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1565 static void source_output_kill_cb(pa_source_output
*o
) {
1568 pa_source_output_assert_ref(o
);
1569 s
= RECORD_STREAM(o
->userdata
);
1570 record_stream_assert_ref(s
);
1572 record_stream_send_killed(s
);
1573 record_stream_unlink(s
);
1576 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1579 pa_source_output_assert_ref(o
);
1580 s
= RECORD_STREAM(o
->userdata
);
1581 record_stream_assert_ref(s
);
1583 /*pa_log("get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
1585 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
1588 /* Called from main context */
1589 static void source_output_suspend_cb(pa_source_output
*o
, pa_bool_t suspend
) {
1593 pa_source_output_assert_ref(o
);
1594 s
= RECORD_STREAM(o
->userdata
);
1595 record_stream_assert_ref(s
);
1597 if (s
->connection
->version
< 12)
1600 t
= pa_tagstruct_new(NULL
, 0);
1601 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_SUSPENDED
);
1602 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1603 pa_tagstruct_putu32(t
, s
->index
);
1604 pa_tagstruct_put_boolean(t
, suspend
);
1605 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1608 /* Called from main context */
1609 static void source_output_moved_cb(pa_source_output
*o
) {
1612 uint32_t maxlength
, fragsize
;
1614 pa_source_output_assert_ref(o
);
1615 s
= RECORD_STREAM(o
->userdata
);
1616 record_stream_assert_ref(s
);
1618 fragsize
= (uint32_t) s
->fragment_size
;
1619 maxlength
= (uint32_t) pa_memblockq_get_length(s
->memblockq
);
1621 fix_record_buffer_attr_pre(s
, TRUE
, FALSE
, &maxlength
, &fragsize
);
1622 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
1623 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
1625 if (s
->connection
->version
< 12)
1628 t
= pa_tagstruct_new(NULL
, 0);
1629 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_MOVED
);
1630 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
1631 pa_tagstruct_putu32(t
, s
->index
);
1632 pa_tagstruct_putu32(t
, o
->source
->index
);
1633 pa_tagstruct_puts(t
, o
->source
->name
);
1634 pa_tagstruct_put_boolean(t
, pa_source_get_state(o
->source
) == PA_SOURCE_SUSPENDED
);
1636 if (s
->connection
->version
>= 13) {
1637 pa_tagstruct_putu32(t
, maxlength
);
1638 pa_tagstruct_putu32(t
, fragsize
);
1639 pa_tagstruct_put_usec(t
, s
->source_latency
);
1642 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
1645 /*** pdispatch callbacks ***/
1647 static void protocol_error(pa_native_connection
*c
) {
1648 pa_log("protocol error, kicking client");
1649 native_connection_unlink(c
);
1652 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
1653 if (!(expression)) { \
1654 pa_pstream_send_error((pstream), (tag), (error)); \
1659 static pa_tagstruct
*reply_new(uint32_t tag
) {
1660 pa_tagstruct
*reply
;
1662 reply
= pa_tagstruct_new(NULL
, 0);
1663 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1664 pa_tagstruct_putu32(reply
, tag
);
1668 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1669 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1671 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
, missing
;
1672 const char *name
= NULL
, *sink_name
;
1675 pa_tagstruct
*reply
;
1676 pa_sink
*sink
= NULL
;
1684 fix_channels
= FALSE
,
1686 variable_rate
= FALSE
,
1688 adjust_latency
= FALSE
,
1689 early_requests
= FALSE
,
1690 dont_inhibit_auto_suspend
= FALSE
,
1693 pa_sink_input_flags_t flags
= 0;
1695 pa_bool_t volume_set
= TRUE
;
1697 pa_native_connection_assert_ref(c
);
1700 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1703 PA_TAG_SAMPLE_SPEC
, &ss
,
1704 PA_TAG_CHANNEL_MAP
, &map
,
1705 PA_TAG_U32
, &sink_index
,
1706 PA_TAG_STRING
, &sink_name
,
1707 PA_TAG_U32
, &maxlength
,
1708 PA_TAG_BOOLEAN
, &corked
,
1709 PA_TAG_U32
, &tlength
,
1710 PA_TAG_U32
, &prebuf
,
1711 PA_TAG_U32
, &minreq
,
1712 PA_TAG_U32
, &syncid
,
1713 PA_TAG_CVOLUME
, &volume
,
1714 PA_TAG_INVALID
) < 0) {
1720 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1721 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
1722 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
1723 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1724 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1725 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1726 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1727 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1729 p
= pa_proplist_new();
1732 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1734 if (c
->version
>= 12) {
1735 /* Since 0.9.8 the user can ask for a couple of additional flags */
1737 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1738 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1739 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1740 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1741 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1742 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1743 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1746 pa_proplist_free(p
);
1751 if (c
->version
>= 13) {
1753 if (pa_tagstruct_get_boolean(t
, &muted
) < 0 ||
1754 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1755 pa_tagstruct_get_proplist(t
, p
) < 0) {
1757 pa_proplist_free(p
);
1762 if (c
->version
>= 14) {
1764 if (pa_tagstruct_get_boolean(t
, &volume_set
) < 0 ||
1765 pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
1767 pa_proplist_free(p
);
1772 if (c
->version
>= 15) {
1774 if (pa_tagstruct_get_boolean(t
, &muted_set
) < 0 ||
1775 pa_tagstruct_get_boolean(t
, &dont_inhibit_auto_suspend
) < 0) {
1777 pa_proplist_free(p
);
1782 if (!pa_tagstruct_eof(t
)) {
1784 pa_proplist_free(p
);
1788 if (sink_index
!= PA_INVALID_INDEX
) {
1790 if (!(sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
))) {
1791 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1792 pa_proplist_free(p
);
1796 } else if (sink_name
) {
1798 if (!(sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
))) {
1799 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1800 pa_proplist_free(p
);
1806 (corked
? PA_SINK_INPUT_START_CORKED
: 0) |
1807 (no_remap
? PA_SINK_INPUT_NO_REMAP
: 0) |
1808 (no_remix
? PA_SINK_INPUT_NO_REMIX
: 0) |
1809 (fix_format
? PA_SINK_INPUT_FIX_FORMAT
: 0) |
1810 (fix_rate
? PA_SINK_INPUT_FIX_RATE
: 0) |
1811 (fix_channels
? PA_SINK_INPUT_FIX_CHANNELS
: 0) |
1812 (no_move
? PA_SINK_INPUT_DONT_MOVE
: 0) |
1813 (variable_rate
? PA_SINK_INPUT_VARIABLE_RATE
: 0) |
1814 (dont_inhibit_auto_suspend
? PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
: 0);
1816 /* Only since protocol version 15 there's a seperate muted_set
1817 * flag. For older versions we synthesize it here */
1818 muted_set
= muted_set
|| muted
;
1820 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
);
1821 pa_proplist_free(p
);
1823 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1825 reply
= reply_new(tag
);
1826 pa_tagstruct_putu32(reply
, s
->index
);
1827 pa_assert(s
->sink_input
);
1828 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
1829 pa_tagstruct_putu32(reply
, missing
);
1831 /* pa_log("initial request is %u", missing); */
1833 if (c
->version
>= 9) {
1834 /* Since 0.9.0 we support sending the buffer metrics back to the client */
1836 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
1837 pa_tagstruct_putu32(reply
, (uint32_t) tlength
);
1838 pa_tagstruct_putu32(reply
, (uint32_t) prebuf
);
1839 pa_tagstruct_putu32(reply
, (uint32_t) minreq
);
1842 if (c
->version
>= 12) {
1843 /* Since 0.9.8 we support sending the chosen sample
1844 * spec/channel map/device/suspend status back to the
1847 pa_tagstruct_put_sample_spec(reply
, &ss
);
1848 pa_tagstruct_put_channel_map(reply
, &map
);
1850 pa_tagstruct_putu32(reply
, s
->sink_input
->sink
->index
);
1851 pa_tagstruct_puts(reply
, s
->sink_input
->sink
->name
);
1853 pa_tagstruct_put_boolean(reply
, pa_sink_get_state(s
->sink_input
->sink
) == PA_SINK_SUSPENDED
);
1856 if (c
->version
>= 13)
1857 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
1859 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1862 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1863 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1866 pa_native_connection_assert_ref(c
);
1869 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1870 !pa_tagstruct_eof(t
)) {
1875 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1879 case PA_COMMAND_DELETE_PLAYBACK_STREAM
: {
1881 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !playback_stream_isinstance(s
)) {
1882 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1886 playback_stream_unlink(s
);
1890 case PA_COMMAND_DELETE_RECORD_STREAM
: {
1892 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
1893 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1897 record_stream_unlink(s
);
1901 case PA_COMMAND_DELETE_UPLOAD_STREAM
: {
1904 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || !upload_stream_isinstance(s
)) {
1905 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1909 upload_stream_unlink(s
);
1914 pa_assert_not_reached();
1917 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1920 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1921 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
1923 uint32_t maxlength
, fragment_size
;
1924 uint32_t source_index
;
1925 const char *name
= NULL
, *source_name
;
1928 pa_tagstruct
*reply
;
1929 pa_source
*source
= NULL
;
1936 fix_channels
= FALSE
,
1938 variable_rate
= FALSE
,
1939 adjust_latency
= FALSE
,
1940 peak_detect
= FALSE
,
1941 early_requests
= FALSE
,
1942 dont_inhibit_auto_suspend
= FALSE
;
1943 pa_source_output_flags_t flags
= 0;
1945 uint32_t direct_on_input_idx
= PA_INVALID_INDEX
;
1946 pa_sink_input
*direct_on_input
= NULL
;
1948 pa_native_connection_assert_ref(c
);
1951 if ((c
->version
< 13 && (pa_tagstruct_gets(t
, &name
) < 0 || !name
)) ||
1952 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1953 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1954 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
1955 pa_tagstruct_gets(t
, &source_name
) < 0 ||
1956 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
1957 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
1958 pa_tagstruct_getu32(t
, &fragment_size
) < 0) {
1963 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1964 CHECK_VALIDITY(c
->pstream
, !source_name
|| pa_namereg_is_valid_name(source_name
), tag
, PA_ERR_INVALID
);
1965 CHECK_VALIDITY(c
->pstream
, source_index
== PA_INVALID_INDEX
|| !source_name
, tag
, PA_ERR_INVALID
);
1966 CHECK_VALIDITY(c
->pstream
, !source_name
|| source_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1967 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1968 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1969 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1971 p
= pa_proplist_new();
1974 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
1976 if (c
->version
>= 12) {
1977 /* Since 0.9.8 the user can ask for a couple of additional flags */
1979 if (pa_tagstruct_get_boolean(t
, &no_remap
) < 0 ||
1980 pa_tagstruct_get_boolean(t
, &no_remix
) < 0 ||
1981 pa_tagstruct_get_boolean(t
, &fix_format
) < 0 ||
1982 pa_tagstruct_get_boolean(t
, &fix_rate
) < 0 ||
1983 pa_tagstruct_get_boolean(t
, &fix_channels
) < 0 ||
1984 pa_tagstruct_get_boolean(t
, &no_move
) < 0 ||
1985 pa_tagstruct_get_boolean(t
, &variable_rate
) < 0) {
1988 pa_proplist_free(p
);
1993 if (c
->version
>= 13) {
1995 if (pa_tagstruct_get_boolean(t
, &peak_detect
) < 0 ||
1996 pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0 ||
1997 pa_tagstruct_get_proplist(t
, p
) < 0 ||
1998 pa_tagstruct_getu32(t
, &direct_on_input_idx
) < 0) {
2000 pa_proplist_free(p
);
2005 if (c
->version
>= 14) {
2007 if (pa_tagstruct_get_boolean(t
, &early_requests
) < 0) {
2009 pa_proplist_free(p
);
2014 if (c
->version
>= 15) {
2016 if (pa_tagstruct_get_boolean(t
, &dont_inhibit_auto_suspend
) < 0) {
2018 pa_proplist_free(p
);
2023 if (!pa_tagstruct_eof(t
)) {
2025 pa_proplist_free(p
);
2029 if (source_index
!= PA_INVALID_INDEX
) {
2031 if (!(source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
))) {
2032 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2033 pa_proplist_free(p
);
2037 } else if (source_name
) {
2039 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
))) {
2040 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2041 pa_proplist_free(p
);
2046 if (direct_on_input_idx
!= PA_INVALID_INDEX
) {
2048 if (!(direct_on_input
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, direct_on_input_idx
))) {
2049 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2050 pa_proplist_free(p
);
2056 (corked
? PA_SOURCE_OUTPUT_START_CORKED
: 0) |
2057 (no_remap
? PA_SOURCE_OUTPUT_NO_REMAP
: 0) |
2058 (no_remix
? PA_SOURCE_OUTPUT_NO_REMIX
: 0) |
2059 (fix_format
? PA_SOURCE_OUTPUT_FIX_FORMAT
: 0) |
2060 (fix_rate
? PA_SOURCE_OUTPUT_FIX_RATE
: 0) |
2061 (fix_channels
? PA_SOURCE_OUTPUT_FIX_CHANNELS
: 0) |
2062 (no_move
? PA_SOURCE_OUTPUT_DONT_MOVE
: 0) |
2063 (variable_rate
? PA_SOURCE_OUTPUT_VARIABLE_RATE
: 0) |
2064 (dont_inhibit_auto_suspend
? PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND
: 0);
2066 s
= record_stream_new(c
, source
, &ss
, &map
, peak_detect
, &maxlength
, &fragment_size
, flags
, p
, adjust_latency
, direct_on_input
, early_requests
);
2067 pa_proplist_free(p
);
2069 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2071 reply
= reply_new(tag
);
2072 pa_tagstruct_putu32(reply
, s
->index
);
2073 pa_assert(s
->source_output
);
2074 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
2076 if (c
->version
>= 9) {
2077 /* Since 0.9 we support sending the buffer metrics back to the client */
2079 pa_tagstruct_putu32(reply
, (uint32_t) maxlength
);
2080 pa_tagstruct_putu32(reply
, (uint32_t) fragment_size
);
2083 if (c
->version
>= 12) {
2084 /* Since 0.9.8 we support sending the chosen sample
2085 * spec/channel map/device/suspend status back to the
2088 pa_tagstruct_put_sample_spec(reply
, &ss
);
2089 pa_tagstruct_put_channel_map(reply
, &map
);
2091 pa_tagstruct_putu32(reply
, s
->source_output
->source
->index
);
2092 pa_tagstruct_puts(reply
, s
->source_output
->source
->name
);
2094 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_SUSPENDED
);
2097 if (c
->version
>= 13)
2098 pa_tagstruct_put_usec(reply
, s
->source_latency
);
2100 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2103 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2104 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2107 pa_native_connection_assert_ref(c
);
2110 if (!pa_tagstruct_eof(t
)) {
2115 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2116 ret
= pa_core_exit(c
->protocol
->core
, FALSE
, 0);
2117 CHECK_VALIDITY(c
->pstream
, ret
>= 0, tag
, PA_ERR_ACCESS
);
2119 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
2122 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2123 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2125 pa_tagstruct
*reply
;
2126 pa_bool_t shm_on_remote
= FALSE
, do_shm
;
2128 pa_native_connection_assert_ref(c
);
2131 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
2132 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
2133 !pa_tagstruct_eof(t
)) {
2138 /* Minimum supported version */
2139 if (c
->version
< 8) {
2140 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
2144 /* Starting with protocol version 13 the MSB of the version tag
2145 reflects if shm is available for this pa_native_connection or
2147 if (c
->version
>= 13) {
2148 shm_on_remote
= !!(c
->version
& 0x80000000U
);
2149 c
->version
&= 0x7FFFFFFFU
;
2152 pa_log_debug("Protocol version: remote %u, local %u", c
->version
, PA_PROTOCOL_VERSION
);
2154 pa_proplist_setf(c
->client
->proplist
, "native-protocol.version", "%u", c
->version
);
2156 if (!c
->authorized
) {
2157 pa_bool_t success
= FALSE
;
2160 const pa_creds
*creds
;
2162 if ((creds
= pa_pdispatch_creds(pd
))) {
2163 if (creds
->uid
== getuid())
2165 else if (c
->options
->auth_group
) {
2169 if ((gid
= pa_get_gid_of_group(c
->options
->auth_group
)) == (gid_t
) -1)
2170 pa_log_warn("Failed to get GID of group '%s'", c
->options
->auth_group
);
2171 else if (gid
== creds
->gid
)
2175 if ((r
= pa_uid_in_group(creds
->uid
, c
->options
->auth_group
)) < 0)
2176 pa_log_warn("Failed to check group membership.");
2182 pa_log_info("Got credentials: uid=%lu gid=%lu success=%i",
2183 (unsigned long) creds
->uid
,
2184 (unsigned long) creds
->gid
,
2189 if (!success
&& c
->options
->auth_cookie
) {
2192 if ((ac
= pa_auth_cookie_read(c
->options
->auth_cookie
, PA_NATIVE_COOKIE_LENGTH
)))
2193 if (memcmp(ac
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
2198 pa_log_warn("Denied access to client with invalid authorization data.");
2199 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2203 c
->authorized
= TRUE
;
2204 if (c
->auth_timeout_event
) {
2205 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
2206 c
->auth_timeout_event
= NULL
;
2210 /* Enable shared memory support if possible */
2212 pa_mempool_is_shared(c
->protocol
->core
->mempool
) &&
2215 pa_log_debug("SHM possible: %s", pa_yes_no(do_shm
));
2218 if (c
->version
< 10 || (c
->version
>= 13 && !shm_on_remote
))
2223 /* Only enable SHM if both sides are owned by the same
2224 * user. This is a security measure because otherwise data
2225 * private to the user might leak. */
2227 const pa_creds
*creds
;
2228 if (!(creds
= pa_pdispatch_creds(pd
)) || getuid() != creds
->uid
)
2233 pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm
));
2234 pa_pstream_enable_shm(c
->pstream
, do_shm
);
2236 reply
= reply_new(tag
);
2237 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
| (do_shm
? 0x80000000 : 0));
2241 /* SHM support is only enabled after both sides made sure they are the same user. */
2245 ucred
.uid
= getuid();
2246 ucred
.gid
= getgid();
2248 pa_pstream_send_tagstruct_with_creds(c
->pstream
, reply
, &ucred
);
2251 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2255 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2256 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2257 const char *name
= NULL
;
2259 pa_tagstruct
*reply
;
2261 pa_native_connection_assert_ref(c
);
2264 p
= pa_proplist_new();
2266 if ((c
->version
< 13 && pa_tagstruct_gets(t
, &name
) < 0) ||
2267 (c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2268 !pa_tagstruct_eof(t
)) {
2271 pa_proplist_free(p
);
2276 if (pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, name
) < 0) {
2277 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
2278 pa_proplist_free(p
);
2282 pa_proplist_update(c
->client
->proplist
, PA_UPDATE_REPLACE
, p
);
2283 pa_proplist_free(p
);
2285 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
2287 reply
= reply_new(tag
);
2289 if (c
->version
>= 13)
2290 pa_tagstruct_putu32(reply
, c
->client
->index
);
2292 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2295 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2296 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2298 uint32_t idx
= PA_IDXSET_INVALID
;
2300 pa_native_connection_assert_ref(c
);
2303 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2304 !pa_tagstruct_eof(t
)) {
2309 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2310 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2312 if (command
== PA_COMMAND_LOOKUP_SINK
) {
2314 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
)))
2318 pa_assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
2319 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
)))
2320 idx
= source
->index
;
2323 if (idx
== PA_IDXSET_INVALID
)
2324 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2326 pa_tagstruct
*reply
;
2327 reply
= reply_new(tag
);
2328 pa_tagstruct_putu32(reply
, idx
);
2329 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2333 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2334 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2338 pa_native_connection_assert_ref(c
);
2341 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2342 !pa_tagstruct_eof(t
)) {
2347 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2348 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2349 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2350 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2352 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
);
2355 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2356 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2357 pa_tagstruct
*reply
;
2358 const pa_mempool_stat
*stat
;
2360 pa_native_connection_assert_ref(c
);
2363 if (!pa_tagstruct_eof(t
)) {
2368 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2370 stat
= pa_mempool_get_stat(c
->protocol
->core
->mempool
);
2372 reply
= reply_new(tag
);
2373 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_allocated
));
2374 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->allocated_size
));
2375 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->n_accumulated
));
2376 pa_tagstruct_putu32(reply
, (uint32_t) pa_atomic_load(&stat
->accumulated_size
));
2377 pa_tagstruct_putu32(reply
, (uint32_t) pa_scache_total_size(c
->protocol
->core
));
2378 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2381 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2382 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2383 pa_tagstruct
*reply
;
2385 struct timeval tv
, now
;
2389 pa_native_connection_assert_ref(c
);
2392 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2393 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2394 !pa_tagstruct_eof(t
)) {
2399 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2400 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
2401 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2402 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2403 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
)
2405 reply
= reply_new(tag
);
2407 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
2408 latency
+= pa_bytes_to_usec(s
->render_memblockq_length
, &s
->sink_input
->sample_spec
);
2410 pa_tagstruct_put_usec(reply
, latency
);
2412 pa_tagstruct_put_usec(reply
, 0);
2413 pa_tagstruct_put_boolean(reply
, s
->sink_input
->thread_info
.playing_for
> 0);
2414 pa_tagstruct_put_timeval(reply
, &tv
);
2415 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2416 pa_tagstruct_puts64(reply
, s
->write_index
);
2417 pa_tagstruct_puts64(reply
, s
->read_index
);
2419 if (c
->version
>= 13) {
2420 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.underrun_for
);
2421 pa_tagstruct_putu64(reply
, s
->sink_input
->thread_info
.playing_for
);
2424 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2427 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2428 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2429 pa_tagstruct
*reply
;
2431 struct timeval tv
, now
;
2434 pa_native_connection_assert_ref(c
);
2437 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2438 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
2439 !pa_tagstruct_eof(t
)) {
2444 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2445 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
2446 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2448 reply
= reply_new(tag
);
2449 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
2450 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
2451 pa_tagstruct_put_boolean(reply
, pa_source_get_state(s
->source_output
->source
) == PA_SOURCE_RUNNING
);
2452 pa_tagstruct_put_timeval(reply
, &tv
);
2453 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
2454 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
2455 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
2456 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2459 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2460 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2463 const char *name
= NULL
;
2466 pa_tagstruct
*reply
;
2469 pa_native_connection_assert_ref(c
);
2472 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2473 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
2474 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
2475 pa_tagstruct_getu32(t
, &length
) < 0) {
2480 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2481 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
2482 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
2483 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
2484 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
2485 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
2487 p
= pa_proplist_new();
2489 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2490 !pa_tagstruct_eof(t
)) {
2493 pa_proplist_free(p
);
2497 if (c
->version
< 13)
2498 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2500 if (!(name
= pa_proplist_gets(p
, PA_PROP_EVENT_ID
)))
2501 name
= pa_proplist_gets(p
, PA_PROP_MEDIA_NAME
);
2503 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2505 s
= upload_stream_new(c
, &ss
, &map
, name
, length
, p
);
2506 pa_proplist_free(p
);
2508 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
2510 reply
= reply_new(tag
);
2511 pa_tagstruct_putu32(reply
, s
->index
);
2512 pa_tagstruct_putu32(reply
, length
);
2513 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2516 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2517 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2522 pa_native_connection_assert_ref(c
);
2525 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
2526 !pa_tagstruct_eof(t
)) {
2531 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2533 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
2534 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
2535 CHECK_VALIDITY(c
->pstream
, upload_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
2537 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, s
->proplist
, &idx
) < 0)
2538 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
2540 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2542 upload_stream_unlink(s
);
2545 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2546 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2547 uint32_t sink_index
;
2550 const char *name
, *sink_name
;
2553 pa_tagstruct
*reply
;
2555 pa_native_connection_assert_ref(c
);
2558 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2560 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
2561 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
2562 pa_tagstruct_getu32(t
, &volume
) < 0 ||
2563 pa_tagstruct_gets(t
, &name
) < 0) {
2568 CHECK_VALIDITY(c
->pstream
, !sink_name
|| pa_namereg_is_valid_name(sink_name
), tag
, PA_ERR_INVALID
);
2569 CHECK_VALIDITY(c
->pstream
, sink_index
== PA_INVALID_INDEX
|| !sink_name
, tag
, PA_ERR_INVALID
);
2570 CHECK_VALIDITY(c
->pstream
, !sink_name
|| sink_index
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2571 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2573 if (sink_index
!= PA_INVALID_INDEX
)
2574 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
2576 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
);
2578 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
2580 p
= pa_proplist_new();
2582 if ((c
->version
>= 13 && pa_tagstruct_get_proplist(t
, p
) < 0) ||
2583 !pa_tagstruct_eof(t
)) {
2585 pa_proplist_free(p
);
2589 pa_proplist_update(p
, PA_UPDATE_MERGE
, c
->client
->proplist
);
2591 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
, p
, &idx
) < 0) {
2592 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2593 pa_proplist_free(p
);
2597 pa_proplist_free(p
);
2599 reply
= reply_new(tag
);
2601 if (c
->version
>= 13)
2602 pa_tagstruct_putu32(reply
, idx
);
2604 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2607 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2608 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2611 pa_native_connection_assert_ref(c
);
2614 if (pa_tagstruct_gets(t
, &name
) < 0 ||
2615 !pa_tagstruct_eof(t
)) {
2620 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2621 CHECK_VALIDITY(c
->pstream
, name
&& pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2623 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
2624 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2628 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2631 static void fixup_sample_spec(pa_native_connection
*c
, pa_sample_spec
*fixed
, const pa_sample_spec
*original
) {
2634 pa_assert(original
);
2638 if (c
->version
< 12) {
2639 /* Before protocol version 12 we didn't support S32 samples,
2640 * so we need to lie about this to the client */
2642 if (fixed
->format
== PA_SAMPLE_S32LE
)
2643 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2644 if (fixed
->format
== PA_SAMPLE_S32BE
)
2645 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2648 if (c
->version
< 15) {
2649 if (fixed
->format
== PA_SAMPLE_S24LE
|| fixed
->format
== PA_SAMPLE_S24_32LE
)
2650 fixed
->format
= PA_SAMPLE_FLOAT32LE
;
2651 if (fixed
->format
== PA_SAMPLE_S24BE
|| fixed
->format
== PA_SAMPLE_S24_32BE
)
2652 fixed
->format
= PA_SAMPLE_FLOAT32BE
;
2656 static void sink_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink
*sink
) {
2657 pa_sample_spec fixed_ss
;
2660 pa_sink_assert_ref(sink
);
2662 fixup_sample_spec(c
, &fixed_ss
, &sink
->sample_spec
);
2666 PA_TAG_U32
, sink
->index
,
2667 PA_TAG_STRING
, sink
->name
,
2668 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2669 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2670 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
2671 PA_TAG_U32
, sink
->module
? sink
->module
->index
: PA_INVALID_INDEX
,
2672 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, FALSE
),
2673 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
, FALSE
),
2674 PA_TAG_U32
, sink
->monitor_source
? sink
->monitor_source
->index
: PA_INVALID_INDEX
,
2675 PA_TAG_STRING
, sink
->monitor_source
? sink
->monitor_source
->name
: NULL
,
2676 PA_TAG_USEC
, pa_sink_get_latency(sink
),
2677 PA_TAG_STRING
, sink
->driver
,
2678 PA_TAG_U32
, sink
->flags
,
2681 if (c
->version
>= 13) {
2682 pa_tagstruct_put_proplist(t
, sink
->proplist
);
2683 pa_tagstruct_put_usec(t
, pa_sink_get_requested_latency(sink
));
2686 if (c
->version
>= 15) {
2687 pa_tagstruct_put_volume(t
, sink
->base_volume
);
2688 if (PA_UNLIKELY(pa_sink_get_state(sink
) == PA_SINK_INVALID_STATE
))
2689 pa_log_error("Internal sink state is invalid.");
2690 pa_tagstruct_putu32(t
, pa_sink_get_state(sink
));
2694 static void source_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source
*source
) {
2695 pa_sample_spec fixed_ss
;
2698 pa_source_assert_ref(source
);
2700 fixup_sample_spec(c
, &fixed_ss
, &source
->sample_spec
);
2704 PA_TAG_U32
, source
->index
,
2705 PA_TAG_STRING
, source
->name
,
2706 PA_TAG_STRING
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)),
2707 PA_TAG_SAMPLE_SPEC
, &fixed_ss
,
2708 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
2709 PA_TAG_U32
, source
->module
? source
->module
->index
: PA_INVALID_INDEX
,
2710 PA_TAG_CVOLUME
, pa_source_get_volume(source
, FALSE
),
2711 PA_TAG_BOOLEAN
, pa_source_get_mute(source
, FALSE
),
2712 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
2713 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
2714 PA_TAG_USEC
, pa_source_get_latency(source
),
2715 PA_TAG_STRING
, source
->driver
,
2716 PA_TAG_U32
, source
->flags
,
2719 if (c
->version
>= 13) {
2720 pa_tagstruct_put_proplist(t
, source
->proplist
);
2721 pa_tagstruct_put_usec(t
, pa_source_get_requested_latency(source
));
2724 if (c
->version
>= 15) {
2725 pa_tagstruct_put_volume(t
, source
->base_volume
);
2726 if (PA_UNLIKELY(pa_source_get_state(source
) == PA_SOURCE_INVALID_STATE
))
2727 pa_log_error("Internal source state is invalid.");
2728 pa_tagstruct_putu32(t
, pa_source_get_state(source
));
2732 static void client_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_client
*client
) {
2736 pa_tagstruct_putu32(t
, client
->index
);
2737 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(client
->proplist
, PA_PROP_APPLICATION_NAME
)));
2738 pa_tagstruct_putu32(t
, client
->module
? client
->module
->index
: PA_INVALID_INDEX
);
2739 pa_tagstruct_puts(t
, client
->driver
);
2741 if (c
->version
>= 13)
2742 pa_tagstruct_put_proplist(t
, client
->proplist
);
2745 static void card_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_card
*card
) {
2752 pa_tagstruct_putu32(t
, card
->index
);
2753 pa_tagstruct_puts(t
, card
->name
);
2754 pa_tagstruct_putu32(t
, card
->module
? card
->module
->index
: PA_INVALID_INDEX
);
2755 pa_tagstruct_puts(t
, card
->driver
);
2757 pa_tagstruct_putu32(t
, card
->profiles
? pa_hashmap_size(card
->profiles
) : 0);
2759 if (card
->profiles
) {
2760 while ((p
= pa_hashmap_iterate(card
->profiles
, &state
, NULL
))) {
2761 pa_tagstruct_puts(t
, p
->name
);
2762 pa_tagstruct_puts(t
, p
->description
);
2766 pa_tagstruct_puts(t
, card
->active_profile
? card
->active_profile
->name
: NULL
);
2767 pa_tagstruct_put_proplist(t
, card
->proplist
);
2770 static void module_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_module
*module
) {
2774 pa_tagstruct_putu32(t
, module
->index
);
2775 pa_tagstruct_puts(t
, module
->name
);
2776 pa_tagstruct_puts(t
, module
->argument
);
2777 pa_tagstruct_putu32(t
, (uint32_t) pa_module_get_n_used(module
));
2779 if (c
->version
< 15)
2780 pa_tagstruct_put_boolean(t
, FALSE
); /* autoload is obsolete */
2782 if (c
->version
>= 15)
2783 pa_tagstruct_put_proplist(t
, module
->proplist
);
2786 static void sink_input_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_sink_input
*s
) {
2787 pa_sample_spec fixed_ss
;
2788 pa_usec_t sink_latency
;
2791 pa_sink_input_assert_ref(s
);
2793 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2795 pa_tagstruct_putu32(t
, s
->index
);
2796 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2797 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2798 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2799 pa_tagstruct_putu32(t
, s
->sink
->index
);
2800 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2801 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2802 pa_tagstruct_put_cvolume(t
, pa_sink_input_get_volume(s
));
2803 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
, &sink_latency
));
2804 pa_tagstruct_put_usec(t
, sink_latency
);
2805 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
2806 pa_tagstruct_puts(t
, s
->driver
);
2807 if (c
->version
>= 11)
2808 pa_tagstruct_put_boolean(t
, pa_sink_input_get_mute(s
));
2809 if (c
->version
>= 13)
2810 pa_tagstruct_put_proplist(t
, s
->proplist
);
2813 static void source_output_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_source_output
*s
) {
2814 pa_sample_spec fixed_ss
;
2815 pa_usec_t source_latency
;
2818 pa_source_output_assert_ref(s
);
2820 fixup_sample_spec(c
, &fixed_ss
, &s
->sample_spec
);
2822 pa_tagstruct_putu32(t
, s
->index
);
2823 pa_tagstruct_puts(t
, pa_strnull(pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
)));
2824 pa_tagstruct_putu32(t
, s
->module
? s
->module
->index
: PA_INVALID_INDEX
);
2825 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
2826 pa_tagstruct_putu32(t
, s
->source
->index
);
2827 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2828 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
2829 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
, &source_latency
));
2830 pa_tagstruct_put_usec(t
, source_latency
);
2831 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
2832 pa_tagstruct_puts(t
, s
->driver
);
2834 if (c
->version
>= 13)
2835 pa_tagstruct_put_proplist(t
, s
->proplist
);
2838 static void scache_fill_tagstruct(pa_native_connection
*c
, pa_tagstruct
*t
, pa_scache_entry
*e
) {
2839 pa_sample_spec fixed_ss
;
2844 if (e
->memchunk
.memblock
)
2845 fixup_sample_spec(c
, &fixed_ss
, &e
->sample_spec
);
2847 memset(&fixed_ss
, 0, sizeof(fixed_ss
));
2849 pa_tagstruct_putu32(t
, e
->index
);
2850 pa_tagstruct_puts(t
, e
->name
);
2851 pa_tagstruct_put_cvolume(t
, &e
->volume
);
2852 pa_tagstruct_put_usec(t
, e
->memchunk
.memblock
? pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
) : 0);
2853 pa_tagstruct_put_sample_spec(t
, &fixed_ss
);
2854 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
2855 pa_tagstruct_putu32(t
, (uint32_t) e
->memchunk
.length
);
2856 pa_tagstruct_put_boolean(t
, e
->lazy
);
2857 pa_tagstruct_puts(t
, e
->filename
);
2859 if (c
->version
>= 13)
2860 pa_tagstruct_put_proplist(t
, e
->proplist
);
2863 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2864 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2866 pa_sink
*sink
= NULL
;
2867 pa_source
*source
= NULL
;
2868 pa_client
*client
= NULL
;
2869 pa_card
*card
= NULL
;
2870 pa_core
*core
= NULL
;
2871 pa_module
*module
= NULL
;
2872 pa_sink_input
*si
= NULL
;
2873 pa_source_output
*so
= NULL
;
2874 pa_scache_entry
*sce
= NULL
;
2875 const char *name
= NULL
;
2876 pa_tagstruct
*reply
;
2878 pa_native_connection_assert_ref(c
);
2881 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2882 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2883 command
!= PA_COMMAND_GET_CARD_INFO
&&
2884 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2885 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2886 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2887 pa_tagstruct_gets(t
, &name
) < 0) ||
2888 !pa_tagstruct_eof(t
)) {
2893 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2894 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2895 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2896 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2897 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2899 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2900 if (idx
!= PA_INVALID_INDEX
)
2901 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2903 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
2904 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2905 if (idx
!= PA_INVALID_INDEX
)
2906 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2908 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
2909 } else if (command
== PA_COMMAND_GET_CARD_INFO
) {
2910 if (idx
!= PA_INVALID_INDEX
)
2911 card
= pa_idxset_get_by_index(c
->protocol
->core
->cards
, idx
);
2913 card
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_CARD
);
2914 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2915 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2916 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2917 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2918 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2919 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2920 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2921 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2923 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2924 if (idx
!= PA_INVALID_INDEX
)
2925 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2927 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
);
2930 if (!sink
&& !source
&& !client
&& !card
&& !module
&& !si
&& !so
&& !sce
) {
2931 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2935 reply
= reply_new(tag
);
2937 sink_fill_tagstruct(c
, reply
, sink
);
2939 source_fill_tagstruct(c
, reply
, source
);
2941 client_fill_tagstruct(c
, reply
, client
);
2943 card_fill_tagstruct(c
, reply
, card
);
2945 module_fill_tagstruct(c
, reply
, module
);
2947 sink_input_fill_tagstruct(c
, reply
, si
);
2949 source_output_fill_tagstruct(c
, reply
, so
);
2951 scache_fill_tagstruct(c
, reply
, sce
);
2952 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2955 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2956 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2960 pa_tagstruct
*reply
;
2962 pa_native_connection_assert_ref(c
);
2965 if (!pa_tagstruct_eof(t
)) {
2970 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2972 reply
= reply_new(tag
);
2974 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2975 i
= c
->protocol
->core
->sinks
;
2976 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2977 i
= c
->protocol
->core
->sources
;
2978 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2979 i
= c
->protocol
->core
->clients
;
2980 else if (command
== PA_COMMAND_GET_CARD_INFO_LIST
)
2981 i
= c
->protocol
->core
->cards
;
2982 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2983 i
= c
->protocol
->core
->modules
;
2984 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2985 i
= c
->protocol
->core
->sink_inputs
;
2986 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2987 i
= c
->protocol
->core
->source_outputs
;
2989 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2990 i
= c
->protocol
->core
->scache
;
2994 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2995 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2996 sink_fill_tagstruct(c
, reply
, p
);
2997 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2998 source_fill_tagstruct(c
, reply
, p
);
2999 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
3000 client_fill_tagstruct(c
, reply
, p
);
3001 else if (command
== PA_COMMAND_GET_CARD_INFO_LIST
)
3002 card_fill_tagstruct(c
, reply
, p
);
3003 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
3004 module_fill_tagstruct(c
, reply
, p
);
3005 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
3006 sink_input_fill_tagstruct(c
, reply
, p
);
3007 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
3008 source_output_fill_tagstruct(c
, reply
, p
);
3010 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
3011 scache_fill_tagstruct(c
, reply
, p
);
3016 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3019 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3020 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3021 pa_tagstruct
*reply
;
3024 pa_sample_spec fixed_ss
;
3026 pa_native_connection_assert_ref(c
);
3029 if (!pa_tagstruct_eof(t
)) {
3034 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3036 reply
= reply_new(tag
);
3037 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
3038 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
3039 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
3040 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
3042 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
3043 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
3045 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
3046 pa_tagstruct_puts(reply
, n
);
3047 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
3048 pa_tagstruct_puts(reply
, n
);
3050 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
3052 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3055 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
3057 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3059 pa_native_connection_assert_ref(c
);
3061 t
= pa_tagstruct_new(NULL
, 0);
3062 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
3063 pa_tagstruct_putu32(t
, (uint32_t) -1);
3064 pa_tagstruct_putu32(t
, e
);
3065 pa_tagstruct_putu32(t
, idx
);
3066 pa_pstream_send_tagstruct(c
->pstream
, t
);
3069 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3070 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3071 pa_subscription_mask_t m
;
3073 pa_native_connection_assert_ref(c
);
3076 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
3077 !pa_tagstruct_eof(t
)) {
3082 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3083 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
3085 if (c
->subscription
)
3086 pa_subscription_free(c
->subscription
);
3089 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
3090 pa_assert(c
->subscription
);
3092 c
->subscription
= NULL
;
3094 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3097 static void command_set_volume(
3104 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3107 pa_sink
*sink
= NULL
;
3108 pa_source
*source
= NULL
;
3109 pa_sink_input
*si
= NULL
;
3110 const char *name
= NULL
;
3112 pa_native_connection_assert_ref(c
);
3115 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3116 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3117 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3118 pa_tagstruct_get_cvolume(t
, &volume
) ||
3119 !pa_tagstruct_eof(t
)) {
3124 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3125 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3126 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3127 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3128 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3129 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
3133 case PA_COMMAND_SET_SINK_VOLUME
:
3134 if (idx
!= PA_INVALID_INDEX
)
3135 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3137 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3140 case PA_COMMAND_SET_SOURCE_VOLUME
:
3141 if (idx
!= PA_INVALID_INDEX
)
3142 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3144 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3147 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
3148 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3152 pa_assert_not_reached();
3155 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3158 pa_sink_set_volume(sink
, &volume
);
3160 pa_source_set_volume(source
, &volume
);
3162 pa_sink_input_set_volume(si
, &volume
);
3164 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3167 static void command_set_mute(
3174 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3177 pa_sink
*sink
= NULL
;
3178 pa_source
*source
= NULL
;
3179 pa_sink_input
*si
= NULL
;
3180 const char *name
= NULL
;
3182 pa_native_connection_assert_ref(c
);
3185 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3186 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3187 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3188 pa_tagstruct_get_boolean(t
, &mute
) ||
3189 !pa_tagstruct_eof(t
)) {
3194 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3195 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3196 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3197 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3198 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3202 case PA_COMMAND_SET_SINK_MUTE
:
3204 if (idx
!= PA_INVALID_INDEX
)
3205 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3207 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3211 case PA_COMMAND_SET_SOURCE_MUTE
:
3212 if (idx
!= PA_INVALID_INDEX
)
3213 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3215 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3219 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
3220 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3224 pa_assert_not_reached();
3227 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3230 pa_sink_set_mute(sink
, mute
);
3232 pa_source_set_mute(source
, mute
);
3234 pa_sink_input_set_mute(si
, mute
);
3236 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3239 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3240 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3245 pa_native_connection_assert_ref(c
);
3248 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3249 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3250 !pa_tagstruct_eof(t
)) {
3255 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3256 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3257 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3258 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3259 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3261 pa_sink_input_cork(s
->sink_input
, b
);
3264 s
->is_underrun
= TRUE
;
3266 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3269 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3270 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3274 pa_native_connection_assert_ref(c
);
3277 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3278 !pa_tagstruct_eof(t
)) {
3283 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3284 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3285 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3286 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3287 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3290 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3291 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3294 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3295 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3298 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3299 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3303 pa_assert_not_reached();
3306 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3309 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3310 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3315 pa_native_connection_assert_ref(c
);
3318 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3319 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3320 !pa_tagstruct_eof(t
)) {
3325 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3326 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3327 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3329 pa_source_output_cork(s
->source_output
, b
);
3330 pa_memblockq_prebuf_force(s
->memblockq
);
3331 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3334 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3335 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3339 pa_native_connection_assert_ref(c
);
3342 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3343 !pa_tagstruct_eof(t
)) {
3348 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3349 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3350 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3352 pa_memblockq_flush_read(s
->memblockq
);
3353 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3356 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3357 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3359 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3360 pa_tagstruct
*reply
;
3362 pa_native_connection_assert_ref(c
);
3365 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3370 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3372 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3374 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3376 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3377 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3378 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3380 if (pa_tagstruct_get(
3382 PA_TAG_U32
, &maxlength
,
3383 PA_TAG_U32
, &tlength
,
3384 PA_TAG_U32
, &prebuf
,
3385 PA_TAG_U32
, &minreq
,
3386 PA_TAG_INVALID
) < 0 ||
3387 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3388 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3389 !pa_tagstruct_eof(t
)) {
3394 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3395 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3396 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3397 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3398 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3399 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3401 reply
= reply_new(tag
);
3402 pa_tagstruct_putu32(reply
, maxlength
);
3403 pa_tagstruct_putu32(reply
, tlength
);
3404 pa_tagstruct_putu32(reply
, prebuf
);
3405 pa_tagstruct_putu32(reply
, minreq
);
3407 if (c
->version
>= 13)
3408 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3412 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3413 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3415 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3416 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3418 if (pa_tagstruct_get(
3420 PA_TAG_U32
, &maxlength
,
3421 PA_TAG_U32
, &fragsize
,
3422 PA_TAG_INVALID
) < 0 ||
3423 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3424 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3425 !pa_tagstruct_eof(t
)) {
3430 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &fragsize
);
3431 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3432 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3434 reply
= reply_new(tag
);
3435 pa_tagstruct_putu32(reply
, maxlength
);
3436 pa_tagstruct_putu32(reply
, fragsize
);
3438 if (c
->version
>= 13)
3439 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3442 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3445 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3446 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3450 pa_native_connection_assert_ref(c
);
3453 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3454 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3455 !pa_tagstruct_eof(t
)) {
3460 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3461 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3463 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3466 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3467 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3468 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3470 pa_sink_input_set_rate(s
->sink_input
, rate
);
3474 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3476 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3477 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3479 pa_source_output_set_rate(s
->source_output
, rate
);
3482 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3485 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3486 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3491 pa_native_connection_assert_ref(c
);
3494 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3496 p
= pa_proplist_new();
3498 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3500 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3501 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3502 !pa_tagstruct_eof(t
)) {
3504 pa_proplist_free(p
);
3510 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3511 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3512 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3513 !pa_tagstruct_eof(t
)) {
3515 pa_proplist_free(p
);
3520 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3522 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3525 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3526 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3527 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3529 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
3530 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3532 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3535 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3536 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3538 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
3539 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3541 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3543 pa_proplist_update(c
->client
->proplist
, mode
, p
);
3544 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3547 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3550 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3551 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3553 unsigned changed
= 0;
3555 pa_strlist
*l
= NULL
;
3557 pa_native_connection_assert_ref(c
);
3560 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3562 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3564 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3570 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3573 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3574 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3575 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3577 p
= s
->sink_input
->proplist
;
3579 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3582 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3583 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3585 p
= s
->source_output
->proplist
;
3587 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3589 p
= c
->client
->proplist
;
3595 if (pa_tagstruct_gets(t
, &k
) < 0) {
3604 l
= pa_strlist_prepend(l
, k
);
3607 if (!pa_tagstruct_eof(t
)) {
3616 l
= pa_strlist_pop(l
, &z
);
3621 changed
+= (unsigned) (pa_proplist_unset(p
, z
) >= 0);
3625 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3628 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3631 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3632 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3634 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3637 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3638 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3641 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3642 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3647 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3648 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3651 pa_native_connection_assert_ref(c
);
3654 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3655 !pa_tagstruct_eof(t
)) {
3660 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3661 CHECK_VALIDITY(c
->pstream
, !s
|| pa_namereg_is_valid_name(s
), tag
, PA_ERR_INVALID
);
3663 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
3664 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3667 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3668 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3672 pa_native_connection_assert_ref(c
);
3675 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3676 pa_tagstruct_gets(t
, &name
) < 0 ||
3677 !pa_tagstruct_eof(t
)) {
3682 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3683 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3685 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3688 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3689 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3690 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3692 pa_sink_input_set_name(s
->sink_input
, name
);
3696 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3698 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3699 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3701 pa_source_output_set_name(s
->source_output
, name
);
3704 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3707 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3708 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3711 pa_native_connection_assert_ref(c
);
3714 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3715 !pa_tagstruct_eof(t
)) {
3720 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3722 if (command
== PA_COMMAND_KILL_CLIENT
) {
3725 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3726 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3728 pa_native_connection_ref(c
);
3729 pa_client_kill(client
);
3731 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3734 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3735 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3737 pa_native_connection_ref(c
);
3738 pa_sink_input_kill(s
);
3740 pa_source_output
*s
;
3742 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3744 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3745 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3747 pa_native_connection_ref(c
);
3748 pa_source_output_kill(s
);
3751 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3752 pa_native_connection_unref(c
);
3755 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3756 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3758 const char *name
, *argument
;
3759 pa_tagstruct
*reply
;
3761 pa_native_connection_assert_ref(c
);
3764 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3765 pa_tagstruct_gets(t
, &argument
) < 0 ||
3766 !pa_tagstruct_eof(t
)) {
3771 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3772 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3773 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3775 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3776 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3780 reply
= reply_new(tag
);
3781 pa_tagstruct_putu32(reply
, m
->index
);
3782 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3785 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3786 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3790 pa_native_connection_assert_ref(c
);
3793 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3794 !pa_tagstruct_eof(t
)) {
3799 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3800 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3801 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3803 pa_module_unload_request(m
, FALSE
);
3804 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3807 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3808 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3809 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3810 const char *name_device
= NULL
;
3812 pa_native_connection_assert_ref(c
);
3815 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3816 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3817 pa_tagstruct_gets(t
, &name_device
) < 0 ||
3818 !pa_tagstruct_eof(t
)) {
3823 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3824 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3826 CHECK_VALIDITY(c
->pstream
, !name_device
|| pa_namereg_is_valid_name(name_device
), tag
, PA_ERR_INVALID
);
3827 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| name_device
, tag
, PA_ERR_INVALID
);
3828 CHECK_VALIDITY(c
->pstream
, idx_device
== PA_INVALID_INDEX
|| !name_device
, tag
, PA_ERR_INVALID
);
3829 CHECK_VALIDITY(c
->pstream
, !name_device
|| idx_device
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3831 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3832 pa_sink_input
*si
= NULL
;
3833 pa_sink
*sink
= NULL
;
3835 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3837 if (idx_device
!= PA_INVALID_INDEX
)
3838 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3840 sink
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SINK
);
3842 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3844 if (pa_sink_input_move_to(si
, sink
) < 0) {
3845 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3849 pa_source_output
*so
= NULL
;
3852 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3854 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3856 if (idx_device
!= PA_INVALID_INDEX
)
3857 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3859 source
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SOURCE
);
3861 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3863 if (pa_source_output_move_to(so
, source
) < 0) {
3864 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3869 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3872 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3873 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3874 uint32_t idx
= PA_INVALID_INDEX
;
3875 const char *name
= NULL
;
3878 pa_native_connection_assert_ref(c
);
3881 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3882 pa_tagstruct_gets(t
, &name
) < 0 ||
3883 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3884 !pa_tagstruct_eof(t
)) {
3889 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3890 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
) || *name
== 0, tag
, PA_ERR_INVALID
);
3891 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3892 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3893 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3895 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3897 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3899 pa_log_debug("%s all sinks", b
? "Suspending" : "Resuming");
3901 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3902 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3906 pa_sink
*sink
= NULL
;
3908 if (idx
!= PA_INVALID_INDEX
)
3909 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3911 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3913 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3915 if (pa_sink_suspend(sink
, b
) < 0) {
3916 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3922 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3924 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3926 pa_log_debug("%s all sources", b
? "Suspending" : "Resuming");
3928 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3929 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3936 if (idx
!= PA_INVALID_INDEX
)
3937 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3939 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3941 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3943 if (pa_source_suspend(source
, b
) < 0) {
3944 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3950 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3953 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3954 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3955 uint32_t idx
= PA_INVALID_INDEX
;
3956 const char *name
= NULL
;
3958 pa_native_protocol_ext_cb_t cb
;
3960 pa_native_connection_assert_ref(c
);
3963 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3964 pa_tagstruct_gets(t
, &name
) < 0) {
3969 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3970 CHECK_VALIDITY(c
->pstream
, !name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3971 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3972 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3973 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3975 if (idx
!= PA_INVALID_INDEX
)
3976 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3978 for (m
= pa_idxset_first(c
->protocol
->core
->modules
, &idx
); m
; m
= pa_idxset_next(c
->protocol
->core
->modules
, &idx
))
3979 if (strcmp(name
, m
->name
) == 0)
3983 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3984 CHECK_VALIDITY(c
->pstream
, m
->load_once
|| idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3986 cb
= (pa_native_protocol_ext_cb_t
) pa_hashmap_get(c
->protocol
->extensions
, m
);
3987 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3989 if (cb(c
->protocol
, m
, c
, tag
, t
) < 0)
3994 /*** pstream callbacks ***/
3996 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
3997 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4001 pa_native_connection_assert_ref(c
);
4003 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
4004 pa_log("invalid packet.");
4005 native_connection_unlink(c
);
4009 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
) {
4010 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4011 output_stream
*stream
;
4015 pa_native_connection_assert_ref(c
);
4017 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
4018 pa_log("client sent block for invalid stream.");
4023 /* pa_log("got %lu bytes", (unsigned long) chunk->length); */
4025 if (playback_stream_isinstance(stream
)) {
4026 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
4028 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
4029 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
);
4031 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
4034 upload_stream
*u
= UPLOAD_STREAM(stream
);
4037 if (!u
->memchunk
.memblock
) {
4038 if (u
->length
== chunk
->length
) {
4039 u
->memchunk
= *chunk
;
4040 pa_memblock_ref(u
->memchunk
.memblock
);
4043 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
4044 u
->memchunk
.index
= u
->memchunk
.length
= 0;
4048 pa_assert(u
->memchunk
.memblock
);
4051 if (l
> chunk
->length
)
4057 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
4058 src
= pa_memblock_acquire(chunk
->memblock
);
4060 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
4061 (uint8_t*) src
+chunk
->index
, l
);
4063 pa_memblock_release(u
->memchunk
.memblock
);
4064 pa_memblock_release(chunk
->memblock
);
4066 u
->memchunk
.length
+= l
;
4072 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
4073 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4076 pa_native_connection_assert_ref(c
);
4078 native_connection_unlink(c
);
4079 pa_log_info("Connection died.");
4082 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
4083 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4086 pa_native_connection_assert_ref(c
);
4088 native_connection_send_memblock(c
);
4091 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4094 if (!(q
= pa_thread_mq_get()))
4095 pa_pstream_send_revoke(p
, block_id
);
4097 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4100 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4103 if (!(q
= pa_thread_mq_get()))
4104 pa_pstream_send_release(p
, block_id
);
4106 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4109 /*** client callbacks ***/
4111 static void client_kill_cb(pa_client
*c
) {
4114 native_connection_unlink(PA_NATIVE_CONNECTION(c
->userdata
));
4115 pa_log_info("Connection killed.");
4118 /*** module entry points ***/
4120 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
4121 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4125 pa_native_connection_assert_ref(c
);
4126 pa_assert(c
->auth_timeout_event
== e
);
4128 if (!c
->authorized
) {
4129 native_connection_unlink(c
);
4130 pa_log_info("Connection terminated due to authentication timeout.");
4134 void pa_native_protocol_connect(pa_native_protocol
*p
, pa_iochannel
*io
, pa_native_options
*o
) {
4135 pa_native_connection
*c
;
4138 pa_client_new_data data
;
4144 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
4145 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
4146 pa_iochannel_free(io
);
4150 pa_client_new_data_init(&data
);
4151 data
.module
= o
->module
;
4152 data
.driver
= __FILE__
;
4153 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
4154 pa_proplist_setf(data
.proplist
, PA_PROP_APPLICATION_NAME
, "Native client (%s)", pname
);
4155 pa_proplist_sets(data
.proplist
, "native-protocol.peer", pname
);
4156 client
= pa_client_new(p
->core
, &data
);
4157 pa_client_new_data_done(&data
);
4162 c
= pa_msgobject_new(pa_native_connection
);
4163 c
->parent
.parent
.free
= native_connection_free
;
4164 c
->parent
.process_msg
= native_connection_process_msg
;
4166 c
->options
= pa_native_options_ref(o
);
4167 c
->authorized
= FALSE
;
4169 if (o
->auth_anonymous
) {
4170 pa_log_info("Client authenticated anonymously.");
4171 c
->authorized
= TRUE
;
4174 if (!c
->authorized
&&
4176 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
4178 pa_log_info("Client authenticated by IP ACL.");
4179 c
->authorized
= TRUE
;
4182 if (!c
->authorized
) {
4184 pa_gettimeofday(&tv
);
4185 tv
.tv_sec
+= AUTH_TIMEOUT
;
4186 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
4188 c
->auth_timeout_event
= NULL
;
4190 c
->is_local
= pa_iochannel_socket_is_local(io
);
4194 c
->client
->kill
= client_kill_cb
;
4195 c
->client
->userdata
= c
;
4197 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
4198 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
4199 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
4200 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
4201 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
4202 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
4203 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
4205 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
4207 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
4208 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
4210 c
->rrobin_index
= PA_IDXSET_INVALID
;
4211 c
->subscription
= NULL
;
4213 pa_idxset_put(p
->connections
, c
, NULL
);
4216 if (pa_iochannel_creds_supported(io
))
4217 pa_iochannel_creds_enable(io
);
4220 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_CONNECTION_PUT
], c
);
4223 void pa_native_protocol_disconnect(pa_native_protocol
*p
, pa_module
*m
) {
4224 pa_native_connection
*c
;
4230 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
4231 if (c
->options
->module
== m
)
4232 native_connection_unlink(c
);
4235 static pa_native_protocol
* native_protocol_new(pa_core
*c
) {
4236 pa_native_protocol
*p
;
4241 p
= pa_xnew(pa_native_protocol
, 1);
4244 p
->connections
= pa_idxset_new(NULL
, NULL
);
4248 p
->extensions
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
4250 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4251 pa_hook_init(&p
->hooks
[h
], p
);
4253 pa_assert_se(pa_shared_set(c
, "native-protocol", p
) >= 0);
4258 pa_native_protocol
* pa_native_protocol_get(pa_core
*c
) {
4259 pa_native_protocol
*p
;
4261 if ((p
= pa_shared_get(c
, "native-protocol")))
4262 return pa_native_protocol_ref(p
);
4264 return native_protocol_new(c
);
4267 pa_native_protocol
* pa_native_protocol_ref(pa_native_protocol
*p
) {
4269 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4276 void pa_native_protocol_unref(pa_native_protocol
*p
) {
4277 pa_native_connection
*c
;
4281 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4283 if (PA_REFCNT_DEC(p
) > 0)
4286 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4287 native_connection_unlink(c
);
4289 pa_idxset_free(p
->connections
, NULL
, NULL
);
4291 pa_strlist_free(p
->servers
);
4293 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4294 pa_hook_done(&p
->hooks
[h
]);
4296 pa_hashmap_free(p
->extensions
, NULL
, NULL
);
4298 pa_assert_se(pa_shared_remove(p
->core
, "native-protocol") >= 0);
4303 void pa_native_protocol_add_server_string(pa_native_protocol
*p
, const char *name
) {
4305 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4308 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
4310 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4313 void pa_native_protocol_remove_server_string(pa_native_protocol
*p
, const char *name
) {
4315 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4318 p
->servers
= pa_strlist_remove(p
->servers
, name
);
4320 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4323 pa_hook
*pa_native_protocol_hooks(pa_native_protocol
*p
) {
4325 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4330 pa_strlist
*pa_native_protocol_servers(pa_native_protocol
*p
) {
4332 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4337 int pa_native_protocol_install_ext(pa_native_protocol
*p
, pa_module
*m
, pa_native_protocol_ext_cb_t cb
) {
4339 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4342 pa_assert(!pa_hashmap_get(p
->extensions
, m
));
4344 pa_assert_se(pa_hashmap_put(p
->extensions
, m
, (void*) cb
) == 0);
4348 void pa_native_protocol_remove_ext(pa_native_protocol
*p
, pa_module
*m
) {
4350 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4353 pa_assert_se(pa_hashmap_remove(p
->extensions
, m
));
4356 pa_native_options
* pa_native_options_new(void) {
4357 pa_native_options
*o
;
4359 o
= pa_xnew0(pa_native_options
, 1);
4365 pa_native_options
* pa_native_options_ref(pa_native_options
*o
) {
4367 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4374 void pa_native_options_unref(pa_native_options
*o
) {
4376 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4378 if (PA_REFCNT_DEC(o
) > 0)
4381 pa_xfree(o
->auth_group
);
4384 pa_ip_acl_free(o
->auth_ip_acl
);
4387 pa_auth_cookie_unref(o
->auth_cookie
);
4392 int pa_native_options_parse(pa_native_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
4397 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4400 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
4401 pa_log("auth-anonymous= expects a boolean argument.");
4406 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &enabled
) < 0) {
4407 pa_log("auth-group-enabled= expects a boolean argument.");
4411 pa_xfree(o
->auth_group
);
4412 o
->auth_group
= enabled
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4416 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4419 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4422 if (!(ipa
= pa_ip_acl_new(acl
))) {
4423 pa_log("Failed to parse IP ACL '%s'", acl
);
4428 pa_ip_acl_free(o
->auth_ip_acl
);
4430 o
->auth_ip_acl
= ipa
;
4434 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
4435 pa_log("auth-cookie-enabled= expects a boolean argument.");
4440 pa_auth_cookie_unref(o
->auth_cookie
);
4445 /* The new name for this is 'auth-cookie', for compat reasons
4446 * we check the old name too */
4447 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
4448 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
4449 cn
= PA_NATIVE_COOKIE_FILE
;
4451 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, PA_NATIVE_COOKIE_LENGTH
)))
4455 o
->auth_cookie
= NULL
;
4460 pa_pstream
* pa_native_connection_get_pstream(pa_native_connection
*c
) {
4461 pa_native_connection_assert_ref(c
);