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 "protocol-native.h"
32 #include "native-common.h"
35 #include "source-output.h"
36 #include "sink-input.h"
38 #include "tagstruct.h"
39 #include "pdispatch.h"
40 #include "pstream-util.h"
46 #include "subscribe.h"
49 #include "authkey-prop.h"
53 /* Kick a client if it doesn't authenticate within this time */
54 #define AUTH_TIMEOUT 5
56 /* Don't accept more connection than this */
57 #define MAX_CONNECTIONS 10
60 struct pa_protocol_native
;
62 struct record_stream
{
63 struct connection
*connection
;
65 pa_source_output
*source_output
;
66 pa_memblockq
*memblockq
;
70 struct playback_stream
{
72 struct connection
*connection
;
74 pa_sink_input
*sink_input
;
75 pa_memblockq
*memblockq
;
76 size_t requested_bytes
;
81 struct upload_stream
{
83 struct connection
*connection
;
88 pa_sample_spec sample_spec
;
89 pa_channel_map channel_map
;
92 struct output_stream
{
103 pa_protocol_native
*protocol
;
106 pa_pdispatch
*pdispatch
;
107 pa_idxset
*record_streams
, *output_streams
;
108 uint32_t rrobin_index
;
109 pa_subscription
*subscription
;
110 pa_time_event
*auth_timeout_event
;
113 struct pa_protocol_native
{
117 pa_socket_server
*server
;
118 pa_idxset
*connections
;
119 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
120 int auth_cookie_in_property
;
123 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
);
124 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
);
125 static void sink_input_kill_cb(pa_sink_input
*i
);
126 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
);
128 static void request_bytes(struct playback_stream
*s
);
130 static void source_output_kill_cb(pa_source_output
*o
);
131 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
132 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
134 static void command_exit(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
135 static void command_create_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
136 static void command_drain_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
137 static void command_create_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
138 static void command_delete_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
139 static void command_auth(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
140 static void command_set_client_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
141 static void command_lookup(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
142 static void command_stat(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
143 static void command_get_playback_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
144 static void command_get_record_latency(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
145 static void command_create_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
146 static void command_finish_upload_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
147 static void command_play_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
148 static void command_remove_sample(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
149 static void command_get_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
150 static void command_get_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
151 static void command_get_server_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
152 static void command_subscribe(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
153 static void command_set_volume(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
154 static void command_cork_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
155 static void command_flush_or_trigger_playback_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
156 static void command_set_default_sink_or_source(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
157 static void command_set_stream_name(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
158 static void command_kill(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
159 static void command_load_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
160 static void command_unload_module(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
161 static void command_add_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
162 static void command_remove_autoload(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
163 static void command_get_autoload_info(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
164 static void command_get_autoload_info_list(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
165 static void command_cork_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
166 static void command_flush_record_stream(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
);
168 static const pa_pdispatch_callback command_table
[PA_COMMAND_MAX
] = {
169 [PA_COMMAND_ERROR
] = NULL
,
170 [PA_COMMAND_TIMEOUT
] = NULL
,
171 [PA_COMMAND_REPLY
] = NULL
,
172 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = command_create_playback_stream
,
173 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = command_delete_stream
,
174 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = command_drain_playback_stream
,
175 [PA_COMMAND_CREATE_RECORD_STREAM
] = command_create_record_stream
,
176 [PA_COMMAND_DELETE_RECORD_STREAM
] = command_delete_stream
,
177 [PA_COMMAND_AUTH
] = command_auth
,
178 [PA_COMMAND_REQUEST
] = NULL
,
179 [PA_COMMAND_EXIT
] = command_exit
,
180 [PA_COMMAND_SET_CLIENT_NAME
] = command_set_client_name
,
181 [PA_COMMAND_LOOKUP_SINK
] = command_lookup
,
182 [PA_COMMAND_LOOKUP_SOURCE
] = command_lookup
,
183 [PA_COMMAND_STAT
] = command_stat
,
184 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = command_get_playback_latency
,
185 [PA_COMMAND_GET_RECORD_LATENCY
] = command_get_record_latency
,
186 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = command_create_upload_stream
,
187 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = command_delete_stream
,
188 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = command_finish_upload_stream
,
189 [PA_COMMAND_PLAY_SAMPLE
] = command_play_sample
,
190 [PA_COMMAND_REMOVE_SAMPLE
] = command_remove_sample
,
191 [PA_COMMAND_GET_SINK_INFO
] = command_get_info
,
192 [PA_COMMAND_GET_SOURCE_INFO
] = command_get_info
,
193 [PA_COMMAND_GET_CLIENT_INFO
] = command_get_info
,
194 [PA_COMMAND_GET_MODULE_INFO
] = command_get_info
,
195 [PA_COMMAND_GET_SINK_INPUT_INFO
] = command_get_info
,
196 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = command_get_info
,
197 [PA_COMMAND_GET_SAMPLE_INFO
] = command_get_info
,
198 [PA_COMMAND_GET_SINK_INFO_LIST
] = command_get_info_list
,
199 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = command_get_info_list
,
200 [PA_COMMAND_GET_MODULE_INFO_LIST
] = command_get_info_list
,
201 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = command_get_info_list
,
202 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = command_get_info_list
,
203 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = command_get_info_list
,
204 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = command_get_info_list
,
205 [PA_COMMAND_GET_SERVER_INFO
] = command_get_server_info
,
206 [PA_COMMAND_SUBSCRIBE
] = command_subscribe
,
208 [PA_COMMAND_SET_SINK_VOLUME
] = command_set_volume
,
209 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = command_set_volume
,
211 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = command_cork_playback_stream
,
212 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = command_flush_or_trigger_playback_stream
,
213 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = command_flush_or_trigger_playback_stream
,
214 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = command_flush_or_trigger_playback_stream
,
216 [PA_COMMAND_CORK_RECORD_STREAM
] = command_cork_record_stream
,
217 [PA_COMMAND_FLUSH_RECORD_STREAM
] = command_flush_record_stream
,
219 [PA_COMMAND_SET_DEFAULT_SINK
] = command_set_default_sink_or_source
,
220 [PA_COMMAND_SET_DEFAULT_SOURCE
] = command_set_default_sink_or_source
,
221 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = command_set_stream_name
,
222 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = command_set_stream_name
,
223 [PA_COMMAND_KILL_CLIENT
] = command_kill
,
224 [PA_COMMAND_KILL_SINK_INPUT
] = command_kill
,
225 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = command_kill
,
226 [PA_COMMAND_LOAD_MODULE
] = command_load_module
,
227 [PA_COMMAND_UNLOAD_MODULE
] = command_unload_module
,
228 [PA_COMMAND_GET_AUTOLOAD_INFO
] = command_get_autoload_info
,
229 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = command_get_autoload_info_list
,
230 [PA_COMMAND_ADD_AUTOLOAD
] = command_add_autoload
,
231 [PA_COMMAND_REMOVE_AUTOLOAD
] = command_remove_autoload
,
235 /* structure management */
237 static struct upload_stream
* upload_stream_new(
238 struct connection
*c
,
239 const pa_sample_spec
*ss
,
240 const pa_channel_map
*map
,
241 const char *name
, size_t length
) {
243 struct upload_stream
*s
;
244 assert(c
&& ss
&& name
&& length
);
246 s
= pa_xmalloc(sizeof(struct upload_stream
));
247 s
->type
= UPLOAD_STREAM
;
249 s
->sample_spec
= *ss
;
250 s
->channel_map
= *map
;
251 s
->name
= pa_xstrdup(name
);
253 s
->memchunk
.memblock
= NULL
;
254 s
->memchunk
.index
= 0;
255 s
->memchunk
.length
= 0;
259 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
263 static void upload_stream_free(struct upload_stream
*o
) {
264 assert(o
&& o
->connection
);
266 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
270 if (o
->memchunk
.memblock
)
271 pa_memblock_unref(o
->memchunk
.memblock
);
276 static struct record_stream
* record_stream_new(
277 struct connection
*c
,
279 const pa_sample_spec
*ss
,
280 const pa_channel_map
*map
,
283 size_t fragment_size
) {
285 struct record_stream
*s
;
286 pa_source_output
*source_output
;
288 assert(c
&& source
&& ss
&& name
&& maxlength
);
290 if (!(source_output
= pa_source_output_new(source
, __FILE__
, name
, ss
, map
, -1)))
293 s
= pa_xmalloc(sizeof(struct record_stream
));
295 s
->source_output
= source_output
;
296 s
->source_output
->push
= source_output_push_cb
;
297 s
->source_output
->kill
= source_output_kill_cb
;
298 s
->source_output
->get_latency
= source_output_get_latency_cb
;
299 s
->source_output
->userdata
= s
;
300 s
->source_output
->owner
= c
->protocol
->module
;
301 s
->source_output
->client
= c
->client
;
303 s
->memblockq
= pa_memblockq_new(maxlength
, 0, base
= pa_frame_size(ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
304 assert(s
->memblockq
);
306 s
->fragment_size
= (fragment_size
/base
)*base
;
307 if (!s
->fragment_size
)
308 s
->fragment_size
= base
;
310 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
314 static void record_stream_free(struct record_stream
* r
) {
315 assert(r
&& r
->connection
);
317 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
318 pa_source_output_disconnect(r
->source_output
);
319 pa_source_output_unref(r
->source_output
);
320 pa_memblockq_free(r
->memblockq
);
324 static struct playback_stream
* playback_stream_new(
325 struct connection
*c
,
327 const pa_sample_spec
*ss
,
328 const pa_channel_map
*map
,
334 pa_cvolume
*volume
) {
336 struct playback_stream
*s
;
337 pa_sink_input
*sink_input
;
338 assert(c
&& sink
&& ss
&& name
&& maxlength
);
340 if (!(sink_input
= pa_sink_input_new(sink
, __FILE__
, name
, ss
, map
, 0, -1)))
343 s
= pa_xmalloc(sizeof(struct playback_stream
));
344 s
->type
= PLAYBACK_STREAM
;
346 s
->sink_input
= sink_input
;
348 s
->sink_input
->peek
= sink_input_peek_cb
;
349 s
->sink_input
->drop
= sink_input_drop_cb
;
350 s
->sink_input
->kill
= sink_input_kill_cb
;
351 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
352 s
->sink_input
->userdata
= s
;
353 s
->sink_input
->owner
= c
->protocol
->module
;
354 s
->sink_input
->client
= c
->client
;
356 s
->memblockq
= pa_memblockq_new(maxlength
, tlength
, pa_frame_size(ss
), prebuf
, minreq
, c
->protocol
->core
->memblock_stat
);
357 assert(s
->memblockq
);
359 s
->requested_bytes
= 0;
360 s
->drain_request
= 0;
362 s
->sink_input
->volume
= *volume
;
364 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
368 static void playback_stream_free(struct playback_stream
* p
) {
369 assert(p
&& p
->connection
);
371 if (p
->drain_request
)
372 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERROR_NOENTITY
);
374 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
375 pa_sink_input_disconnect(p
->sink_input
);
376 pa_sink_input_unref(p
->sink_input
);
377 pa_memblockq_free(p
->memblockq
);
381 static void connection_free(struct connection
*c
) {
382 struct record_stream
*r
;
383 struct output_stream
*o
;
384 assert(c
&& c
->protocol
);
386 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
387 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
388 record_stream_free(r
);
389 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
391 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
392 if (o
->type
== PLAYBACK_STREAM
)
393 playback_stream_free((struct playback_stream
*) o
);
395 upload_stream_free((struct upload_stream
*) o
);
396 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
398 pa_pdispatch_unref(c
->pdispatch
);
399 pa_pstream_close(c
->pstream
);
400 pa_pstream_unref(c
->pstream
);
401 pa_client_free(c
->client
);
404 pa_subscription_free(c
->subscription
);
406 if (c
->auth_timeout_event
)
407 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
412 static void request_bytes(struct playback_stream
*s
) {
417 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
420 if (l
<= s
->requested_bytes
)
423 l
-= s
->requested_bytes
;
425 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
428 s
->requested_bytes
+= l
;
430 t
= pa_tagstruct_new(NULL
, 0);
432 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
433 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
434 pa_tagstruct_putu32(t
, s
->index
);
435 pa_tagstruct_putu32(t
, l
);
436 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
438 /* pa_log(__FILE__": Requesting %u bytes\n", l); */
441 static void send_memblock(struct connection
*c
) {
443 struct record_stream
*r
;
445 start
= PA_IDXSET_INVALID
;
449 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
452 if (start
== PA_IDXSET_INVALID
)
453 start
= c
->rrobin_index
;
454 else if (start
== c
->rrobin_index
)
457 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
458 pa_memchunk schunk
= chunk
;
460 if (schunk
.length
> r
->fragment_size
)
461 schunk
.length
= r
->fragment_size
;
463 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, &schunk
);
464 pa_memblockq_drop(r
->memblockq
, &chunk
, schunk
.length
);
465 pa_memblock_unref(schunk
.memblock
);
472 static void send_playback_stream_killed(struct playback_stream
*p
) {
476 t
= pa_tagstruct_new(NULL
, 0);
478 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
479 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
480 pa_tagstruct_putu32(t
, p
->index
);
481 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
484 static void send_record_stream_killed(struct record_stream
*r
) {
488 t
= pa_tagstruct_new(NULL
, 0);
490 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
491 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
492 pa_tagstruct_putu32(t
, r
->index
);
493 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
496 /*** sinkinput callbacks ***/
498 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
499 struct playback_stream
*s
;
500 assert(i
&& i
->userdata
&& chunk
);
503 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
509 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
510 struct playback_stream
*s
;
511 assert(i
&& i
->userdata
&& length
);
514 pa_memblockq_drop(s
->memblockq
, chunk
, length
);
517 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
518 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
519 s
->drain_request
= 0;
522 /* pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq)); */
525 static void sink_input_kill_cb(pa_sink_input
*i
) {
526 assert(i
&& i
->userdata
);
527 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
528 playback_stream_free((struct playback_stream
*) i
->userdata
);
531 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
532 struct playback_stream
*s
;
533 assert(i
&& i
->userdata
);
536 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
538 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
541 /*** source_output callbacks ***/
543 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
544 struct record_stream
*s
;
545 assert(o
&& o
->userdata
&& chunk
);
548 pa_memblockq_push_align(s
->memblockq
, chunk
, 0);
549 if (!pa_pstream_is_pending(s
->connection
->pstream
))
550 send_memblock(s
->connection
);
553 static void source_output_kill_cb(pa_source_output
*o
) {
554 assert(o
&& o
->userdata
);
555 send_record_stream_killed((struct record_stream
*) o
->userdata
);
556 record_stream_free((struct record_stream
*) o
->userdata
);
559 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
560 struct record_stream
*s
;
561 assert(o
&& o
->userdata
);
564 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
566 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
569 /*** pdispatch callbacks ***/
571 static void protocol_error(struct connection
*c
) {
572 pa_log(__FILE__
": protocol error, kicking client\n");
576 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
) {
577 struct connection
*c
= userdata
;
578 struct playback_stream
*s
;
579 size_t maxlength
, tlength
, prebuf
, minreq
;
581 const char *name
, *sink_name
;
589 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
591 if (pa_tagstruct_get(
593 PA_TAG_STRING
, &name
,
594 PA_TAG_SAMPLE_SPEC
, &ss
,
595 PA_TAG_CHANNEL_MAP
, &map
,
596 PA_TAG_U32
, &sink_index
,
597 PA_TAG_STRING
, &sink_name
,
598 PA_TAG_U32
, &maxlength
,
599 PA_TAG_BOOLEAN
, &corked
,
600 PA_TAG_U32
, &tlength
,
603 PA_TAG_CVOLUME
, &volume
,
604 PA_TAG_INVALID
) < 0 ||
605 !pa_tagstruct_eof(t
) ||
611 if (!c
->authorized
) {
612 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
616 if (sink_index
!= (uint32_t) -1)
617 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
619 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
622 pa_log("%s: Can't find a suitable sink.\n", __FILE__
);
623 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
627 if (!(s
= playback_stream_new(c
, sink
, &ss
, &map
, name
, maxlength
, tlength
, prebuf
, minreq
, &volume
))) {
628 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
632 pa_sink_input_cork(s
->sink_input
, corked
);
634 reply
= pa_tagstruct_new(NULL
, 0);
636 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
637 pa_tagstruct_putu32(reply
, tag
);
638 pa_tagstruct_putu32(reply
, s
->index
);
639 assert(s
->sink_input
);
640 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
641 pa_tagstruct_putu32(reply
, s
->requested_bytes
= pa_memblockq_missing(s
->memblockq
));
642 pa_pstream_send_tagstruct(c
->pstream
, reply
);
646 static void command_delete_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
647 struct connection
*c
= userdata
;
651 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
652 !pa_tagstruct_eof(t
)) {
657 if (!c
->authorized
) {
658 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
662 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
663 struct playback_stream
*s
;
664 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
665 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
669 playback_stream_free(s
);
670 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
671 struct record_stream
*s
;
672 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
673 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
677 record_stream_free(s
);
679 struct upload_stream
*s
;
680 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
681 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
682 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
686 upload_stream_free(s
);
689 pa_pstream_send_simple_ack(c
->pstream
, tag
);
692 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
) {
693 struct connection
*c
= userdata
;
694 struct record_stream
*s
;
695 size_t maxlength
, fragment_size
;
696 uint32_t source_index
;
697 const char *name
, *source_name
;
703 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
705 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
706 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
707 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
708 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
709 pa_tagstruct_gets(t
, &source_name
) < 0 ||
710 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
711 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
712 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
713 !pa_tagstruct_eof(t
)) {
718 if (!c
->authorized
) {
719 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
723 if (source_index
!= (uint32_t) -1)
724 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
726 source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1);
729 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
733 if (!(s
= record_stream_new(c
, source
, &ss
, &map
, name
, maxlength
, fragment_size
))) {
734 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
738 pa_source_output_cork(s
->source_output
, corked
);
740 reply
= pa_tagstruct_new(NULL
, 0);
742 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
743 pa_tagstruct_putu32(reply
, tag
);
744 pa_tagstruct_putu32(reply
, s
->index
);
745 assert(s
->source_output
);
746 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
747 pa_pstream_send_tagstruct(c
->pstream
, reply
);
750 static void command_exit(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
751 struct connection
*c
= userdata
;
754 if (!pa_tagstruct_eof(t
)) {
759 if (!c
->authorized
) {
760 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
764 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
765 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
766 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
770 static void command_auth(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
771 struct connection
*c
= userdata
;
775 if (pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
776 !pa_tagstruct_eof(t
)) {
781 if (!c
->authorized
) {
782 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) != 0) {
783 pa_log(__FILE__
": Denied access to client with invalid authorization key.\n");
784 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
789 if (c
->auth_timeout_event
) {
790 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
791 c
->auth_timeout_event
= NULL
;
795 pa_pstream_send_simple_ack(c
->pstream
, tag
);
799 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
) {
800 struct connection
*c
= userdata
;
804 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
805 !pa_tagstruct_eof(t
)) {
810 pa_client_set_name(c
->client
, name
);
811 pa_pstream_send_simple_ack(c
->pstream
, tag
);
815 static void command_lookup(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
816 struct connection
*c
= userdata
;
818 uint32_t idx
= PA_IDXSET_INVALID
;
821 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
822 !pa_tagstruct_eof(t
)) {
827 if (!c
->authorized
) {
828 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
832 if (command
== PA_COMMAND_LOOKUP_SINK
) {
834 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
838 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
839 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
843 if (idx
== PA_IDXSET_INVALID
)
844 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
847 reply
= pa_tagstruct_new(NULL
, 0);
849 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
850 pa_tagstruct_putu32(reply
, tag
);
851 pa_tagstruct_putu32(reply
, idx
);
852 pa_pstream_send_tagstruct(c
->pstream
, reply
);
856 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
) {
857 struct connection
*c
= userdata
;
859 struct playback_stream
*s
;
862 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
863 !pa_tagstruct_eof(t
)) {
868 if (!c
->authorized
) {
869 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
873 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
874 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
878 s
->drain_request
= 0;
880 pa_memblockq_prebuf_disable(s
->memblockq
);
882 if (!pa_memblockq_is_readable(s
->memblockq
)) {
883 /* pa_log("immediate drain: %u\n", pa_memblockq_get_length(s->memblockq)); */
884 pa_pstream_send_simple_ack(c
->pstream
, tag
);
886 /* pa_log("slow drain triggered\n"); */
887 s
->drain_request
= 1;
890 pa_sink_notify(s
->sink_input
->sink
);
894 static void command_stat(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
895 struct connection
*c
= userdata
;
899 if (!pa_tagstruct_eof(t
)) {
904 if (!c
->authorized
) {
905 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
909 reply
= pa_tagstruct_new(NULL
, 0);
911 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
912 pa_tagstruct_putu32(reply
, tag
);
913 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total
);
914 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total_size
);
915 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated
);
916 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated_size
);
917 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
918 pa_pstream_send_tagstruct(c
->pstream
, reply
);
921 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
) {
922 struct connection
*c
= userdata
;
924 struct playback_stream
*s
;
925 struct timeval tv
, now
;
930 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
931 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
932 pa_tagstruct_getu64(t
, &counter
) < 0 ||
933 !pa_tagstruct_eof(t
)) {
938 if (!c
->authorized
) {
939 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
943 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
944 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
948 reply
= pa_tagstruct_new(NULL
, 0);
950 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
951 pa_tagstruct_putu32(reply
, tag
);
952 pa_tagstruct_put_usec(reply
, pa_sink_input_get_latency(s
->sink_input
));
953 pa_tagstruct_put_usec(reply
, pa_sink_get_latency(s
->sink_input
->sink
));
954 pa_tagstruct_put_usec(reply
, 0);
955 pa_tagstruct_put_boolean(reply
, pa_memblockq_is_readable(s
->memblockq
));
956 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
957 pa_tagstruct_put_timeval(reply
, &tv
);
958 pa_gettimeofday(&now
);
959 pa_tagstruct_put_timeval(reply
, &now
);
960 pa_tagstruct_putu64(reply
, counter
);
961 pa_pstream_send_tagstruct(c
->pstream
, reply
);
964 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
) {
965 struct connection
*c
= userdata
;
967 struct record_stream
*s
;
968 struct timeval tv
, now
;
973 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
974 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
975 pa_tagstruct_getu64(t
, &counter
) < 0 ||
976 !pa_tagstruct_eof(t
)) {
981 if (!c
->authorized
) {
982 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
986 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, idx
))) {
987 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
991 reply
= pa_tagstruct_new(NULL
, 0);
993 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
994 pa_tagstruct_putu32(reply
, tag
);
995 pa_tagstruct_put_usec(reply
, pa_source_output_get_latency(s
->source_output
));
996 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
997 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
998 pa_tagstruct_put_boolean(reply
, 0);
999 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
1000 pa_tagstruct_put_timeval(reply
, &tv
);
1001 pa_gettimeofday(&now
);
1002 pa_tagstruct_put_timeval(reply
, &now
);
1003 pa_tagstruct_putu64(reply
, counter
);
1004 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1008 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
) {
1009 struct connection
*c
= userdata
;
1010 struct upload_stream
*s
;
1015 pa_tagstruct
*reply
;
1016 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
1018 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1019 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1020 pa_tagstruct_get_channel_map(t
, &map
) < 0 ||
1021 pa_tagstruct_getu32(t
, &length
) < 0 ||
1022 !pa_tagstruct_eof(t
)) {
1027 if (!c
->authorized
) {
1028 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1032 if ((length
% pa_frame_size(&ss
)) != 0 || length
<= 0 || !*name
) {
1033 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
1037 if (!(s
= upload_stream_new(c
, &ss
, &map
, name
, length
))) {
1038 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
1042 reply
= pa_tagstruct_new(NULL
, 0);
1044 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1045 pa_tagstruct_putu32(reply
, tag
);
1046 pa_tagstruct_putu32(reply
, s
->index
);
1047 pa_tagstruct_putu32(reply
, length
);
1048 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1051 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
) {
1052 struct connection
*c
= userdata
;
1054 struct upload_stream
*s
;
1058 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1059 !pa_tagstruct_eof(t
)) {
1064 if (!c
->authorized
) {
1065 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1069 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
1070 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1074 pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->channel_map
, &s
->memchunk
, &idx
);
1075 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1076 upload_stream_free(s
);
1079 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
) {
1080 struct connection
*c
= userdata
;
1081 uint32_t sink_index
;
1084 const char *name
, *sink_name
;
1087 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1088 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1089 pa_tagstruct_get_cvolume(t
, &volume
) < 0 ||
1090 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1091 !pa_tagstruct_eof(t
)) {
1096 if (!c
->authorized
) {
1097 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1101 if (sink_index
!= (uint32_t) -1)
1102 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1104 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1107 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1111 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, &volume
) < 0) {
1112 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1116 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1119 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
) {
1120 struct connection
*c
= userdata
;
1124 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1125 !pa_tagstruct_eof(t
)) {
1130 if (!c
->authorized
) {
1131 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1135 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
1136 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1140 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1143 static void sink_fill_tagstruct(pa_tagstruct
*t
, pa_sink
*sink
) {
1147 PA_TAG_U32
, sink
->index
,
1148 PA_TAG_STRING
, sink
->name
,
1149 PA_TAG_STRING
, sink
->description
,
1150 PA_TAG_SAMPLE_SPEC
, &sink
->sample_spec
,
1151 PA_TAG_CHANNEL_MAP
, &sink
->channel_map
,
1152 PA_TAG_U32
, sink
->owner
? sink
->owner
->index
: PA_INVALID_INDEX
,
1153 PA_TAG_CVOLUME
, pa_sink_get_volume(sink
, PA_MIXER_HARDWARE
),
1154 PA_TAG_U32
, sink
->monitor_source
->index
,
1155 PA_TAG_STRING
, sink
->monitor_source
->name
,
1156 PA_TAG_USEC
, pa_sink_get_latency(sink
),
1157 PA_TAG_STRING
, sink
->driver
,
1161 static void source_fill_tagstruct(pa_tagstruct
*t
, pa_source
*source
) {
1162 assert(t
&& source
);
1163 pa_tagstruct_putu32(t
, source
->index
);
1164 pa_tagstruct_puts(t
, source
->name
);
1165 pa_tagstruct_puts(t
, source
->description
);
1166 pa_tagstruct_put_sample_spec(t
, &source
->sample_spec
);
1167 pa_tagstruct_put_channel_map(t
, &source
->channel_map
);
1168 pa_tagstruct_putu32(t
, source
->owner
? source
->owner
->index
: (uint32_t) -1);
1169 pa_tagstruct_putu32(t
, source
->monitor_of
? source
->monitor_of
->index
: (uint32_t) -1);
1170 pa_tagstruct_puts(t
, source
->monitor_of
? source
->monitor_of
->name
: NULL
);
1171 pa_tagstruct_put_usec(t
, pa_source_get_latency(source
));
1172 pa_tagstruct_puts(t
, source
->driver
);
1175 static void client_fill_tagstruct(pa_tagstruct
*t
, pa_client
*client
) {
1176 assert(t
&& client
);
1177 pa_tagstruct_putu32(t
, client
->index
);
1178 pa_tagstruct_puts(t
, client
->name
);
1179 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: (uint32_t) -1);
1180 pa_tagstruct_puts(t
, client
->driver
);
1183 static void module_fill_tagstruct(pa_tagstruct
*t
, pa_module
*module
) {
1184 assert(t
&& module
);
1185 pa_tagstruct_putu32(t
, module
->index
);
1186 pa_tagstruct_puts(t
, module
->name
);
1187 pa_tagstruct_puts(t
, module
->argument
);
1188 pa_tagstruct_putu32(t
, module
->n_used
);
1189 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
1192 static void sink_input_fill_tagstruct(pa_tagstruct
*t
, pa_sink_input
*s
) {
1194 pa_tagstruct_putu32(t
, s
->index
);
1195 pa_tagstruct_puts(t
, s
->name
);
1196 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1197 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1198 pa_tagstruct_putu32(t
, s
->sink
->index
);
1199 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1200 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
1201 pa_tagstruct_put_cvolume(t
, &s
->volume
);
1202 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
1203 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
1204 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
1205 pa_tagstruct_puts(t
, s
->driver
);
1208 static void source_output_fill_tagstruct(pa_tagstruct
*t
, pa_source_output
*s
) {
1210 pa_tagstruct_putu32(t
, s
->index
);
1211 pa_tagstruct_puts(t
, s
->name
);
1212 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1213 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1214 pa_tagstruct_putu32(t
, s
->source
->index
);
1215 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1216 pa_tagstruct_put_channel_map(t
, &s
->channel_map
);
1217 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
1218 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
1219 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
1220 pa_tagstruct_puts(t
, s
->driver
);
1223 static void scache_fill_tagstruct(pa_tagstruct
*t
, pa_scache_entry
*e
) {
1225 pa_tagstruct_putu32(t
, e
->index
);
1226 pa_tagstruct_puts(t
, e
->name
);
1227 pa_tagstruct_put_cvolume(t
, &e
->volume
);
1228 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1229 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1230 pa_tagstruct_put_channel_map(t
, &e
->channel_map
);
1231 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
1232 pa_tagstruct_put_boolean(t
, e
->lazy
);
1233 pa_tagstruct_puts(t
, e
->filename
);
1236 static void command_get_info(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1237 struct connection
*c
= userdata
;
1239 pa_sink
*sink
= NULL
;
1240 pa_source
*source
= NULL
;
1241 pa_client
*client
= NULL
;
1242 pa_module
*module
= NULL
;
1243 pa_sink_input
*si
= NULL
;
1244 pa_source_output
*so
= NULL
;
1245 pa_scache_entry
*sce
= NULL
;
1247 pa_tagstruct
*reply
;
1251 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1252 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1253 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1254 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1255 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1256 pa_tagstruct_gets(t
, &name
) < 0) ||
1257 !pa_tagstruct_eof(t
)) {
1262 if (!c
->authorized
) {
1263 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1267 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1268 if (idx
!= (uint32_t) -1)
1269 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
1271 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1272 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1273 if (idx
!= (uint32_t) -1)
1274 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, idx
);
1276 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1277 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1278 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
);
1279 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1280 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
);
1281 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1282 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
1283 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1284 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
);
1286 assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
1287 if (idx
!= (uint32_t) -1)
1288 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, idx
);
1290 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1293 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1294 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1298 reply
= pa_tagstruct_new(NULL
, 0);
1300 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1301 pa_tagstruct_putu32(reply
, tag
);
1303 sink_fill_tagstruct(reply
, sink
);
1305 source_fill_tagstruct(reply
, source
);
1307 client_fill_tagstruct(reply
, client
);
1309 module_fill_tagstruct(reply
, module
);
1311 sink_input_fill_tagstruct(reply
, si
);
1313 source_output_fill_tagstruct(reply
, so
);
1315 scache_fill_tagstruct(reply
, sce
);
1316 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1319 static void command_get_info_list(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1320 struct connection
*c
= userdata
;
1324 pa_tagstruct
*reply
;
1327 if (!pa_tagstruct_eof(t
)) {
1332 if (!c
->authorized
) {
1333 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1337 reply
= pa_tagstruct_new(NULL
, 0);
1339 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1340 pa_tagstruct_putu32(reply
, tag
);
1342 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1343 i
= c
->protocol
->core
->sinks
;
1344 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1345 i
= c
->protocol
->core
->sources
;
1346 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1347 i
= c
->protocol
->core
->clients
;
1348 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1349 i
= c
->protocol
->core
->modules
;
1350 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1351 i
= c
->protocol
->core
->sink_inputs
;
1352 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1353 i
= c
->protocol
->core
->source_outputs
;
1355 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1356 i
= c
->protocol
->core
->scache
;
1360 for (p
= pa_idxset_first(i
, &idx
); p
; p
= pa_idxset_next(i
, &idx
)) {
1361 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1362 sink_fill_tagstruct(reply
, p
);
1363 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1364 source_fill_tagstruct(reply
, p
);
1365 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1366 client_fill_tagstruct(reply
, p
);
1367 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1368 module_fill_tagstruct(reply
, p
);
1369 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1370 sink_input_fill_tagstruct(reply
, p
);
1371 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1372 source_output_fill_tagstruct(reply
, p
);
1374 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1375 scache_fill_tagstruct(reply
, p
);
1380 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1383 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
) {
1384 struct connection
*c
= userdata
;
1385 pa_tagstruct
*reply
;
1390 if (!pa_tagstruct_eof(t
)) {
1395 if (!c
->authorized
) {
1396 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1400 reply
= pa_tagstruct_new(NULL
, 0);
1402 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1403 pa_tagstruct_putu32(reply
, tag
);
1404 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1405 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1406 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1407 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
1408 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1410 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
1411 pa_tagstruct_puts(reply
, n
);
1412 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
1413 pa_tagstruct_puts(reply
, n
);
1415 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
1417 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1420 static void subscription_cb(pa_core
*core
, pa_subscription_event_type_t e
, uint32_t idx
, void *userdata
) {
1422 struct connection
*c
= userdata
;
1425 t
= pa_tagstruct_new(NULL
, 0);
1427 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
1428 pa_tagstruct_putu32(t
, (uint32_t) -1);
1429 pa_tagstruct_putu32(t
, e
);
1430 pa_tagstruct_putu32(t
, idx
);
1431 pa_pstream_send_tagstruct(c
->pstream
, t
);
1434 static void command_subscribe(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1435 struct connection
*c
= userdata
;
1436 pa_subscription_mask_t m
;
1439 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
1440 !pa_tagstruct_eof(t
)) {
1445 if (!c
->authorized
) {
1446 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1450 if (c
->subscription
)
1451 pa_subscription_free(c
->subscription
);
1454 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
1455 assert(c
->subscription
);
1457 c
->subscription
= NULL
;
1459 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1462 static void command_set_volume(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1463 struct connection
*c
= userdata
;
1466 pa_sink
*sink
= NULL
;
1467 pa_sink_input
*si
= NULL
;
1468 const char *name
= NULL
;
1471 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1472 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1473 pa_tagstruct_get_cvolume(t
, &volume
) ||
1474 !pa_tagstruct_eof(t
)) {
1479 if (!c
->authorized
) {
1480 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1484 if (command
== PA_COMMAND_SET_SINK_VOLUME
) {
1485 if (idx
!= (uint32_t) -1)
1486 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, idx
);
1488 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1490 assert(command
== PA_COMMAND_SET_SINK_INPUT_VOLUME
);
1491 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
);
1495 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1500 pa_sink_set_volume(sink
, PA_MIXER_HARDWARE
, &volume
);
1502 pa_sink_input_set_volume(si
, &volume
);
1504 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1507 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
) {
1508 struct connection
*c
= userdata
;
1511 struct playback_stream
*s
;
1514 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1515 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1516 !pa_tagstruct_eof(t
)) {
1521 if (!c
->authorized
) {
1522 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1526 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
1527 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1531 pa_sink_input_cork(s
->sink_input
, b
);
1532 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1535 static void command_flush_or_trigger_playback_stream(PA_GCC_UNUSED pa_pdispatch
*pd
, PA_GCC_UNUSED
uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1536 struct connection
*c
= userdata
;
1538 struct playback_stream
*s
;
1541 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1542 !pa_tagstruct_eof(t
)) {
1547 if (!c
->authorized
) {
1548 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1552 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
1553 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1557 if (command
== PA_COMMAND_PREBUF_PLAYBACK_STREAM
)
1558 pa_memblockq_prebuf_reenable(s
->memblockq
);
1559 else if (command
== PA_COMMAND_TRIGGER_PLAYBACK_STREAM
)
1560 pa_memblockq_prebuf_disable(s
->memblockq
);
1562 assert(command
== PA_COMMAND_FLUSH_PLAYBACK_STREAM
);
1563 pa_memblockq_flush(s
->memblockq
);
1564 /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1567 pa_sink_notify(s
->sink_input
->sink
);
1568 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1572 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
) {
1573 struct connection
*c
= userdata
;
1575 struct record_stream
*s
;
1579 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1580 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1581 !pa_tagstruct_eof(t
)) {
1586 if (!c
->authorized
) {
1587 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1591 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, idx
))) {
1592 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1596 pa_source_output_cork(s
->source_output
, b
);
1597 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1600 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
) {
1601 struct connection
*c
= userdata
;
1603 struct record_stream
*s
;
1606 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1607 !pa_tagstruct_eof(t
)) {
1612 if (!c
->authorized
) {
1613 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1617 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, idx
))) {
1618 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1622 pa_memblockq_flush(s
->memblockq
);
1623 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1626 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
) {
1627 struct connection
*c
= userdata
;
1632 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1633 pa_tagstruct_gets(t
, &s
) < 0 || !s
||
1634 !pa_tagstruct_eof(t
)) {
1639 if (!c
->authorized
) {
1640 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1644 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
1645 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1648 static void command_set_stream_name(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1649 struct connection
*c
= userdata
;
1654 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1655 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1656 !pa_tagstruct_eof(t
)) {
1661 if (!c
->authorized
) {
1662 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1666 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
1667 struct playback_stream
*s
;
1669 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, idx
)) || s
->type
!= PLAYBACK_STREAM
) {
1670 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1674 pa_sink_input_set_name(s
->sink_input
, name
);
1677 struct record_stream
*s
;
1679 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, idx
))) {
1680 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1684 pa_source_output_set_name(s
->source_output
, name
);
1687 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1690 static void command_kill(PA_GCC_UNUSED pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1691 struct connection
*c
= userdata
;
1695 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1696 !pa_tagstruct_eof(t
)) {
1701 if (!c
->authorized
) {
1702 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1706 if (command
== PA_COMMAND_KILL_CLIENT
) {
1709 if (!(client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, idx
))) {
1710 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1714 pa_client_kill(client
);
1715 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
1718 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, idx
))) {
1719 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1723 pa_sink_input_kill(s
);
1725 pa_source_output
*s
;
1727 assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
1729 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, idx
))) {
1730 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1734 pa_source_output_kill(s
);
1737 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1740 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
) {
1741 struct connection
*c
= userdata
;
1743 const char *name
, *argument
;
1744 pa_tagstruct
*reply
;
1747 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1748 pa_tagstruct_gets(t
, &argument
) < 0 ||
1749 !pa_tagstruct_eof(t
)) {
1754 if (!c
->authorized
) {
1755 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1759 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
1760 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INITFAILED
);
1764 reply
= pa_tagstruct_new(NULL
, 0);
1765 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1766 pa_tagstruct_putu32(reply
, tag
);
1767 pa_tagstruct_putu32(reply
, m
->index
);
1768 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1771 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
) {
1772 struct connection
*c
= userdata
;
1777 if (pa_tagstruct_getu32(t
, &idx
) < 0 ||
1778 !pa_tagstruct_eof(t
)) {
1783 if (!c
->authorized
) {
1784 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1788 if (!(m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, idx
))) {
1789 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1793 pa_module_unload_request(m
);
1794 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1797 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
) {
1798 struct connection
*c
= userdata
;
1799 const char *name
, *module
, *argument
;
1802 pa_tagstruct
*reply
;
1805 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1806 pa_tagstruct_getu32(t
, &type
) < 0 || type
> 1 ||
1807 pa_tagstruct_gets(t
, &module
) < 0 || !module
||
1808 pa_tagstruct_gets(t
, &argument
) < 0 ||
1809 !pa_tagstruct_eof(t
)) {
1814 if (!c
->authorized
) {
1815 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1819 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &idx
) < 0) {
1820 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1824 reply
= pa_tagstruct_new(NULL
, 0);
1825 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1826 pa_tagstruct_putu32(reply
, tag
);
1827 pa_tagstruct_putu32(reply
, idx
);
1828 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1831 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
) {
1832 struct connection
*c
= userdata
;
1833 const char *name
= NULL
;
1834 uint32_t type
, idx
= PA_IDXSET_INVALID
;
1838 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
1839 (pa_tagstruct_gets(t
, &name
) < 0 ||
1840 pa_tagstruct_getu32(t
, &type
) < 0)) ||
1841 (!name
&& idx
== PA_IDXSET_INVALID
) ||
1842 (name
&& type
> 1) ||
1843 !pa_tagstruct_eof(t
)) {
1848 if (!c
->authorized
) {
1849 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1854 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
1856 r
= pa_autoload_remove_by_index(c
->protocol
->core
, idx
);
1859 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1863 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1866 static void autoload_fill_tagstruct(pa_tagstruct
*t
, const pa_autoload_entry
*e
) {
1869 pa_tagstruct_putu32(t
, e
->index
);
1870 pa_tagstruct_puts(t
, e
->name
);
1871 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
1872 pa_tagstruct_puts(t
, e
->module
);
1873 pa_tagstruct_puts(t
, e
->argument
);
1876 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
) {
1877 struct connection
*c
= userdata
;
1878 const pa_autoload_entry
*a
= NULL
;
1881 pa_tagstruct
*reply
;
1884 if ((pa_tagstruct_getu32(t
, &idx
) < 0 &&
1885 (pa_tagstruct_gets(t
, &name
) < 0 ||
1886 pa_tagstruct_getu32(t
, &type
) < 0)) ||
1887 (!name
&& idx
== PA_IDXSET_INVALID
) ||
1888 (name
&& type
> 1) ||
1889 !pa_tagstruct_eof(t
)) {
1894 if (!c
->authorized
) {
1895 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1901 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
1903 a
= pa_autoload_get_by_index(c
->protocol
->core
, idx
);
1906 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1910 reply
= pa_tagstruct_new(NULL
, 0);
1912 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1913 pa_tagstruct_putu32(reply
, tag
);
1914 autoload_fill_tagstruct(reply
, a
);
1915 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1918 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
) {
1919 struct connection
*c
= userdata
;
1920 pa_tagstruct
*reply
;
1923 if (!pa_tagstruct_eof(t
)) {
1928 if (!c
->authorized
) {
1929 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1933 reply
= pa_tagstruct_new(NULL
, 0);
1934 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1935 pa_tagstruct_putu32(reply
, tag
);
1937 if (c
->protocol
->core
->autoload_hashmap
) {
1938 pa_autoload_entry
*a
;
1941 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
1942 autoload_fill_tagstruct(reply
, a
);
1945 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1948 /*** pstream callbacks ***/
1950 static void pstream_packet_callback(pa_pstream
*p
, pa_packet
*packet
, void *userdata
) {
1951 struct connection
*c
= userdata
;
1952 assert(p
&& packet
&& packet
->data
&& c
);
1954 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
1955 pa_log(__FILE__
": invalid packet.\n");
1960 static void pstream_memblock_callback(pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const pa_memchunk
*chunk
, void *userdata
) {
1961 struct connection
*c
= userdata
;
1962 struct output_stream
*stream
;
1963 assert(p
&& chunk
&& userdata
);
1965 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
1966 pa_log(__FILE__
": client sent block for invalid stream.\n");
1971 if (stream
->type
== PLAYBACK_STREAM
) {
1972 struct playback_stream
*ps
= (struct playback_stream
*) stream
;
1973 if (chunk
->length
>= ps
->requested_bytes
)
1974 ps
->requested_bytes
= 0;
1976 ps
->requested_bytes
-= chunk
->length
;
1978 pa_memblockq_push_align(ps
->memblockq
, chunk
, delta
);
1979 assert(ps
->sink_input
);
1980 /* pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq)); */
1982 pa_sink_notify(ps
->sink_input
->sink
);
1983 /* pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1986 struct upload_stream
*u
= (struct upload_stream
*) stream
;
1988 assert(u
->type
== UPLOAD_STREAM
);
1990 if (!u
->memchunk
.memblock
) {
1991 if (u
->length
== chunk
->length
) {
1992 u
->memchunk
= *chunk
;
1993 pa_memblock_ref(u
->memchunk
.memblock
);
1996 u
->memchunk
.memblock
= pa_memblock_new(u
->length
, c
->protocol
->core
->memblock_stat
);
1997 u
->memchunk
.index
= u
->memchunk
.length
= 0;
2001 assert(u
->memchunk
.memblock
);
2004 if (l
> chunk
->length
)
2008 memcpy((uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
,
2009 (uint8_t*) chunk
->memblock
->data
+chunk
->index
, l
);
2010 u
->memchunk
.length
+= l
;
2016 static void pstream_die_callback(pa_pstream
*p
, void *userdata
) {
2017 struct connection
*c
= userdata
;
2021 /* pa_log(__FILE__": connection died.\n");*/
2025 static void pstream_drain_callback(pa_pstream
*p
, void *userdata
) {
2026 struct connection
*c
= userdata
;
2032 /*** client callbacks ***/
2034 static void client_kill_cb(pa_client
*c
) {
2035 assert(c
&& c
->userdata
);
2036 connection_free(c
->userdata
);
2039 /*** socket server callbacks ***/
2041 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
2042 struct connection
*c
= userdata
;
2043 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
2049 static void on_connection(PA_GCC_UNUSED pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
2050 pa_protocol_native
*p
= userdata
;
2051 struct connection
*c
;
2054 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
2055 pa_log_warn(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
2056 pa_iochannel_free(io
);
2060 c
= pa_xmalloc(sizeof(struct connection
));
2062 c
->authorized
=!! p
->public;
2064 if (!c
->authorized
) {
2066 pa_gettimeofday(&tv
);
2067 tv
.tv_sec
+= AUTH_TIMEOUT
;
2068 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
2070 c
->auth_timeout_event
= NULL
;
2074 c
->client
= pa_client_new(p
->core
, __FILE__
, "Client");
2076 c
->client
->kill
= client_kill_cb
;
2077 c
->client
->userdata
= c
;
2078 c
->client
->owner
= p
->module
;
2080 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->memblock_stat
);
2083 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
2084 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
2085 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
2086 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
2088 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
2089 assert(c
->pdispatch
);
2091 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
2092 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
2093 assert(c
->record_streams
&& c
->output_streams
);
2095 c
->rrobin_index
= PA_IDXSET_INVALID
;
2096 c
->subscription
= NULL
;
2098 pa_idxset_put(p
->connections
, c
, NULL
);
2101 /*** module entry points ***/
2103 static int load_key(pa_protocol_native
*p
, const char*fn
) {
2106 p
->auth_cookie_in_property
= 0;
2108 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
2109 pa_log_info(__FILE__
": using already loaded auth cookie.\n");
2110 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2111 p
->auth_cookie_in_property
= 1;
2116 fn
= PA_NATIVE_COOKIE_FILE
;
2118 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
2121 pa_log_info(__FILE__
": loading cookie from disk.\n");
2123 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
2124 p
->auth_cookie_in_property
= 1;
2129 static pa_protocol_native
* protocol_new_internal(pa_core
*c
, pa_module
*m
, pa_modargs
*ma
) {
2130 pa_protocol_native
*p
;
2134 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
2135 pa_log(__FILE__
": public= expects a boolean argument.\n");
2139 p
= pa_xmalloc(sizeof(pa_protocol_native
));
2145 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0) {
2150 p
->connections
= pa_idxset_new(NULL
, NULL
);
2151 assert(p
->connections
);
2156 pa_protocol_native
* pa_protocol_native_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
2158 pa_protocol_native
*p
;
2160 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2164 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
2166 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2168 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2169 l
= pa_strlist_prepend(l
, t
);
2170 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2176 void pa_protocol_native_free(pa_protocol_native
*p
) {
2177 struct connection
*c
;
2180 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
2182 pa_idxset_free(p
->connections
, NULL
, NULL
);
2187 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2189 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2190 l
= pa_strlist_remove(l
, t
);
2193 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2195 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2198 pa_socket_server_unref(p
->server
);
2201 if (p
->auth_cookie_in_property
)
2202 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2207 pa_protocol_native
* pa_protocol_native_new_iochannel(pa_core
*core
, pa_iochannel
*io
, pa_module
*m
, pa_modargs
*ma
) {
2208 pa_protocol_native
*p
;
2210 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2213 on_connection(NULL
, io
, p
);