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 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 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"
51 struct pa_protocol_native
;
53 struct record_stream
{
54 struct connection
*connection
;
56 struct pa_source_output
*source_output
;
57 struct pa_memblockq
*memblockq
;
61 struct playback_stream
{
63 struct connection
*connection
;
65 struct pa_sink_input
*sink_input
;
66 struct pa_memblockq
*memblockq
;
67 size_t requested_bytes
;
72 struct upload_stream
{
74 struct connection
*connection
;
76 struct pa_memchunk memchunk
;
79 struct pa_sample_spec sample_spec
;
82 struct output_stream
{
93 struct pa_protocol_native
*protocol
;
94 struct pa_client
*client
;
95 struct pa_pstream
*pstream
;
96 struct pa_pdispatch
*pdispatch
;
97 struct pa_idxset
*record_streams
, *output_streams
;
98 uint32_t rrobin_index
;
99 struct pa_subscription
*subscription
;
102 struct pa_protocol_native
{
103 struct pa_module
*module
;
105 struct pa_core
*core
;
106 struct pa_socket_server
*server
;
107 struct pa_idxset
*connections
;
108 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
111 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
112 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
);
113 static void sink_input_kill_cb(struct pa_sink_input
*i
);
114 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
);
116 static void request_bytes(struct playback_stream
*s
);
118 static void source_output_kill_cb(struct pa_source_output
*o
);
119 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
120 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
);
122 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
123 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
124 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
125 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
126 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
127 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
128 static void command_set_client_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
129 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
130 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
131 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
132 static void command_get_record_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
133 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
134 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
135 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
136 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
137 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
138 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
139 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
140 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
141 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
142 static void command_cork_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
143 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
144 static void command_set_default_sink_or_source(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
145 static void command_set_stream_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
146 static void command_kill(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
147 static void command_load_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
148 static void command_unload_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
149 static void command_add_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
150 static void command_remove_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
151 static void command_get_autoload_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
152 static void command_get_autoload_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
153 static void command_cork_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
154 static void command_flush_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
156 static const struct pa_pdispatch_command command_table
[PA_COMMAND_MAX
] = {
157 [PA_COMMAND_ERROR
] = { NULL
},
158 [PA_COMMAND_TIMEOUT
] = { NULL
},
159 [PA_COMMAND_REPLY
] = { NULL
},
160 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = { command_create_playback_stream
},
161 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = { command_delete_stream
},
162 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = { command_drain_playback_stream
},
163 [PA_COMMAND_CREATE_RECORD_STREAM
] = { command_create_record_stream
},
164 [PA_COMMAND_DELETE_RECORD_STREAM
] = { command_delete_stream
},
165 [PA_COMMAND_AUTH
] = { command_auth
},
166 [PA_COMMAND_REQUEST
] = { NULL
},
167 [PA_COMMAND_EXIT
] = { command_exit
},
168 [PA_COMMAND_SET_CLIENT_NAME
] = { command_set_client_name
},
169 [PA_COMMAND_LOOKUP_SINK
] = { command_lookup
},
170 [PA_COMMAND_LOOKUP_SOURCE
] = { command_lookup
},
171 [PA_COMMAND_STAT
] = { command_stat
},
172 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = { command_get_playback_latency
},
173 [PA_COMMAND_GET_RECORD_LATENCY
] = { command_get_record_latency
},
174 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = { command_create_upload_stream
},
175 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = { command_delete_stream
},
176 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = { command_finish_upload_stream
},
177 [PA_COMMAND_PLAY_SAMPLE
] = { command_play_sample
},
178 [PA_COMMAND_REMOVE_SAMPLE
] = { command_remove_sample
},
179 [PA_COMMAND_GET_SINK_INFO
] = { command_get_info
},
180 [PA_COMMAND_GET_SOURCE_INFO
] = { command_get_info
},
181 [PA_COMMAND_GET_CLIENT_INFO
] = { command_get_info
},
182 [PA_COMMAND_GET_MODULE_INFO
] = { command_get_info
},
183 [PA_COMMAND_GET_SINK_INPUT_INFO
] = { command_get_info
},
184 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = { command_get_info
},
185 [PA_COMMAND_GET_SAMPLE_INFO
] = { command_get_info
},
186 [PA_COMMAND_GET_SINK_INFO_LIST
] = { command_get_info_list
},
187 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = { command_get_info_list
},
188 [PA_COMMAND_GET_MODULE_INFO_LIST
] = { command_get_info_list
},
189 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = { command_get_info_list
},
190 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = { command_get_info_list
},
191 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = { command_get_info_list
},
192 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = { command_get_info_list
},
193 [PA_COMMAND_GET_SERVER_INFO
] = { command_get_server_info
},
194 [PA_COMMAND_SUBSCRIBE
] = { command_subscribe
},
196 [PA_COMMAND_SET_SINK_VOLUME
] = { command_set_volume
},
197 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = { command_set_volume
},
199 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = { command_cork_playback_stream
},
200 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
201 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
202 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
204 [PA_COMMAND_CORK_RECORD_STREAM
] = { command_cork_record_stream
},
205 [PA_COMMAND_FLUSH_RECORD_STREAM
] = { command_flush_record_stream
},
207 [PA_COMMAND_SET_DEFAULT_SINK
] = { command_set_default_sink_or_source
},
208 [PA_COMMAND_SET_DEFAULT_SOURCE
] = { command_set_default_sink_or_source
},
209 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = { command_set_stream_name
},
210 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = { command_set_stream_name
},
211 [PA_COMMAND_KILL_CLIENT
] = { command_kill
},
212 [PA_COMMAND_KILL_SINK_INPUT
] = { command_kill
},
213 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = { command_kill
},
214 [PA_COMMAND_LOAD_MODULE
] = { command_load_module
},
215 [PA_COMMAND_UNLOAD_MODULE
] = { command_unload_module
},
216 [PA_COMMAND_GET_AUTOLOAD_INFO
] = { command_get_autoload_info
},
217 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = { command_get_autoload_info_list
},
218 [PA_COMMAND_ADD_AUTOLOAD
] = { command_add_autoload
},
219 [PA_COMMAND_REMOVE_AUTOLOAD
] = { command_remove_autoload
},
223 /* structure management */
225 static struct upload_stream
* upload_stream_new(struct connection
*c
, const struct pa_sample_spec
*ss
, const char *name
, size_t length
) {
226 struct upload_stream
*s
;
227 assert(c
&& ss
&& name
&& length
);
229 s
= pa_xmalloc(sizeof(struct upload_stream
));
230 s
->type
= UPLOAD_STREAM
;
232 s
->sample_spec
= *ss
;
233 s
->name
= pa_xstrdup(name
);
235 s
->memchunk
.memblock
= NULL
;
236 s
->memchunk
.index
= 0;
237 s
->memchunk
.length
= 0;
241 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
245 static void upload_stream_free(struct upload_stream
*o
) {
246 assert(o
&& o
->connection
);
248 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
252 if (o
->memchunk
.memblock
)
253 pa_memblock_unref(o
->memchunk
.memblock
);
258 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
) {
259 struct record_stream
*s
;
260 struct pa_source_output
*source_output
;
262 assert(c
&& source
&& ss
&& name
&& maxlength
);
264 if (!(source_output
= pa_source_output_new(source
, name
, ss
, -1)))
267 s
= pa_xmalloc(sizeof(struct record_stream
));
269 s
->source_output
= source_output
;
270 s
->source_output
->push
= source_output_push_cb
;
271 s
->source_output
->kill
= source_output_kill_cb
;
272 s
->source_output
->get_latency
= source_output_get_latency_cb
;
273 s
->source_output
->userdata
= s
;
274 s
->source_output
->owner
= c
->protocol
->module
;
275 s
->source_output
->client
= c
->client
;
277 s
->memblockq
= pa_memblockq_new(maxlength
, 0, base
= pa_frame_size(ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
278 assert(s
->memblockq
);
280 s
->fragment_size
= (fragment_size
/base
)*base
;
281 if (!s
->fragment_size
)
282 s
->fragment_size
= base
;
284 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
288 static void record_stream_free(struct record_stream
* r
) {
289 assert(r
&& r
->connection
);
291 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
292 pa_source_output_disconnect(r
->source_output
);
293 pa_source_output_unref(r
->source_output
);
294 pa_memblockq_free(r
->memblockq
);
298 static struct playback_stream
* playback_stream_new(struct connection
*c
, struct pa_sink
*sink
, const struct pa_sample_spec
*ss
, const char *name
,
303 pa_volume_t volume
) {
304 struct playback_stream
*s
;
305 struct pa_sink_input
*sink_input
;
306 assert(c
&& sink
&& ss
&& name
&& maxlength
);
308 if (!(sink_input
= pa_sink_input_new(sink
, name
, ss
, 0, -1)))
311 s
= pa_xmalloc(sizeof(struct playback_stream
));
312 s
->type
= PLAYBACK_STREAM
;
314 s
->sink_input
= sink_input
;
316 s
->sink_input
->peek
= sink_input_peek_cb
;
317 s
->sink_input
->drop
= sink_input_drop_cb
;
318 s
->sink_input
->kill
= sink_input_kill_cb
;
319 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
320 s
->sink_input
->userdata
= s
;
321 s
->sink_input
->owner
= c
->protocol
->module
;
322 s
->sink_input
->client
= c
->client
;
324 s
->memblockq
= pa_memblockq_new(maxlength
, tlength
, pa_frame_size(ss
), prebuf
, minreq
, c
->protocol
->core
->memblock_stat
);
325 assert(s
->memblockq
);
327 s
->requested_bytes
= 0;
328 s
->drain_request
= 0;
330 s
->sink_input
->volume
= volume
;
332 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
336 static void playback_stream_free(struct playback_stream
* p
) {
337 assert(p
&& p
->connection
);
339 if (p
->drain_request
)
340 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERROR_NOENTITY
);
342 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
343 pa_sink_input_disconnect(p
->sink_input
);
344 pa_sink_input_unref(p
->sink_input
);
345 pa_memblockq_free(p
->memblockq
);
349 static void connection_free(struct connection
*c
) {
350 struct record_stream
*r
;
351 struct output_stream
*o
;
352 assert(c
&& c
->protocol
);
354 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
355 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
356 record_stream_free(r
);
357 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
359 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
360 if (o
->type
== PLAYBACK_STREAM
)
361 playback_stream_free((struct playback_stream
*) o
);
363 upload_stream_free((struct upload_stream
*) o
);
364 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
366 pa_pdispatch_unref(c
->pdispatch
);
367 pa_pstream_close(c
->pstream
);
368 pa_pstream_unref(c
->pstream
);
369 pa_client_free(c
->client
);
372 pa_subscription_free(c
->subscription
);
377 static void request_bytes(struct playback_stream
*s
) {
378 struct pa_tagstruct
*t
;
382 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
385 if (l
<= s
->requested_bytes
)
388 l
-= s
->requested_bytes
;
390 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
393 s
->requested_bytes
+= l
;
395 t
= pa_tagstruct_new(NULL
, 0);
397 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
398 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
399 pa_tagstruct_putu32(t
, s
->index
);
400 pa_tagstruct_putu32(t
, l
);
401 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
403 /* pa_log(__FILE__": Requesting %u bytes\n", l); */
406 static void send_memblock(struct connection
*c
) {
408 struct record_stream
*r
;
410 start
= PA_IDXSET_INVALID
;
412 struct pa_memchunk chunk
;
414 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
417 if (start
== PA_IDXSET_INVALID
)
418 start
= c
->rrobin_index
;
419 else if (start
== c
->rrobin_index
)
422 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
423 struct pa_memchunk schunk
= chunk
;
425 if (schunk
.length
> r
->fragment_size
)
426 schunk
.length
= r
->fragment_size
;
428 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, &schunk
);
429 pa_memblockq_drop(r
->memblockq
, &chunk
, schunk
.length
);
430 pa_memblock_unref(schunk
.memblock
);
437 static void send_playback_stream_killed(struct playback_stream
*p
) {
438 struct pa_tagstruct
*t
;
441 t
= pa_tagstruct_new(NULL
, 0);
443 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
444 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
445 pa_tagstruct_putu32(t
, p
->index
);
446 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
449 static void send_record_stream_killed(struct record_stream
*r
) {
450 struct pa_tagstruct
*t
;
453 t
= pa_tagstruct_new(NULL
, 0);
455 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
456 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
457 pa_tagstruct_putu32(t
, r
->index
);
458 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
461 /*** sinkinput callbacks ***/
463 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
464 struct playback_stream
*s
;
465 assert(i
&& i
->userdata
&& chunk
);
468 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
474 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
475 struct playback_stream
*s
;
476 assert(i
&& i
->userdata
&& length
);
479 pa_memblockq_drop(s
->memblockq
, chunk
, length
);
482 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
483 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
484 s
->drain_request
= 0;
487 /* pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq)); */
490 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
491 assert(i
&& i
->userdata
);
492 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
493 playback_stream_free((struct playback_stream
*) i
->userdata
);
496 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
497 struct playback_stream
*s
;
498 assert(i
&& i
->userdata
);
501 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
503 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
506 /*** source_output callbacks ***/
508 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
509 struct record_stream
*s
;
510 assert(o
&& o
->userdata
&& chunk
);
513 pa_memblockq_push_align(s
->memblockq
, chunk
, 0);
514 if (!pa_pstream_is_pending(s
->connection
->pstream
))
515 send_memblock(s
->connection
);
518 static void source_output_kill_cb(struct pa_source_output
*o
) {
519 assert(o
&& o
->userdata
);
520 send_record_stream_killed((struct record_stream
*) o
->userdata
);
521 record_stream_free((struct record_stream
*) o
->userdata
);
524 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
) {
525 struct record_stream
*s
;
526 assert(o
&& o
->userdata
);
529 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
531 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
534 /*** pdispatch callbacks ***/
536 static void protocol_error(struct connection
*c
) {
537 pa_log(__FILE__
": protocol error, kicking client\n");
541 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
542 struct connection
*c
= userdata
;
543 struct playback_stream
*s
;
544 size_t maxlength
, tlength
, prebuf
, minreq
;
546 const char *name
, *sink_name
;
547 struct pa_sample_spec ss
;
548 struct pa_tagstruct
*reply
;
549 struct pa_sink
*sink
;
552 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
554 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
555 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
556 pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
557 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
558 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
559 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
560 pa_tagstruct_getu32(t
, &tlength
) < 0 ||
561 pa_tagstruct_getu32(t
, &prebuf
) < 0 ||
562 pa_tagstruct_getu32(t
, &minreq
) < 0 ||
563 pa_tagstruct_getu32(t
, &volume
) < 0 ||
564 !pa_tagstruct_eof(t
)) {
570 if (!c
->authorized
) {
571 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
575 if (sink_index
!= (uint32_t) -1)
576 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
578 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
581 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
585 if (!(s
= playback_stream_new(c
, sink
, &ss
, name
, maxlength
, tlength
, prebuf
, minreq
, volume
))) {
586 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
590 pa_sink_input_cork(s
->sink_input
, corked
);
592 reply
= pa_tagstruct_new(NULL
, 0);
594 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
595 pa_tagstruct_putu32(reply
, tag
);
596 pa_tagstruct_putu32(reply
, s
->index
);
597 assert(s
->sink_input
);
598 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
599 pa_tagstruct_putu32(reply
, s
->requested_bytes
= pa_memblockq_missing(s
->memblockq
));
600 pa_pstream_send_tagstruct(c
->pstream
, reply
);
604 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
605 struct connection
*c
= userdata
;
609 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
610 !pa_tagstruct_eof(t
)) {
615 if (!c
->authorized
) {
616 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
620 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
621 struct playback_stream
*s
;
622 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
623 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
627 playback_stream_free(s
);
628 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
629 struct record_stream
*s
;
630 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
631 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
635 record_stream_free(s
);
637 struct upload_stream
*s
;
638 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
639 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
640 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
644 upload_stream_free(s
);
647 pa_pstream_send_simple_ack(c
->pstream
, tag
);
650 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
651 struct connection
*c
= userdata
;
652 struct record_stream
*s
;
653 size_t maxlength
, fragment_size
;
654 uint32_t source_index
;
655 const char *name
, *source_name
;
656 struct pa_sample_spec ss
;
657 struct pa_tagstruct
*reply
;
658 struct pa_source
*source
;
660 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
662 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
663 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
664 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
665 pa_tagstruct_gets(t
, &source_name
) < 0 ||
666 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
667 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
668 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
669 !pa_tagstruct_eof(t
)) {
674 if (!c
->authorized
) {
675 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
679 if (source_index
!= (uint32_t) -1)
680 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
682 source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1);
685 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
689 if (!(s
= record_stream_new(c
, source
, &ss
, name
, maxlength
, fragment_size
))) {
690 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
694 pa_source_output_cork(s
->source_output
, corked
);
696 reply
= pa_tagstruct_new(NULL
, 0);
698 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
699 pa_tagstruct_putu32(reply
, tag
);
700 pa_tagstruct_putu32(reply
, s
->index
);
701 assert(s
->source_output
);
702 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
703 pa_pstream_send_tagstruct(c
->pstream
, reply
);
706 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
707 struct connection
*c
= userdata
;
710 if (!pa_tagstruct_eof(t
)) {
715 if (!c
->authorized
) {
716 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
720 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
721 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
722 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
726 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
727 struct connection
*c
= userdata
;
731 if (pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
732 !pa_tagstruct_eof(t
)) {
737 if (!c
->authorized
) {
738 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) != 0) {
739 pa_log(__FILE__
": Denied access to client with invalid authorization key.\n");
740 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
747 pa_pstream_send_simple_ack(c
->pstream
, tag
);
751 static void command_set_client_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
752 struct connection
*c
= userdata
;
756 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
757 !pa_tagstruct_eof(t
)) {
762 pa_client_set_name(c
->client
, name
);
763 pa_pstream_send_simple_ack(c
->pstream
, tag
);
767 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
768 struct connection
*c
= userdata
;
770 uint32_t index
= PA_IDXSET_INVALID
;
773 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
774 !pa_tagstruct_eof(t
)) {
779 if (!c
->authorized
) {
780 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
784 if (command
== PA_COMMAND_LOOKUP_SINK
) {
785 struct pa_sink
*sink
;
786 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
789 struct pa_source
*source
;
790 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
791 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
792 index
= source
->index
;
795 if (index
== PA_IDXSET_INVALID
)
796 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
798 struct pa_tagstruct
*reply
;
799 reply
= pa_tagstruct_new(NULL
, 0);
801 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
802 pa_tagstruct_putu32(reply
, tag
);
803 pa_tagstruct_putu32(reply
, index
);
804 pa_pstream_send_tagstruct(c
->pstream
, reply
);
808 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
809 struct connection
*c
= userdata
;
811 struct playback_stream
*s
;
814 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
815 !pa_tagstruct_eof(t
)) {
820 if (!c
->authorized
) {
821 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
825 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
826 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
830 s
->drain_request
= 0;
832 pa_memblockq_prebuf_disable(s
->memblockq
);
834 if (!pa_memblockq_is_readable(s
->memblockq
)) {
835 /* pa_log("immediate drain: %u\n", pa_memblockq_get_length(s->memblockq)); */
836 pa_pstream_send_simple_ack(c
->pstream
, tag
);
838 /* pa_log("slow drain triggered\n"); */
839 s
->drain_request
= 1;
842 pa_sink_notify(s
->sink_input
->sink
);
846 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
847 struct connection
*c
= userdata
;
848 struct pa_tagstruct
*reply
;
851 if (!pa_tagstruct_eof(t
)) {
856 if (!c
->authorized
) {
857 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
861 reply
= pa_tagstruct_new(NULL
, 0);
863 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
864 pa_tagstruct_putu32(reply
, tag
);
865 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total
);
866 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total_size
);
867 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated
);
868 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated_size
);
869 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
870 pa_pstream_send_tagstruct(c
->pstream
, reply
);
873 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
874 struct connection
*c
= userdata
;
875 struct pa_tagstruct
*reply
;
876 struct playback_stream
*s
;
877 struct timeval tv
, now
;
882 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
883 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
884 pa_tagstruct_getu64(t
, &counter
) < 0 ||
885 !pa_tagstruct_eof(t
)) {
890 if (!c
->authorized
) {
891 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
895 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
896 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
900 reply
= pa_tagstruct_new(NULL
, 0);
902 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
903 pa_tagstruct_putu32(reply
, tag
);
904 pa_tagstruct_put_usec(reply
, pa_sink_input_get_latency(s
->sink_input
));
905 pa_tagstruct_put_usec(reply
, pa_sink_get_latency(s
->sink_input
->sink
));
906 pa_tagstruct_put_usec(reply
, 0);
907 pa_tagstruct_put_boolean(reply
, pa_memblockq_is_readable(s
->memblockq
));
908 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
909 pa_tagstruct_put_timeval(reply
, &tv
);
910 gettimeofday(&now
, NULL
);
911 pa_tagstruct_put_timeval(reply
, &now
);
912 pa_tagstruct_putu64(reply
, counter
);
913 pa_pstream_send_tagstruct(c
->pstream
, reply
);
916 static void command_get_record_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
917 struct connection
*c
= userdata
;
918 struct pa_tagstruct
*reply
;
919 struct record_stream
*s
;
920 struct timeval tv
, now
;
925 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
926 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
927 pa_tagstruct_getu64(t
, &counter
) < 0 ||
928 !pa_tagstruct_eof(t
)) {
933 if (!c
->authorized
) {
934 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
938 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
939 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
943 reply
= pa_tagstruct_new(NULL
, 0);
945 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
946 pa_tagstruct_putu32(reply
, tag
);
947 pa_tagstruct_put_usec(reply
, pa_source_output_get_latency(s
->source_output
));
948 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
949 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
950 pa_tagstruct_put_boolean(reply
, 0);
951 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
952 pa_tagstruct_put_timeval(reply
, &tv
);
953 gettimeofday(&now
, NULL
);
954 pa_tagstruct_put_timeval(reply
, &now
);
955 pa_tagstruct_putu64(reply
, counter
);
956 pa_pstream_send_tagstruct(c
->pstream
, reply
);
960 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
961 struct connection
*c
= userdata
;
962 struct upload_stream
*s
;
965 struct pa_sample_spec ss
;
966 struct pa_tagstruct
*reply
;
967 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
969 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
970 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
971 pa_tagstruct_getu32(t
, &length
) < 0 ||
972 !pa_tagstruct_eof(t
)) {
977 if (!c
->authorized
) {
978 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
982 if ((length
% pa_frame_size(&ss
)) != 0 || length
<= 0 || !*name
) {
983 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
987 if (!(s
= upload_stream_new(c
, &ss
, name
, length
))) {
988 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
992 reply
= pa_tagstruct_new(NULL
, 0);
994 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
995 pa_tagstruct_putu32(reply
, tag
);
996 pa_tagstruct_putu32(reply
, s
->index
);
997 pa_tagstruct_putu32(reply
, length
);
998 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1001 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1002 struct connection
*c
= userdata
;
1004 struct upload_stream
*s
;
1008 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1009 !pa_tagstruct_eof(t
)) {
1014 if (!c
->authorized
) {
1015 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1019 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
1020 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1024 pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->memchunk
, &index
);
1025 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1026 upload_stream_free(s
);
1029 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1030 struct connection
*c
= userdata
;
1031 uint32_t sink_index
, volume
;
1032 struct pa_sink
*sink
;
1033 const char *name
, *sink_name
;
1036 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1037 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1038 pa_tagstruct_getu32(t
, &volume
) < 0 ||
1039 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1040 !pa_tagstruct_eof(t
)) {
1045 if (!c
->authorized
) {
1046 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1050 if (sink_index
!= (uint32_t) -1)
1051 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1053 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1056 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1060 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
1061 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1065 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1068 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1069 struct connection
*c
= userdata
;
1073 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1074 !pa_tagstruct_eof(t
)) {
1079 if (!c
->authorized
) {
1080 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1084 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
1085 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1089 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1092 static void sink_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink
*sink
) {
1094 pa_tagstruct_putu32(t
, sink
->index
);
1095 pa_tagstruct_puts(t
, sink
->name
);
1096 pa_tagstruct_puts(t
, sink
->description
);
1097 pa_tagstruct_put_sample_spec(t
, &sink
->sample_spec
);
1098 pa_tagstruct_putu32(t
, sink
->owner
? sink
->owner
->index
: (uint32_t) -1);
1099 pa_tagstruct_putu32(t
, sink
->volume
);
1100 pa_tagstruct_putu32(t
, sink
->monitor_source
->index
);
1101 pa_tagstruct_puts(t
, sink
->monitor_source
->name
);
1102 pa_tagstruct_put_usec(t
, pa_sink_get_latency(sink
));
1105 static void source_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source
*source
) {
1106 assert(t
&& source
);
1107 pa_tagstruct_putu32(t
, source
->index
);
1108 pa_tagstruct_puts(t
, source
->name
);
1109 pa_tagstruct_puts(t
, source
->description
);
1110 pa_tagstruct_put_sample_spec(t
, &source
->sample_spec
);
1111 pa_tagstruct_putu32(t
, source
->owner
? source
->owner
->index
: (uint32_t) -1);
1112 pa_tagstruct_putu32(t
, source
->monitor_of
? source
->monitor_of
->index
: (uint32_t) -1);
1113 pa_tagstruct_puts(t
, source
->monitor_of
? source
->monitor_of
->name
: NULL
);
1114 pa_tagstruct_put_usec(t
, pa_source_get_latency(source
));
1117 static void client_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_client
*client
) {
1118 assert(t
&& client
);
1119 pa_tagstruct_putu32(t
, client
->index
);
1120 pa_tagstruct_puts(t
, client
->name
);
1121 pa_tagstruct_puts(t
, client
->protocol_name
);
1122 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: (uint32_t) -1);
1125 static void module_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_module
*module
) {
1126 assert(t
&& module
);
1127 pa_tagstruct_putu32(t
, module
->index
);
1128 pa_tagstruct_puts(t
, module
->name
);
1129 pa_tagstruct_puts(t
, module
->argument
);
1130 pa_tagstruct_putu32(t
, module
->n_used
);
1131 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
1134 static void sink_input_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink_input
*s
) {
1136 pa_tagstruct_putu32(t
, s
->index
);
1137 pa_tagstruct_puts(t
, s
->name
);
1138 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1139 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1140 pa_tagstruct_putu32(t
, s
->sink
->index
);
1141 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1142 pa_tagstruct_putu32(t
, s
->volume
);
1143 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
1144 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
1147 static void source_output_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source_output
*s
) {
1149 pa_tagstruct_putu32(t
, s
->index
);
1150 pa_tagstruct_puts(t
, s
->name
);
1151 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1152 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1153 pa_tagstruct_putu32(t
, s
->source
->index
);
1154 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1155 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
1156 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
1159 static void scache_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_scache_entry
*e
) {
1161 pa_tagstruct_putu32(t
, e
->index
);
1162 pa_tagstruct_puts(t
, e
->name
);
1163 pa_tagstruct_putu32(t
, e
->volume
);
1164 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1165 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1166 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
1167 pa_tagstruct_put_boolean(t
, e
->lazy
);
1168 pa_tagstruct_puts(t
, e
->filename
);
1171 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1172 struct connection
*c
= userdata
;
1174 struct pa_sink
*sink
= NULL
;
1175 struct pa_source
*source
= NULL
;
1176 struct pa_client
*client
= NULL
;
1177 struct pa_module
*module
= NULL
;
1178 struct pa_sink_input
*si
= NULL
;
1179 struct pa_source_output
*so
= NULL
;
1180 struct pa_scache_entry
*sce
= NULL
;
1182 struct pa_tagstruct
*reply
;
1186 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1187 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1188 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1189 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1190 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1191 pa_tagstruct_gets(t
, &name
) < 0) ||
1192 !pa_tagstruct_eof(t
)) {
1197 if (!c
->authorized
) {
1198 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1202 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1203 if (index
!= (uint32_t) -1)
1204 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1206 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1207 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1208 if (index
!= (uint32_t) -1)
1209 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, index
);
1211 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1212 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1213 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
);
1214 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1215 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
);
1216 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1217 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1218 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1219 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
);
1221 assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
1222 if (index
!= (uint32_t) -1)
1223 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, index
);
1225 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1228 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1229 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1233 reply
= pa_tagstruct_new(NULL
, 0);
1235 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1236 pa_tagstruct_putu32(reply
, tag
);
1238 sink_fill_tagstruct(reply
, sink
);
1240 source_fill_tagstruct(reply
, source
);
1242 client_fill_tagstruct(reply
, client
);
1244 module_fill_tagstruct(reply
, module
);
1246 sink_input_fill_tagstruct(reply
, si
);
1248 source_output_fill_tagstruct(reply
, so
);
1250 scache_fill_tagstruct(reply
, sce
);
1251 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1254 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1255 struct connection
*c
= userdata
;
1256 struct pa_idxset
*i
;
1259 struct pa_tagstruct
*reply
;
1262 if (!pa_tagstruct_eof(t
)) {
1267 if (!c
->authorized
) {
1268 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1272 reply
= pa_tagstruct_new(NULL
, 0);
1274 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1275 pa_tagstruct_putu32(reply
, tag
);
1277 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1278 i
= c
->protocol
->core
->sinks
;
1279 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1280 i
= c
->protocol
->core
->sources
;
1281 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1282 i
= c
->protocol
->core
->clients
;
1283 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1284 i
= c
->protocol
->core
->modules
;
1285 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1286 i
= c
->protocol
->core
->sink_inputs
;
1287 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1288 i
= c
->protocol
->core
->source_outputs
;
1290 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1291 i
= c
->protocol
->core
->scache
;
1295 for (p
= pa_idxset_first(i
, &index
); p
; p
= pa_idxset_next(i
, &index
)) {
1296 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1297 sink_fill_tagstruct(reply
, p
);
1298 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1299 source_fill_tagstruct(reply
, p
);
1300 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1301 client_fill_tagstruct(reply
, p
);
1302 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1303 module_fill_tagstruct(reply
, p
);
1304 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1305 sink_input_fill_tagstruct(reply
, p
);
1306 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1307 source_output_fill_tagstruct(reply
, p
);
1309 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1310 scache_fill_tagstruct(reply
, p
);
1315 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1318 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1319 struct connection
*c
= userdata
;
1320 struct pa_tagstruct
*reply
;
1325 if (!pa_tagstruct_eof(t
)) {
1330 if (!c
->authorized
) {
1331 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1335 reply
= pa_tagstruct_new(NULL
, 0);
1337 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1338 pa_tagstruct_putu32(reply
, tag
);
1339 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1340 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1341 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1342 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
1343 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1345 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
1346 pa_tagstruct_puts(reply
, n
);
1347 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
1348 pa_tagstruct_puts(reply
, n
);
1349 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1352 static void subscription_cb(struct pa_core
*core
, enum pa_subscription_event_type e
, uint32_t index
, void *userdata
) {
1353 struct pa_tagstruct
*t
;
1354 struct connection
*c
= userdata
;
1357 t
= pa_tagstruct_new(NULL
, 0);
1359 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
1360 pa_tagstruct_putu32(t
, (uint32_t) -1);
1361 pa_tagstruct_putu32(t
, e
);
1362 pa_tagstruct_putu32(t
, index
);
1363 pa_pstream_send_tagstruct(c
->pstream
, t
);
1366 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1367 struct connection
*c
= userdata
;
1368 enum pa_subscription_mask m
;
1371 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
1372 !pa_tagstruct_eof(t
)) {
1377 if (!c
->authorized
) {
1378 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1382 if (c
->subscription
)
1383 pa_subscription_free(c
->subscription
);
1386 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
1387 assert(c
->subscription
);
1389 c
->subscription
= NULL
;
1391 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1394 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1395 struct connection
*c
= userdata
;
1396 uint32_t index
, volume
;
1397 struct pa_sink
*sink
= NULL
;
1398 struct pa_sink_input
*si
= NULL
;
1399 const char *name
= NULL
;
1402 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1403 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1404 pa_tagstruct_getu32(t
, &volume
) ||
1405 !pa_tagstruct_eof(t
)) {
1410 if (!c
->authorized
) {
1411 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1415 if (command
== PA_COMMAND_SET_SINK_VOLUME
) {
1416 if (index
!= (uint32_t) -1)
1417 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1419 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1421 assert(command
== PA_COMMAND_SET_SINK_INPUT_VOLUME
);
1422 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1426 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1431 pa_sink_set_volume(sink
, volume
);
1433 pa_sink_input_set_volume(si
, volume
);
1435 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1438 static void command_cork_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1439 struct connection
*c
= userdata
;
1442 struct playback_stream
*s
;
1445 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1446 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1447 !pa_tagstruct_eof(t
)) {
1452 if (!c
->authorized
) {
1453 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1457 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1458 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1462 pa_sink_input_cork(s
->sink_input
, b
);
1463 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1466 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1467 struct connection
*c
= userdata
;
1469 struct playback_stream
*s
;
1472 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1473 !pa_tagstruct_eof(t
)) {
1478 if (!c
->authorized
) {
1479 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1483 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1484 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1488 if (command
== PA_COMMAND_PREBUF_PLAYBACK_STREAM
)
1489 pa_memblockq_prebuf_reenable(s
->memblockq
);
1490 else if (command
== PA_COMMAND_TRIGGER_PLAYBACK_STREAM
)
1491 pa_memblockq_prebuf_disable(s
->memblockq
);
1493 assert(command
== PA_COMMAND_FLUSH_PLAYBACK_STREAM
);
1494 pa_memblockq_flush(s
->memblockq
);
1495 /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1498 pa_sink_notify(s
->sink_input
->sink
);
1499 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1503 static void command_cork_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1504 struct connection
*c
= userdata
;
1506 struct record_stream
*s
;
1510 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1511 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1512 !pa_tagstruct_eof(t
)) {
1517 if (!c
->authorized
) {
1518 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1522 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1523 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1527 pa_source_output_cork(s
->source_output
, b
);
1528 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1531 static void command_flush_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1532 struct connection
*c
= userdata
;
1534 struct record_stream
*s
;
1537 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1538 !pa_tagstruct_eof(t
)) {
1543 if (!c
->authorized
) {
1544 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1548 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1549 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1553 pa_memblockq_flush(s
->memblockq
);
1554 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1557 static void command_set_default_sink_or_source(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1558 struct connection
*c
= userdata
;
1563 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1564 pa_tagstruct_gets(t
, &s
) < 0 || !s
||
1565 !pa_tagstruct_eof(t
)) {
1570 if (!c
->authorized
) {
1571 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1575 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
1576 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1579 static void command_set_stream_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1580 struct connection
*c
= userdata
;
1585 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1586 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1587 !pa_tagstruct_eof(t
)) {
1592 if (!c
->authorized
) {
1593 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1597 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
1598 struct playback_stream
*s
;
1600 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1601 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1605 pa_sink_input_set_name(s
->sink_input
, name
);
1608 struct record_stream
*s
;
1610 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1611 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1615 pa_source_output_set_name(s
->source_output
, name
);
1618 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1621 static void command_kill(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1622 struct connection
*c
= userdata
;
1626 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1627 !pa_tagstruct_eof(t
)) {
1632 if (!c
->authorized
) {
1633 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1637 if (command
== PA_COMMAND_KILL_CLIENT
) {
1638 struct pa_client
*client
;
1640 if (!(client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
))) {
1641 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1645 pa_client_kill(client
);
1646 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
1647 struct pa_sink_input
*s
;
1649 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
))) {
1650 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1654 pa_sink_input_kill(s
);
1656 struct pa_source_output
*s
;
1658 assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
1660 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
))) {
1661 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1665 pa_source_output_kill(s
);
1668 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1671 static void command_load_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1672 struct connection
*c
= userdata
;
1673 struct pa_module
*m
;
1674 const char *name
, *argument
;
1675 struct pa_tagstruct
*reply
;
1678 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1679 pa_tagstruct_gets(t
, &argument
) < 0 ||
1680 !pa_tagstruct_eof(t
)) {
1685 if (!c
->authorized
) {
1686 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1690 if (!(m
= pa_module_load(c
->protocol
->core
, name
, argument
))) {
1691 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INITFAILED
);
1695 reply
= pa_tagstruct_new(NULL
, 0);
1696 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1697 pa_tagstruct_putu32(reply
, tag
);
1698 pa_tagstruct_putu32(reply
, m
->index
);
1699 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1702 static void command_unload_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1703 struct connection
*c
= userdata
;
1705 struct pa_module
*m
;
1708 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1709 !pa_tagstruct_eof(t
)) {
1714 if (!c
->authorized
) {
1715 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1719 if (!(m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
))) {
1720 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1724 pa_module_unload_request(m
);
1725 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1728 static void command_add_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1729 struct connection
*c
= userdata
;
1730 const char *name
, *module
, *argument
;
1734 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1735 pa_tagstruct_getu32(t
, &type
) < 0 || type
> 1 ||
1736 pa_tagstruct_gets(t
, &module
) < 0 || !module
||
1737 pa_tagstruct_gets(t
, &argument
) < 0 ||
1738 !pa_tagstruct_eof(t
)) {
1743 if (!c
->authorized
) {
1744 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1748 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
) < 0) {
1749 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1753 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1756 static void command_remove_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1757 struct connection
*c
= userdata
;
1762 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1763 pa_tagstruct_getu32(t
, &type
) < 0 || type
> 1 ||
1764 !pa_tagstruct_eof(t
)) {
1769 if (!c
->authorized
) {
1770 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1774 if (pa_autoload_remove(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
) < 0) {
1775 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1779 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1782 static void autoload_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_autoload_entry
*e
) {
1784 pa_tagstruct_puts(t
, e
->name
);
1785 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
1786 pa_tagstruct_puts(t
, e
->module
);
1787 pa_tagstruct_puts(t
, e
->argument
);
1790 static void command_get_autoload_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1791 struct connection
*c
= userdata
;
1792 struct pa_autoload_entry
*a
= NULL
;
1795 struct pa_tagstruct
*reply
;
1798 if (pa_tagstruct_gets(t
, &name
) < 0 || name
||
1799 pa_tagstruct_getu32(t
, &type
) < 0 || type
> 1 ||
1800 !pa_tagstruct_eof(t
)) {
1805 if (!c
->authorized
) {
1806 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1810 if (!c
->protocol
->core
->autoload_hashmap
|| !(a
= pa_hashmap_get(c
->protocol
->core
->autoload_hashmap
, name
)) || (a
->type
== PA_NAMEREG_SINK
&& type
!= 0) || (a
->type
== PA_NAMEREG_SOURCE
&& type
!= 1)) {
1811 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1815 reply
= pa_tagstruct_new(NULL
, 0);
1817 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1818 pa_tagstruct_putu32(reply
, tag
);
1819 autoload_fill_tagstruct(reply
, a
);
1820 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1823 static void command_get_autoload_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1824 struct connection
*c
= userdata
;
1825 struct pa_tagstruct
*reply
;
1828 if (!pa_tagstruct_eof(t
)) {
1833 if (!c
->authorized
) {
1834 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1838 reply
= pa_tagstruct_new(NULL
, 0);
1839 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1840 pa_tagstruct_putu32(reply
, tag
);
1842 if (c
->protocol
->core
->autoload_hashmap
) {
1843 struct pa_autoload_entry
*a
;
1846 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
)))
1847 autoload_fill_tagstruct(reply
, a
);
1850 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1853 /*** pstream callbacks ***/
1855 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
1856 struct connection
*c
= userdata
;
1857 assert(p
&& packet
&& packet
->data
&& c
);
1859 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
1860 pa_log(__FILE__
": invalid packet.\n");
1865 static void pstream_memblock_callback(struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
) {
1866 struct connection
*c
= userdata
;
1867 struct output_stream
*stream
;
1868 assert(p
&& chunk
&& userdata
);
1870 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
1871 pa_log(__FILE__
": client sent block for invalid stream.\n");
1876 if (stream
->type
== PLAYBACK_STREAM
) {
1877 struct playback_stream
*p
= (struct playback_stream
*) stream
;
1878 if (chunk
->length
>= p
->requested_bytes
)
1879 p
->requested_bytes
= 0;
1881 p
->requested_bytes
-= chunk
->length
;
1883 pa_memblockq_push_align(p
->memblockq
, chunk
, delta
);
1884 assert(p
->sink_input
);
1885 /* pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq)); */
1887 pa_sink_notify(p
->sink_input
->sink
);
1888 /* pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1891 struct upload_stream
*u
= (struct upload_stream
*) stream
;
1893 assert(u
->type
== UPLOAD_STREAM
);
1895 if (!u
->memchunk
.memblock
) {
1896 if (u
->length
== chunk
->length
) {
1897 u
->memchunk
= *chunk
;
1898 pa_memblock_ref(u
->memchunk
.memblock
);
1901 u
->memchunk
.memblock
= pa_memblock_new(u
->length
, c
->protocol
->core
->memblock_stat
);
1902 u
->memchunk
.index
= u
->memchunk
.length
= 0;
1906 assert(u
->memchunk
.memblock
);
1909 if (l
> chunk
->length
)
1913 memcpy((uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
,
1914 (uint8_t*) chunk
->memblock
->data
+chunk
->index
, l
);
1915 u
->memchunk
.length
+= l
;
1921 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
1922 struct connection
*c
= userdata
;
1926 /* pa_log(__FILE__": connection died.\n");*/
1930 static void pstream_drain_callback(struct pa_pstream
*p
, void *userdata
) {
1931 struct connection
*c
= userdata
;
1937 /*** client callbacks ***/
1939 static void client_kill_cb(struct pa_client
*c
) {
1940 assert(c
&& c
->userdata
);
1941 connection_free(c
->userdata
);
1944 /*** socket server callbacks ***/
1946 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
1947 struct pa_protocol_native
*p
= userdata
;
1948 struct connection
*c
;
1951 c
= pa_xmalloc(sizeof(struct connection
));
1953 c
->authorized
=!! p
->public;
1956 c
->client
= pa_client_new(p
->core
, "NATIVE", "Client");
1958 c
->client
->kill
= client_kill_cb
;
1959 c
->client
->userdata
= c
;
1960 c
->client
->owner
= p
->module
;
1962 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->memblock_stat
);
1965 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
1966 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
1967 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
1968 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
1970 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
1971 assert(c
->pdispatch
);
1973 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
1974 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
1975 assert(c
->record_streams
&& c
->output_streams
);
1977 c
->rrobin_index
= PA_IDXSET_INVALID
;
1978 c
->subscription
= NULL
;
1980 pa_idxset_put(p
->connections
, c
, NULL
);
1983 /*** module entry points ***/
1985 static struct pa_protocol_native
* protocol_new_internal(struct pa_core
*c
, struct pa_module
*m
, struct pa_modargs
*ma
) {
1986 struct pa_protocol_native
*p
;
1990 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1991 pa_log(__FILE__
": public= expects a boolean argument.\n");
1995 p
= pa_xmalloc(sizeof(struct pa_protocol_native
));
1997 if (pa_authkey_load_from_home(pa_modargs_get_value(ma
, "cookie", PA_NATIVE_COOKIE_FILE
), p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0) {
2006 p
->connections
= pa_idxset_new(NULL
, NULL
);
2007 assert(p
->connections
);
2012 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
) {
2013 struct pa_protocol_native
*p
;
2015 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2019 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
2024 void pa_protocol_native_free(struct pa_protocol_native
*p
) {
2025 struct connection
*c
;
2028 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
2030 pa_idxset_free(p
->connections
, NULL
, NULL
);
2033 pa_socket_server_unref(p
->server
);
2038 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
) {
2039 struct pa_protocol_native
*p
;
2041 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2044 on_connection(NULL
, io
, p
);