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_module
*module
= NULL
;
2871 pa_sink_input
*si
= NULL
;
2872 pa_source_output
*so
= NULL
;
2873 pa_scache_entry
*sce
= NULL
;
2874 const char *name
= NULL
;
2875 pa_tagstruct
*reply
;
2877 pa_native_connection_assert_ref(c
);
2880 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
2881 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
2882 command
!= PA_COMMAND_GET_CARD_INFO
&&
2883 command
!= PA_COMMAND_GET_MODULE_INFO
&&
2884 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
2885 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
2886 pa_tagstruct_gets(t
, &name
) < 0) ||
2887 !pa_tagstruct_eof(t
)) {
2892 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2893 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
2894 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
2895 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
2896 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
2898 if (command
== PA_COMMAND_GET_SINK_INFO
) {
2899 if (idx
!= PA_INVALID_INDEX
)
2900 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
2902 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
2903 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
2904 if (idx
!= PA_INVALID_INDEX
)
2905 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
2907 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
2908 } else if (command
== PA_COMMAND_GET_CARD_INFO
) {
2909 if (idx
!= PA_INVALID_INDEX
)
2910 card
= pa_idxset_get_by_index(c
->protocol
->core
->cards
, idx
);
2912 card
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_CARD
);
2913 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
2914 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
2915 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
2916 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
2917 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
2918 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
2919 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
2920 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
2922 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
2923 if (idx
!= PA_INVALID_INDEX
)
2924 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
2926 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
);
2929 if (!sink
&& !source
&& !client
&& !card
&& !module
&& !si
&& !so
&& !sce
) {
2930 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2934 reply
= reply_new(tag
);
2936 sink_fill_tagstruct(c
, reply
, sink
);
2938 source_fill_tagstruct(c
, reply
, source
);
2940 client_fill_tagstruct(c
, reply
, client
);
2942 card_fill_tagstruct(c
, reply
, card
);
2944 module_fill_tagstruct(c
, reply
, module
);
2946 sink_input_fill_tagstruct(c
, reply
, si
);
2948 source_output_fill_tagstruct(c
, reply
, so
);
2950 scache_fill_tagstruct(c
, reply
, sce
);
2951 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2954 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2955 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
2959 pa_tagstruct
*reply
;
2961 pa_native_connection_assert_ref(c
);
2964 if (!pa_tagstruct_eof(t
)) {
2969 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2971 reply
= reply_new(tag
);
2973 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2974 i
= c
->protocol
->core
->sinks
;
2975 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2976 i
= c
->protocol
->core
->sources
;
2977 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2978 i
= c
->protocol
->core
->clients
;
2979 else if (command
== PA_COMMAND_GET_CARD_INFO_LIST
)
2980 i
= c
->protocol
->core
->cards
;
2981 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
2982 i
= c
->protocol
->core
->modules
;
2983 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
2984 i
= c
->protocol
->core
->sink_inputs
;
2985 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
2986 i
= c
->protocol
->core
->source_outputs
;
2988 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
2989 i
= c
->protocol
->core
->scache
;
2993 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
2994 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
2995 sink_fill_tagstruct(c
, reply
, p
);
2996 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
2997 source_fill_tagstruct(c
, reply
, p
);
2998 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
2999 client_fill_tagstruct(c
, reply
, p
);
3000 else if (command
== PA_COMMAND_GET_CARD_INFO_LIST
)
3001 card_fill_tagstruct(c
, reply
, p
);
3002 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
3003 module_fill_tagstruct(c
, reply
, p
);
3004 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
3005 sink_input_fill_tagstruct(c
, reply
, p
);
3006 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
3007 source_output_fill_tagstruct(c
, reply
, p
);
3009 pa_assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
3010 scache_fill_tagstruct(c
, reply
, p
);
3015 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3018 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3019 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3020 pa_tagstruct
*reply
;
3023 pa_sample_spec fixed_ss
;
3025 pa_native_connection_assert_ref(c
);
3028 if (!pa_tagstruct_eof(t
)) {
3033 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3035 reply
= reply_new(tag
);
3036 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
3037 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
3038 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
3039 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
3041 fixup_sample_spec(c
, &fixed_ss
, &c
->protocol
->core
->default_sample_spec
);
3042 pa_tagstruct_put_sample_spec(reply
, &fixed_ss
);
3044 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
3045 pa_tagstruct_puts(reply
, n
);
3046 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
3047 pa_tagstruct_puts(reply
, n
);
3049 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
3051 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3054 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
3056 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3058 pa_native_connection_assert_ref(c
);
3060 t
= pa_tagstruct_new(NULL
, 0);
3061 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
3062 pa_tagstruct_putu32(t
, (uint32_t) -1);
3063 pa_tagstruct_putu32(t
, e
);
3064 pa_tagstruct_putu32(t
, idx
);
3065 pa_pstream_send_tagstruct(c
->pstream
, t
);
3068 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3069 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3070 pa_subscription_mask_t m
;
3072 pa_native_connection_assert_ref(c
);
3075 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
3076 !pa_tagstruct_eof(t
)) {
3081 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3082 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
3084 if (c
->subscription
)
3085 pa_subscription_free(c
->subscription
);
3088 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
3089 pa_assert(c
->subscription
);
3091 c
->subscription
= NULL
;
3093 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3096 static void command_set_volume(
3103 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3106 pa_sink
*sink
= NULL
;
3107 pa_source
*source
= NULL
;
3108 pa_sink_input
*si
= NULL
;
3109 const char *name
= NULL
;
3111 pa_native_connection_assert_ref(c
);
3114 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3115 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3116 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3117 pa_tagstruct_get_cvolume(t
, &volume
) ||
3118 !pa_tagstruct_eof(t
)) {
3123 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3124 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3125 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3126 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3127 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3128 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
3132 case PA_COMMAND_SET_SINK_VOLUME
:
3133 if (idx
!= PA_INVALID_INDEX
)
3134 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3136 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3139 case PA_COMMAND_SET_SOURCE_VOLUME
:
3140 if (idx
!= PA_INVALID_INDEX
)
3141 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3143 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3146 case PA_COMMAND_SET_SINK_INPUT_VOLUME
:
3147 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3151 pa_assert_not_reached();
3154 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3157 pa_sink_set_volume(sink
, &volume
);
3159 pa_source_set_volume(source
, &volume
);
3161 pa_sink_input_set_volume(si
, &volume
);
3163 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3166 static void command_set_mute(
3173 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3176 pa_sink
*sink
= NULL
;
3177 pa_source
*source
= NULL
;
3178 pa_sink_input
*si
= NULL
;
3179 const char *name
= NULL
;
3181 pa_native_connection_assert_ref(c
);
3184 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3185 (command
== PA_COMMAND_SET_SINK_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3186 (command
== PA_COMMAND_SET_SOURCE_MUTE
&& pa_tagstruct_gets(t
, &name
) < 0) ||
3187 pa_tagstruct_get_boolean(t
, &mute
) ||
3188 !pa_tagstruct_eof(t
)) {
3193 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3194 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
), tag
, PA_ERR_INVALID
);
3195 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3196 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3197 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3201 case PA_COMMAND_SET_SINK_MUTE
:
3203 if (idx
!= PA_INVALID_INDEX
)
3204 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3206 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3210 case PA_COMMAND_SET_SOURCE_MUTE
:
3211 if (idx
!= PA_INVALID_INDEX
)
3212 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3214 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3218 case PA_COMMAND_SET_SINK_INPUT_MUTE
:
3219 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3223 pa_assert_not_reached();
3226 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
3229 pa_sink_set_mute(sink
, mute
);
3231 pa_source_set_mute(source
, mute
);
3233 pa_sink_input_set_mute(si
, mute
);
3235 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3238 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3239 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3244 pa_native_connection_assert_ref(c
);
3247 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3248 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3249 !pa_tagstruct_eof(t
)) {
3254 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3255 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3256 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3257 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3258 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3260 pa_sink_input_cork(s
->sink_input
, b
);
3263 s
->is_underrun
= TRUE
;
3265 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3268 static void command_trigger_or_flush_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3269 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3273 pa_native_connection_assert_ref(c
);
3276 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3277 !pa_tagstruct_eof(t
)) {
3282 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3283 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3284 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3285 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3286 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3289 case PA_COMMAND_FLUSH_PLAYBACK_STREAM
:
3290 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_FLUSH
, NULL
, 0, NULL
);
3293 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
3294 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_PREBUF_FORCE
, NULL
, 0, NULL
);
3297 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
3298 pa_asyncmsgq_send(s
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(s
->sink_input
), SINK_INPUT_MESSAGE_TRIGGER
, NULL
, 0, NULL
);
3302 pa_assert_not_reached();
3305 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3308 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3309 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3314 pa_native_connection_assert_ref(c
);
3317 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3318 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3319 !pa_tagstruct_eof(t
)) {
3324 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3325 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3326 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3328 pa_source_output_cork(s
->source_output
, b
);
3329 pa_memblockq_prebuf_force(s
->memblockq
);
3330 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3333 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3334 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3338 pa_native_connection_assert_ref(c
);
3341 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3342 !pa_tagstruct_eof(t
)) {
3347 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3348 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3349 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3351 pa_memblockq_flush_read(s
->memblockq
);
3352 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3355 static void command_set_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3356 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3358 uint32_t maxlength
, tlength
, prebuf
, minreq
, fragsize
;
3359 pa_tagstruct
*reply
;
3361 pa_native_connection_assert_ref(c
);
3364 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3369 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3371 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
) {
3373 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3375 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3376 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3377 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3379 if (pa_tagstruct_get(
3381 PA_TAG_U32
, &maxlength
,
3382 PA_TAG_U32
, &tlength
,
3383 PA_TAG_U32
, &prebuf
,
3384 PA_TAG_U32
, &minreq
,
3385 PA_TAG_INVALID
) < 0 ||
3386 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3387 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3388 !pa_tagstruct_eof(t
)) {
3393 fix_playback_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3394 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3395 pa_memblockq_set_tlength(s
->memblockq
, tlength
);
3396 pa_memblockq_set_prebuf(s
->memblockq
, prebuf
);
3397 pa_memblockq_set_minreq(s
->memblockq
, minreq
);
3398 fix_playback_buffer_attr_post(s
, &maxlength
, &tlength
, &prebuf
, &minreq
);
3400 reply
= reply_new(tag
);
3401 pa_tagstruct_putu32(reply
, maxlength
);
3402 pa_tagstruct_putu32(reply
, tlength
);
3403 pa_tagstruct_putu32(reply
, prebuf
);
3404 pa_tagstruct_putu32(reply
, minreq
);
3406 if (c
->version
>= 13)
3407 pa_tagstruct_put_usec(reply
, s
->sink_latency
);
3411 pa_bool_t adjust_latency
= FALSE
, early_requests
= FALSE
;
3412 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
);
3414 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3415 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3417 if (pa_tagstruct_get(
3419 PA_TAG_U32
, &maxlength
,
3420 PA_TAG_U32
, &fragsize
,
3421 PA_TAG_INVALID
) < 0 ||
3422 (c
->version
>= 13 && pa_tagstruct_get_boolean(t
, &adjust_latency
) < 0) ||
3423 (c
->version
>= 14 && pa_tagstruct_get_boolean(t
, &early_requests
) < 0) ||
3424 !pa_tagstruct_eof(t
)) {
3429 fix_record_buffer_attr_pre(s
, adjust_latency
, early_requests
, &maxlength
, &fragsize
);
3430 pa_memblockq_set_maxlength(s
->memblockq
, maxlength
);
3431 fix_record_buffer_attr_post(s
, &maxlength
, &fragsize
);
3433 reply
= reply_new(tag
);
3434 pa_tagstruct_putu32(reply
, maxlength
);
3435 pa_tagstruct_putu32(reply
, fragsize
);
3437 if (c
->version
>= 13)
3438 pa_tagstruct_put_usec(reply
, s
->source_latency
);
3441 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3444 static void command_update_stream_sample_rate(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3445 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3449 pa_native_connection_assert_ref(c
);
3452 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3453 pa_tagstruct_getu32(t
, &rate
) < 0 ||
3454 !pa_tagstruct_eof(t
)) {
3459 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3460 CHECK_VALIDITY(c
->pstream
, rate
> 0 && rate
<= PA_RATE_MAX
, tag
, PA_ERR_INVALID
);
3462 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
) {
3465 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3466 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3467 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3469 pa_sink_input_set_rate(s
->sink_input
, rate
);
3473 pa_assert(command
== PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
);
3475 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3476 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3478 pa_source_output_set_rate(s
->source_output
, rate
);
3481 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3484 static void command_update_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3485 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3490 pa_native_connection_assert_ref(c
);
3493 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3495 p
= pa_proplist_new();
3497 if (command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
) {
3499 if (pa_tagstruct_getu32(t
, &mode
) < 0 ||
3500 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3501 !pa_tagstruct_eof(t
)) {
3503 pa_proplist_free(p
);
3509 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3510 pa_tagstruct_getu32(t
, &mode
) < 0 ||
3511 pa_tagstruct_get_proplist(t
, p
) < 0 ||
3512 !pa_tagstruct_eof(t
)) {
3514 pa_proplist_free(p
);
3519 CHECK_VALIDITY(c
->pstream
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, tag
, PA_ERR_INVALID
);
3521 if (command
== PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
) {
3524 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3525 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3526 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3528 pa_proplist_update(s
->sink_input
->proplist
, mode
, p
);
3529 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3531 } else if (command
== PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
) {
3534 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3535 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3537 pa_proplist_update(s
->source_output
->proplist
, mode
, p
);
3538 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3540 pa_assert(command
== PA_COMMAND_UPDATE_CLIENT_PROPLIST
);
3542 pa_proplist_update(c
->client
->proplist
, mode
, p
);
3543 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3546 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3549 static void command_remove_proplist(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3550 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3552 unsigned changed
= 0;
3554 pa_strlist
*l
= NULL
;
3556 pa_native_connection_assert_ref(c
);
3559 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3561 if (command
!= PA_COMMAND_REMOVE_CLIENT_PROPLIST
) {
3563 if (pa_tagstruct_getu32(t
, &idx
) < 0) {
3569 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3572 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3573 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3574 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3576 p
= s
->sink_input
->proplist
;
3578 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3581 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3582 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3584 p
= s
->source_output
->proplist
;
3586 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3588 p
= c
->client
->proplist
;
3594 if (pa_tagstruct_gets(t
, &k
) < 0) {
3603 l
= pa_strlist_prepend(l
, k
);
3606 if (!pa_tagstruct_eof(t
)) {
3615 l
= pa_strlist_pop(l
, &z
);
3620 changed
+= (unsigned) (pa_proplist_unset(p
, z
) >= 0);
3624 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3627 if (command
== PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
) {
3630 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3631 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->sink_input
->index
);
3633 } else if (command
== PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
) {
3636 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3637 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->source_output
->index
);
3640 pa_assert(command
== PA_COMMAND_REMOVE_CLIENT_PROPLIST
);
3641 pa_subscription_post(c
->protocol
->core
, PA_SUBSCRIPTION_EVENT_CLIENT
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->client
->index
);
3646 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3647 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3650 pa_native_connection_assert_ref(c
);
3653 if (pa_tagstruct_gets(t
, &s
) < 0 ||
3654 !pa_tagstruct_eof(t
)) {
3659 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3660 CHECK_VALIDITY(c
->pstream
, !s
|| pa_namereg_is_valid_name(s
), tag
, PA_ERR_INVALID
);
3662 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
3663 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3666 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3667 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3671 pa_native_connection_assert_ref(c
);
3674 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3675 pa_tagstruct_gets(t
, &name
) < 0 ||
3676 !pa_tagstruct_eof(t
)) {
3681 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3682 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3684 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
3687 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
3688 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3689 CHECK_VALIDITY(c
->pstream
, playback_stream_isinstance(s
), tag
, PA_ERR_NOENTITY
);
3691 pa_sink_input_set_name(s
->sink_input
, name
);
3695 pa_assert(command
== PA_COMMAND_SET_RECORD_STREAM_NAME
);
3697 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
3698 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3700 pa_source_output_set_name(s
->source_output
, name
);
3703 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3706 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3707 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3710 pa_native_connection_assert_ref(c
);
3713 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3714 !pa_tagstruct_eof(t
)) {
3719 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3721 if (command
== PA_COMMAND_KILL_CLIENT
) {
3724 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
3725 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
3727 pa_native_connection_ref(c
);
3728 pa_client_kill(client
);
3730 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
3733 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3734 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3736 pa_native_connection_ref(c
);
3737 pa_sink_input_kill(s
);
3739 pa_source_output
*s
;
3741 pa_assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
3743 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3744 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
3746 pa_native_connection_ref(c
);
3747 pa_source_output_kill(s
);
3750 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3751 pa_native_connection_unref(c
);
3754 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3755 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3757 const char *name
, *argument
;
3758 pa_tagstruct
*reply
;
3760 pa_native_connection_assert_ref(c
);
3763 if (pa_tagstruct_gets(t
, &name
) < 0 ||
3764 pa_tagstruct_gets(t
, &argument
) < 0 ||
3765 !pa_tagstruct_eof(t
)) {
3770 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3771 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
3772 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
3774 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
3775 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
3779 reply
= reply_new(tag
);
3780 pa_tagstruct_putu32(reply
, m
->index
);
3781 pa_pstream_send_tagstruct(c
->pstream
, reply
);
3784 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3785 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3789 pa_native_connection_assert_ref(c
);
3792 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3793 !pa_tagstruct_eof(t
)) {
3798 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3799 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3800 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
3802 pa_module_unload_request(m
, FALSE
);
3803 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3806 static void command_move_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3807 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3808 uint32_t idx
= PA_INVALID_INDEX
, idx_device
= PA_INVALID_INDEX
;
3809 const char *name_device
= NULL
;
3811 pa_native_connection_assert_ref(c
);
3814 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3815 pa_tagstruct_getu32(t
, &idx_device
) < 0 ||
3816 pa_tagstruct_gets(t
, &name_device
) < 0 ||
3817 !pa_tagstruct_eof(t
)) {
3822 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3823 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3825 CHECK_VALIDITY(c
->pstream
, !name_device
|| pa_namereg_is_valid_name(name_device
), tag
, PA_ERR_INVALID
);
3826 CHECK_VALIDITY(c
->pstream
, idx_device
!= PA_INVALID_INDEX
|| 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
, !name_device
|| idx_device
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3830 if (command
== PA_COMMAND_MOVE_SINK_INPUT
) {
3831 pa_sink_input
*si
= NULL
;
3832 pa_sink
*sink
= NULL
;
3834 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
3836 if (idx_device
!= PA_INVALID_INDEX
)
3837 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx_device
);
3839 sink
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SINK
);
3841 CHECK_VALIDITY(c
->pstream
, si
&& sink
, tag
, PA_ERR_NOENTITY
);
3843 if (pa_sink_input_move_to(si
, sink
) < 0) {
3844 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3848 pa_source_output
*so
= NULL
;
3851 pa_assert(command
== PA_COMMAND_MOVE_SOURCE_OUTPUT
);
3853 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
3855 if (idx_device
!= PA_INVALID_INDEX
)
3856 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx_device
);
3858 source
= pa_namereg_get(c
->protocol
->core
, name_device
, PA_NAMEREG_SOURCE
);
3860 CHECK_VALIDITY(c
->pstream
, so
&& source
, tag
, PA_ERR_NOENTITY
);
3862 if (pa_source_output_move_to(so
, source
) < 0) {
3863 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3868 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3871 static void command_suspend(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3872 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3873 uint32_t idx
= PA_INVALID_INDEX
;
3874 const char *name
= NULL
;
3877 pa_native_connection_assert_ref(c
);
3880 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3881 pa_tagstruct_gets(t
, &name
) < 0 ||
3882 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
3883 !pa_tagstruct_eof(t
)) {
3888 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3889 CHECK_VALIDITY(c
->pstream
, !name
|| pa_namereg_is_valid_name(name
) || *name
== 0, tag
, PA_ERR_INVALID
);
3890 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3891 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3892 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3894 if (command
== PA_COMMAND_SUSPEND_SINK
) {
3896 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3898 pa_log_debug("%s all sinks", b
? "Suspending" : "Resuming");
3900 if (pa_sink_suspend_all(c
->protocol
->core
, b
) < 0) {
3901 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3905 pa_sink
*sink
= NULL
;
3907 if (idx
!= PA_INVALID_INDEX
)
3908 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
3910 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
);
3912 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
3914 if (pa_sink_suspend(sink
, b
) < 0) {
3915 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3921 pa_assert(command
== PA_COMMAND_SUSPEND_SOURCE
);
3923 if (idx
== PA_INVALID_INDEX
&& name
&& !*name
) {
3925 pa_log_debug("%s all sources", b
? "Suspending" : "Resuming");
3927 if (pa_source_suspend_all(c
->protocol
->core
, b
) < 0) {
3928 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3935 if (idx
!= PA_INVALID_INDEX
)
3936 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
3938 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
);
3940 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
3942 if (pa_source_suspend(source
, b
) < 0) {
3943 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
3949 pa_pstream_send_simple_ack(c
->pstream
, tag
);
3952 static void command_extension(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
3953 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
3954 uint32_t idx
= PA_INVALID_INDEX
;
3955 const char *name
= NULL
;
3957 pa_native_protocol_ext_cb_t cb
;
3959 pa_native_connection_assert_ref(c
);
3962 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
3963 pa_tagstruct_gets(t
, &name
) < 0) {
3968 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
3969 CHECK_VALIDITY(c
->pstream
, !name
|| pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
3970 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| name
, tag
, PA_ERR_INVALID
);
3971 CHECK_VALIDITY(c
->pstream
, idx
== PA_INVALID_INDEX
|| !name
, tag
, PA_ERR_INVALID
);
3972 CHECK_VALIDITY(c
->pstream
, !name
|| idx
== PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3974 if (idx
!= PA_INVALID_INDEX
)
3975 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
3977 for (m
= pa_idxset_first(c
->protocol
->core
->modules
, &idx
); m
; m
= pa_idxset_next(c
->protocol
->core
->modules
, &idx
))
3978 if (strcmp(name
, m
->name
) == 0)
3982 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3983 CHECK_VALIDITY(c
->pstream
, m
->load_once
|| idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
3985 cb
= (pa_native_protocol_ext_cb_t
) pa_hashmap_get(c
->protocol
->extensions
, m
);
3986 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOEXTENSION
);
3988 if (cb(c
->protocol
, m
, c
, tag
, t
) < 0)
3993 /*** pstream callbacks ***/
3995 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
3996 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4000 pa_native_connection_assert_ref(c
);
4002 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
4003 pa_log("invalid packet.");
4004 native_connection_unlink(c
);
4008 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
) {
4009 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4010 output_stream
*stream
;
4014 pa_native_connection_assert_ref(c
);
4016 if (!(stream
= OUTPUT_STREAM(pa_idxset_get_by_index(c
->output_streams
, channel
)))) {
4017 pa_log("client sent block for invalid stream.");
4022 /* pa_log("got %lu bytes", (unsigned long) chunk->length); */
4024 if (playback_stream_isinstance(stream
)) {
4025 playback_stream
*ps
= PLAYBACK_STREAM(stream
);
4027 if (seek
!= PA_SEEK_RELATIVE
|| offset
!= 0)
4028 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
);
4030 pa_asyncmsgq_post(ps
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(ps
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
4033 upload_stream
*u
= UPLOAD_STREAM(stream
);
4036 if (!u
->memchunk
.memblock
) {
4037 if (u
->length
== chunk
->length
) {
4038 u
->memchunk
= *chunk
;
4039 pa_memblock_ref(u
->memchunk
.memblock
);
4042 u
->memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, u
->length
);
4043 u
->memchunk
.index
= u
->memchunk
.length
= 0;
4047 pa_assert(u
->memchunk
.memblock
);
4050 if (l
> chunk
->length
)
4056 dst
= pa_memblock_acquire(u
->memchunk
.memblock
);
4057 src
= pa_memblock_acquire(chunk
->memblock
);
4059 memcpy((uint8_t*) dst
+ u
->memchunk
.index
+ u
->memchunk
.length
,
4060 (uint8_t*) src
+chunk
->index
, l
);
4062 pa_memblock_release(u
->memchunk
.memblock
);
4063 pa_memblock_release(chunk
->memblock
);
4065 u
->memchunk
.length
+= l
;
4071 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
4072 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4075 pa_native_connection_assert_ref(c
);
4077 native_connection_unlink(c
);
4078 pa_log_info("Connection died.");
4081 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
4082 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4085 pa_native_connection_assert_ref(c
);
4087 native_connection_send_memblock(c
);
4090 static void pstream_revoke_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4093 if (!(q
= pa_thread_mq_get()))
4094 pa_pstream_send_revoke(p
, block_id
);
4096 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_REVOKE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4099 static void pstream_release_callback(pa_pstream
*p
, uint32_t block_id
, void *userdata
) {
4102 if (!(q
= pa_thread_mq_get()))
4103 pa_pstream_send_release(p
, block_id
);
4105 pa_asyncmsgq_post(q
->outq
, PA_MSGOBJECT(userdata
), CONNECTION_MESSAGE_RELEASE
, PA_UINT_TO_PTR(block_id
), 0, NULL
, NULL
);
4108 /*** client callbacks ***/
4110 static void client_kill_cb(pa_client
*c
) {
4113 native_connection_unlink(PA_NATIVE_CONNECTION(c
->userdata
));
4114 pa_log_info("Connection killed.");
4117 /*** module entry points ***/
4119 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
4120 pa_native_connection
*c
= PA_NATIVE_CONNECTION(userdata
);
4124 pa_native_connection_assert_ref(c
);
4125 pa_assert(c
->auth_timeout_event
== e
);
4127 if (!c
->authorized
) {
4128 native_connection_unlink(c
);
4129 pa_log_info("Connection terminated due to authentication timeout.");
4133 void pa_native_protocol_connect(pa_native_protocol
*p
, pa_iochannel
*io
, pa_native_options
*o
) {
4134 pa_native_connection
*c
;
4137 pa_client_new_data data
;
4143 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
4144 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
4145 pa_iochannel_free(io
);
4149 pa_client_new_data_init(&data
);
4150 data
.module
= o
->module
;
4151 data
.driver
= __FILE__
;
4152 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
4153 pa_proplist_setf(data
.proplist
, PA_PROP_APPLICATION_NAME
, "Native client (%s)", pname
);
4154 pa_proplist_sets(data
.proplist
, "native-protocol.peer", pname
);
4155 client
= pa_client_new(p
->core
, &data
);
4156 pa_client_new_data_done(&data
);
4161 c
= pa_msgobject_new(pa_native_connection
);
4162 c
->parent
.parent
.free
= native_connection_free
;
4163 c
->parent
.process_msg
= native_connection_process_msg
;
4165 c
->options
= pa_native_options_ref(o
);
4166 c
->authorized
= FALSE
;
4168 if (o
->auth_anonymous
) {
4169 pa_log_info("Client authenticated anonymously.");
4170 c
->authorized
= TRUE
;
4173 if (!c
->authorized
&&
4175 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
4177 pa_log_info("Client authenticated by IP ACL.");
4178 c
->authorized
= TRUE
;
4181 if (!c
->authorized
) {
4183 pa_gettimeofday(&tv
);
4184 tv
.tv_sec
+= AUTH_TIMEOUT
;
4185 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
4187 c
->auth_timeout_event
= NULL
;
4189 c
->is_local
= pa_iochannel_socket_is_local(io
);
4193 c
->client
->kill
= client_kill_cb
;
4194 c
->client
->userdata
= c
;
4196 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->mempool
);
4197 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
4198 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
4199 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
4200 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
4201 pa_pstream_set_revoke_callback(c
->pstream
, pstream_revoke_callback
, c
);
4202 pa_pstream_set_release_callback(c
->pstream
, pstream_release_callback
, c
);
4204 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
4206 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
4207 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
4209 c
->rrobin_index
= PA_IDXSET_INVALID
;
4210 c
->subscription
= NULL
;
4212 pa_idxset_put(p
->connections
, c
, NULL
);
4215 if (pa_iochannel_creds_supported(io
))
4216 pa_iochannel_creds_enable(io
);
4219 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_CONNECTION_PUT
], c
);
4222 void pa_native_protocol_disconnect(pa_native_protocol
*p
, pa_module
*m
) {
4223 pa_native_connection
*c
;
4229 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
4230 if (c
->options
->module
== m
)
4231 native_connection_unlink(c
);
4234 static pa_native_protocol
* native_protocol_new(pa_core
*c
) {
4235 pa_native_protocol
*p
;
4240 p
= pa_xnew(pa_native_protocol
, 1);
4243 p
->connections
= pa_idxset_new(NULL
, NULL
);
4247 p
->extensions
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
4249 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4250 pa_hook_init(&p
->hooks
[h
], p
);
4252 pa_assert_se(pa_shared_set(c
, "native-protocol", p
) >= 0);
4257 pa_native_protocol
* pa_native_protocol_get(pa_core
*c
) {
4258 pa_native_protocol
*p
;
4260 if ((p
= pa_shared_get(c
, "native-protocol")))
4261 return pa_native_protocol_ref(p
);
4263 return native_protocol_new(c
);
4266 pa_native_protocol
* pa_native_protocol_ref(pa_native_protocol
*p
) {
4268 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4275 void pa_native_protocol_unref(pa_native_protocol
*p
) {
4276 pa_native_connection
*c
;
4280 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4282 if (PA_REFCNT_DEC(p
) > 0)
4285 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
4286 native_connection_unlink(c
);
4288 pa_idxset_free(p
->connections
, NULL
, NULL
);
4290 pa_strlist_free(p
->servers
);
4292 for (h
= 0; h
< PA_NATIVE_HOOK_MAX
; h
++)
4293 pa_hook_done(&p
->hooks
[h
]);
4295 pa_hashmap_free(p
->extensions
, NULL
, NULL
);
4297 pa_assert_se(pa_shared_remove(p
->core
, "native-protocol") >= 0);
4302 void pa_native_protocol_add_server_string(pa_native_protocol
*p
, const char *name
) {
4304 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4307 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
4309 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4312 void pa_native_protocol_remove_server_string(pa_native_protocol
*p
, const char *name
) {
4314 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4317 p
->servers
= pa_strlist_remove(p
->servers
, name
);
4319 pa_hook_fire(&p
->hooks
[PA_NATIVE_HOOK_SERVERS_CHANGED
], p
->servers
);
4322 pa_hook
*pa_native_protocol_hooks(pa_native_protocol
*p
) {
4324 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4329 pa_strlist
*pa_native_protocol_servers(pa_native_protocol
*p
) {
4331 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4336 int pa_native_protocol_install_ext(pa_native_protocol
*p
, pa_module
*m
, pa_native_protocol_ext_cb_t cb
) {
4338 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4341 pa_assert(!pa_hashmap_get(p
->extensions
, m
));
4343 pa_assert_se(pa_hashmap_put(p
->extensions
, m
, (void*) cb
) == 0);
4347 void pa_native_protocol_remove_ext(pa_native_protocol
*p
, pa_module
*m
) {
4349 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
4352 pa_assert_se(pa_hashmap_remove(p
->extensions
, m
));
4355 pa_native_options
* pa_native_options_new(void) {
4356 pa_native_options
*o
;
4358 o
= pa_xnew0(pa_native_options
, 1);
4364 pa_native_options
* pa_native_options_ref(pa_native_options
*o
) {
4366 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4373 void pa_native_options_unref(pa_native_options
*o
) {
4375 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4377 if (PA_REFCNT_DEC(o
) > 0)
4380 pa_xfree(o
->auth_group
);
4383 pa_ip_acl_free(o
->auth_ip_acl
);
4386 pa_auth_cookie_unref(o
->auth_cookie
);
4391 int pa_native_options_parse(pa_native_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
4396 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
4399 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
4400 pa_log("auth-anonymous= expects a boolean argument.");
4405 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &enabled
) < 0) {
4406 pa_log("auth-group-enabled= expects a boolean argument.");
4410 pa_xfree(o
->auth_group
);
4411 o
->auth_group
= enabled
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", pa_in_system_mode() ? PA_ACCESS_GROUP
: NULL
)) : NULL
;
4415 pa_log_warn("Authentication group configured, but not available on local system. Ignoring.");
4418 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
4421 if (!(ipa
= pa_ip_acl_new(acl
))) {
4422 pa_log("Failed to parse IP ACL '%s'", acl
);
4427 pa_ip_acl_free(o
->auth_ip_acl
);
4429 o
->auth_ip_acl
= ipa
;
4433 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
4434 pa_log("auth-cookie-enabled= expects a boolean argument.");
4439 pa_auth_cookie_unref(o
->auth_cookie
);
4444 /* The new name for this is 'auth-cookie', for compat reasons
4445 * we check the old name too */
4446 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
4447 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
4448 cn
= PA_NATIVE_COOKIE_FILE
;
4450 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, PA_NATIVE_COOKIE_LENGTH
)))
4454 o
->auth_cookie
= NULL
;
4459 pa_pstream
* pa_native_connection_get_pstream(pa_native_connection
*c
) {
4460 pa_native_connection_assert_ref(c
);