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 struct pa_source_output
*source_output
;
66 struct pa_memblockq
*memblockq
;
70 struct playback_stream
{
72 struct connection
*connection
;
74 struct pa_sink_input
*sink_input
;
75 struct pa_memblockq
*memblockq
;
76 size_t requested_bytes
;
81 struct upload_stream
{
83 struct connection
*connection
;
85 struct pa_memchunk memchunk
;
88 struct pa_sample_spec sample_spec
;
91 struct output_stream
{
102 struct pa_protocol_native
*protocol
;
103 struct pa_client
*client
;
104 struct pa_pstream
*pstream
;
105 struct pa_pdispatch
*pdispatch
;
106 struct pa_idxset
*record_streams
, *output_streams
;
107 uint32_t rrobin_index
;
108 struct pa_subscription
*subscription
;
109 struct pa_time_event
*auth_timeout_event
;
112 struct pa_protocol_native
{
113 struct pa_module
*module
;
115 struct pa_core
*core
;
116 struct pa_socket_server
*server
;
117 struct pa_idxset
*connections
;
118 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
119 int auth_cookie_in_property
;
122 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
123 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
);
124 static void sink_input_kill_cb(struct pa_sink_input
*i
);
125 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
);
127 static void request_bytes(struct playback_stream
*s
);
129 static void source_output_kill_cb(struct pa_source_output
*o
);
130 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
131 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
);
133 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
134 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
135 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
136 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
137 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
138 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
139 static void command_set_client_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
140 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
141 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
142 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
143 static void command_get_record_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
144 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
145 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
146 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
147 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
148 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
149 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
150 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
151 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
152 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
153 static void command_cork_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
154 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
155 static void command_set_default_sink_or_source(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
156 static void command_set_stream_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
157 static void command_kill(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
158 static void command_load_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
159 static void command_unload_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
160 static void command_add_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
161 static void command_remove_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
162 static void command_get_autoload_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
163 static void command_get_autoload_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
164 static void command_cork_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
165 static void command_flush_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
167 static const struct pa_pdispatch_command command_table
[PA_COMMAND_MAX
] = {
168 [PA_COMMAND_ERROR
] = { NULL
},
169 [PA_COMMAND_TIMEOUT
] = { NULL
},
170 [PA_COMMAND_REPLY
] = { NULL
},
171 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = { command_create_playback_stream
},
172 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = { command_delete_stream
},
173 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = { command_drain_playback_stream
},
174 [PA_COMMAND_CREATE_RECORD_STREAM
] = { command_create_record_stream
},
175 [PA_COMMAND_DELETE_RECORD_STREAM
] = { command_delete_stream
},
176 [PA_COMMAND_AUTH
] = { command_auth
},
177 [PA_COMMAND_REQUEST
] = { NULL
},
178 [PA_COMMAND_EXIT
] = { command_exit
},
179 [PA_COMMAND_SET_CLIENT_NAME
] = { command_set_client_name
},
180 [PA_COMMAND_LOOKUP_SINK
] = { command_lookup
},
181 [PA_COMMAND_LOOKUP_SOURCE
] = { command_lookup
},
182 [PA_COMMAND_STAT
] = { command_stat
},
183 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = { command_get_playback_latency
},
184 [PA_COMMAND_GET_RECORD_LATENCY
] = { command_get_record_latency
},
185 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = { command_create_upload_stream
},
186 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = { command_delete_stream
},
187 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = { command_finish_upload_stream
},
188 [PA_COMMAND_PLAY_SAMPLE
] = { command_play_sample
},
189 [PA_COMMAND_REMOVE_SAMPLE
] = { command_remove_sample
},
190 [PA_COMMAND_GET_SINK_INFO
] = { command_get_info
},
191 [PA_COMMAND_GET_SOURCE_INFO
] = { command_get_info
},
192 [PA_COMMAND_GET_CLIENT_INFO
] = { command_get_info
},
193 [PA_COMMAND_GET_MODULE_INFO
] = { command_get_info
},
194 [PA_COMMAND_GET_SINK_INPUT_INFO
] = { command_get_info
},
195 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = { command_get_info
},
196 [PA_COMMAND_GET_SAMPLE_INFO
] = { command_get_info
},
197 [PA_COMMAND_GET_SINK_INFO_LIST
] = { command_get_info_list
},
198 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = { command_get_info_list
},
199 [PA_COMMAND_GET_MODULE_INFO_LIST
] = { command_get_info_list
},
200 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = { command_get_info_list
},
201 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = { command_get_info_list
},
202 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = { command_get_info_list
},
203 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = { command_get_info_list
},
204 [PA_COMMAND_GET_SERVER_INFO
] = { command_get_server_info
},
205 [PA_COMMAND_SUBSCRIBE
] = { command_subscribe
},
207 [PA_COMMAND_SET_SINK_VOLUME
] = { command_set_volume
},
208 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = { command_set_volume
},
210 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = { command_cork_playback_stream
},
211 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
212 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
213 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
215 [PA_COMMAND_CORK_RECORD_STREAM
] = { command_cork_record_stream
},
216 [PA_COMMAND_FLUSH_RECORD_STREAM
] = { command_flush_record_stream
},
218 [PA_COMMAND_SET_DEFAULT_SINK
] = { command_set_default_sink_or_source
},
219 [PA_COMMAND_SET_DEFAULT_SOURCE
] = { command_set_default_sink_or_source
},
220 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = { command_set_stream_name
},
221 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = { command_set_stream_name
},
222 [PA_COMMAND_KILL_CLIENT
] = { command_kill
},
223 [PA_COMMAND_KILL_SINK_INPUT
] = { command_kill
},
224 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = { command_kill
},
225 [PA_COMMAND_LOAD_MODULE
] = { command_load_module
},
226 [PA_COMMAND_UNLOAD_MODULE
] = { command_unload_module
},
227 [PA_COMMAND_GET_AUTOLOAD_INFO
] = { command_get_autoload_info
},
228 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = { command_get_autoload_info_list
},
229 [PA_COMMAND_ADD_AUTOLOAD
] = { command_add_autoload
},
230 [PA_COMMAND_REMOVE_AUTOLOAD
] = { command_remove_autoload
},
234 /* structure management */
236 static struct upload_stream
* upload_stream_new(struct connection
*c
, const struct pa_sample_spec
*ss
, const char *name
, size_t length
) {
237 struct upload_stream
*s
;
238 assert(c
&& ss
&& name
&& length
);
240 s
= pa_xmalloc(sizeof(struct upload_stream
));
241 s
->type
= UPLOAD_STREAM
;
243 s
->sample_spec
= *ss
;
244 s
->name
= pa_xstrdup(name
);
246 s
->memchunk
.memblock
= NULL
;
247 s
->memchunk
.index
= 0;
248 s
->memchunk
.length
= 0;
252 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
256 static void upload_stream_free(struct upload_stream
*o
) {
257 assert(o
&& o
->connection
);
259 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
263 if (o
->memchunk
.memblock
)
264 pa_memblock_unref(o
->memchunk
.memblock
);
269 static struct record_stream
* record_stream_new(struct connection
*c
, struct pa_source
*source
, const struct pa_sample_spec
*ss
, const char *name
, size_t maxlength
, size_t fragment_size
) {
270 struct record_stream
*s
;
271 struct pa_source_output
*source_output
;
273 assert(c
&& source
&& ss
&& name
&& maxlength
);
275 if (!(source_output
= pa_source_output_new(source
, name
, ss
, -1)))
278 s
= pa_xmalloc(sizeof(struct record_stream
));
280 s
->source_output
= source_output
;
281 s
->source_output
->push
= source_output_push_cb
;
282 s
->source_output
->kill
= source_output_kill_cb
;
283 s
->source_output
->get_latency
= source_output_get_latency_cb
;
284 s
->source_output
->userdata
= s
;
285 s
->source_output
->owner
= c
->protocol
->module
;
286 s
->source_output
->client
= c
->client
;
288 s
->memblockq
= pa_memblockq_new(maxlength
, 0, base
= pa_frame_size(ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
289 assert(s
->memblockq
);
291 s
->fragment_size
= (fragment_size
/base
)*base
;
292 if (!s
->fragment_size
)
293 s
->fragment_size
= base
;
295 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
299 static void record_stream_free(struct record_stream
* r
) {
300 assert(r
&& r
->connection
);
302 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
303 pa_source_output_disconnect(r
->source_output
);
304 pa_source_output_unref(r
->source_output
);
305 pa_memblockq_free(r
->memblockq
);
309 static struct playback_stream
* playback_stream_new(struct connection
*c
, struct pa_sink
*sink
, const struct pa_sample_spec
*ss
, const char *name
,
314 pa_volume_t volume
) {
315 struct playback_stream
*s
;
316 struct pa_sink_input
*sink_input
;
317 assert(c
&& sink
&& ss
&& name
&& maxlength
);
319 if (!(sink_input
= pa_sink_input_new(sink
, name
, ss
, 0, -1)))
322 s
= pa_xmalloc(sizeof(struct playback_stream
));
323 s
->type
= PLAYBACK_STREAM
;
325 s
->sink_input
= sink_input
;
327 s
->sink_input
->peek
= sink_input_peek_cb
;
328 s
->sink_input
->drop
= sink_input_drop_cb
;
329 s
->sink_input
->kill
= sink_input_kill_cb
;
330 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
331 s
->sink_input
->userdata
= s
;
332 s
->sink_input
->owner
= c
->protocol
->module
;
333 s
->sink_input
->client
= c
->client
;
335 s
->memblockq
= pa_memblockq_new(maxlength
, tlength
, pa_frame_size(ss
), prebuf
, minreq
, c
->protocol
->core
->memblock_stat
);
336 assert(s
->memblockq
);
338 s
->requested_bytes
= 0;
339 s
->drain_request
= 0;
341 s
->sink_input
->volume
= volume
;
343 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
347 static void playback_stream_free(struct playback_stream
* p
) {
348 assert(p
&& p
->connection
);
350 if (p
->drain_request
)
351 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERROR_NOENTITY
);
353 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
354 pa_sink_input_disconnect(p
->sink_input
);
355 pa_sink_input_unref(p
->sink_input
);
356 pa_memblockq_free(p
->memblockq
);
360 static void connection_free(struct connection
*c
) {
361 struct record_stream
*r
;
362 struct output_stream
*o
;
363 assert(c
&& c
->protocol
);
365 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
366 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
367 record_stream_free(r
);
368 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
370 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
371 if (o
->type
== PLAYBACK_STREAM
)
372 playback_stream_free((struct playback_stream
*) o
);
374 upload_stream_free((struct upload_stream
*) o
);
375 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
377 pa_pdispatch_unref(c
->pdispatch
);
378 pa_pstream_close(c
->pstream
);
379 pa_pstream_unref(c
->pstream
);
380 pa_client_free(c
->client
);
383 pa_subscription_free(c
->subscription
);
385 if (c
->auth_timeout_event
)
386 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
391 static void request_bytes(struct playback_stream
*s
) {
392 struct pa_tagstruct
*t
;
396 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
399 if (l
<= s
->requested_bytes
)
402 l
-= s
->requested_bytes
;
404 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
407 s
->requested_bytes
+= l
;
409 t
= pa_tagstruct_new(NULL
, 0);
411 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
412 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
413 pa_tagstruct_putu32(t
, s
->index
);
414 pa_tagstruct_putu32(t
, l
);
415 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
417 /* pa_log(__FILE__": Requesting %u bytes\n", l); */
420 static void send_memblock(struct connection
*c
) {
422 struct record_stream
*r
;
424 start
= PA_IDXSET_INVALID
;
426 struct pa_memchunk chunk
;
428 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
431 if (start
== PA_IDXSET_INVALID
)
432 start
= c
->rrobin_index
;
433 else if (start
== c
->rrobin_index
)
436 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
437 struct pa_memchunk schunk
= chunk
;
439 if (schunk
.length
> r
->fragment_size
)
440 schunk
.length
= r
->fragment_size
;
442 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, &schunk
);
443 pa_memblockq_drop(r
->memblockq
, &chunk
, schunk
.length
);
444 pa_memblock_unref(schunk
.memblock
);
451 static void send_playback_stream_killed(struct playback_stream
*p
) {
452 struct pa_tagstruct
*t
;
455 t
= pa_tagstruct_new(NULL
, 0);
457 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
458 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
459 pa_tagstruct_putu32(t
, p
->index
);
460 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
463 static void send_record_stream_killed(struct record_stream
*r
) {
464 struct pa_tagstruct
*t
;
467 t
= pa_tagstruct_new(NULL
, 0);
469 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
470 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
471 pa_tagstruct_putu32(t
, r
->index
);
472 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
475 /*** sinkinput callbacks ***/
477 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
478 struct playback_stream
*s
;
479 assert(i
&& i
->userdata
&& chunk
);
482 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
488 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
489 struct playback_stream
*s
;
490 assert(i
&& i
->userdata
&& length
);
493 pa_memblockq_drop(s
->memblockq
, chunk
, length
);
496 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
497 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
498 s
->drain_request
= 0;
501 /* pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq)); */
504 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
505 assert(i
&& i
->userdata
);
506 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
507 playback_stream_free((struct playback_stream
*) i
->userdata
);
510 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
511 struct playback_stream
*s
;
512 assert(i
&& i
->userdata
);
515 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
517 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
520 /*** source_output callbacks ***/
522 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
523 struct record_stream
*s
;
524 assert(o
&& o
->userdata
&& chunk
);
527 pa_memblockq_push_align(s
->memblockq
, chunk
, 0);
528 if (!pa_pstream_is_pending(s
->connection
->pstream
))
529 send_memblock(s
->connection
);
532 static void source_output_kill_cb(struct pa_source_output
*o
) {
533 assert(o
&& o
->userdata
);
534 send_record_stream_killed((struct record_stream
*) o
->userdata
);
535 record_stream_free((struct record_stream
*) o
->userdata
);
538 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
) {
539 struct record_stream
*s
;
540 assert(o
&& o
->userdata
);
543 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
545 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
548 /*** pdispatch callbacks ***/
550 static void protocol_error(struct connection
*c
) {
551 pa_log(__FILE__
": protocol error, kicking client\n");
555 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
556 struct connection
*c
= userdata
;
557 struct playback_stream
*s
;
558 size_t maxlength
, tlength
, prebuf
, minreq
;
560 const char *name
, *sink_name
;
561 struct pa_sample_spec ss
;
562 struct pa_tagstruct
*reply
;
563 struct pa_sink
*sink
;
566 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
568 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
569 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
570 pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
571 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
572 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
573 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
574 pa_tagstruct_getu32(t
, &tlength
) < 0 ||
575 pa_tagstruct_getu32(t
, &prebuf
) < 0 ||
576 pa_tagstruct_getu32(t
, &minreq
) < 0 ||
577 pa_tagstruct_getu32(t
, &volume
) < 0 ||
578 !pa_tagstruct_eof(t
)) {
584 if (!c
->authorized
) {
585 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
589 if (sink_index
!= (uint32_t) -1)
590 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
592 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
595 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
599 if (!(s
= playback_stream_new(c
, sink
, &ss
, name
, maxlength
, tlength
, prebuf
, minreq
, volume
))) {
600 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
604 pa_sink_input_cork(s
->sink_input
, corked
);
606 reply
= pa_tagstruct_new(NULL
, 0);
608 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
609 pa_tagstruct_putu32(reply
, tag
);
610 pa_tagstruct_putu32(reply
, s
->index
);
611 assert(s
->sink_input
);
612 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
613 pa_tagstruct_putu32(reply
, s
->requested_bytes
= pa_memblockq_missing(s
->memblockq
));
614 pa_pstream_send_tagstruct(c
->pstream
, reply
);
618 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
619 struct connection
*c
= userdata
;
623 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
624 !pa_tagstruct_eof(t
)) {
629 if (!c
->authorized
) {
630 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
634 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
635 struct playback_stream
*s
;
636 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
637 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
641 playback_stream_free(s
);
642 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
643 struct record_stream
*s
;
644 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
645 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
649 record_stream_free(s
);
651 struct upload_stream
*s
;
652 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
653 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
654 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
658 upload_stream_free(s
);
661 pa_pstream_send_simple_ack(c
->pstream
, tag
);
664 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
665 struct connection
*c
= userdata
;
666 struct record_stream
*s
;
667 size_t maxlength
, fragment_size
;
668 uint32_t source_index
;
669 const char *name
, *source_name
;
670 struct pa_sample_spec ss
;
671 struct pa_tagstruct
*reply
;
672 struct pa_source
*source
;
674 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
676 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
677 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
678 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
679 pa_tagstruct_gets(t
, &source_name
) < 0 ||
680 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
681 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
682 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
683 !pa_tagstruct_eof(t
)) {
688 if (!c
->authorized
) {
689 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
693 if (source_index
!= (uint32_t) -1)
694 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
696 source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1);
699 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
703 if (!(s
= record_stream_new(c
, source
, &ss
, name
, maxlength
, fragment_size
))) {
704 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
708 pa_source_output_cork(s
->source_output
, corked
);
710 reply
= pa_tagstruct_new(NULL
, 0);
712 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
713 pa_tagstruct_putu32(reply
, tag
);
714 pa_tagstruct_putu32(reply
, s
->index
);
715 assert(s
->source_output
);
716 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
717 pa_pstream_send_tagstruct(c
->pstream
, reply
);
720 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
721 struct connection
*c
= userdata
;
724 if (!pa_tagstruct_eof(t
)) {
729 if (!c
->authorized
) {
730 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
734 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
735 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
736 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
740 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
741 struct connection
*c
= userdata
;
745 if (pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
746 !pa_tagstruct_eof(t
)) {
751 if (!c
->authorized
) {
752 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) != 0) {
753 pa_log(__FILE__
": Denied access to client with invalid authorization key.\n");
754 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
759 if (c
->auth_timeout_event
) {
760 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
761 c
->auth_timeout_event
= NULL
;
765 pa_pstream_send_simple_ack(c
->pstream
, tag
);
769 static void command_set_client_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
770 struct connection
*c
= userdata
;
774 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
775 !pa_tagstruct_eof(t
)) {
780 pa_client_set_name(c
->client
, name
);
781 pa_pstream_send_simple_ack(c
->pstream
, tag
);
785 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
786 struct connection
*c
= userdata
;
788 uint32_t index
= PA_IDXSET_INVALID
;
791 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
792 !pa_tagstruct_eof(t
)) {
797 if (!c
->authorized
) {
798 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
802 if (command
== PA_COMMAND_LOOKUP_SINK
) {
803 struct pa_sink
*sink
;
804 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
807 struct pa_source
*source
;
808 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
809 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
810 index
= source
->index
;
813 if (index
== PA_IDXSET_INVALID
)
814 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
816 struct pa_tagstruct
*reply
;
817 reply
= pa_tagstruct_new(NULL
, 0);
819 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
820 pa_tagstruct_putu32(reply
, tag
);
821 pa_tagstruct_putu32(reply
, index
);
822 pa_pstream_send_tagstruct(c
->pstream
, reply
);
826 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
827 struct connection
*c
= userdata
;
829 struct playback_stream
*s
;
832 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
833 !pa_tagstruct_eof(t
)) {
838 if (!c
->authorized
) {
839 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
843 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
844 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
848 s
->drain_request
= 0;
850 pa_memblockq_prebuf_disable(s
->memblockq
);
852 if (!pa_memblockq_is_readable(s
->memblockq
)) {
853 /* pa_log("immediate drain: %u\n", pa_memblockq_get_length(s->memblockq)); */
854 pa_pstream_send_simple_ack(c
->pstream
, tag
);
856 /* pa_log("slow drain triggered\n"); */
857 s
->drain_request
= 1;
860 pa_sink_notify(s
->sink_input
->sink
);
864 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
865 struct connection
*c
= userdata
;
866 struct pa_tagstruct
*reply
;
869 if (!pa_tagstruct_eof(t
)) {
874 if (!c
->authorized
) {
875 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
879 reply
= pa_tagstruct_new(NULL
, 0);
881 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
882 pa_tagstruct_putu32(reply
, tag
);
883 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total
);
884 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total_size
);
885 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated
);
886 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated_size
);
887 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
888 pa_pstream_send_tagstruct(c
->pstream
, reply
);
891 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
892 struct connection
*c
= userdata
;
893 struct pa_tagstruct
*reply
;
894 struct playback_stream
*s
;
895 struct timeval tv
, now
;
900 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
901 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
902 pa_tagstruct_getu64(t
, &counter
) < 0 ||
903 !pa_tagstruct_eof(t
)) {
908 if (!c
->authorized
) {
909 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
913 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
914 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
918 reply
= pa_tagstruct_new(NULL
, 0);
920 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
921 pa_tagstruct_putu32(reply
, tag
);
922 pa_tagstruct_put_usec(reply
, pa_sink_input_get_latency(s
->sink_input
));
923 pa_tagstruct_put_usec(reply
, pa_sink_get_latency(s
->sink_input
->sink
));
924 pa_tagstruct_put_usec(reply
, 0);
925 pa_tagstruct_put_boolean(reply
, pa_memblockq_is_readable(s
->memblockq
));
926 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
927 pa_tagstruct_put_timeval(reply
, &tv
);
928 gettimeofday(&now
, NULL
);
929 pa_tagstruct_put_timeval(reply
, &now
);
930 pa_tagstruct_putu64(reply
, counter
);
931 pa_pstream_send_tagstruct(c
->pstream
, reply
);
934 static void command_get_record_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
935 struct connection
*c
= userdata
;
936 struct pa_tagstruct
*reply
;
937 struct record_stream
*s
;
938 struct timeval tv
, now
;
943 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
944 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
945 pa_tagstruct_getu64(t
, &counter
) < 0 ||
946 !pa_tagstruct_eof(t
)) {
951 if (!c
->authorized
) {
952 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
956 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
957 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
961 reply
= pa_tagstruct_new(NULL
, 0);
963 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
964 pa_tagstruct_putu32(reply
, tag
);
965 pa_tagstruct_put_usec(reply
, pa_source_output_get_latency(s
->source_output
));
966 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
967 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
968 pa_tagstruct_put_boolean(reply
, 0);
969 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
970 pa_tagstruct_put_timeval(reply
, &tv
);
971 gettimeofday(&now
, NULL
);
972 pa_tagstruct_put_timeval(reply
, &now
);
973 pa_tagstruct_putu64(reply
, counter
);
974 pa_pstream_send_tagstruct(c
->pstream
, reply
);
978 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
979 struct connection
*c
= userdata
;
980 struct upload_stream
*s
;
983 struct pa_sample_spec ss
;
984 struct pa_tagstruct
*reply
;
985 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
987 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
988 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
989 pa_tagstruct_getu32(t
, &length
) < 0 ||
990 !pa_tagstruct_eof(t
)) {
995 if (!c
->authorized
) {
996 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1000 if ((length
% pa_frame_size(&ss
)) != 0 || length
<= 0 || !*name
) {
1001 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
1005 if (!(s
= upload_stream_new(c
, &ss
, name
, length
))) {
1006 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
1010 reply
= pa_tagstruct_new(NULL
, 0);
1012 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1013 pa_tagstruct_putu32(reply
, tag
);
1014 pa_tagstruct_putu32(reply
, s
->index
);
1015 pa_tagstruct_putu32(reply
, length
);
1016 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1019 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1020 struct connection
*c
= userdata
;
1022 struct upload_stream
*s
;
1026 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1027 !pa_tagstruct_eof(t
)) {
1032 if (!c
->authorized
) {
1033 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1037 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
1038 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1042 pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->memchunk
, &index
);
1043 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1044 upload_stream_free(s
);
1047 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1048 struct connection
*c
= userdata
;
1049 uint32_t sink_index
, volume
;
1050 struct pa_sink
*sink
;
1051 const char *name
, *sink_name
;
1054 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1055 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1056 pa_tagstruct_getu32(t
, &volume
) < 0 ||
1057 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1058 !pa_tagstruct_eof(t
)) {
1063 if (!c
->authorized
) {
1064 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1068 if (sink_index
!= (uint32_t) -1)
1069 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1071 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1074 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1078 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
1079 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1083 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1086 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1087 struct connection
*c
= userdata
;
1091 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1092 !pa_tagstruct_eof(t
)) {
1097 if (!c
->authorized
) {
1098 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1102 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
1103 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1107 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1110 static void sink_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink
*sink
) {
1112 pa_tagstruct_putu32(t
, sink
->index
);
1113 pa_tagstruct_puts(t
, sink
->name
);
1114 pa_tagstruct_puts(t
, sink
->description
);
1115 pa_tagstruct_put_sample_spec(t
, &sink
->sample_spec
);
1116 pa_tagstruct_putu32(t
, sink
->owner
? sink
->owner
->index
: (uint32_t) -1);
1117 pa_tagstruct_putu32(t
, sink
->volume
);
1118 pa_tagstruct_putu32(t
, sink
->monitor_source
->index
);
1119 pa_tagstruct_puts(t
, sink
->monitor_source
->name
);
1120 pa_tagstruct_put_usec(t
, pa_sink_get_latency(sink
));
1123 static void source_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source
*source
) {
1124 assert(t
&& source
);
1125 pa_tagstruct_putu32(t
, source
->index
);
1126 pa_tagstruct_puts(t
, source
->name
);
1127 pa_tagstruct_puts(t
, source
->description
);
1128 pa_tagstruct_put_sample_spec(t
, &source
->sample_spec
);
1129 pa_tagstruct_putu32(t
, source
->owner
? source
->owner
->index
: (uint32_t) -1);
1130 pa_tagstruct_putu32(t
, source
->monitor_of
? source
->monitor_of
->index
: (uint32_t) -1);
1131 pa_tagstruct_puts(t
, source
->monitor_of
? source
->monitor_of
->name
: NULL
);
1132 pa_tagstruct_put_usec(t
, pa_source_get_latency(source
));
1135 static void client_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_client
*client
) {
1136 assert(t
&& client
);
1137 pa_tagstruct_putu32(t
, client
->index
);
1138 pa_tagstruct_puts(t
, client
->name
);
1139 pa_tagstruct_puts(t
, client
->protocol_name
);
1140 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: (uint32_t) -1);
1143 static void module_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_module
*module
) {
1144 assert(t
&& module
);
1145 pa_tagstruct_putu32(t
, module
->index
);
1146 pa_tagstruct_puts(t
, module
->name
);
1147 pa_tagstruct_puts(t
, module
->argument
);
1148 pa_tagstruct_putu32(t
, module
->n_used
);
1149 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
1152 static void sink_input_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink_input
*s
) {
1154 pa_tagstruct_putu32(t
, s
->index
);
1155 pa_tagstruct_puts(t
, s
->name
);
1156 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1157 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1158 pa_tagstruct_putu32(t
, s
->sink
->index
);
1159 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1160 pa_tagstruct_putu32(t
, s
->volume
);
1161 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
1162 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
1163 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
1166 static void source_output_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source_output
*s
) {
1168 pa_tagstruct_putu32(t
, s
->index
);
1169 pa_tagstruct_puts(t
, s
->name
);
1170 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1171 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1172 pa_tagstruct_putu32(t
, s
->source
->index
);
1173 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1174 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
1175 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
1176 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
1179 static void scache_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_scache_entry
*e
) {
1181 pa_tagstruct_putu32(t
, e
->index
);
1182 pa_tagstruct_puts(t
, e
->name
);
1183 pa_tagstruct_putu32(t
, e
->volume
);
1184 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1185 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1186 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
1187 pa_tagstruct_put_boolean(t
, e
->lazy
);
1188 pa_tagstruct_puts(t
, e
->filename
);
1191 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1192 struct connection
*c
= userdata
;
1194 struct pa_sink
*sink
= NULL
;
1195 struct pa_source
*source
= NULL
;
1196 struct pa_client
*client
= NULL
;
1197 struct pa_module
*module
= NULL
;
1198 struct pa_sink_input
*si
= NULL
;
1199 struct pa_source_output
*so
= NULL
;
1200 struct pa_scache_entry
*sce
= NULL
;
1202 struct pa_tagstruct
*reply
;
1206 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1207 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1208 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1209 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1210 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1211 pa_tagstruct_gets(t
, &name
) < 0) ||
1212 !pa_tagstruct_eof(t
)) {
1217 if (!c
->authorized
) {
1218 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1222 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1223 if (index
!= (uint32_t) -1)
1224 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1226 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1227 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1228 if (index
!= (uint32_t) -1)
1229 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, index
);
1231 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1232 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1233 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
);
1234 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1235 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
);
1236 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1237 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1238 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1239 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
);
1241 assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
1242 if (index
!= (uint32_t) -1)
1243 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, index
);
1245 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1248 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1249 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1253 reply
= pa_tagstruct_new(NULL
, 0);
1255 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1256 pa_tagstruct_putu32(reply
, tag
);
1258 sink_fill_tagstruct(reply
, sink
);
1260 source_fill_tagstruct(reply
, source
);
1262 client_fill_tagstruct(reply
, client
);
1264 module_fill_tagstruct(reply
, module
);
1266 sink_input_fill_tagstruct(reply
, si
);
1268 source_output_fill_tagstruct(reply
, so
);
1270 scache_fill_tagstruct(reply
, sce
);
1271 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1274 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1275 struct connection
*c
= userdata
;
1276 struct pa_idxset
*i
;
1279 struct pa_tagstruct
*reply
;
1282 if (!pa_tagstruct_eof(t
)) {
1287 if (!c
->authorized
) {
1288 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1292 reply
= pa_tagstruct_new(NULL
, 0);
1294 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1295 pa_tagstruct_putu32(reply
, tag
);
1297 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1298 i
= c
->protocol
->core
->sinks
;
1299 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1300 i
= c
->protocol
->core
->sources
;
1301 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1302 i
= c
->protocol
->core
->clients
;
1303 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1304 i
= c
->protocol
->core
->modules
;
1305 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1306 i
= c
->protocol
->core
->sink_inputs
;
1307 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1308 i
= c
->protocol
->core
->source_outputs
;
1310 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1311 i
= c
->protocol
->core
->scache
;
1315 for (p
= pa_idxset_first(i
, &index
); p
; p
= pa_idxset_next(i
, &index
)) {
1316 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1317 sink_fill_tagstruct(reply
, p
);
1318 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1319 source_fill_tagstruct(reply
, p
);
1320 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1321 client_fill_tagstruct(reply
, p
);
1322 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1323 module_fill_tagstruct(reply
, p
);
1324 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1325 sink_input_fill_tagstruct(reply
, p
);
1326 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1327 source_output_fill_tagstruct(reply
, p
);
1329 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1330 scache_fill_tagstruct(reply
, p
);
1335 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1338 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1339 struct connection
*c
= userdata
;
1340 struct pa_tagstruct
*reply
;
1345 if (!pa_tagstruct_eof(t
)) {
1350 if (!c
->authorized
) {
1351 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1355 reply
= pa_tagstruct_new(NULL
, 0);
1357 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1358 pa_tagstruct_putu32(reply
, tag
);
1359 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1360 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1361 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1362 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
1363 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1365 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
1366 pa_tagstruct_puts(reply
, n
);
1367 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
1368 pa_tagstruct_puts(reply
, n
);
1369 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1372 static void subscription_cb(struct pa_core
*core
, enum pa_subscription_event_type e
, uint32_t index
, void *userdata
) {
1373 struct pa_tagstruct
*t
;
1374 struct connection
*c
= userdata
;
1377 t
= pa_tagstruct_new(NULL
, 0);
1379 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
1380 pa_tagstruct_putu32(t
, (uint32_t) -1);
1381 pa_tagstruct_putu32(t
, e
);
1382 pa_tagstruct_putu32(t
, index
);
1383 pa_pstream_send_tagstruct(c
->pstream
, t
);
1386 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1387 struct connection
*c
= userdata
;
1388 enum pa_subscription_mask m
;
1391 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
1392 !pa_tagstruct_eof(t
)) {
1397 if (!c
->authorized
) {
1398 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1402 if (c
->subscription
)
1403 pa_subscription_free(c
->subscription
);
1406 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
1407 assert(c
->subscription
);
1409 c
->subscription
= NULL
;
1411 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1414 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1415 struct connection
*c
= userdata
;
1416 uint32_t index
, volume
;
1417 struct pa_sink
*sink
= NULL
;
1418 struct pa_sink_input
*si
= NULL
;
1419 const char *name
= NULL
;
1422 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1423 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1424 pa_tagstruct_getu32(t
, &volume
) ||
1425 !pa_tagstruct_eof(t
)) {
1430 if (!c
->authorized
) {
1431 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1435 if (command
== PA_COMMAND_SET_SINK_VOLUME
) {
1436 if (index
!= (uint32_t) -1)
1437 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1439 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1441 assert(command
== PA_COMMAND_SET_SINK_INPUT_VOLUME
);
1442 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1446 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1451 pa_sink_set_volume(sink
, volume
);
1453 pa_sink_input_set_volume(si
, volume
);
1455 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1458 static void command_cork_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1459 struct connection
*c
= userdata
;
1462 struct playback_stream
*s
;
1465 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1466 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1467 !pa_tagstruct_eof(t
)) {
1472 if (!c
->authorized
) {
1473 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1477 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1478 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1482 pa_sink_input_cork(s
->sink_input
, b
);
1483 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1486 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1487 struct connection
*c
= userdata
;
1489 struct playback_stream
*s
;
1492 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1493 !pa_tagstruct_eof(t
)) {
1498 if (!c
->authorized
) {
1499 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1503 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1504 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1508 if (command
== PA_COMMAND_PREBUF_PLAYBACK_STREAM
)
1509 pa_memblockq_prebuf_reenable(s
->memblockq
);
1510 else if (command
== PA_COMMAND_TRIGGER_PLAYBACK_STREAM
)
1511 pa_memblockq_prebuf_disable(s
->memblockq
);
1513 assert(command
== PA_COMMAND_FLUSH_PLAYBACK_STREAM
);
1514 pa_memblockq_flush(s
->memblockq
);
1515 /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1518 pa_sink_notify(s
->sink_input
->sink
);
1519 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1523 static void command_cork_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1524 struct connection
*c
= userdata
;
1526 struct record_stream
*s
;
1530 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1531 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1532 !pa_tagstruct_eof(t
)) {
1537 if (!c
->authorized
) {
1538 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1542 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1543 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1547 pa_source_output_cork(s
->source_output
, b
);
1548 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1551 static void command_flush_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1552 struct connection
*c
= userdata
;
1554 struct record_stream
*s
;
1557 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1558 !pa_tagstruct_eof(t
)) {
1563 if (!c
->authorized
) {
1564 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1568 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1569 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1573 pa_memblockq_flush(s
->memblockq
);
1574 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1577 static void command_set_default_sink_or_source(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1578 struct connection
*c
= userdata
;
1583 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1584 pa_tagstruct_gets(t
, &s
) < 0 || !s
||
1585 !pa_tagstruct_eof(t
)) {
1590 if (!c
->authorized
) {
1591 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1595 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
1596 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1599 static void command_set_stream_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1600 struct connection
*c
= userdata
;
1605 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1606 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1607 !pa_tagstruct_eof(t
)) {
1612 if (!c
->authorized
) {
1613 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1617 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
1618 struct playback_stream
*s
;
1620 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1621 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1625 pa_sink_input_set_name(s
->sink_input
, name
);
1628 struct record_stream
*s
;
1630 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1631 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1635 pa_source_output_set_name(s
->source_output
, name
);
1638 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1641 static void command_kill(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1642 struct connection
*c
= userdata
;
1646 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1647 !pa_tagstruct_eof(t
)) {
1652 if (!c
->authorized
) {
1653 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1657 if (command
== PA_COMMAND_KILL_CLIENT
) {
1658 struct pa_client
*client
;
1660 if (!(client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
))) {
1661 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1665 pa_client_kill(client
);
1666 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
1667 struct pa_sink_input
*s
;
1669 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
))) {
1670 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1674 pa_sink_input_kill(s
);
1676 struct pa_source_output
*s
;
1678 assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
1680 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
))) {
1681 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1685 pa_source_output_kill(s
);
1688 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1691 static void command_load_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1692 struct connection
*c
= userdata
;
1693 struct pa_module
*m
;
1694 const char *name
, *argument
;
1695 struct pa_tagstruct
*reply
;
1698 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1699 pa_tagstruct_gets(t
, &argument
) < 0 ||
1700 !pa_tagstruct_eof(t
)) {
1705 if (!c
->authorized
) {
1706 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1710 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
1711 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INITFAILED
);
1715 reply
= pa_tagstruct_new(NULL
, 0);
1716 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1717 pa_tagstruct_putu32(reply
, tag
);
1718 pa_tagstruct_putu32(reply
, m
->index
);
1719 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1722 static void command_unload_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1723 struct connection
*c
= userdata
;
1725 struct pa_module
*m
;
1728 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1729 !pa_tagstruct_eof(t
)) {
1734 if (!c
->authorized
) {
1735 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1739 if (!(m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
))) {
1740 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1744 pa_module_unload_request(m
);
1745 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1748 static void command_add_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1749 struct connection
*c
= userdata
;
1750 const char *name
, *module
, *argument
;
1753 struct pa_tagstruct
*reply
;
1756 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1757 pa_tagstruct_getu32(t
, &type
) < 0 || type
> 1 ||
1758 pa_tagstruct_gets(t
, &module
) < 0 || !module
||
1759 pa_tagstruct_gets(t
, &argument
) < 0 ||
1760 !pa_tagstruct_eof(t
)) {
1765 if (!c
->authorized
) {
1766 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1770 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &index
) < 0) {
1771 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1775 reply
= pa_tagstruct_new(NULL
, 0);
1776 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1777 pa_tagstruct_putu32(reply
, tag
);
1778 pa_tagstruct_putu32(reply
, index
);
1779 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1782 static void command_remove_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1783 struct connection
*c
= userdata
;
1784 const char *name
= NULL
;
1785 uint32_t type
, index
= PA_IDXSET_INVALID
;
1789 if ((pa_tagstruct_getu32(t
, &index
) < 0 &&
1790 (pa_tagstruct_gets(t
, &name
) < 0 ||
1791 pa_tagstruct_getu32(t
, &type
) < 0)) ||
1792 (!name
&& index
== PA_IDXSET_INVALID
) ||
1793 (name
&& type
> 1) ||
1794 !pa_tagstruct_eof(t
)) {
1799 if (!c
->authorized
) {
1800 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1805 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
1807 r
= pa_autoload_remove_by_index(c
->protocol
->core
, index
);
1810 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1814 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1817 static void autoload_fill_tagstruct(struct pa_tagstruct
*t
, const struct pa_autoload_entry
*e
) {
1820 pa_tagstruct_putu32(t
, e
->index
);
1821 pa_tagstruct_puts(t
, e
->name
);
1822 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
1823 pa_tagstruct_puts(t
, e
->module
);
1824 pa_tagstruct_puts(t
, e
->argument
);
1827 static void command_get_autoload_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1828 struct connection
*c
= userdata
;
1829 const struct pa_autoload_entry
*a
= NULL
;
1830 uint32_t type
, index
;
1832 struct pa_tagstruct
*reply
;
1835 if ((pa_tagstruct_getu32(t
, &index
) < 0 &&
1836 (pa_tagstruct_gets(t
, &name
) < 0 ||
1837 pa_tagstruct_getu32(t
, &type
) < 0)) ||
1838 (!name
&& index
== PA_IDXSET_INVALID
) ||
1839 (name
&& type
> 1) ||
1840 !pa_tagstruct_eof(t
)) {
1845 if (!c
->authorized
) {
1846 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1852 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
1854 a
= pa_autoload_get_by_index(c
->protocol
->core
, index
);
1857 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1861 reply
= pa_tagstruct_new(NULL
, 0);
1863 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1864 pa_tagstruct_putu32(reply
, tag
);
1865 autoload_fill_tagstruct(reply
, a
);
1866 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1869 static void command_get_autoload_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1870 struct connection
*c
= userdata
;
1871 struct pa_tagstruct
*reply
;
1874 if (!pa_tagstruct_eof(t
)) {
1879 if (!c
->authorized
) {
1880 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1884 reply
= pa_tagstruct_new(NULL
, 0);
1885 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1886 pa_tagstruct_putu32(reply
, tag
);
1888 if (c
->protocol
->core
->autoload_hashmap
) {
1889 struct pa_autoload_entry
*a
;
1892 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
1893 autoload_fill_tagstruct(reply
, a
);
1896 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1899 /*** pstream callbacks ***/
1901 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
1902 struct connection
*c
= userdata
;
1903 assert(p
&& packet
&& packet
->data
&& c
);
1905 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
1906 pa_log(__FILE__
": invalid packet.\n");
1911 static void pstream_memblock_callback(struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
) {
1912 struct connection
*c
= userdata
;
1913 struct output_stream
*stream
;
1914 assert(p
&& chunk
&& userdata
);
1916 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
1917 pa_log(__FILE__
": client sent block for invalid stream.\n");
1922 if (stream
->type
== PLAYBACK_STREAM
) {
1923 struct playback_stream
*p
= (struct playback_stream
*) stream
;
1924 if (chunk
->length
>= p
->requested_bytes
)
1925 p
->requested_bytes
= 0;
1927 p
->requested_bytes
-= chunk
->length
;
1929 pa_memblockq_push_align(p
->memblockq
, chunk
, delta
);
1930 assert(p
->sink_input
);
1931 /* pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq)); */
1933 pa_sink_notify(p
->sink_input
->sink
);
1934 /* pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1937 struct upload_stream
*u
= (struct upload_stream
*) stream
;
1939 assert(u
->type
== UPLOAD_STREAM
);
1941 if (!u
->memchunk
.memblock
) {
1942 if (u
->length
== chunk
->length
) {
1943 u
->memchunk
= *chunk
;
1944 pa_memblock_ref(u
->memchunk
.memblock
);
1947 u
->memchunk
.memblock
= pa_memblock_new(u
->length
, c
->protocol
->core
->memblock_stat
);
1948 u
->memchunk
.index
= u
->memchunk
.length
= 0;
1952 assert(u
->memchunk
.memblock
);
1955 if (l
> chunk
->length
)
1959 memcpy((uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
,
1960 (uint8_t*) chunk
->memblock
->data
+chunk
->index
, l
);
1961 u
->memchunk
.length
+= l
;
1967 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
1968 struct connection
*c
= userdata
;
1972 /* pa_log(__FILE__": connection died.\n");*/
1976 static void pstream_drain_callback(struct pa_pstream
*p
, void *userdata
) {
1977 struct connection
*c
= userdata
;
1983 /*** client callbacks ***/
1985 static void client_kill_cb(struct pa_client
*c
) {
1986 assert(c
&& c
->userdata
);
1987 connection_free(c
->userdata
);
1990 /*** socket server callbacks ***/
1992 static void auth_timeout(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1993 struct connection
*c
= userdata
;
1994 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
2000 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
2001 struct pa_protocol_native
*p
= userdata
;
2002 struct connection
*c
;
2005 if (pa_idxset_ncontents(p
->connections
)+1 > MAX_CONNECTIONS
) {
2006 pa_log_warn(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
2007 pa_iochannel_free(io
);
2011 c
= pa_xmalloc(sizeof(struct connection
));
2013 c
->authorized
=!! p
->public;
2015 if (!c
->authorized
) {
2017 gettimeofday(&tv
, NULL
);
2018 tv
.tv_sec
+= AUTH_TIMEOUT
;
2019 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
2021 c
->auth_timeout_event
= NULL
;
2025 c
->client
= pa_client_new(p
->core
, "NATIVE", "Client");
2027 c
->client
->kill
= client_kill_cb
;
2028 c
->client
->userdata
= c
;
2029 c
->client
->owner
= p
->module
;
2031 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->memblock_stat
);
2034 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
2035 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
2036 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
2037 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
2039 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
2040 assert(c
->pdispatch
);
2042 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
2043 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
2044 assert(c
->record_streams
&& c
->output_streams
);
2046 c
->rrobin_index
= PA_IDXSET_INVALID
;
2047 c
->subscription
= NULL
;
2049 pa_idxset_put(p
->connections
, c
, NULL
);
2052 /*** module entry points ***/
2054 static int load_key(struct pa_protocol_native
*p
, const char*fn
) {
2057 p
->auth_cookie_in_property
= 0;
2059 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
2060 pa_log_info(__FILE__
": using already loaded auth cookie.\n");
2061 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2062 p
->auth_cookie_in_property
= 1;
2067 fn
= PA_NATIVE_COOKIE_FILE
;
2069 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
2072 pa_log_info(__FILE__
": loading cookie from disk.\n");
2074 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
2075 p
->auth_cookie_in_property
= 1;
2080 static struct pa_protocol_native
* protocol_new_internal(struct pa_core
*c
, struct pa_module
*m
, struct pa_modargs
*ma
) {
2081 struct pa_protocol_native
*p
;
2085 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
2086 pa_log(__FILE__
": public= expects a boolean argument.\n");
2090 p
= pa_xmalloc(sizeof(struct pa_protocol_native
));
2096 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0) {
2101 p
->connections
= pa_idxset_new(NULL
, NULL
);
2102 assert(p
->connections
);
2107 struct pa_protocol_native
* pa_protocol_native_new(struct pa_core
*core
, struct pa_socket_server
*server
, struct pa_module
*m
, struct pa_modargs
*ma
) {
2109 struct pa_protocol_native
*p
;
2111 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2115 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
2117 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2118 struct pa_strlist
*l
;
2119 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2120 l
= pa_strlist_prepend(l
, t
);
2121 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2127 void pa_protocol_native_free(struct pa_protocol_native
*p
) {
2128 struct connection
*c
;
2131 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
2133 pa_idxset_free(p
->connections
, NULL
, NULL
);
2138 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2139 struct pa_strlist
*l
;
2140 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2141 l
= pa_strlist_remove(l
, t
);
2144 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2146 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2149 pa_socket_server_unref(p
->server
);
2152 if (p
->auth_cookie_in_property
)
2153 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2158 struct pa_protocol_native
* pa_protocol_native_new_iochannel(struct pa_core
*core
, struct pa_iochannel
*io
, struct pa_module
*m
, struct pa_modargs
*ma
) {
2159 struct pa_protocol_native
*p
;
2161 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2164 on_connection(NULL
, io
, p
);