4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <polypcore/native-common.h>
32 #include <polypcore/packet.h>
33 #include <polypcore/client.h>
34 #include <polypcore/source-output.h>
35 #include <polypcore/sink-input.h>
36 #include <polypcore/pstream.h>
37 #include <polypcore/tagstruct.h>
38 #include <polypcore/pdispatch.h>
39 #include <polypcore/pstream-util.h>
40 #include <polypcore/authkey.h>
41 #include <polypcore/namereg.h>
42 #include <polypcore/core-scache.h>
43 #include <polypcore/xmalloc.h>
44 #include <polypcore/util.h>
45 #include <polypcore/core-subscribe.h>
46 #include <polypcore/log.h>
47 #include <polypcore/autoload.h>
48 #include <polypcore/authkey-prop.h>
49 #include <polypcore/strlist.h>
50 #include <polypcore/props.h>
51 #include <polypcore/sample-util.h>
52 #include <polypcore/llist.h>
54 #include "protocol-native.h"
56 /* Kick a client if it doesn't authenticate within this time */
57 #define AUTH_TIMEOUT 5
59 /* Don't accept more connection than this */
60 #define MAX_CONNECTIONS 10
63 struct pa_protocol_native
;
65 struct record_stream
{
66 struct connection
*connection
;
68 pa_source_output
*source_output
;
69 pa_memblockq
*memblockq
;
73 struct playback_stream
{
75 struct connection
*connection
;
77 pa_sink_input
*sink_input
;
78 pa_memblockq
*memblockq
;
79 size_t requested_bytes
;
85 /* Sync group members */
86 PA_LLIST_FIELDS(struct playback_stream
);
89 struct upload_stream
{
91 struct connection
*connection
;
96 pa_sample_spec sample_spec
;
97 pa_channel_map channel_map
;
100 struct output_stream
{
111 pa_protocol_native
*protocol
;
114 pa_pdispatch
*pdispatch
;
115 pa_idxset
*record_streams
, *output_streams
;
116 uint32_t rrobin_index
;
117 pa_subscription
*subscription
;
118 pa_time_event
*auth_timeout_event
;
121 struct pa_protocol_native
{
125 pa_socket_server
*server
;
126 pa_idxset
*connections
;
127 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
128 int auth_cookie_in_property
;
131 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
);
132 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
);
133 static void sink_input_kill_cb(pa_sink_input
*i
);
134 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
);
136 static void request_bytes(struct playback_stream
*s
);
138 static void source_output_kill_cb(pa_source_output
*o
);
139 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
140 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
142 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
143 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
144 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
145 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
146 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
147 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
148 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
149 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
150 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
151 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
152 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
153 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
154 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
155 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
156 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
157 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
158 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
159 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
160 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
161 static void command_set_volume(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
162 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
163 static void command_flush_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
164 static void command_trigger_or_prebuf_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
165 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
166 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
167 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
168 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
169 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
170 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
171 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
172 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
173 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
174 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
175 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
177 static const pa_pdispatch_cb_t command_table
[PA_COMMAND_MAX
] = {
178 [PA_COMMAND_ERROR
] = NULL
,
179 [PA_COMMAND_TIMEOUT
] = NULL
,
180 [PA_COMMAND_REPLY
] = NULL
,
181 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
182 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
183 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
184 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
185 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
186 [PA_COMMAND_AUTH
] = command_auth
,
187 [PA_COMMAND_REQUEST
] = NULL
,
188 [PA_COMMAND_EXIT
] = command_exit
,
189 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
190 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
191 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
192 [PA_COMMAND_STAT
] = command_stat
,
193 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
194 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
195 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
196 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
197 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
198 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
199 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
200 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
201 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
202 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
203 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
204 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
205 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
206 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
207 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
208 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
209 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
210 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
211 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
212 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
213 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
214 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
215 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
217 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
218 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
220 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
221 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_flush_playback_stream
,
222 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_trigger_or_prebuf_playback_stream
,
223 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_trigger_or_prebuf_playback_stream
,
225 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
226 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
228 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
229 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
230 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
231 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
232 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
233 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
234 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
235 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
236 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
237 [PA_COMMAND_GET_AUTOLOAD_INFO
] = command_get_autoload_info
,
238 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = command_get_autoload_info_list
,
239 [PA_COMMAND_ADD_AUTOLOAD
] = command_add_autoload
,
240 [PA_COMMAND_REMOVE_AUTOLOAD
] = command_remove_autoload
,
244 /* structure management */
246 static struct upload_stream
* upload_stream_new(
247 struct connection
*c
,
248 const pa_sample_spec
*ss
,
249 const pa_channel_map
*map
,
250 const char *name
, size_t length
) {
252 struct upload_stream
*s
;
253 assert(c
&& ss
&& name
&& length
);
255 s
= pa_xnew(struct upload_stream
, 1);
256 s
->type
= UPLOAD_STREAM
;
258 s
->sample_spec
= *ss
;
259 s
->channel_map
= *map
;
260 s
->name
= pa_xstrdup(name
);
262 s
->memchunk
.memblock
= NULL
;
263 s
->memchunk
.index
= 0;
264 s
->memchunk
.length
= 0;
268 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
272 static void upload_stream_free(struct upload_stream
*o
) {
273 assert(o
&& o
->connection
);
275 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
279 if (o
->memchunk
.memblock
)
280 pa_memblock_unref(o
->memchunk
.memblock
);
285 static struct record_stream
* record_stream_new(
286 struct connection
*c
,
288 const pa_sample_spec
*ss
,
289 const pa_channel_map
*map
,
292 size_t fragment_size
) {
294 struct record_stream
*s
;
295 pa_source_output
*source_output
;
297 assert(c
&& source
&& ss
&& name
&& maxlength
);
299 if (!(source_output
= pa_source_output_new(source
, __FILE__
, name
, ss
, map
, -1)))
302 s
= pa_xnew(struct record_stream
, 1);
304 s
->source_output
= source_output
;
305 s
->source_output
->push
= source_output_push_cb
;
306 s
->source_output
->kill
= source_output_kill_cb
;
307 s
->source_output
->get_latency
= source_output_get_latency_cb
;
308 s
->source_output
->userdata
= s
;
309 s
->source_output
->owner
= c
->protocol
->module
;
310 s
->source_output
->client
= c
->client
;
312 s
->memblockq
= pa_memblockq_new(
316 base
= pa_frame_size(ss
),
320 c
->protocol
->core
->memblock_stat
);
321 assert(s
->memblockq
);
323 s
->fragment_size
= (fragment_size
/base
)*base
;
324 if (!s
->fragment_size
)
325 s
->fragment_size
= base
;
327 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
331 static void record_stream_free(struct record_stream
* r
) {
332 assert(r
&& r
->connection
);
334 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
335 pa_source_output_disconnect(r
->source_output
);
336 pa_source_output_unref(r
->source_output
);
337 pa_memblockq_free(r
->memblockq
);
341 static struct playback_stream
* playback_stream_new(
342 struct connection
*c
,
344 const pa_sample_spec
*ss
,
345 const pa_channel_map
*map
,
354 struct playback_stream
*s
, *sync
;
355 pa_sink_input
*sink_input
;
356 pa_memblock
*silence
;
360 assert(c
&& sink
&& ss
&& name
&& maxlength
);
362 /* Find syncid group */
363 for (sync
= pa_idxset_first(c
->output_streams
, &idx
); sync
; sync
= pa_idxset_next(c
->output_streams
, &idx
)) {
365 if (sync
->type
!= PLAYBACK_STREAM
)
368 if (sync
->syncid
== syncid
)
372 /* Synced streams must connect to the same sink */
373 if (sync
&& sync
->sink_input
->sink
!= sink
)
376 if (!(sink_input
= pa_sink_input_new(sink
, __FILE__
, name
, ss
, map
, 0, -1)))
379 s
= pa_xnew(struct playback_stream
, 1);
380 s
->type
= PLAYBACK_STREAM
;
383 s
->sink_input
= sink_input
;
386 s
->sink_input
->peek
= sink_input_peek_cb
;
387 s
->sink_input
->drop
= sink_input_drop_cb
;
388 s
->sink_input
->kill
= sink_input_kill_cb
;
389 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
390 s
->sink_input
->userdata
= s
;
391 s
->sink_input
->owner
= c
->protocol
->module
;
392 s
->sink_input
->client
= c
->client
;
395 /* Sync id found, now find head of list */
396 PA_LLIST_FIND_HEAD(struct playback_stream
, sync
, &sync
);
398 /* Prepend ourselves */
399 PA_LLIST_PREPEND(struct playback_stream
, sync
, s
);
401 /* Set our start index to the current read index of the other grozp member(s) */
403 start_index
= pa_memblockq_get_read_index(sync
->next
->memblockq
);
405 /* This ia a new sync group */
406 PA_LLIST_INIT(struct playback_stream
, s
);
410 silence
= pa_silence_memblock_new(ss
, 0, c
->protocol
->core
->memblock_stat
);
412 s
->memblockq
= pa_memblockq_new(
420 c
->protocol
->core
->memblock_stat
);
422 pa_memblock_unref(silence
);
424 s
->requested_bytes
= 0;
425 s
->drain_request
= 0;
427 s
->sink_input
->volume
= *volume
;
429 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
434 static void playback_stream_free(struct playback_stream
* p
) {
435 struct playback_stream
*head
;
436 assert(p
&& p
->connection
);
438 if (p
->drain_request
)
439 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERR_NOENTITY
);
441 PA_LLIST_FIND_HEAD(struct playback_stream
, p
, &head
);
442 PA_LLIST_REMOVE(struct playback_stream
, head
, p
);
444 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
445 pa_sink_input_disconnect(p
->sink_input
);
446 pa_sink_input_unref(p
->sink_input
);
447 pa_memblockq_free(p
->memblockq
);
451 static void connection_free(struct connection
*c
) {
452 struct record_stream
*r
;
453 struct output_stream
*o
;
454 assert(c
&& c
->protocol
);
456 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
457 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
458 record_stream_free(r
);
459 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
461 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
462 if (o
->type
== PLAYBACK_STREAM
)
463 playback_stream_free((struct playback_stream
*) o
);
465 upload_stream_free((struct upload_stream
*) o
);
466 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
468 pa_pdispatch_unref(c
->pdispatch
);
469 pa_pstream_close(c
->pstream
);
470 pa_pstream_unref(c
->pstream
);
471 pa_client_free(c
->client
);
474 pa_subscription_free(c
->subscription
);
476 if (c
->auth_timeout_event
)
477 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
482 static void request_bytes(struct playback_stream
*s
) {
487 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
490 if (l
<= s
->requested_bytes
)
493 l
-= s
->requested_bytes
;
495 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
498 s
->requested_bytes
+= l
;
500 t
= pa_tagstruct_new(NULL
, 0);
502 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
503 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
504 pa_tagstruct_putu32(t
, s
->index
);
505 pa_tagstruct_putu32(t
, l
);
506 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
508 /* pa_log(__FILE__": Requesting %u bytes\n", l); */
511 static void send_memblock(struct connection
*c
) {
513 struct record_stream
*r
;
515 start
= PA_IDXSET_INVALID
;
519 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
522 if (start
== PA_IDXSET_INVALID
)
523 start
= c
->rrobin_index
;
524 else if (start
== c
->rrobin_index
)
527 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
528 pa_memchunk schunk
= chunk
;
530 if (schunk
.length
> r
->fragment_size
)
531 schunk
.length
= r
->fragment_size
;
533 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, PA_SEEK_RELATIVE
, &schunk
);
534 pa_memblockq_drop(r
->memblockq
, &chunk
, schunk
.length
);
535 pa_memblock_unref(schunk
.memblock
);
542 static void send_playback_stream_killed(struct playback_stream
*p
) {
546 t
= pa_tagstruct_new(NULL
, 0);
548 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
549 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
550 pa_tagstruct_putu32(t
, p
->index
);
551 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
554 static void send_record_stream_killed(struct record_stream
*r
) {
558 t
= pa_tagstruct_new(NULL
, 0);
560 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
561 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
562 pa_tagstruct_putu32(t
, r
->index
);
563 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
566 /*** sinkinput callbacks ***/
568 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
569 struct playback_stream
*s
;
570 assert(i
&& i
->userdata
&& chunk
);
573 if (pa_memblockq_get_length(s
->memblockq
) <= 0 && !s
->underrun
) {
576 /* Report that we're empty */
578 t
= pa_tagstruct_new(NULL
, 0);
579 pa_tagstruct_putu32(t
, PA_COMMAND_UNDERFLOW
);
580 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
581 pa_tagstruct_putu32(t
, s
->index
);
582 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
587 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0) {
588 pa_log(__FILE__
": peek: failure\n");
592 /* pa_log(__FILE__": peek: %u\n", chunk->length); */
597 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
598 struct playback_stream
*s
;
599 assert(i
&& i
->userdata
&& length
);
602 pa_memblockq_drop(s
->memblockq
, chunk
, length
);
606 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
607 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
608 s
->drain_request
= 0;
611 /* pa_log(__FILE__": after_drop: %u %u\n", pa_memblockq_get_length(s->memblockq), pa_memblockq_is_readable(s->memblockq)); */
614 static void sink_input_kill_cb(pa_sink_input
*i
) {
615 assert(i
&& i
->userdata
);
616 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
617 playback_stream_free((struct playback_stream
*) i
->userdata
);
620 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
621 struct playback_stream
*s
;
622 assert(i
&& i
->userdata
);
625 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
627 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
630 /*** source_output callbacks ***/
632 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
633 struct record_stream
*s
;
634 assert(o
&& o
->userdata
&& chunk
);
637 if (pa_memblockq_push_align(s
->memblockq
, chunk
) < 0) {
638 pa_log_warn(__FILE__
": Failed to push data into output queue.\n");
642 if (!pa_pstream_is_pending(s
->connection
->pstream
))
643 send_memblock(s
->connection
);
646 static void source_output_kill_cb(pa_source_output
*o
) {
647 assert(o
&& o
->userdata
);
648 send_record_stream_killed((struct record_stream
*) o
->userdata
);
649 record_stream_free((struct record_stream
*) o
->userdata
);
652 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
653 struct record_stream
*s
;
654 assert(o
&& o
->userdata
);
657 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
659 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
662 /*** pdispatch callbacks ***/
664 static void protocol_error(struct connection
*c
) {
665 pa_log(__FILE__
": protocol error, kicking client\n");
669 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
) {
670 struct connection
*c
= userdata
;
671 struct playback_stream
*s
;
672 size_t maxlength
, tlength
, prebuf
, minreq
;
673 uint32_t sink_index
, syncid
;
674 const char *name
, *sink_name
;
682 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
684 if (pa_tagstruct_get(
686 PA_TAG_STRING
, &name
,
687 PA_TAG_SAMPLE_SPEC
, &ss
,
688 PA_TAG_CHANNEL_MAP
, &map
,
689 PA_TAG_U32
, &sink_index
,
690 PA_TAG_STRING
, &sink_name
,
691 PA_TAG_U32
, &maxlength
,
692 PA_TAG_BOOLEAN
, &corked
,
693 PA_TAG_U32
, &tlength
,
697 PA_TAG_CVOLUME
, &volume
,
698 PA_TAG_INVALID
) < 0 ||
699 !pa_tagstruct_eof(t
) ||
705 if (!c
->authorized
) {
706 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
710 if (sink_index
!= PA_INVALID_INDEX
)
711 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
713 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
716 pa_log_warn(__FILE__
": Can't find a suitable sink.\n");
717 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
721 if (!(s
= playback_stream_new(c
, sink
, &ss
, &map
, name
, maxlength
, tlength
, prebuf
, minreq
, &volume
, syncid
))) {
722 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
726 pa_sink_input_cork(s
->sink_input
, corked
);
728 reply
= pa_tagstruct_new(NULL
, 0);
730 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
731 pa_tagstruct_putu32(reply
, tag
);
732 pa_tagstruct_putu32(reply
, s
->index
);
733 assert(s
->sink_input
);
734 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
735 pa_tagstruct_putu32(reply
, s
->requested_bytes
= pa_memblockq_missing(s
->memblockq
));
736 pa_pstream_send_tagstruct(c
->pstream
, reply
);
740 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
741 struct connection
*c
= userdata
;
745 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
746 !pa_tagstruct_eof(t
)) {
751 if (!c
->authorized
) {
752 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
756 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
757 struct playback_stream
*s
;
758 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
759 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
763 playback_stream_free(s
);
764 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
765 struct record_stream
*s
;
766 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
767 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
771 record_stream_free(s
);
773 struct upload_stream
*s
;
774 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
775 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
776 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
780 upload_stream_free(s
);
783 pa_pstream_send_simple_ack(c
->pstream
, tag
);
786 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
) {
787 struct connection
*c
= userdata
;
788 struct record_stream
*s
;
789 size_t maxlength
, fragment_size
;
790 uint32_t source_index
;
791 const char *name
, *source_name
;
797 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
799 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
800 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
801 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
802 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
803 pa_tagstruct_gets(t
, &source_name
) < 0 ||
804 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
805 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
806 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
807 !pa_tagstruct_eof(t
)) {
812 if (!c
->authorized
) {
813 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
817 if (source_index
!= (uint32_t) -1)
818 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
820 source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1);
823 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
827 if (!(s
= record_stream_new(c
, source
, &ss
, &map
, name
, maxlength
, fragment_size
))) {
828 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
832 pa_source_output_cork(s
->source_output
, corked
);
834 reply
= pa_tagstruct_new(NULL
, 0);
836 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
837 pa_tagstruct_putu32(reply
, tag
);
838 pa_tagstruct_putu32(reply
, s
->index
);
839 assert(s
->source_output
);
840 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
841 pa_pstream_send_tagstruct(c
->pstream
, reply
);
844 static void command_exit(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
845 struct connection
*c
= userdata
;
848 if (!pa_tagstruct_eof(t
)) {
853 if (!c
->authorized
) {
854 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
858 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
859 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
860 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
864 static void command_auth(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
865 struct connection
*c
= userdata
;
869 if (pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
870 !pa_tagstruct_eof(t
)) {
875 if (!c
->authorized
) {
876 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) != 0) {
877 pa_log(__FILE__
": Denied access to client with invalid authorization key.\n");
878 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
883 if (c
->auth_timeout_event
) {
884 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
885 c
->auth_timeout_event
= NULL
;
889 pa_pstream_send_simple_ack(c
->pstream
, tag
);
893 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
) {
894 struct connection
*c
= userdata
;
898 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
899 !pa_tagstruct_eof(t
)) {
904 pa_client_set_name(c
->client
, name
);
905 pa_pstream_send_simple_ack(c
->pstream
, tag
);
909 static void command_lookup(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
910 struct connection
*c
= userdata
;
912 uint32_t idx
= PA_IDXSET_INVALID
;
915 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
916 !pa_tagstruct_eof(t
)) {
921 if (!c
->authorized
) {
922 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
926 if (command
== PA_COMMAND_LOOKUP_SINK
) {
928 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
932 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
933 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
937 if (idx
== PA_IDXSET_INVALID
)
938 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
941 reply
= pa_tagstruct_new(NULL
, 0);
943 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
944 pa_tagstruct_putu32(reply
, tag
);
945 pa_tagstruct_putu32(reply
, idx
);
946 pa_pstream_send_tagstruct(c
->pstream
, reply
);
950 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
) {
951 struct connection
*c
= userdata
;
953 struct playback_stream
*s
;
956 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
957 !pa_tagstruct_eof(t
)) {
962 if (!c
->authorized
) {
963 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
967 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
968 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
972 s
->drain_request
= 0;
974 pa_memblockq_prebuf_disable(s
->memblockq
);
976 if (!pa_memblockq_is_readable(s
->memblockq
)) {
977 /* pa_log("immediate drain: %u\n", pa_memblockq_get_length(s->memblockq)); */
978 pa_pstream_send_simple_ack(c
->pstream
, tag
);
980 /* pa_log("slow drain triggered\n"); */
981 s
->drain_request
= 1;
984 pa_sink_notify(s
->sink_input
->sink
);
988 static void command_stat(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
989 struct connection
*c
= userdata
;
993 if (!pa_tagstruct_eof(t
)) {
998 if (!c
->authorized
) {
999 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1003 reply
= pa_tagstruct_new(NULL
, 0);
1005 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1006 pa_tagstruct_putu32(reply
, tag
);
1007 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total
);
1008 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total_size
);
1009 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated
);
1010 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated_size
);
1011 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
1012 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1015 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
) {
1016 struct connection
*c
= userdata
;
1017 pa_tagstruct
*reply
;
1018 struct playback_stream
*s
;
1019 struct timeval tv
, now
;
1024 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1025 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1026 pa_tagstruct_getu64(t
, &counter
) < 0 ||
1027 !pa_tagstruct_eof(t
)) {
1032 if (!c
->authorized
) {
1033 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1037 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
1038 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1042 reply
= pa_tagstruct_new(NULL
, 0);
1044 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1045 pa_tagstruct_putu32(reply
, tag
);
1046 pa_tagstruct_put_usec(reply
, pa_sink_input_get_latency(s
->sink_input
));
1047 pa_tagstruct_put_usec(reply
, pa_sink_get_latency(s
->sink_input
->sink
));
1048 pa_tagstruct_put_usec(reply
, 0);
1049 pa_tagstruct_put_boolean(reply
, pa_memblockq_is_readable(s
->memblockq
));
1050 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
1051 pa_tagstruct_put_timeval(reply
, &tv
);
1052 pa_gettimeofday(&now
);
1053 pa_tagstruct_put_timeval(reply
, &now
);
1054 pa_tagstruct_putu64(reply
, counter
);
1055 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1058 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
) {
1059 struct connection
*c
= userdata
;
1060 pa_tagstruct
*reply
;
1061 struct record_stream
*s
;
1062 struct timeval tv
, now
;
1067 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1068 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
1069 pa_tagstruct_getu64(t
, &counter
) < 0 ||
1070 !pa_tagstruct_eof(t
)) {
1075 if (!c
->authorized
) {
1076 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1080 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, idx
))) {
1081 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1085 reply
= pa_tagstruct_new(NULL
, 0);
1087 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1088 pa_tagstruct_putu32(reply
, tag
);
1089 pa_tagstruct_put_usec(reply
, pa_source_output_get_latency(s
->source_output
));
1090 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
1091 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
1092 pa_tagstruct_put_boolean(reply
, 0);
1093 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
1094 pa_tagstruct_put_timeval(reply
, &tv
);
1095 pa_gettimeofday(&now
);
1096 pa_tagstruct_put_timeval(reply
, &now
);
1097 pa_tagstruct_putu64(reply
, counter
);
1098 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1102 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
) {
1103 struct connection
*c
= userdata
;
1104 struct upload_stream
*s
;
1109 pa_tagstruct
*reply
;
1110 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
1112 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1113 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1114 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1115 pa_tagstruct_getu32(t
, &length
) < 0 ||
1116 !pa_tagstruct_eof(t
)) {
1121 if (!c
->authorized
) {
1122 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1126 if ((length
% pa_frame_size(&ss
)) != 0 || length
<= 0 || !*name
) {
1127 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
1131 if (!(s
= upload_stream_new(c
, &ss
, &map
, name
, length
))) {
1132 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_INVALID
);
1136 reply
= pa_tagstruct_new(NULL
, 0);
1138 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1139 pa_tagstruct_putu32(reply
, tag
);
1140 pa_tagstruct_putu32(reply
, s
->index
);
1141 pa_tagstruct_putu32(reply
, length
);
1142 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1145 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
) {
1146 struct connection
*c
= userdata
;
1148 struct upload_stream
*s
;
1152 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1153 !pa_tagstruct_eof(t
)) {
1158 if (!c
->authorized
) {
1159 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1163 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
1164 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1168 pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, &idx
);
1169 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1170 upload_stream_free(s
);
1173 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
) {
1174 struct connection
*c
= userdata
;
1175 uint32_t sink_index
;
1178 const char *name
, *sink_name
;
1181 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1182 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1183 pa_tagstruct_get_cvolume(t
, &volume
) < 0 ||
1184 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1185 !pa_tagstruct_eof(t
)) {
1190 if (!c
->authorized
) {
1191 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1195 if (sink_index
!= (uint32_t) -1)
1196 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1198 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1201 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1205 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, &volume
) < 0) {
1206 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1210 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1213 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
) {
1214 struct connection
*c
= userdata
;
1218 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1219 !pa_tagstruct_eof(t
)) {
1224 if (!c
->authorized
) {
1225 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1229 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
1230 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1234 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1237 static void sink_fill_tagstruct(pa_tagstruct
*t
, pa_sink
*sink
) {
1241 PA_TAG_U32
, sink
->index
,
1242 PA_TAG_STRING
, sink
->name
,
1243 PA_TAG_STRING
, sink
->description
,
1244 PA_TAG_SAMPLE_SPEC
, &sink
->sample_spec
,
1245 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
1246 PA_TAG_U32
, sink
->owner
? sink
->owner
->index
: PA_INVALID_INDEX
,
1247 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, PA_MIXER_HARDWARE
),
1248 PA_TAG_U32
, sink
->monitor_source
->index
,
1249 PA_TAG_STRING
, sink
->monitor_source
->name
,
1250 PA_TAG_USEC
, pa_sink_get_latency(sink
),
1251 PA_TAG_STRING
, sink
->driver
,
1255 static void source_fill_tagstruct(pa_tagstruct
*t
, pa_source
*source
) {
1256 assert(t
&& source
);
1257 pa_tagstruct_putu32(t
, source
->index
);
1258 pa_tagstruct_puts(t
, source
->name
);
1259 pa_tagstruct_puts(t
, source
->description
);
1260 pa_tagstruct_put_sample_spec(t
, &source
->sample_spec
);
1261 pa_tagstruct_put_channel_map(t
, &source
->channel_map
);
1262 pa_tagstruct_putu32(t
, source
->owner
? source
->owner
->index
: (uint32_t) -1);
1263 pa_tagstruct_putu32(t
, source
->monitor_of
? source
->monitor_of
->index
: (uint32_t) -1);
1264 pa_tagstruct_puts(t
, source
->monitor_of
? source
->monitor_of
->name
: NULL
);
1265 pa_tagstruct_put_usec(t
, pa_source_get_latency(source
));
1266 pa_tagstruct_puts(t
, source
->driver
);
1269 static void client_fill_tagstruct(pa_tagstruct
*t
, pa_client
*client
) {
1270 assert(t
&& client
);
1271 pa_tagstruct_putu32(t
, client
->index
);
1272 pa_tagstruct_puts(t
, client
->name
);
1273 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: (uint32_t) -1);
1274 pa_tagstruct_puts(t
, client
->driver
);
1277 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
1278 assert(t
&& module
);
1279 pa_tagstruct_putu32(t
, module
->index
);
1280 pa_tagstruct_puts(t
, module
->name
);
1281 pa_tagstruct_puts(t
, module
->argument
);
1282 pa_tagstruct_putu32(t
, module
->n_used
);
1283 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
1286 static void sink_input_fill_tagstruct(pa_tagstruct
*t
, pa_sink_input
*s
) {
1288 pa_tagstruct_putu32(t
, s
->index
);
1289 pa_tagstruct_puts(t
, s
->name
);
1290 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1291 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1292 pa_tagstruct_putu32(t
, s
->sink
->index
);
1293 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1294 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
1295 pa_tagstruct_put_cvolume(t
, &s
->volume
);
1296 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
1297 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
1298 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
1299 pa_tagstruct_puts(t
, s
->driver
);
1302 static void source_output_fill_tagstruct(pa_tagstruct
*t
, pa_source_output
*s
) {
1304 pa_tagstruct_putu32(t
, s
->index
);
1305 pa_tagstruct_puts(t
, s
->name
);
1306 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1307 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1308 pa_tagstruct_putu32(t
, s
->source
->index
);
1309 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1310 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
1311 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
1312 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
1313 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
1314 pa_tagstruct_puts(t
, s
->driver
);
1317 static void scache_fill_tagstruct(pa_tagstruct
*t
, pa_scache_entry
*e
) {
1319 pa_tagstruct_putu32(t
, e
->index
);
1320 pa_tagstruct_puts(t
, e
->name
);
1321 pa_tagstruct_put_cvolume(t
, &e
->volume
);
1322 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1323 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1324 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
1325 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
1326 pa_tagstruct_put_boolean(t
, e
->lazy
);
1327 pa_tagstruct_puts(t
, e
->filename
);
1330 static void command_get_info(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1331 struct connection
*c
= userdata
;
1333 pa_sink
*sink
= NULL
;
1334 pa_source
*source
= NULL
;
1335 pa_client
*client
= NULL
;
1336 pa_module
*module
= NULL
;
1337 pa_sink_input
*si
= NULL
;
1338 pa_source_output
*so
= NULL
;
1339 pa_scache_entry
*sce
= NULL
;
1341 pa_tagstruct
*reply
;
1345 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1346 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1347 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1348 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1349 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1350 pa_tagstruct_gets(t
, &name
) < 0) ||
1351 !pa_tagstruct_eof(t
)) {
1356 if (!c
->authorized
) {
1357 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1361 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1362 if (idx
!= (uint32_t) -1)
1363 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
1365 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1366 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1367 if (idx
!= (uint32_t) -1)
1368 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
1370 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1371 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1372 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
1373 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1374 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
1375 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1376 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
1377 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1378 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
1380 assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
1381 if (idx
!= (uint32_t) -1)
1382 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
1384 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1387 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1388 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1392 reply
= pa_tagstruct_new(NULL
, 0);
1394 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1395 pa_tagstruct_putu32(reply
, tag
);
1397 sink_fill_tagstruct(reply
, sink
);
1399 source_fill_tagstruct(reply
, source
);
1401 client_fill_tagstruct(reply
, client
);
1403 module_fill_tagstruct(reply
, module
);
1405 sink_input_fill_tagstruct(reply
, si
);
1407 source_output_fill_tagstruct(reply
, so
);
1409 scache_fill_tagstruct(reply
, sce
);
1410 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1413 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1414 struct connection
*c
= userdata
;
1418 pa_tagstruct
*reply
;
1421 if (!pa_tagstruct_eof(t
)) {
1426 if (!c
->authorized
) {
1427 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1431 reply
= pa_tagstruct_new(NULL
, 0);
1433 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1434 pa_tagstruct_putu32(reply
, tag
);
1436 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1437 i
= c
->protocol
->core
->sinks
;
1438 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1439 i
= c
->protocol
->core
->sources
;
1440 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1441 i
= c
->protocol
->core
->clients
;
1442 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1443 i
= c
->protocol
->core
->modules
;
1444 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1445 i
= c
->protocol
->core
->sink_inputs
;
1446 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1447 i
= c
->protocol
->core
->source_outputs
;
1449 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1450 i
= c
->protocol
->core
->scache
;
1454 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
1455 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1456 sink_fill_tagstruct(reply
, p
);
1457 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1458 source_fill_tagstruct(reply
, p
);
1459 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1460 client_fill_tagstruct(reply
, p
);
1461 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1462 module_fill_tagstruct(reply
, p
);
1463 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1464 sink_input_fill_tagstruct(reply
, p
);
1465 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1466 source_output_fill_tagstruct(reply
, p
);
1468 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1469 scache_fill_tagstruct(reply
, p
);
1474 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1477 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
) {
1478 struct connection
*c
= userdata
;
1479 pa_tagstruct
*reply
;
1484 if (!pa_tagstruct_eof(t
)) {
1489 if (!c
->authorized
) {
1490 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1494 reply
= pa_tagstruct_new(NULL
, 0);
1496 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1497 pa_tagstruct_putu32(reply
, tag
);
1498 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1499 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1500 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1501 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
1502 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1504 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
1505 pa_tagstruct_puts(reply
, n
);
1506 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
1507 pa_tagstruct_puts(reply
, n
);
1509 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
1511 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1514 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
1516 struct connection
*c
= userdata
;
1519 t
= pa_tagstruct_new(NULL
, 0);
1521 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
1522 pa_tagstruct_putu32(t
, (uint32_t) -1);
1523 pa_tagstruct_putu32(t
, e
);
1524 pa_tagstruct_putu32(t
, idx
);
1525 pa_pstream_send_tagstruct(c
->pstream
, t
);
1528 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1529 struct connection
*c
= userdata
;
1530 pa_subscription_mask_t m
;
1533 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
1534 !pa_tagstruct_eof(t
)) {
1539 if (!c
->authorized
) {
1540 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1544 if (c
->subscription
)
1545 pa_subscription_free(c
->subscription
);
1548 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
1549 assert(c
->subscription
);
1551 c
->subscription
= NULL
;
1553 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1556 static void command_set_volume(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1557 struct connection
*c
= userdata
;
1560 pa_sink
*sink
= NULL
;
1561 pa_sink_input
*si
= NULL
;
1562 const char *name
= NULL
;
1565 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1566 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1567 pa_tagstruct_get_cvolume(t
, &volume
) ||
1568 !pa_tagstruct_eof(t
)) {
1573 if (!c
->authorized
) {
1574 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1578 if (command
== PA_COMMAND_SET_SINK_VOLUME
) {
1579 if (idx
!= (uint32_t) -1)
1580 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
1582 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1584 assert(command
== PA_COMMAND_SET_SINK_INPUT_VOLUME
);
1585 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
1589 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1594 pa_sink_set_volume(sink
, PA_MIXER_HARDWARE
, &volume
);
1596 pa_sink_input_set_volume(si
, &volume
);
1598 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1601 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
) {
1602 struct connection
*c
= userdata
;
1605 struct playback_stream
*s
, *sync
;
1608 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1609 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1610 !pa_tagstruct_eof(t
)) {
1615 if (!c
->authorized
) {
1616 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1620 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
1621 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1625 fprintf(stderr
, "Corking %i\n", b
);
1627 pa_sink_input_cork(s
->sink_input
, b
);
1628 pa_memblockq_prebuf_force(s
->memblockq
);
1630 /* Do the same for all other members in the sync group */
1631 for (sync
= s
->prev
; sync
; sync
= sync
->prev
) {
1632 pa_sink_input_cork(sync
->sink_input
, b
);
1633 pa_memblockq_prebuf_force(sync
->memblockq
);
1636 for (sync
= s
->next
; sync
; sync
= sync
->next
) {
1637 pa_sink_input_cork(sync
->sink_input
, b
);
1638 pa_memblockq_prebuf_force(sync
->memblockq
);
1641 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1644 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
) {
1645 struct connection
*c
= userdata
;
1647 struct playback_stream
*s
, *sync
;
1650 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1651 !pa_tagstruct_eof(t
)) {
1656 if (!c
->authorized
) {
1657 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1661 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
1662 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1666 pa_memblockq_flush(s
->memblockq
);
1669 /* Do the same for all other members in the sync group */
1670 for (sync
= s
->prev
; sync
; sync
= sync
->prev
) {
1671 pa_memblockq_flush(sync
->memblockq
);
1675 for (sync
= s
->next
; sync
; sync
= sync
->next
) {
1676 pa_memblockq_flush(sync
->memblockq
);
1680 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1681 pa_sink_notify(s
->sink_input
->sink
);
1684 for (sync
= s
->prev
; sync
; sync
= sync
->prev
)
1685 request_bytes(sync
);
1687 for (sync
= s
->next
; sync
; sync
= sync
->next
)
1688 request_bytes(sync
);
1691 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
) {
1692 struct connection
*c
= userdata
;
1694 struct playback_stream
*s
;
1697 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1698 !pa_tagstruct_eof(t
)) {
1703 if (!c
->authorized
) {
1704 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1708 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
1709 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1714 case PA_COMMAND_PREBUF_PLAYBACK_STREAM
:
1715 pa_memblockq_prebuf_force(s
->memblockq
);
1718 case PA_COMMAND_TRIGGER_PLAYBACK_STREAM
:
1719 pa_memblockq_prebuf_disable(s
->memblockq
);
1726 pa_sink_notify(s
->sink_input
->sink
);
1727 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1731 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
) {
1732 struct connection
*c
= userdata
;
1734 struct record_stream
*s
;
1738 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1739 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1740 !pa_tagstruct_eof(t
)) {
1745 if (!c
->authorized
) {
1746 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1750 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, idx
))) {
1751 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1755 pa_source_output_cork(s
->source_output
, b
);
1756 pa_memblockq_prebuf_force(s
->memblockq
);
1757 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1760 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
) {
1761 struct connection
*c
= userdata
;
1763 struct record_stream
*s
;
1766 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1767 !pa_tagstruct_eof(t
)) {
1772 if (!c
->authorized
) {
1773 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1777 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, idx
))) {
1778 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1782 pa_memblockq_flush(s
->memblockq
);
1783 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1786 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
) {
1787 struct connection
*c
= userdata
;
1792 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1793 pa_tagstruct_gets(t
, &s
) < 0 || !s
||
1794 !pa_tagstruct_eof(t
)) {
1799 if (!c
->authorized
) {
1800 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1804 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
1805 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1808 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1809 struct connection
*c
= userdata
;
1814 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1815 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1816 !pa_tagstruct_eof(t
)) {
1821 if (!c
->authorized
) {
1822 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1826 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
1827 struct playback_stream
*s
;
1829 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
1830 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1834 pa_sink_input_set_name(s
->sink_input
, name
);
1837 struct record_stream
*s
;
1839 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, idx
))) {
1840 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1844 pa_source_output_set_name(s
->source_output
, name
);
1847 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1850 static void command_kill(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1851 struct connection
*c
= userdata
;
1855 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1856 !pa_tagstruct_eof(t
)) {
1861 if (!c
->authorized
) {
1862 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1866 if (command
== PA_COMMAND_KILL_CLIENT
) {
1869 if (!(client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
))) {
1870 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1874 pa_client_kill(client
);
1875 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
1878 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
))) {
1879 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1883 pa_sink_input_kill(s
);
1885 pa_source_output
*s
;
1887 assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
1889 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
))) {
1890 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1894 pa_source_output_kill(s
);
1897 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1900 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
) {
1901 struct connection
*c
= userdata
;
1903 const char *name
, *argument
;
1904 pa_tagstruct
*reply
;
1907 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1908 pa_tagstruct_gets(t
, &argument
) < 0 ||
1909 !pa_tagstruct_eof(t
)) {
1914 if (!c
->authorized
) {
1915 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1919 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
1920 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_MODINITFAILED
);
1924 reply
= pa_tagstruct_new(NULL
, 0);
1925 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1926 pa_tagstruct_putu32(reply
, tag
);
1927 pa_tagstruct_putu32(reply
, m
->index
);
1928 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1931 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
) {
1932 struct connection
*c
= userdata
;
1937 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1938 !pa_tagstruct_eof(t
)) {
1943 if (!c
->authorized
) {
1944 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1948 if (!(m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
))) {
1949 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
1953 pa_module_unload_request(m
);
1954 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1957 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
) {
1958 struct connection
*c
= userdata
;
1959 const char *name
, *module
, *argument
;
1962 pa_tagstruct
*reply
;
1965 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1966 pa_tagstruct_getu32(t
, &type
) < 0 || type
> 1 ||
1967 pa_tagstruct_gets(t
, &module
) < 0 || !module
||
1968 pa_tagstruct_gets(t
, &argument
) < 0 ||
1969 !pa_tagstruct_eof(t
)) {
1974 if (!c
->authorized
) {
1975 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
1979 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
1980 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_EXIST
);
1984 reply
= pa_tagstruct_new(NULL
, 0);
1985 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1986 pa_tagstruct_putu32(reply
, tag
);
1987 pa_tagstruct_putu32(reply
, idx
);
1988 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1991 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
) {
1992 struct connection
*c
= userdata
;
1993 const char *name
= NULL
;
1994 uint32_t type
, idx
= PA_IDXSET_INVALID
;
1998 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
1999 (pa_tagstruct_gets(t
, &name
) < 0 ||
2000 pa_tagstruct_getu32(t
, &type
) < 0)) ||
2001 (!name
&& idx
== PA_IDXSET_INVALID
) ||
2002 (name
&& type
> 1) ||
2003 !pa_tagstruct_eof(t
)) {
2008 if (!c
->authorized
) {
2009 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2014 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
2016 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
2019 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2023 pa_pstream_send_simple_ack(c
->pstream
, tag
);
2026 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
2029 pa_tagstruct_putu32(t
, e
->index
);
2030 pa_tagstruct_puts(t
, e
->name
);
2031 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
2032 pa_tagstruct_puts(t
, e
->module
);
2033 pa_tagstruct_puts(t
, e
->argument
);
2036 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
) {
2037 struct connection
*c
= userdata
;
2038 const pa_autoload_entry
*a
= NULL
;
2041 pa_tagstruct
*reply
;
2044 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
2045 (pa_tagstruct_gets(t
, &name
) < 0 ||
2046 pa_tagstruct_getu32(t
, &type
) < 0)) ||
2047 (!name
&& idx
== PA_IDXSET_INVALID
) ||
2048 (name
&& type
> 1) ||
2049 !pa_tagstruct_eof(t
)) {
2054 if (!c
->authorized
) {
2055 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2061 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
2063 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
2066 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_NOENTITY
);
2070 reply
= pa_tagstruct_new(NULL
, 0);
2072 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
2073 pa_tagstruct_putu32(reply
, tag
);
2074 autoload_fill_tagstruct(reply
, a
);
2075 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2078 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
) {
2079 struct connection
*c
= userdata
;
2080 pa_tagstruct
*reply
;
2083 if (!pa_tagstruct_eof(t
)) {
2088 if (!c
->authorized
) {
2089 pa_pstream_send_error(c
->pstream
, tag
, PA_ERR_ACCESS
);
2093 reply
= pa_tagstruct_new(NULL
, 0);
2094 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
2095 pa_tagstruct_putu32(reply
, tag
);
2097 if (c
->protocol
->core
->autoload_hashmap
) {
2098 pa_autoload_entry
*a
;
2101 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
2102 autoload_fill_tagstruct(reply
, a
);
2105 pa_pstream_send_tagstruct(c
->pstream
, reply
);
2108 /*** pstream callbacks ***/
2110 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, void *userdata
) {
2111 struct connection
*c
= userdata
;
2112 assert(p
&& packet
&& packet
->data
&& c
);
2114 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
2115 pa_log(__FILE__
": invalid packet.\n");
2120 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
) {
2121 struct connection
*c
= userdata
;
2122 struct output_stream
*stream
;
2123 assert(p
&& chunk
&& userdata
);
2125 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
2126 pa_log(__FILE__
": client sent block for invalid stream.\n");
2131 if (stream
->type
== PLAYBACK_STREAM
) {
2132 struct playback_stream
*ps
= (struct playback_stream
*) stream
;
2133 if (chunk
->length
>= ps
->requested_bytes
)
2134 ps
->requested_bytes
= 0;
2136 ps
->requested_bytes
-= chunk
->length
;
2138 pa_memblockq_seek(ps
->memblockq
, offset
, seek
);
2140 if (pa_memblockq_push_align(ps
->memblockq
, chunk
) < 0) {
2143 pa_log_warn(__FILE__
": failed to push data into queue\n");
2145 /* Pushing this block into the queue failed, so we simulate
2146 * it by skipping ahead */
2148 pa_memblockq_seek(ps
->memblockq
, chunk
->length
, PA_SEEK_RELATIVE
);
2150 /* Notify the user */
2151 t
= pa_tagstruct_new(NULL
, 0);
2152 pa_tagstruct_putu32(t
, PA_COMMAND_OVERFLOW
);
2153 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
2154 pa_tagstruct_putu32(t
, ps
->index
);
2155 pa_pstream_send_tagstruct(p
, t
);
2160 pa_sink_notify(ps
->sink_input
->sink
);
2163 struct upload_stream
*u
= (struct upload_stream
*) stream
;
2165 assert(u
->type
== UPLOAD_STREAM
);
2167 if (!u
->memchunk
.memblock
) {
2168 if (u
->length
== chunk
->length
) {
2169 u
->memchunk
= *chunk
;
2170 pa_memblock_ref(u
->memchunk
.memblock
);
2173 u
->memchunk
.memblock
= pa_memblock_new(u
->length
, c
->protocol
->core
->memblock_stat
);
2174 u
->memchunk
.index
= u
->memchunk
.length
= 0;
2178 assert(u
->memchunk
.memblock
);
2181 if (l
> chunk
->length
)
2185 memcpy((uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
,
2186 (uint8_t*) chunk
->memblock
->data
+chunk
->index
, l
);
2187 u
->memchunk
.length
+= l
;
2193 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
2194 struct connection
*c
= userdata
;
2198 /* pa_log(__FILE__": connection died.\n");*/
2202 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
2203 struct connection
*c
= userdata
;
2209 /*** client callbacks ***/
2211 static void client_kill_cb(pa_client
*c
) {
2212 assert(c
&& c
->userdata
);
2213 connection_free(c
->userdata
);
2216 /*** socket server callbacks ***/
2218 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
2219 struct connection
*c
= userdata
;
2220 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
2226 static void on_connection(PA_GCC_UNUSED pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
2227 pa_protocol_native
*p
= userdata
;
2228 struct connection
*c
;
2231 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
2232 pa_log_warn(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
2233 pa_iochannel_free(io
);
2237 c
= pa_xmalloc(sizeof(struct connection
));
2239 c
->authorized
=!! p
->public;
2241 if (!c
->authorized
) {
2243 pa_gettimeofday(&tv
);
2244 tv
.tv_sec
+= AUTH_TIMEOUT
;
2245 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
2247 c
->auth_timeout_event
= NULL
;
2251 c
->client
= pa_client_new(p
->core
, __FILE__
, "Client");
2253 c
->client
->kill
= client_kill_cb
;
2254 c
->client
->userdata
= c
;
2255 c
->client
->owner
= p
->module
;
2257 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->memblock_stat
);
2260 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
2261 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
2262 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
2263 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
2265 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
2266 assert(c
->pdispatch
);
2268 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
2269 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
2270 assert(c
->record_streams
&& c
->output_streams
);
2272 c
->rrobin_index
= PA_IDXSET_INVALID
;
2273 c
->subscription
= NULL
;
2275 pa_idxset_put(p
->connections
, c
, NULL
);
2278 /*** module entry points ***/
2280 static int load_key(pa_protocol_native
*p
, const char*fn
) {
2283 p
->auth_cookie_in_property
= 0;
2285 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
2286 pa_log_info(__FILE__
": using already loaded auth cookie.\n");
2287 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2288 p
->auth_cookie_in_property
= 1;
2293 fn
= PA_NATIVE_COOKIE_FILE
;
2295 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
2298 pa_log_info(__FILE__
": loading cookie from disk.\n");
2300 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
2301 p
->auth_cookie_in_property
= 1;
2306 static pa_protocol_native
* protocol_new_internal(pa_core
*c
, pa_module
*m
, pa_modargs
*ma
) {
2307 pa_protocol_native
*p
;
2311 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
2312 pa_log(__FILE__
": public= expects a boolean argument.\n");
2316 p
= pa_xmalloc(sizeof(pa_protocol_native
));
2322 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0) {
2327 p
->connections
= pa_idxset_new(NULL
, NULL
);
2328 assert(p
->connections
);
2333 pa_protocol_native
* pa_protocol_native_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
2335 pa_protocol_native
*p
;
2337 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2341 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
2343 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2345 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2346 l
= pa_strlist_prepend(l
, t
);
2347 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2353 void pa_protocol_native_free(pa_protocol_native
*p
) {
2354 struct connection
*c
;
2357 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
2359 pa_idxset_free(p
->connections
, NULL
, NULL
);
2364 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2366 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2367 l
= pa_strlist_remove(l
, t
);
2370 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2372 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2375 pa_socket_server_unref(p
->server
);
2378 if (p
->auth_cookie_in_property
)
2379 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2384 pa_protocol_native
* pa_protocol_native_new_iochannel(pa_core
*core
, pa_iochannel
*io
, pa_module
*m
, pa_modargs
*ma
) {
2385 pa_protocol_native
*p
;
2387 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2390 on_connection(NULL
, io
, p
);