4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 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/autoload.h>
53 #include <pulsecore/authkey-prop.h>
54 #include <pulsecore/strlist.h>
55 #include <pulsecore/props.h>
56 #include <pulsecore/sample-util.h>
57 #include <pulsecore/llist.h>
58 #include <pulsecore/creds.h>
59 #include <pulsecore/core-util.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 */
72 struct pa_protocol_native
;
74 struct record_stream
{
75 struct connection
*connection
;
77 pa_source_output
*source_output
;
78 pa_memblockq
*memblockq
;
82 struct playback_stream
{
84 struct connection
*connection
;
86 pa_sink_input
*sink_input
;
87 pa_memblockq
*memblockq
;
88 size_t requested_bytes
;
94 /* Sync group members */
95 PA_LLIST_FIELDS(struct playback_stream
);
98 struct upload_stream
{
100 struct connection
*connection
;
102 pa_memchunk memchunk
;
105 pa_sample_spec sample_spec
;
106 pa_channel_map channel_map
;
109 struct output_stream
{
121 pa_protocol_native
*protocol
;
124 pa_pdispatch
*pdispatch
;
125 pa_idxset
*record_streams
, *output_streams
;
126 uint32_t rrobin_index
;
127 pa_subscription
*subscription
;
128 pa_time_event
*auth_timeout_event
;
131 struct pa_protocol_native
{
135 pa_socket_server
*server
;
136 pa_idxset
*connections
;
137 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
138 int auth_cookie_in_property
;
144 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
);
145 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
);
146 static void sink_input_kill_cb(pa_sink_input
*i
);
147 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
);
149 static void request_bytes(struct playback_stream
*s
);
151 static void source_output_kill_cb(pa_source_output
*o
);
152 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
153 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
155 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
156 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
157 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
158 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
159 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
160 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
161 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
162 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
163 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
164 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
165 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
166 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
167 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
168 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
169 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
170 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
171 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
172 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
173 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
174 static void command_set_volume(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
175 static void command_set_mute(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
176 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
177 static void command_flush_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
178 static void command_trigger_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
179 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
180 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
181 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
182 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
183 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
184 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
185 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
186 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
187 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
188 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
189 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
191 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
192 [PA_COMMAND_ERROR
] = NULL
,
193 [PA_COMMAND_TIMEOUT
] = NULL
,
194 [PA_COMMAND_REPLY
] = NULL
,
195 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
196 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
197 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
198 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
199 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
200 [PA_COMMAND_AUTH
] = command_auth
,
201 [PA_COMMAND_REQUEST
] = NULL
,
202 [PA_COMMAND_EXIT
] = command_exit
,
203 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
204 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
205 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
206 [PA_COMMAND_STAT
] = command_stat
,
207 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
208 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
209 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
210 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
211 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
212 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
213 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
214 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
215 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
216 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
217 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
218 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
219 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
220 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
221 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
222 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
223 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
224 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
225 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
226 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
227 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
228 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
229 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
231 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
232 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
233 [PA_COMMAND_SET_SOURCE_VOLUME
] = command_set_volume
,
235 [PA_COMMAND_SET_SINK_MUTE
] = command_set_mute
,
236 [PA_COMMAND_SET_SOURCE_MUTE
] = command_set_mute
,
238 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
239 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_flush_playback_stream
,
240 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_prebuf_playback_stream
,
241 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_prebuf_playback_stream
,
243 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
244 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
246 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
247 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
248 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
249 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
250 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
251 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
252 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
253 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
254 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
255 [PA_COMMAND_GET_AUTOLOAD_INFO
] = command_get_autoload_info
,
256 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = command_get_autoload_info_list
,
257 [PA_COMMAND_ADD_AUTOLOAD
] = command_add_autoload
,
258 [PA_COMMAND_REMOVE_AUTOLOAD
] = command_remove_autoload
261 /* structure management */
263 static struct upload_stream
* upload_stream_new(
264 struct connection
*c
,
265 const pa_sample_spec
*ss
,
266 const pa_channel_map
*map
,
267 const char *name
, size_t length
) {
269 struct upload_stream
*s
;
270 assert(c
&& ss
&& name
&& length
);
272 s
= pa_xnew(struct upload_stream
, 1);
273 s
->type
= UPLOAD_STREAM
;
275 s
->sample_spec
= *ss
;
276 s
->channel_map
= *map
;
277 s
->name
= pa_xstrdup(name
);
279 s
->memchunk
.memblock
= NULL
;
280 s
->memchunk
.index
= 0;
281 s
->memchunk
.length
= 0;
285 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
289 static void upload_stream_free(struct upload_stream
*o
) {
290 assert(o
&& o
->connection
);
292 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
296 if (o
->memchunk
.memblock
)
297 pa_memblock_unref(o
->memchunk
.memblock
);
302 static struct record_stream
* record_stream_new(
303 struct connection
*c
,
305 const pa_sample_spec
*ss
,
306 const pa_channel_map
*map
,
309 size_t fragment_size
) {
311 struct record_stream
*s
;
312 pa_source_output
*source_output
;
314 assert(c
&& source
&& ss
&& name
&& maxlength
);
316 if (!(source_output
= pa_source_output_new(source
, __FILE__
, name
, ss
, map
, -1)))
319 s
= pa_xnew(struct record_stream
, 1);
321 s
->source_output
= source_output
;
322 s
->source_output
->push
= source_output_push_cb
;
323 s
->source_output
->kill
= source_output_kill_cb
;
324 s
->source_output
->get_latency
= source_output_get_latency_cb
;
325 s
->source_output
->userdata
= s
;
326 s
->source_output
->owner
= c
->protocol
->module
;
327 s
->source_output
->client
= c
->client
;
329 s
->memblockq
= pa_memblockq_new(
333 base
= pa_frame_size(ss
),
337 c
->protocol
->core
->memblock_stat
);
338 assert(s
->memblockq
);
340 s
->fragment_size
= (fragment_size
/base
)*base
;
341 if (!s
->fragment_size
)
342 s
->fragment_size
= base
;
344 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
348 static void record_stream_free(struct record_stream
* r
) {
349 assert(r
&& r
->connection
);
351 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
352 pa_source_output_disconnect(r
->source_output
);
353 pa_source_output_unref(r
->source_output
);
354 pa_memblockq_free(r
->memblockq
);
358 static struct playback_stream
* playback_stream_new(
359 struct connection
*c
,
361 const pa_sample_spec
*ss
,
362 const pa_channel_map
*map
,
371 struct playback_stream
*s
, *ssync
;
372 pa_sink_input
*sink_input
;
373 pa_memblock
*silence
;
377 assert(c
&& sink
&& ss
&& name
&& maxlength
);
379 /* Find syncid group */
380 for (ssync
= pa_idxset_first(c
->output_streams
, &idx
); ssync
; ssync
= pa_idxset_next(c
->output_streams
, &idx
)) {
382 if (ssync
->type
!= PLAYBACK_STREAM
)
385 if (ssync
->syncid
== syncid
)
389 /* Synced streams must connect to the same sink */
390 if (ssync
&& ssync
->sink_input
->sink
!= sink
)
393 if (!(sink_input
= pa_sink_input_new(sink
, __FILE__
, name
, ss
, map
, volume
, 0, -1)))
396 s
= pa_xnew(struct playback_stream
, 1);
397 s
->type
= PLAYBACK_STREAM
;
400 s
->sink_input
= sink_input
;
403 s
->sink_input
->peek
= sink_input_peek_cb
;
404 s
->sink_input
->drop
= sink_input_drop_cb
;
405 s
->sink_input
->kill
= sink_input_kill_cb
;
406 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
407 s
->sink_input
->userdata
= s
;
408 s
->sink_input
->owner
= c
->protocol
->module
;
409 s
->sink_input
->client
= c
->client
;
412 /* Sync id found, now find head of list */
413 PA_LLIST_FIND_HEAD(struct playback_stream
, ssync
, &ssync
);
415 /* Prepend ourselves */
416 PA_LLIST_PREPEND(struct playback_stream
, ssync
, s
);
418 /* Set our start index to the current read index of the other grozp member(s) */
420 start_index
= pa_memblockq_get_read_index(ssync
->next
->memblockq
);
422 /* This ia a new sync group */
423 PA_LLIST_INIT(struct playback_stream
, s
);
427 silence
= pa_silence_memblock_new(ss
, 0, c
->protocol
->core
->memblock_stat
);
429 s
->memblockq
= pa_memblockq_new(
437 c
->protocol
->core
->memblock_stat
);
439 pa_memblock_unref(silence
);
441 s
->requested_bytes
= 0;
442 s
->drain_request
= 0;
444 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
449 static void playback_stream_free(struct playback_stream
* p
) {
450 struct playback_stream
*head
;
451 assert(p
&& p
->connection
);
453 if (p
->drain_request
)
454 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERR_NOENTITY
);
456 PA_LLIST_FIND_HEAD(struct playback_stream
, p
, &head
);
457 PA_LLIST_REMOVE(struct playback_stream
, head
, p
);
459 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
460 pa_sink_input_disconnect(p
->sink_input
);
461 pa_sink_input_unref(p
->sink_input
);
462 pa_memblockq_free(p
->memblockq
);
466 static void connection_free(struct connection
*c
) {
467 struct record_stream
*r
;
468 struct output_stream
*o
;
469 assert(c
&& c
->protocol
);
471 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
472 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
473 record_stream_free(r
);
474 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
476 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
477 if (o
->type
== PLAYBACK_STREAM
)
478 playback_stream_free((struct playback_stream
*) o
);
480 upload_stream_free((struct upload_stream
*) o
);
481 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
483 pa_pdispatch_unref(c
->pdispatch
);
484 pa_pstream_close(c
->pstream
);
485 pa_pstream_unref(c
->pstream
);
486 pa_client_free(c
->client
);
489 pa_subscription_free(c
->subscription
);
491 if (c
->auth_timeout_event
)
492 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
497 static void request_bytes(struct playback_stream
*s
) {
502 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
505 if (l
<= s
->requested_bytes
)
508 l
-= s
->requested_bytes
;
510 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
513 s
->requested_bytes
+= l
;
515 t
= pa_tagstruct_new(NULL
, 0);
517 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
518 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
519 pa_tagstruct_putu32(t
, s
->index
);
520 pa_tagstruct_putu32(t
, l
);
521 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
523 /* pa_log(__FILE__": Requesting %u bytes", l); */
526 static void send_memblock(struct connection
*c
) {
528 struct record_stream
*r
;
530 start
= PA_IDXSET_INVALID
;
534 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
537 if (start
== PA_IDXSET_INVALID
)
538 start
= c
->rrobin_index
;
539 else if (start
== c
->rrobin_index
)
542 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
543 pa_memchunk schunk
= chunk
;
545 if (schunk
.length
> r
->fragment_size
)
546 schunk
.length
= r
->fragment_size
;
548 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
549 pa_memblockq_drop(r
->memblockq
, &chunk
, schunk
.length
);
550 pa_memblock_unref(schunk
.memblock
);
557 static void send_playback_stream_killed(struct playback_stream
*p
) {
561 t
= pa_tagstruct_new(NULL
, 0);
562 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
563 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
564 pa_tagstruct_putu32(t
, p
->index
);
565 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
568 static void send_record_stream_killed(struct record_stream
*r
) {
572 t
= pa_tagstruct_new(NULL
, 0);
573 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
574 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
575 pa_tagstruct_putu32(t
, r
->index
);
576 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
579 /*** sinkinput callbacks ***/
581 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
582 struct playback_stream
*s
;
583 assert(i
&& i
->userdata
&& chunk
);
586 if (pa_memblockq_get_length(s
->memblockq
) <= 0 && !s
->underrun
) {
589 /* Report that we're empty */
591 t
= pa_tagstruct_new(NULL
, 0);
592 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
593 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
594 pa_tagstruct_putu32(t
, s
->index
);
595 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
600 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0) {
601 /* pa_log(__FILE__": peek: failure"); */
605 /* pa_log(__FILE__": peek: %u", chunk->length); */
610 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
611 struct playback_stream
*s
;
612 assert(i
&& i
->userdata
&& length
);
615 pa_memblockq_drop(s
->memblockq
, chunk
, length
);
619 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
620 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
621 s
->drain_request
= 0;
624 /* pa_log(__FILE__": after_drop: %u %u", pa_memblockq_get_length(s->memblockq), pa_memblockq_is_readable(s->memblockq)); */
627 static void sink_input_kill_cb(pa_sink_input
*i
) {
628 assert(i
&& i
->userdata
);
629 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
630 playback_stream_free((struct playback_stream
*) i
->userdata
);
633 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
634 struct playback_stream
*s
;
635 assert(i
&& i
->userdata
);
638 /*pa_log(__FILE__": get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
640 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
643 /*** source_output callbacks ***/
645 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
646 struct record_stream
*s
;
647 assert(o
&& o
->userdata
&& chunk
);
650 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
651 pa_log_warn(__FILE__
": Failed to push data into output queue.");
655 if (!pa_pstream_is_pending(s
->connection
->pstream
))
656 send_memblock(s
->connection
);
659 static void source_output_kill_cb(pa_source_output
*o
) {
660 assert(o
&& o
->userdata
);
661 send_record_stream_killed((struct record_stream
*) o
->userdata
);
662 record_stream_free((struct record_stream
*) o
->userdata
);
665 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
666 struct record_stream
*s
;
667 assert(o
&& o
->userdata
);
670 /*pa_log(__FILE__": get_latency: %u", pa_memblockq_get_length(s->memblockq));*/
672 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
675 /*** pdispatch callbacks ***/
677 static void protocol_error(struct connection
*c
) {
678 pa_log(__FILE__
": protocol error, kicking client");
682 #define CHECK_VALIDITY(pstream, expression, tag, error) do { \
683 if (!(expression)) { \
684 pa_pstream_send_error((pstream), (tag), (error)); \
689 static pa_tagstruct
*reply_new(uint32_t tag
) {
692 reply
= pa_tagstruct_new(NULL
, 0);
693 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
694 pa_tagstruct_putu32(reply
, tag
);
698 static void command_create_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
699 struct connection
*c
= userdata
;
700 struct playback_stream
*s
;
701 uint32_t maxlength
, tlength
, prebuf
, minreq
, sink_index
, syncid
;
702 const char *name
, *sink_name
;
710 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
712 if (pa_tagstruct_get(
714 PA_TAG_STRING
, &name
,
715 PA_TAG_SAMPLE_SPEC
, &ss
,
716 PA_TAG_CHANNEL_MAP
, &map
,
717 PA_TAG_U32
, &sink_index
,
718 PA_TAG_STRING
, &sink_name
,
719 PA_TAG_U32
, &maxlength
,
720 PA_TAG_BOOLEAN
, &corked
,
721 PA_TAG_U32
, &tlength
,
725 PA_TAG_CVOLUME
, &volume
,
726 PA_TAG_INVALID
) < 0 ||
727 !pa_tagstruct_eof(t
) ||
733 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
734 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
735 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
736 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
737 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
738 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
739 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
&& volume
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
740 CHECK_VALIDITY(c
->pstream
, maxlength
> 0 && maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
742 if (sink_index
!= PA_INVALID_INDEX
)
743 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
745 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
747 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
749 s
= playback_stream_new(c
, sink
, &ss
, &map
, name
, maxlength
, tlength
, prebuf
, minreq
, &volume
, syncid
);
750 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
752 pa_sink_input_cork(s
->sink_input
, corked
);
754 reply
= reply_new(tag
);
755 pa_tagstruct_putu32(reply
, s
->index
);
756 assert(s
->sink_input
);
757 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
758 pa_tagstruct_putu32(reply
, s
->requested_bytes
= pa_memblockq_missing(s
->memblockq
));
760 if (c
->version
>= 9) {
761 /* Since 0.9 we support sending the buffer metrics back to the client */
763 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
));
764 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_tlength(s
->memblockq
));
765 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_prebuf(s
->memblockq
));
766 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_minreq(s
->memblockq
));
769 pa_pstream_send_tagstruct(c
->pstream
, reply
);
773 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
774 struct connection
*c
= userdata
;
778 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
779 !pa_tagstruct_eof(t
)) {
784 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
786 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
787 struct playback_stream
*s
;
788 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
789 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
793 playback_stream_free(s
);
794 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
795 struct record_stream
*s
;
796 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
797 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
801 record_stream_free(s
);
803 struct upload_stream
*s
;
804 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
805 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
806 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
810 upload_stream_free(s
);
813 pa_pstream_send_simple_ack(c
->pstream
, tag
);
816 static void command_create_record_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
817 struct connection
*c
= userdata
;
818 struct record_stream
*s
;
819 uint32_t maxlength
, fragment_size
;
820 uint32_t source_index
;
821 const char *name
, *source_name
;
827 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
829 if (pa_tagstruct_gets(t
, &name
) < 0 ||
830 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
831 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
832 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
833 pa_tagstruct_gets(t
, &source_name
) < 0 ||
834 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
835 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
836 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
837 !pa_tagstruct_eof(t
)) {
842 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
843 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
844 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
845 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
846 CHECK_VALIDITY(c
->pstream
, source_index
!= PA_INVALID_INDEX
|| !source_name
|| (*source_name
&& pa_utf8_valid(source_name
)), tag
, PA_ERR_INVALID
);
847 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
848 CHECK_VALIDITY(c
->pstream
, maxlength
<= MAX_MEMBLOCKQ_LENGTH
, tag
, PA_ERR_INVALID
);
850 if (source_index
!= PA_INVALID_INDEX
)
851 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
853 source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1);
855 CHECK_VALIDITY(c
->pstream
, source
, tag
, PA_ERR_NOENTITY
);
857 s
= record_stream_new(c
, source
, &ss
, &map
, name
, maxlength
, fragment_size
);
858 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
860 pa_source_output_cork(s
->source_output
, corked
);
862 reply
= reply_new(tag
);
863 pa_tagstruct_putu32(reply
, s
->index
);
864 assert(s
->source_output
);
865 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
867 if (c
->version
>= 9) {
868 /* Since 0.9 we support sending the buffer metrics back to the client */
870 pa_tagstruct_putu32(reply
, (uint32_t) pa_memblockq_get_maxlength(s
->memblockq
));
871 pa_tagstruct_putu32(reply
, (uint32_t) s
->fragment_size
);
874 pa_pstream_send_tagstruct(c
->pstream
, reply
);
877 static void command_exit(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
878 struct connection
*c
= userdata
;
881 if (!pa_tagstruct_eof(t
)) {
886 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
888 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
889 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
890 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
893 static void command_auth(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
894 struct connection
*c
= userdata
;
899 if (pa_tagstruct_getu32(t
, &c
->version
) < 0 ||
900 pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
901 !pa_tagstruct_eof(t
)) {
906 /* Minimum supported version */
907 if (c
->version
< 8) {
908 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_VERSION
);
912 if (!c
->authorized
) {
916 const pa_creds
*creds
;
918 if ((creds
= pa_pdispatch_creds(pd
))) {
919 if (creds
->uid
== getuid())
921 else if (c
->protocol
->auth_group
) {
925 if ((gid
= pa_get_gid_of_group(c
->protocol
->auth_group
)) == (gid_t
) -1)
926 pa_log_warn(__FILE__
": failed to get GID of group '%s'", c
->protocol
->auth_group
);
927 else if (gid
== creds
->gid
)
931 if ((r
= pa_uid_in_group(creds
->uid
, c
->protocol
->auth_group
)) < 0)
932 pa_log_warn(__FILE__
": failed to check group membership.");
938 pa_log_info(__FILE__
": Got credentials: uid=%lu gid=%lu success=%i",
939 (unsigned long) creds
->uid
,
940 (unsigned long) creds
->gid
,
945 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) == 0)
949 pa_log_warn(__FILE__
": Denied access to client with invalid authorization data.");
950 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
955 if (c
->auth_timeout_event
) {
956 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
957 c
->auth_timeout_event
= NULL
;
961 reply
= reply_new(tag
);
962 pa_tagstruct_putu32(reply
, PA_PROTOCOL_VERSION
);
963 pa_pstream_send_tagstruct(c
->pstream
, reply
);
966 static void command_set_client_name(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
967 struct connection
*c
= userdata
;
971 if (pa_tagstruct_gets(t
, &name
) < 0 ||
972 !pa_tagstruct_eof(t
)) {
977 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
979 pa_client_set_name(c
->client
, name
);
980 pa_pstream_send_simple_ack(c
->pstream
, tag
);
983 static void command_lookup(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
984 struct connection
*c
= userdata
;
986 uint32_t idx
= PA_IDXSET_INVALID
;
989 if (pa_tagstruct_gets(t
, &name
) < 0 ||
990 !pa_tagstruct_eof(t
)) {
995 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
996 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
998 if (command
== PA_COMMAND_LOOKUP_SINK
) {
1000 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
1004 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
1005 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
1006 idx
= source
->index
;
1009 if (idx
== PA_IDXSET_INVALID
)
1010 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1012 pa_tagstruct
*reply
;
1013 reply
= reply_new(tag
);
1014 pa_tagstruct_putu32(reply
, idx
);
1015 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1019 static void command_drain_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1020 struct connection
*c
= userdata
;
1022 struct playback_stream
*s
;
1025 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1026 !pa_tagstruct_eof(t
)) {
1031 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1032 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1033 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1034 CHECK_VALIDITY(c
->pstream
, s
->type
== PLAYBACK_STREAM
, tag
, PA_ERR_NOENTITY
);
1036 s
->drain_request
= 0;
1038 pa_memblockq_prebuf_disable(s
->memblockq
);
1040 if (!pa_memblockq_is_readable(s
->memblockq
)) {
1041 /* pa_log("immediate drain: %u", pa_memblockq_get_length(s->memblockq)); */
1042 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1044 /* pa_log("slow drain triggered"); */
1045 s
->drain_request
= 1;
1048 pa_sink_notify(s
->sink_input
->sink
);
1052 static void command_stat(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1053 struct connection
*c
= userdata
;
1054 pa_tagstruct
*reply
;
1057 if (!pa_tagstruct_eof(t
)) {
1062 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1064 reply
= reply_new(tag
);
1065 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total
);
1066 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total_size
);
1067 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated
);
1068 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated_size
);
1069 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
1070 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1073 static void command_get_playback_latency(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1074 struct connection
*c
= userdata
;
1075 pa_tagstruct
*reply
;
1076 struct playback_stream
*s
;
1077 struct timeval tv
, now
;
1082 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1083 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1084 !pa_tagstruct_eof(t
)) {
1089 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1090 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1091 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1092 CHECK_VALIDITY(c
->pstream
, s
->type
== PLAYBACK_STREAM
, tag
, PA_ERR_NOENTITY
);
1094 reply
= reply_new(tag
);
1096 latency
= pa_sink_get_latency(s
->sink_input
->sink
);
1097 if (s
->sink_input
->resampled_chunk
.memblock
)
1098 latency
+= pa_bytes_to_usec(s
->sink_input
->resampled_chunk
.length
, &s
->sink_input
->sample_spec
);
1099 pa_tagstruct_put_usec(reply
, latency
);
1101 pa_tagstruct_put_usec(reply
, 0);
1102 pa_tagstruct_put_boolean(reply
, pa_memblockq_is_readable(s
->memblockq
));
1103 pa_tagstruct_put_timeval(reply
, &tv
);
1104 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
1105 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
1106 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
1107 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1110 static void command_get_record_latency(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1111 struct connection
*c
= userdata
;
1112 pa_tagstruct
*reply
;
1113 struct record_stream
*s
;
1114 struct timeval tv
, now
;
1118 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1119 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1120 !pa_tagstruct_eof(t
)) {
1125 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1126 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
1127 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1129 reply
= reply_new(tag
);
1130 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
1131 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
1132 pa_tagstruct_put_boolean(reply
, 0);
1133 pa_tagstruct_put_timeval(reply
, &tv
);
1134 pa_tagstruct_put_timeval(reply
, pa_gettimeofday(&now
));
1135 pa_tagstruct_puts64(reply
, pa_memblockq_get_write_index(s
->memblockq
));
1136 pa_tagstruct_puts64(reply
, pa_memblockq_get_read_index(s
->memblockq
));
1137 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1140 static void command_create_upload_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1141 struct connection
*c
= userdata
;
1142 struct upload_stream
*s
;
1147 pa_tagstruct
*reply
;
1148 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
1150 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1151 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1152 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1153 pa_tagstruct_getu32(t
, &length
) < 0 ||
1154 !pa_tagstruct_eof(t
)) {
1159 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1160 CHECK_VALIDITY(c
->pstream
, pa_sample_spec_valid(&ss
), tag
, PA_ERR_INVALID
);
1161 CHECK_VALIDITY(c
->pstream
, pa_channel_map_valid(&map
), tag
, PA_ERR_INVALID
);
1162 CHECK_VALIDITY(c
->pstream
, map
.channels
== ss
.channels
, tag
, PA_ERR_INVALID
);
1163 CHECK_VALIDITY(c
->pstream
, (length
% pa_frame_size(&ss
)) == 0 && length
> 0, tag
, PA_ERR_INVALID
);
1164 CHECK_VALIDITY(c
->pstream
, length
<= PA_SCACHE_ENTRY_SIZE_MAX
, tag
, PA_ERR_TOOLARGE
);
1165 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1167 s
= upload_stream_new(c
, &ss
, &map
, name
, length
);
1168 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_INVALID
);
1170 reply
= reply_new(tag
);
1171 pa_tagstruct_putu32(reply
, s
->index
);
1172 pa_tagstruct_putu32(reply
, length
);
1173 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1176 static void command_finish_upload_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1177 struct connection
*c
= userdata
;
1179 struct upload_stream
*s
;
1183 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1184 !pa_tagstruct_eof(t
)) {
1189 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1191 s
= pa_idxset_get_by_index(c
->output_streams
, channel
);
1192 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1193 CHECK_VALIDITY(c
->pstream
, s
->type
== UPLOAD_STREAM
, tag
, PA_ERR_NOENTITY
);
1195 if (pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, &idx
) < 0)
1196 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INTERNAL
);
1198 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1200 upload_stream_free(s
);
1203 static void command_play_sample(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1204 struct connection
*c
= userdata
;
1205 uint32_t sink_index
;
1208 const char *name
, *sink_name
;
1211 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1212 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1213 pa_tagstruct_getu32(t
, &volume
) < 0 ||
1214 pa_tagstruct_gets(t
, &name
) < 0 ||
1215 !pa_tagstruct_eof(t
)) {
1220 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1221 CHECK_VALIDITY(c
->pstream
, sink_index
!= PA_INVALID_INDEX
|| !sink_name
|| (*sink_name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1222 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1224 if (sink_index
!= PA_INVALID_INDEX
)
1225 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1227 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1229 CHECK_VALIDITY(c
->pstream
, sink
, tag
, PA_ERR_NOENTITY
);
1231 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
1232 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1236 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1239 static void command_remove_sample(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1240 struct connection
*c
= userdata
;
1244 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1245 !pa_tagstruct_eof(t
)) {
1250 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1251 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1253 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
1254 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1258 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1261 static void sink_fill_tagstruct(pa_tagstruct
*t
, pa_sink
*sink
) {
1265 PA_TAG_U32
, sink
->index
,
1266 PA_TAG_STRING
, sink
->name
,
1267 PA_TAG_STRING
, sink
->description
,
1268 PA_TAG_SAMPLE_SPEC
, &sink
->sample_spec
,
1269 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
1270 PA_TAG_U32
, sink
->owner
? sink
->owner
->index
: PA_INVALID_INDEX
,
1271 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, PA_MIXER_HARDWARE
),
1272 PA_TAG_BOOLEAN
, pa_sink_get_mute(sink
, PA_MIXER_HARDWARE
),
1273 PA_TAG_U32
, sink
->monitor_source
->index
,
1274 PA_TAG_STRING
, sink
->monitor_source
->name
,
1275 PA_TAG_USEC
, pa_sink_get_latency(sink
),
1276 PA_TAG_STRING
, sink
->driver
,
1278 (sink
->get_hw_volume
? PA_SINK_HW_VOLUME_CTRL
: 0) |
1279 (sink
->get_latency
? PA_SINK_LATENCY
: 0) |
1280 (sink
->is_hardware
? PA_SINK_HARDWARE
: 0),
1284 static void source_fill_tagstruct(pa_tagstruct
*t
, pa_source
*source
) {
1285 assert(t
&& source
);
1288 PA_TAG_U32
, source
->index
,
1289 PA_TAG_STRING
, source
->name
,
1290 PA_TAG_STRING
, source
->description
,
1291 PA_TAG_SAMPLE_SPEC
, &source
->sample_spec
,
1292 PA_TAG_CHANNEL_MAP
, &source
->channel_map
,
1293 PA_TAG_U32
, source
->owner
? source
->owner
->index
: PA_INVALID_INDEX
,
1294 PA_TAG_CVOLUME
, pa_source_get_volume(source
, PA_MIXER_HARDWARE
),
1295 PA_TAG_BOOLEAN
, pa_source_get_mute(source
, PA_MIXER_HARDWARE
),
1296 PA_TAG_U32
, source
->monitor_of
? source
->monitor_of
->index
: PA_INVALID_INDEX
,
1297 PA_TAG_STRING
, source
->monitor_of
? source
->monitor_of
->name
: NULL
,
1298 PA_TAG_USEC
, pa_source_get_latency(source
),
1299 PA_TAG_STRING
, source
->driver
,
1301 (source
->get_hw_volume
? PA_SOURCE_HW_VOLUME_CTRL
: 0) |
1302 (source
->get_latency
? PA_SOURCE_LATENCY
: 0) |
1303 (source
->is_hardware
? PA_SOURCE_HARDWARE
: 0),
1307 static void client_fill_tagstruct(pa_tagstruct
*t
, pa_client
*client
) {
1308 assert(t
&& client
);
1309 pa_tagstruct_putu32(t
, client
->index
);
1310 pa_tagstruct_puts(t
, client
->name
);
1311 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: PA_INVALID_INDEX
);
1312 pa_tagstruct_puts(t
, client
->driver
);
1315 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
1316 assert(t
&& module
);
1317 pa_tagstruct_putu32(t
, module
->index
);
1318 pa_tagstruct_puts(t
, module
->name
);
1319 pa_tagstruct_puts(t
, module
->argument
);
1320 pa_tagstruct_putu32(t
, module
->n_used
);
1321 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
1324 static void sink_input_fill_tagstruct(pa_tagstruct
*t
, pa_sink_input
*s
) {
1326 pa_tagstruct_putu32(t
, s
->index
);
1327 pa_tagstruct_puts(t
, s
->name
);
1328 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: PA_INVALID_INDEX
);
1329 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
1330 pa_tagstruct_putu32(t
, s
->sink
->index
);
1331 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1332 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
1333 pa_tagstruct_put_cvolume(t
, &s
->volume
);
1334 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
1335 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
1336 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
1337 pa_tagstruct_puts(t
, s
->driver
);
1340 static void source_output_fill_tagstruct(pa_tagstruct
*t
, pa_source_output
*s
) {
1342 pa_tagstruct_putu32(t
, s
->index
);
1343 pa_tagstruct_puts(t
, s
->name
);
1344 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: PA_INVALID_INDEX
);
1345 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: PA_INVALID_INDEX
);
1346 pa_tagstruct_putu32(t
, s
->source
->index
);
1347 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1348 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
1349 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
1350 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
1351 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
1352 pa_tagstruct_puts(t
, s
->driver
);
1355 static void scache_fill_tagstruct(pa_tagstruct
*t
, pa_scache_entry
*e
) {
1357 pa_tagstruct_putu32(t
, e
->index
);
1358 pa_tagstruct_puts(t
, e
->name
);
1359 pa_tagstruct_put_cvolume(t
, &e
->volume
);
1360 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1361 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1362 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
1363 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
1364 pa_tagstruct_put_boolean(t
, e
->lazy
);
1365 pa_tagstruct_puts(t
, e
->filename
);
1368 static void command_get_info(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1369 struct connection
*c
= userdata
;
1371 pa_sink
*sink
= NULL
;
1372 pa_source
*source
= NULL
;
1373 pa_client
*client
= NULL
;
1374 pa_module
*module
= NULL
;
1375 pa_sink_input
*si
= NULL
;
1376 pa_source_output
*so
= NULL
;
1377 pa_scache_entry
*sce
= NULL
;
1379 pa_tagstruct
*reply
;
1382 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1383 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1384 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1385 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1386 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1387 pa_tagstruct_gets(t
, &name
) < 0) ||
1388 !pa_tagstruct_eof(t
)) {
1393 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1394 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1396 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1397 if (idx
!= PA_INVALID_INDEX
)
1398 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
1400 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1401 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1402 if (idx
!= PA_INVALID_INDEX
)
1403 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
1405 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1406 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1407 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
1408 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1409 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
1410 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1411 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
1412 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1413 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
1415 assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
1416 if (idx
!= PA_INVALID_INDEX
)
1417 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
1419 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1422 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1423 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1427 reply
= reply_new(tag
);
1429 sink_fill_tagstruct(reply
, sink
);
1431 source_fill_tagstruct(reply
, source
);
1433 client_fill_tagstruct(reply
, client
);
1435 module_fill_tagstruct(reply
, module
);
1437 sink_input_fill_tagstruct(reply
, si
);
1439 source_output_fill_tagstruct(reply
, so
);
1441 scache_fill_tagstruct(reply
, sce
);
1442 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1445 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1446 struct connection
*c
= userdata
;
1450 pa_tagstruct
*reply
;
1453 if (!pa_tagstruct_eof(t
)) {
1458 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1460 reply
= reply_new(tag
);
1462 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1463 i
= c
->protocol
->core
->sinks
;
1464 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1465 i
= c
->protocol
->core
->sources
;
1466 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1467 i
= c
->protocol
->core
->clients
;
1468 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1469 i
= c
->protocol
->core
->modules
;
1470 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1471 i
= c
->protocol
->core
->sink_inputs
;
1472 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1473 i
= c
->protocol
->core
->source_outputs
;
1475 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1476 i
= c
->protocol
->core
->scache
;
1480 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
1481 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1482 sink_fill_tagstruct(reply
, p
);
1483 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1484 source_fill_tagstruct(reply
, p
);
1485 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1486 client_fill_tagstruct(reply
, p
);
1487 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1488 module_fill_tagstruct(reply
, p
);
1489 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1490 sink_input_fill_tagstruct(reply
, p
);
1491 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1492 source_output_fill_tagstruct(reply
, p
);
1494 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1495 scache_fill_tagstruct(reply
, p
);
1500 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1503 static void command_get_server_info(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1504 struct connection
*c
= userdata
;
1505 pa_tagstruct
*reply
;
1510 if (!pa_tagstruct_eof(t
)) {
1515 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1517 reply
= reply_new(tag
);
1518 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1519 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1520 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1521 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
1522 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1524 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
1525 pa_tagstruct_puts(reply
, n
);
1526 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
1527 pa_tagstruct_puts(reply
, n
);
1529 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
1531 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1534 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
1536 struct connection
*c
= userdata
;
1539 t
= pa_tagstruct_new(NULL
, 0);
1540 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
1541 pa_tagstruct_putu32(t
, (uint32_t) -1);
1542 pa_tagstruct_putu32(t
, e
);
1543 pa_tagstruct_putu32(t
, idx
);
1544 pa_pstream_send_tagstruct(c
->pstream
, t
);
1547 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1548 struct connection
*c
= userdata
;
1549 pa_subscription_mask_t m
;
1552 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
1553 !pa_tagstruct_eof(t
)) {
1558 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1559 CHECK_VALIDITY(c
->pstream
, (m
& ~PA_SUBSCRIPTION_MASK_ALL
) == 0, tag
, PA_ERR_INVALID
);
1561 if (c
->subscription
)
1562 pa_subscription_free(c
->subscription
);
1565 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
1566 assert(c
->subscription
);
1568 c
->subscription
= NULL
;
1570 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1573 static void command_set_volume(
1574 PA_GCC_UNUSED pa_pdispatch
*pd
,
1580 struct connection
*c
= userdata
;
1583 pa_sink
*sink
= NULL
;
1584 pa_source
*source
= NULL
;
1585 pa_sink_input
*si
= NULL
;
1586 const char *name
= NULL
;
1589 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1590 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1591 (command
== PA_COMMAND_SET_SOURCE_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1592 pa_tagstruct_get_cvolume(t
, &volume
) ||
1593 !pa_tagstruct_eof(t
)) {
1598 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1599 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1600 CHECK_VALIDITY(c
->pstream
, pa_cvolume_valid(&volume
), tag
, PA_ERR_INVALID
);
1602 if (command
== PA_COMMAND_SET_SINK_VOLUME
) {
1603 if (idx
!= PA_INVALID_INDEX
)
1604 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
1606 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1607 } else if (command
== PA_COMMAND_SET_SOURCE_VOLUME
) {
1608 if (idx
!= (uint32_t) -1)
1609 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
1611 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1613 assert(command
== PA_COMMAND_SET_SINK_INPUT_VOLUME
);
1614 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
1617 CHECK_VALIDITY(c
->pstream
, si
|| sink
|| source
, tag
, PA_ERR_NOENTITY
);
1620 pa_sink_set_volume(sink
, PA_MIXER_HARDWARE
, &volume
);
1622 pa_source_set_volume(source
, PA_MIXER_HARDWARE
, &volume
);
1624 pa_sink_input_set_volume(si
, &volume
);
1626 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1629 static void command_set_mute(
1630 PA_GCC_UNUSED pa_pdispatch
*pd
,
1636 struct connection
*c
= userdata
;
1639 pa_sink
*sink
= NULL
;
1640 pa_source
*source
= NULL
;
1641 const char *name
= NULL
;
1644 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1645 pa_tagstruct_gets(t
, &name
) < 0 ||
1646 pa_tagstruct_get_boolean(t
, &mute
) ||
1647 !pa_tagstruct_eof(t
)) {
1652 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1653 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
|| !name
|| (*name
&& pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
1655 if (command
== PA_COMMAND_SET_SINK_MUTE
) {
1656 if (idx
!= PA_INVALID_INDEX
)
1657 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
1659 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1661 assert(command
== PA_COMMAND_SET_SOURCE_MUTE
);
1662 if (idx
!= (uint32_t) -1)
1663 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
1665 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1668 CHECK_VALIDITY(c
->pstream
, sink
|| source
, tag
, PA_ERR_NOENTITY
);
1671 pa_sink_set_mute(sink
, PA_MIXER_HARDWARE
, mute
);
1673 pa_source_set_mute(source
, PA_MIXER_HARDWARE
, mute
);
1675 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1678 static void command_cork_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1679 struct connection
*c
= userdata
;
1682 struct playback_stream
*s
, *ssync
;
1685 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1686 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1687 !pa_tagstruct_eof(t
)) {
1692 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1693 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1694 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1695 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1696 CHECK_VALIDITY(c
->pstream
, s
->type
== PLAYBACK_STREAM
, tag
, PA_ERR_NOENTITY
);
1698 pa_sink_input_cork(s
->sink_input
, b
);
1699 pa_memblockq_prebuf_force(s
->memblockq
);
1701 /* Do the same for all other members in the sync group */
1702 for (ssync
= s
->prev
; ssync
; ssync
= ssync
->prev
) {
1703 pa_sink_input_cork(ssync
->sink_input
, b
);
1704 pa_memblockq_prebuf_force(ssync
->memblockq
);
1707 for (ssync
= s
->next
; ssync
; ssync
= ssync
->next
) {
1708 pa_sink_input_cork(ssync
->sink_input
, b
);
1709 pa_memblockq_prebuf_force(ssync
->memblockq
);
1712 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1715 static void command_flush_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1716 struct connection
*c
= userdata
;
1718 struct playback_stream
*s
, *ssync
;
1721 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1722 !pa_tagstruct_eof(t
)) {
1727 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1728 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1729 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1730 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1731 CHECK_VALIDITY(c
->pstream
, s
->type
== PLAYBACK_STREAM
, tag
, PA_ERR_NOENTITY
);
1733 pa_memblockq_flush(s
->memblockq
);
1736 /* Do the same for all other members in the sync group */
1737 for (ssync
= s
->prev
; ssync
; ssync
= ssync
->prev
) {
1738 pa_memblockq_flush(ssync
->memblockq
);
1739 ssync
->underrun
= 0;
1742 for (ssync
= s
->next
; ssync
; ssync
= ssync
->next
) {
1743 pa_memblockq_flush(ssync
->memblockq
);
1744 ssync
->underrun
= 0;
1747 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1748 pa_sink_notify(s
->sink_input
->sink
);
1751 for (ssync
= s
->prev
; ssync
; ssync
= ssync
->prev
)
1752 request_bytes(ssync
);
1754 for (ssync
= s
->next
; ssync
; ssync
= ssync
->next
)
1755 request_bytes(ssync
);
1758 static void command_trigger_or_prebuf_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1759 struct connection
*c
= userdata
;
1761 struct playback_stream
*s
;
1764 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1765 !pa_tagstruct_eof(t
)) {
1770 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1771 CHECK_VALIDITY(c
->pstream
, idx
!= PA_INVALID_INDEX
, tag
, PA_ERR_INVALID
);
1772 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1773 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1774 CHECK_VALIDITY(c
->pstream
, s
->type
== PLAYBACK_STREAM
, tag
, PA_ERR_NOENTITY
);
1777 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
1778 pa_memblockq_prebuf_force(s
->memblockq
);
1781 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
1782 pa_memblockq_prebuf_disable(s
->memblockq
);
1789 pa_sink_notify(s
->sink_input
->sink
);
1790 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1794 static void command_cork_record_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1795 struct connection
*c
= userdata
;
1797 struct record_stream
*s
;
1801 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1802 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1803 !pa_tagstruct_eof(t
)) {
1808 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1809 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
1810 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1812 pa_source_output_cork(s
->source_output
, b
);
1813 pa_memblockq_prebuf_force(s
->memblockq
);
1814 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1817 static void command_flush_record_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1818 struct connection
*c
= userdata
;
1820 struct record_stream
*s
;
1823 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1824 !pa_tagstruct_eof(t
)) {
1829 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1830 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
1831 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1833 pa_memblockq_flush(s
->memblockq
);
1834 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1837 static void command_set_default_sink_or_source(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1838 struct connection
*c
= userdata
;
1842 if (pa_tagstruct_gets(t
, &s
) < 0 ||
1843 !pa_tagstruct_eof(t
)) {
1848 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1849 CHECK_VALIDITY(c
->pstream
, !s
|| (*s
&& pa_utf8_valid(s
)), tag
, PA_ERR_INVALID
);
1851 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
1852 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1855 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1856 struct connection
*c
= userdata
;
1861 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1862 pa_tagstruct_gets(t
, &name
) < 0 ||
1863 !pa_tagstruct_eof(t
)) {
1868 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1869 CHECK_VALIDITY(c
->pstream
, name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
1871 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
1872 struct playback_stream
*s
;
1874 s
= pa_idxset_get_by_index(c
->output_streams
, idx
);
1875 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1876 CHECK_VALIDITY(c
->pstream
, s
->type
== PLAYBACK_STREAM
, tag
, PA_ERR_NOENTITY
);
1878 pa_sink_input_set_name(s
->sink_input
, name
);
1881 struct record_stream
*s
;
1883 s
= pa_idxset_get_by_index(c
->record_streams
, idx
);
1884 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1886 pa_source_output_set_name(s
->source_output
, name
);
1889 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1892 static void command_kill(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1893 struct connection
*c
= userdata
;
1897 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1898 !pa_tagstruct_eof(t
)) {
1903 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1905 if (command
== PA_COMMAND_KILL_CLIENT
) {
1908 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
1909 CHECK_VALIDITY(c
->pstream
, client
, tag
, PA_ERR_NOENTITY
);
1910 pa_client_kill(client
);
1912 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
1915 s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
1916 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1918 pa_sink_input_kill(s
);
1920 pa_source_output
*s
;
1922 assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
1924 s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
1925 CHECK_VALIDITY(c
->pstream
, s
, tag
, PA_ERR_NOENTITY
);
1927 pa_source_output_kill(s
);
1930 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1933 static void command_load_module(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1934 struct connection
*c
= userdata
;
1936 const char *name
, *argument
;
1937 pa_tagstruct
*reply
;
1940 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1941 pa_tagstruct_gets(t
, &argument
) < 0 ||
1942 !pa_tagstruct_eof(t
)) {
1947 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1948 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
) && !strchr(name
, '/'), tag
, PA_ERR_INVALID
);
1949 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
1951 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
1952 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
1956 reply
= reply_new(tag
);
1957 pa_tagstruct_putu32(reply
, m
->index
);
1958 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1961 static void command_unload_module(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1962 struct connection
*c
= userdata
;
1967 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1968 !pa_tagstruct_eof(t
)) {
1973 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1974 m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
1975 CHECK_VALIDITY(c
->pstream
, m
, tag
, PA_ERR_NOENTITY
);
1977 pa_module_unload_request(m
);
1978 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1981 static void command_add_autoload(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1982 struct connection
*c
= userdata
;
1983 const char *name
, *module
, *argument
;
1986 pa_tagstruct
*reply
;
1989 if (pa_tagstruct_gets(t
, &name
) < 0 ||
1990 pa_tagstruct_getu32(t
, &type
) < 0 ||
1991 pa_tagstruct_gets(t
, &module
) < 0 ||
1992 pa_tagstruct_gets(t
, &argument
) < 0 ||
1993 !pa_tagstruct_eof(t
)) {
1998 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
1999 CHECK_VALIDITY(c
->pstream
, name
&& *name
&& pa_utf8_valid(name
), tag
, PA_ERR_INVALID
);
2000 CHECK_VALIDITY(c
->pstream
, type
== 0 || type
== 1, tag
, PA_ERR_INVALID
);
2001 CHECK_VALIDITY(c
->pstream
, module
&& *module
&& pa_utf8_valid(module
), tag
, PA_ERR_INVALID
);
2002 CHECK_VALIDITY(c
->pstream
, !argument
|| pa_utf8_valid(argument
), tag
, PA_ERR_INVALID
);
2004 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
2005 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
2009 reply
= reply_new(tag
);
2010 pa_tagstruct_putu32(reply
, idx
);
2011 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2014 static void command_remove_autoload(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2015 struct connection
*c
= userdata
;
2016 const char *name
= NULL
;
2017 uint32_t type
, idx
= PA_IDXSET_INVALID
;
2021 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
2022 (pa_tagstruct_gets(t
, &name
) < 0 ||
2023 pa_tagstruct_getu32(t
, &type
) < 0)) ||
2024 !pa_tagstruct_eof(t
)) {
2029 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2030 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
2031 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& pa_utf8_valid(name
) && (type
== 0 || type
== 1)), tag
, PA_ERR_INVALID
);
2034 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
2036 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
2038 CHECK_VALIDITY(c
->pstream
, r
>= 0, tag
, PA_ERR_NOENTITY
);
2040 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2043 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
2046 pa_tagstruct_putu32(t
, e
->index
);
2047 pa_tagstruct_puts(t
, e
->name
);
2048 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
2049 pa_tagstruct_puts(t
, e
->module
);
2050 pa_tagstruct_puts(t
, e
->argument
);
2053 static void command_get_autoload_info(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2054 struct connection
*c
= userdata
;
2055 const pa_autoload_entry
*a
= NULL
;
2058 pa_tagstruct
*reply
;
2061 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
2062 (pa_tagstruct_gets(t
, &name
) < 0 ||
2063 pa_tagstruct_getu32(t
, &type
) < 0)) ||
2064 !pa_tagstruct_eof(t
)) {
2069 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2070 CHECK_VALIDITY(c
->pstream
, name
|| idx
!= PA_IDXSET_INVALID
, tag
, PA_ERR_INVALID
);
2071 CHECK_VALIDITY(c
->pstream
, !name
|| (*name
&& (type
== 0 || type
== 1) && pa_utf8_valid(name
)), tag
, PA_ERR_INVALID
);
2074 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
2076 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
2078 CHECK_VALIDITY(c
->pstream
, a
, tag
, PA_ERR_NOENTITY
);
2080 reply
= reply_new(tag
);
2081 autoload_fill_tagstruct(reply
, a
);
2082 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2085 static void command_get_autoload_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2086 struct connection
*c
= userdata
;
2087 pa_tagstruct
*reply
;
2090 if (!pa_tagstruct_eof(t
)) {
2095 CHECK_VALIDITY(c
->pstream
, c
->authorized
, tag
, PA_ERR_ACCESS
);
2097 reply
= reply_new(tag
);
2099 if (c
->protocol
->core
->autoload_hashmap
) {
2100 pa_autoload_entry
*a
;
2103 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
2104 autoload_fill_tagstruct(reply
, a
);
2107 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2110 /*** pstream callbacks ***/
2112 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
, void *userdata
) {
2113 struct connection
*c
= userdata
;
2114 assert(p
&& packet
&& packet
->data
&& c
);
2116 if (pa_pdispatch_run(c
->pdispatch
, packet
, creds
, c
) < 0) {
2117 pa_log(__FILE__
": invalid packet.");
2122 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
) {
2123 struct connection
*c
= userdata
;
2124 struct output_stream
*stream
;
2125 assert(p
&& chunk
&& userdata
);
2127 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
2128 pa_log(__FILE__
": client sent block for invalid stream.");
2133 if (stream
->type
== PLAYBACK_STREAM
) {
2134 struct playback_stream
*ps
= (struct playback_stream
*) stream
;
2135 if (chunk
->length
>= ps
->requested_bytes
)
2136 ps
->requested_bytes
= 0;
2138 ps
->requested_bytes
-= chunk
->length
;
2140 pa_memblockq_seek(ps
->memblockq
, offset
, seek
);
2142 if (pa_memblockq_push_align(ps
->memblockq
, chunk
) < 0) {
2145 pa_log_warn(__FILE__
": failed to push data into queue");
2147 /* Pushing this block into the queue failed, so we simulate
2148 * it by skipping ahead */
2150 pa_memblockq_seek(ps
->memblockq
, chunk
->length
, PA_SEEK_RELATIVE
);
2152 /* Notify the user */
2153 t
= pa_tagstruct_new(NULL
, 0);
2154 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
2155 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
2156 pa_tagstruct_putu32(t
, ps
->index
);
2157 pa_pstream_send_tagstruct(p
, t
);
2162 pa_sink_notify(ps
->sink_input
->sink
);
2165 struct upload_stream
*u
= (struct upload_stream
*) stream
;
2167 assert(u
->type
== UPLOAD_STREAM
);
2169 if (!u
->memchunk
.memblock
) {
2170 if (u
->length
== chunk
->length
) {
2171 u
->memchunk
= *chunk
;
2172 pa_memblock_ref(u
->memchunk
.memblock
);
2175 u
->memchunk
.memblock
= pa_memblock_new(u
->length
, c
->protocol
->core
->memblock_stat
);
2176 u
->memchunk
.index
= u
->memchunk
.length
= 0;
2180 assert(u
->memchunk
.memblock
);
2183 if (l
> chunk
->length
)
2187 memcpy((uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
,
2188 (uint8_t*) chunk
->memblock
->data
+chunk
->index
, l
);
2189 u
->memchunk
.length
+= l
;
2195 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
2196 struct connection
*c
= userdata
;
2200 /* pa_log(__FILE__": connection died.");*/
2204 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
2205 struct connection
*c
= userdata
;
2211 /*** client callbacks ***/
2213 static void client_kill_cb(pa_client
*c
) {
2214 assert(c
&& c
->userdata
);
2215 connection_free(c
->userdata
);
2218 /*** socket server callbacks ***/
2220 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
2221 struct connection
*c
= userdata
;
2222 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
2228 static void on_connection(PA_GCC_UNUSED pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
2229 pa_protocol_native
*p
= userdata
;
2230 struct connection
*c
;
2231 char cname
[256], pname
[128];
2234 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
2235 pa_log_warn(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
2236 pa_iochannel_free(io
);
2240 c
= pa_xmalloc(sizeof(struct connection
));
2242 c
->authorized
=!! p
->public;
2244 if (!c
->authorized
) {
2246 pa_gettimeofday(&tv
);
2247 tv
.tv_sec
+= AUTH_TIMEOUT
;
2248 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
2250 c
->auth_timeout_event
= NULL
;
2254 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
2255 snprintf(cname
, sizeof(cname
), "Native client (%s)", pname
);
2257 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
2259 c
->client
->kill
= client_kill_cb
;
2260 c
->client
->userdata
= c
;
2261 c
->client
->owner
= p
->module
;
2263 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->memblock_stat
);
2266 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
2267 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
2268 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
2269 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
2271 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
2272 assert(c
->pdispatch
);
2274 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
2275 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
2276 assert(c
->record_streams
&& c
->output_streams
);
2278 c
->rrobin_index
= PA_IDXSET_INVALID
;
2279 c
->subscription
= NULL
;
2281 pa_idxset_put(p
->connections
, c
, NULL
);
2285 if (pa_iochannel_creds_supported(io
))
2286 pa_iochannel_creds_enable(io
);
2291 /*** module entry points ***/
2293 static int load_key(pa_protocol_native
*p
, const char*fn
) {
2296 p
->auth_cookie_in_property
= 0;
2298 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
2299 pa_log_info(__FILE__
": using already loaded auth cookie.");
2300 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2301 p
->auth_cookie_in_property
= 1;
2306 fn
= PA_NATIVE_COOKIE_FILE
;
2308 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
2311 pa_log_info(__FILE__
": loading cookie from disk.");
2313 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
2314 p
->auth_cookie_in_property
= 1;
2319 static pa_protocol_native
* protocol_new_internal(pa_core
*c
, pa_module
*m
, pa_modargs
*ma
) {
2320 pa_protocol_native
*p
;
2324 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
2325 pa_log(__FILE__
": auth-anonymous= expects a boolean argument.");
2329 p
= pa_xnew(pa_protocol_native
, 1);
2338 if (pa_modargs_get_value_boolean(ma
, "auth-group-enabled", &a
) < 0) {
2339 pa_log(__FILE__
": auth-group-enabled= expects a boolean argument.");
2342 p
->auth_group
= a
? pa_xstrdup(pa_modargs_get_value(ma
, "auth-group", c
->is_system_instance
? PA_ACCESS_GROUP
: NULL
)) : NULL
;
2345 pa_log_info(__FILE__
": Allowing access to group '%s'.", p
->auth_group
);
2349 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0) {
2354 p
->connections
= pa_idxset_new(NULL
, NULL
);
2355 assert(p
->connections
);
2360 pa_protocol_native
* pa_protocol_native_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
2362 pa_protocol_native
*p
;
2364 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2368 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
2370 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2372 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2373 l
= pa_strlist_prepend(l
, t
);
2374 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2380 void pa_protocol_native_free(pa_protocol_native
*p
) {
2381 struct connection
*c
;
2384 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
2386 pa_idxset_free(p
->connections
, NULL
, NULL
);
2391 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2393 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2394 l
= pa_strlist_remove(l
, t
);
2397 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2399 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2402 pa_socket_server_unref(p
->server
);
2405 if (p
->auth_cookie_in_property
)
2406 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2409 pa_xfree(p
->auth_group
);
2414 pa_protocol_native
* pa_protocol_native_new_iochannel(pa_core
*core
, pa_iochannel
*io
, pa_module
*m
, pa_modargs
*ma
) {
2415 pa_protocol_native
*p
;
2417 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2420 on_connection(NULL
, io
, p
);