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
));
1165 static void source_output_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source_output
*s
) {
1167 pa_tagstruct_putu32(t
, s
->index
);
1168 pa_tagstruct_puts(t
, s
->name
);
1169 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1170 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1171 pa_tagstruct_putu32(t
, s
->source
->index
);
1172 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1173 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
1174 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
1177 static void scache_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_scache_entry
*e
) {
1179 pa_tagstruct_putu32(t
, e
->index
);
1180 pa_tagstruct_puts(t
, e
->name
);
1181 pa_tagstruct_putu32(t
, e
->volume
);
1182 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1183 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1184 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
1185 pa_tagstruct_put_boolean(t
, e
->lazy
);
1186 pa_tagstruct_puts(t
, e
->filename
);
1189 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1190 struct connection
*c
= userdata
;
1192 struct pa_sink
*sink
= NULL
;
1193 struct pa_source
*source
= NULL
;
1194 struct pa_client
*client
= NULL
;
1195 struct pa_module
*module
= NULL
;
1196 struct pa_sink_input
*si
= NULL
;
1197 struct pa_source_output
*so
= NULL
;
1198 struct pa_scache_entry
*sce
= NULL
;
1200 struct pa_tagstruct
*reply
;
1204 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1205 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1206 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1207 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1208 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1209 pa_tagstruct_gets(t
, &name
) < 0) ||
1210 !pa_tagstruct_eof(t
)) {
1215 if (!c
->authorized
) {
1216 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1220 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1221 if (index
!= (uint32_t) -1)
1222 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1224 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1225 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1226 if (index
!= (uint32_t) -1)
1227 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, index
);
1229 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1230 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1231 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
);
1232 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1233 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
);
1234 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1235 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1236 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1237 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
);
1239 assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
1240 if (index
!= (uint32_t) -1)
1241 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, index
);
1243 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1246 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1247 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1251 reply
= pa_tagstruct_new(NULL
, 0);
1253 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1254 pa_tagstruct_putu32(reply
, tag
);
1256 sink_fill_tagstruct(reply
, sink
);
1258 source_fill_tagstruct(reply
, source
);
1260 client_fill_tagstruct(reply
, client
);
1262 module_fill_tagstruct(reply
, module
);
1264 sink_input_fill_tagstruct(reply
, si
);
1266 source_output_fill_tagstruct(reply
, so
);
1268 scache_fill_tagstruct(reply
, sce
);
1269 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1272 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1273 struct connection
*c
= userdata
;
1274 struct pa_idxset
*i
;
1277 struct pa_tagstruct
*reply
;
1280 if (!pa_tagstruct_eof(t
)) {
1285 if (!c
->authorized
) {
1286 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1290 reply
= pa_tagstruct_new(NULL
, 0);
1292 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1293 pa_tagstruct_putu32(reply
, tag
);
1295 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1296 i
= c
->protocol
->core
->sinks
;
1297 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1298 i
= c
->protocol
->core
->sources
;
1299 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1300 i
= c
->protocol
->core
->clients
;
1301 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1302 i
= c
->protocol
->core
->modules
;
1303 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1304 i
= c
->protocol
->core
->sink_inputs
;
1305 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1306 i
= c
->protocol
->core
->source_outputs
;
1308 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1309 i
= c
->protocol
->core
->scache
;
1313 for (p
= pa_idxset_first(i
, &index
); p
; p
= pa_idxset_next(i
, &index
)) {
1314 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1315 sink_fill_tagstruct(reply
, p
);
1316 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1317 source_fill_tagstruct(reply
, p
);
1318 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1319 client_fill_tagstruct(reply
, p
);
1320 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1321 module_fill_tagstruct(reply
, p
);
1322 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1323 sink_input_fill_tagstruct(reply
, p
);
1324 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1325 source_output_fill_tagstruct(reply
, p
);
1327 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1328 scache_fill_tagstruct(reply
, p
);
1333 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1336 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1337 struct connection
*c
= userdata
;
1338 struct pa_tagstruct
*reply
;
1343 if (!pa_tagstruct_eof(t
)) {
1348 if (!c
->authorized
) {
1349 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1353 reply
= pa_tagstruct_new(NULL
, 0);
1355 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1356 pa_tagstruct_putu32(reply
, tag
);
1357 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1358 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1359 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1360 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
1361 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1363 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
1364 pa_tagstruct_puts(reply
, n
);
1365 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
1366 pa_tagstruct_puts(reply
, n
);
1367 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1370 static void subscription_cb(struct pa_core
*core
, enum pa_subscription_event_type e
, uint32_t index
, void *userdata
) {
1371 struct pa_tagstruct
*t
;
1372 struct connection
*c
= userdata
;
1375 t
= pa_tagstruct_new(NULL
, 0);
1377 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
1378 pa_tagstruct_putu32(t
, (uint32_t) -1);
1379 pa_tagstruct_putu32(t
, e
);
1380 pa_tagstruct_putu32(t
, index
);
1381 pa_pstream_send_tagstruct(c
->pstream
, t
);
1384 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1385 struct connection
*c
= userdata
;
1386 enum pa_subscription_mask m
;
1389 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
1390 !pa_tagstruct_eof(t
)) {
1395 if (!c
->authorized
) {
1396 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1400 if (c
->subscription
)
1401 pa_subscription_free(c
->subscription
);
1404 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
1405 assert(c
->subscription
);
1407 c
->subscription
= NULL
;
1409 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1412 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1413 struct connection
*c
= userdata
;
1414 uint32_t index
, volume
;
1415 struct pa_sink
*sink
= NULL
;
1416 struct pa_sink_input
*si
= NULL
;
1417 const char *name
= NULL
;
1420 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1421 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1422 pa_tagstruct_getu32(t
, &volume
) ||
1423 !pa_tagstruct_eof(t
)) {
1428 if (!c
->authorized
) {
1429 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1433 if (command
== PA_COMMAND_SET_SINK_VOLUME
) {
1434 if (index
!= (uint32_t) -1)
1435 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1437 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1439 assert(command
== PA_COMMAND_SET_SINK_INPUT_VOLUME
);
1440 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1444 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1449 pa_sink_set_volume(sink
, volume
);
1451 pa_sink_input_set_volume(si
, volume
);
1453 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1456 static void command_cork_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1457 struct connection
*c
= userdata
;
1460 struct playback_stream
*s
;
1463 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1464 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1465 !pa_tagstruct_eof(t
)) {
1470 if (!c
->authorized
) {
1471 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1475 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1476 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1480 pa_sink_input_cork(s
->sink_input
, b
);
1481 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1484 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1485 struct connection
*c
= userdata
;
1487 struct playback_stream
*s
;
1490 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1491 !pa_tagstruct_eof(t
)) {
1496 if (!c
->authorized
) {
1497 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1501 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1502 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1506 if (command
== PA_COMMAND_PREBUF_PLAYBACK_STREAM
)
1507 pa_memblockq_prebuf_reenable(s
->memblockq
);
1508 else if (command
== PA_COMMAND_TRIGGER_PLAYBACK_STREAM
)
1509 pa_memblockq_prebuf_disable(s
->memblockq
);
1511 assert(command
== PA_COMMAND_FLUSH_PLAYBACK_STREAM
);
1512 pa_memblockq_flush(s
->memblockq
);
1513 /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1516 pa_sink_notify(s
->sink_input
->sink
);
1517 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1521 static void command_cork_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1522 struct connection
*c
= userdata
;
1524 struct record_stream
*s
;
1528 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1529 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1530 !pa_tagstruct_eof(t
)) {
1535 if (!c
->authorized
) {
1536 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1540 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1541 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1545 pa_source_output_cork(s
->source_output
, b
);
1546 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1549 static void command_flush_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1550 struct connection
*c
= userdata
;
1552 struct record_stream
*s
;
1555 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1556 !pa_tagstruct_eof(t
)) {
1561 if (!c
->authorized
) {
1562 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1566 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1567 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1571 pa_memblockq_flush(s
->memblockq
);
1572 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1575 static void command_set_default_sink_or_source(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1576 struct connection
*c
= userdata
;
1581 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1582 pa_tagstruct_gets(t
, &s
) < 0 || !s
||
1583 !pa_tagstruct_eof(t
)) {
1588 if (!c
->authorized
) {
1589 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1593 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
1594 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1597 static void command_set_stream_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1598 struct connection
*c
= userdata
;
1603 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1604 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1605 !pa_tagstruct_eof(t
)) {
1610 if (!c
->authorized
) {
1611 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1615 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
1616 struct playback_stream
*s
;
1618 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1619 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1623 pa_sink_input_set_name(s
->sink_input
, name
);
1626 struct record_stream
*s
;
1628 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1629 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1633 pa_source_output_set_name(s
->source_output
, name
);
1636 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1639 static void command_kill(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1640 struct connection
*c
= userdata
;
1644 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1645 !pa_tagstruct_eof(t
)) {
1650 if (!c
->authorized
) {
1651 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1655 if (command
== PA_COMMAND_KILL_CLIENT
) {
1656 struct pa_client
*client
;
1658 if (!(client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
))) {
1659 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1663 pa_client_kill(client
);
1664 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
1665 struct pa_sink_input
*s
;
1667 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
))) {
1668 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1672 pa_sink_input_kill(s
);
1674 struct pa_source_output
*s
;
1676 assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
1678 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
))) {
1679 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1683 pa_source_output_kill(s
);
1686 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1689 static void command_load_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1690 struct connection
*c
= userdata
;
1691 struct pa_module
*m
;
1692 const char *name
, *argument
;
1693 struct pa_tagstruct
*reply
;
1696 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1697 pa_tagstruct_gets(t
, &argument
) < 0 ||
1698 !pa_tagstruct_eof(t
)) {
1703 if (!c
->authorized
) {
1704 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1708 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
1709 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INITFAILED
);
1713 reply
= pa_tagstruct_new(NULL
, 0);
1714 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1715 pa_tagstruct_putu32(reply
, tag
);
1716 pa_tagstruct_putu32(reply
, m
->index
);
1717 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1720 static void command_unload_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1721 struct connection
*c
= userdata
;
1723 struct pa_module
*m
;
1726 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1727 !pa_tagstruct_eof(t
)) {
1732 if (!c
->authorized
) {
1733 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1737 if (!(m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
))) {
1738 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1742 pa_module_unload_request(m
);
1743 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1746 static void command_add_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1747 struct connection
*c
= userdata
;
1748 const char *name
, *module
, *argument
;
1751 struct pa_tagstruct
*reply
;
1754 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1755 pa_tagstruct_getu32(t
, &type
) < 0 || type
> 1 ||
1756 pa_tagstruct_gets(t
, &module
) < 0 || !module
||
1757 pa_tagstruct_gets(t
, &argument
) < 0 ||
1758 !pa_tagstruct_eof(t
)) {
1763 if (!c
->authorized
) {
1764 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1768 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &index
) < 0) {
1769 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1773 reply
= pa_tagstruct_new(NULL
, 0);
1774 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1775 pa_tagstruct_putu32(reply
, tag
);
1776 pa_tagstruct_putu32(reply
, index
);
1777 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1780 static void command_remove_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1781 struct connection
*c
= userdata
;
1782 const char *name
= NULL
;
1783 uint32_t type
, index
= PA_IDXSET_INVALID
;
1787 if ((pa_tagstruct_getu32(t
, &index
) < 0 &&
1788 (pa_tagstruct_gets(t
, &name
) < 0 ||
1789 pa_tagstruct_getu32(t
, &type
) < 0)) ||
1790 (!name
&& index
== PA_IDXSET_INVALID
) ||
1791 (name
&& type
> 1) ||
1792 !pa_tagstruct_eof(t
)) {
1797 if (!c
->authorized
) {
1798 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1803 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
1805 r
= pa_autoload_remove_by_index(c
->protocol
->core
, index
);
1808 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1812 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1815 static void autoload_fill_tagstruct(struct pa_tagstruct
*t
, const struct pa_autoload_entry
*e
) {
1818 pa_tagstruct_putu32(t
, e
->index
);
1819 pa_tagstruct_puts(t
, e
->name
);
1820 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
1821 pa_tagstruct_puts(t
, e
->module
);
1822 pa_tagstruct_puts(t
, e
->argument
);
1825 static void command_get_autoload_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1826 struct connection
*c
= userdata
;
1827 const struct pa_autoload_entry
*a
= NULL
;
1828 uint32_t type
, index
;
1830 struct pa_tagstruct
*reply
;
1833 if ((pa_tagstruct_getu32(t
, &index
) < 0 &&
1834 (pa_tagstruct_gets(t
, &name
) < 0 ||
1835 pa_tagstruct_getu32(t
, &type
) < 0)) ||
1836 (!name
&& index
== PA_IDXSET_INVALID
) ||
1837 (name
&& type
> 1) ||
1838 !pa_tagstruct_eof(t
)) {
1843 if (!c
->authorized
) {
1844 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1850 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
1852 a
= pa_autoload_get_by_index(c
->protocol
->core
, index
);
1855 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1859 reply
= pa_tagstruct_new(NULL
, 0);
1861 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1862 pa_tagstruct_putu32(reply
, tag
);
1863 autoload_fill_tagstruct(reply
, a
);
1864 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1867 static void command_get_autoload_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1868 struct connection
*c
= userdata
;
1869 struct pa_tagstruct
*reply
;
1872 if (!pa_tagstruct_eof(t
)) {
1877 if (!c
->authorized
) {
1878 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1882 reply
= pa_tagstruct_new(NULL
, 0);
1883 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1884 pa_tagstruct_putu32(reply
, tag
);
1886 if (c
->protocol
->core
->autoload_hashmap
) {
1887 struct pa_autoload_entry
*a
;
1890 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
1891 autoload_fill_tagstruct(reply
, a
);
1894 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1897 /*** pstream callbacks ***/
1899 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
1900 struct connection
*c
= userdata
;
1901 assert(p
&& packet
&& packet
->data
&& c
);
1903 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
1904 pa_log(__FILE__
": invalid packet.\n");
1909 static void pstream_memblock_callback(struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
) {
1910 struct connection
*c
= userdata
;
1911 struct output_stream
*stream
;
1912 assert(p
&& chunk
&& userdata
);
1914 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
1915 pa_log(__FILE__
": client sent block for invalid stream.\n");
1920 if (stream
->type
== PLAYBACK_STREAM
) {
1921 struct playback_stream
*p
= (struct playback_stream
*) stream
;
1922 if (chunk
->length
>= p
->requested_bytes
)
1923 p
->requested_bytes
= 0;
1925 p
->requested_bytes
-= chunk
->length
;
1927 pa_memblockq_push_align(p
->memblockq
, chunk
, delta
);
1928 assert(p
->sink_input
);
1929 /* pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq)); */
1931 pa_sink_notify(p
->sink_input
->sink
);
1932 /* pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1935 struct upload_stream
*u
= (struct upload_stream
*) stream
;
1937 assert(u
->type
== UPLOAD_STREAM
);
1939 if (!u
->memchunk
.memblock
) {
1940 if (u
->length
== chunk
->length
) {
1941 u
->memchunk
= *chunk
;
1942 pa_memblock_ref(u
->memchunk
.memblock
);
1945 u
->memchunk
.memblock
= pa_memblock_new(u
->length
, c
->protocol
->core
->memblock_stat
);
1946 u
->memchunk
.index
= u
->memchunk
.length
= 0;
1950 assert(u
->memchunk
.memblock
);
1953 if (l
> chunk
->length
)
1957 memcpy((uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
,
1958 (uint8_t*) chunk
->memblock
->data
+chunk
->index
, l
);
1959 u
->memchunk
.length
+= l
;
1965 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
1966 struct connection
*c
= userdata
;
1970 /* pa_log(__FILE__": connection died.\n");*/
1974 static void pstream_drain_callback(struct pa_pstream
*p
, void *userdata
) {
1975 struct connection
*c
= userdata
;
1981 /*** client callbacks ***/
1983 static void client_kill_cb(struct pa_client
*c
) {
1984 assert(c
&& c
->userdata
);
1985 connection_free(c
->userdata
);
1988 /*** socket server callbacks ***/
1990 static void auth_timeout(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1991 struct connection
*c
= userdata
;
1992 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
1998 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
1999 struct pa_protocol_native
*p
= userdata
;
2000 struct connection
*c
;
2003 if (pa_idxset_ncontents(p
->connections
)+1 > MAX_CONNECTIONS
) {
2004 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
2005 pa_iochannel_free(io
);
2009 c
= pa_xmalloc(sizeof(struct connection
));
2011 c
->authorized
=!! p
->public;
2013 if (!c
->authorized
) {
2015 gettimeofday(&tv
, NULL
);
2016 tv
.tv_sec
+= AUTH_TIMEOUT
;
2017 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
2019 c
->auth_timeout_event
= NULL
;
2023 c
->client
= pa_client_new(p
->core
, "NATIVE", "Client");
2025 c
->client
->kill
= client_kill_cb
;
2026 c
->client
->userdata
= c
;
2027 c
->client
->owner
= p
->module
;
2029 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->memblock_stat
);
2032 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
2033 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
2034 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
2035 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
2037 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
2038 assert(c
->pdispatch
);
2040 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
2041 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
2042 assert(c
->record_streams
&& c
->output_streams
);
2044 c
->rrobin_index
= PA_IDXSET_INVALID
;
2045 c
->subscription
= NULL
;
2047 pa_idxset_put(p
->connections
, c
, NULL
);
2050 /*** module entry points ***/
2052 static int load_key(struct pa_protocol_native
*p
, const char*fn
) {
2055 p
->auth_cookie_in_property
= 0;
2057 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
2058 pa_log(__FILE__
": using already loaded auth cookie.\n");
2059 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2060 p
->auth_cookie_in_property
= 1;
2065 fn
= PA_NATIVE_COOKIE_FILE
;
2067 if (pa_authkey_load_from_home(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
2070 pa_log(__FILE__
": loading cookie from disk.\n");
2072 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
2073 p
->auth_cookie_in_property
= 1;
2078 static struct pa_protocol_native
* protocol_new_internal(struct pa_core
*c
, struct pa_module
*m
, struct pa_modargs
*ma
) {
2079 struct pa_protocol_native
*p
;
2083 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
2084 pa_log(__FILE__
": public= expects a boolean argument.\n");
2088 p
= pa_xmalloc(sizeof(struct pa_protocol_native
));
2094 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0) {
2099 p
->connections
= pa_idxset_new(NULL
, NULL
);
2100 assert(p
->connections
);
2105 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
) {
2107 struct pa_protocol_native
*p
;
2109 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2113 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
2115 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2116 struct pa_strlist
*l
;
2117 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2118 l
= pa_strlist_prepend(l
, t
);
2119 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2125 void pa_protocol_native_free(struct pa_protocol_native
*p
) {
2126 struct connection
*c
;
2129 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
2131 pa_idxset_free(p
->connections
, NULL
, NULL
);
2136 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2137 struct pa_strlist
*l
;
2138 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2139 l
= pa_strlist_remove(l
, t
);
2142 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2144 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2147 pa_socket_server_unref(p
->server
);
2150 if (p
->auth_cookie_in_property
)
2151 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2156 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
) {
2157 struct pa_protocol_native
*p
;
2159 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2162 on_connection(NULL
, io
, p
);