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
59 #define PA_TYPEID_NATIVE PA_TYPEID_MAKE('N', 'A', 'T', 'V')
62 struct pa_protocol_native
;
64 struct record_stream
{
65 struct connection
*connection
;
67 struct pa_source_output
*source_output
;
68 struct pa_memblockq
*memblockq
;
72 struct playback_stream
{
74 struct connection
*connection
;
76 struct pa_sink_input
*sink_input
;
77 struct pa_memblockq
*memblockq
;
78 size_t requested_bytes
;
83 struct upload_stream
{
85 struct connection
*connection
;
87 struct pa_memchunk memchunk
;
90 struct pa_sample_spec sample_spec
;
93 struct output_stream
{
104 struct pa_protocol_native
*protocol
;
105 struct pa_client
*client
;
106 struct pa_pstream
*pstream
;
107 struct pa_pdispatch
*pdispatch
;
108 struct pa_idxset
*record_streams
, *output_streams
;
109 uint32_t rrobin_index
;
110 struct pa_subscription
*subscription
;
111 struct pa_time_event
*auth_timeout_event
;
114 struct pa_protocol_native
{
115 struct pa_module
*module
;
117 struct pa_core
*core
;
118 struct pa_socket_server
*server
;
119 struct pa_idxset
*connections
;
120 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
121 int auth_cookie_in_property
;
124 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
125 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
);
126 static void sink_input_kill_cb(struct pa_sink_input
*i
);
127 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
);
129 static void request_bytes(struct playback_stream
*s
);
131 static void source_output_kill_cb(struct pa_source_output
*o
);
132 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
133 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
);
135 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
136 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
137 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
138 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
139 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
140 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
141 static void command_set_client_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
142 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
143 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
144 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
145 static void command_get_record_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
146 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
147 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
148 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
149 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
150 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
151 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
152 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
153 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
154 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
155 static void command_cork_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
156 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
157 static void command_set_default_sink_or_source(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
158 static void command_set_stream_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
159 static void command_kill(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
160 static void command_load_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
161 static void command_unload_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
162 static void command_add_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
163 static void command_remove_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
164 static void command_get_autoload_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
165 static void command_get_autoload_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
166 static void command_cork_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
167 static void command_flush_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
169 static const struct pa_pdispatch_command command_table
[PA_COMMAND_MAX
] = {
170 [PA_COMMAND_ERROR
] = { NULL
},
171 [PA_COMMAND_TIMEOUT
] = { NULL
},
172 [PA_COMMAND_REPLY
] = { NULL
},
173 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = { command_create_playback_stream
},
174 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = { command_delete_stream
},
175 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = { command_drain_playback_stream
},
176 [PA_COMMAND_CREATE_RECORD_STREAM
] = { command_create_record_stream
},
177 [PA_COMMAND_DELETE_RECORD_STREAM
] = { command_delete_stream
},
178 [PA_COMMAND_AUTH
] = { command_auth
},
179 [PA_COMMAND_REQUEST
] = { NULL
},
180 [PA_COMMAND_EXIT
] = { command_exit
},
181 [PA_COMMAND_SET_CLIENT_NAME
] = { command_set_client_name
},
182 [PA_COMMAND_LOOKUP_SINK
] = { command_lookup
},
183 [PA_COMMAND_LOOKUP_SOURCE
] = { command_lookup
},
184 [PA_COMMAND_STAT
] = { command_stat
},
185 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = { command_get_playback_latency
},
186 [PA_COMMAND_GET_RECORD_LATENCY
] = { command_get_record_latency
},
187 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = { command_create_upload_stream
},
188 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = { command_delete_stream
},
189 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = { command_finish_upload_stream
},
190 [PA_COMMAND_PLAY_SAMPLE
] = { command_play_sample
},
191 [PA_COMMAND_REMOVE_SAMPLE
] = { command_remove_sample
},
192 [PA_COMMAND_GET_SINK_INFO
] = { command_get_info
},
193 [PA_COMMAND_GET_SOURCE_INFO
] = { command_get_info
},
194 [PA_COMMAND_GET_CLIENT_INFO
] = { command_get_info
},
195 [PA_COMMAND_GET_MODULE_INFO
] = { command_get_info
},
196 [PA_COMMAND_GET_SINK_INPUT_INFO
] = { command_get_info
},
197 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = { command_get_info
},
198 [PA_COMMAND_GET_SAMPLE_INFO
] = { command_get_info
},
199 [PA_COMMAND_GET_SINK_INFO_LIST
] = { command_get_info_list
},
200 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = { command_get_info_list
},
201 [PA_COMMAND_GET_MODULE_INFO_LIST
] = { command_get_info_list
},
202 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = { command_get_info_list
},
203 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = { command_get_info_list
},
204 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = { command_get_info_list
},
205 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = { command_get_info_list
},
206 [PA_COMMAND_GET_SERVER_INFO
] = { command_get_server_info
},
207 [PA_COMMAND_SUBSCRIBE
] = { command_subscribe
},
209 [PA_COMMAND_SET_SINK_VOLUME
] = { command_set_volume
},
210 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = { command_set_volume
},
212 [PA_COMMAND_CORK_PLAYBACK_STREAM
] = { command_cork_playback_stream
},
213 [PA_COMMAND_FLUSH_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
214 [PA_COMMAND_TRIGGER_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
215 [PA_COMMAND_PREBUF_PLAYBACK_STREAM
] = { command_flush_or_trigger_playback_stream
},
217 [PA_COMMAND_CORK_RECORD_STREAM
] = { command_cork_record_stream
},
218 [PA_COMMAND_FLUSH_RECORD_STREAM
] = { command_flush_record_stream
},
220 [PA_COMMAND_SET_DEFAULT_SINK
] = { command_set_default_sink_or_source
},
221 [PA_COMMAND_SET_DEFAULT_SOURCE
] = { command_set_default_sink_or_source
},
222 [PA_COMMAND_SET_PLAYBACK_STREAM_NAME
] = { command_set_stream_name
},
223 [PA_COMMAND_SET_RECORD_STREAM_NAME
] = { command_set_stream_name
},
224 [PA_COMMAND_KILL_CLIENT
] = { command_kill
},
225 [PA_COMMAND_KILL_SINK_INPUT
] = { command_kill
},
226 [PA_COMMAND_KILL_SOURCE_OUTPUT
] = { command_kill
},
227 [PA_COMMAND_LOAD_MODULE
] = { command_load_module
},
228 [PA_COMMAND_UNLOAD_MODULE
] = { command_unload_module
},
229 [PA_COMMAND_GET_AUTOLOAD_INFO
] = { command_get_autoload_info
},
230 [PA_COMMAND_GET_AUTOLOAD_INFO_LIST
] = { command_get_autoload_info_list
},
231 [PA_COMMAND_ADD_AUTOLOAD
] = { command_add_autoload
},
232 [PA_COMMAND_REMOVE_AUTOLOAD
] = { command_remove_autoload
},
236 /* structure management */
238 static struct upload_stream
* upload_stream_new(struct connection
*c
, const struct pa_sample_spec
*ss
, const char *name
, size_t length
) {
239 struct upload_stream
*s
;
240 assert(c
&& ss
&& name
&& length
);
242 s
= pa_xmalloc(sizeof(struct upload_stream
));
243 s
->type
= UPLOAD_STREAM
;
245 s
->sample_spec
= *ss
;
246 s
->name
= pa_xstrdup(name
);
248 s
->memchunk
.memblock
= NULL
;
249 s
->memchunk
.index
= 0;
250 s
->memchunk
.length
= 0;
254 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
258 static void upload_stream_free(struct upload_stream
*o
) {
259 assert(o
&& o
->connection
);
261 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
265 if (o
->memchunk
.memblock
)
266 pa_memblock_unref(o
->memchunk
.memblock
);
271 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
) {
272 struct record_stream
*s
;
273 struct pa_source_output
*source_output
;
275 assert(c
&& source
&& ss
&& name
&& maxlength
);
277 if (!(source_output
= pa_source_output_new(source
, PA_TYPEID_NATIVE
, name
, ss
, -1)))
280 s
= pa_xmalloc(sizeof(struct record_stream
));
282 s
->source_output
= source_output
;
283 s
->source_output
->push
= source_output_push_cb
;
284 s
->source_output
->kill
= source_output_kill_cb
;
285 s
->source_output
->get_latency
= source_output_get_latency_cb
;
286 s
->source_output
->userdata
= s
;
287 s
->source_output
->owner
= c
->protocol
->module
;
288 s
->source_output
->client
= c
->client
;
290 s
->memblockq
= pa_memblockq_new(maxlength
, 0, base
= pa_frame_size(ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
291 assert(s
->memblockq
);
293 s
->fragment_size
= (fragment_size
/base
)*base
;
294 if (!s
->fragment_size
)
295 s
->fragment_size
= base
;
297 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
301 static void record_stream_free(struct record_stream
* r
) {
302 assert(r
&& r
->connection
);
304 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
305 pa_source_output_disconnect(r
->source_output
);
306 pa_source_output_unref(r
->source_output
);
307 pa_memblockq_free(r
->memblockq
);
311 static struct playback_stream
* playback_stream_new(struct connection
*c
, struct pa_sink
*sink
, const struct pa_sample_spec
*ss
, const char *name
,
316 pa_volume_t volume
) {
317 struct playback_stream
*s
;
318 struct pa_sink_input
*sink_input
;
319 assert(c
&& sink
&& ss
&& name
&& maxlength
);
321 if (!(sink_input
= pa_sink_input_new(sink
, PA_TYPEID_NATIVE
, name
, ss
, 0, -1)))
324 s
= pa_xmalloc(sizeof(struct playback_stream
));
325 s
->type
= PLAYBACK_STREAM
;
327 s
->sink_input
= sink_input
;
329 s
->sink_input
->peek
= sink_input_peek_cb
;
330 s
->sink_input
->drop
= sink_input_drop_cb
;
331 s
->sink_input
->kill
= sink_input_kill_cb
;
332 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
333 s
->sink_input
->userdata
= s
;
334 s
->sink_input
->owner
= c
->protocol
->module
;
335 s
->sink_input
->client
= c
->client
;
337 s
->memblockq
= pa_memblockq_new(maxlength
, tlength
, pa_frame_size(ss
), prebuf
, minreq
, c
->protocol
->core
->memblock_stat
);
338 assert(s
->memblockq
);
340 s
->requested_bytes
= 0;
341 s
->drain_request
= 0;
343 s
->sink_input
->volume
= volume
;
345 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
349 static void playback_stream_free(struct playback_stream
* p
) {
350 assert(p
&& p
->connection
);
352 if (p
->drain_request
)
353 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERROR_NOENTITY
);
355 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
356 pa_sink_input_disconnect(p
->sink_input
);
357 pa_sink_input_unref(p
->sink_input
);
358 pa_memblockq_free(p
->memblockq
);
362 static void connection_free(struct connection
*c
) {
363 struct record_stream
*r
;
364 struct output_stream
*o
;
365 assert(c
&& c
->protocol
);
367 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
368 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
369 record_stream_free(r
);
370 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
372 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
373 if (o
->type
== PLAYBACK_STREAM
)
374 playback_stream_free((struct playback_stream
*) o
);
376 upload_stream_free((struct upload_stream
*) o
);
377 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
379 pa_pdispatch_unref(c
->pdispatch
);
380 pa_pstream_close(c
->pstream
);
381 pa_pstream_unref(c
->pstream
);
382 pa_client_free(c
->client
);
385 pa_subscription_free(c
->subscription
);
387 if (c
->auth_timeout_event
)
388 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
393 static void request_bytes(struct playback_stream
*s
) {
394 struct pa_tagstruct
*t
;
398 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
401 if (l
<= s
->requested_bytes
)
404 l
-= s
->requested_bytes
;
406 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
409 s
->requested_bytes
+= l
;
411 t
= pa_tagstruct_new(NULL
, 0);
413 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
414 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
415 pa_tagstruct_putu32(t
, s
->index
);
416 pa_tagstruct_putu32(t
, l
);
417 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
419 /* pa_log(__FILE__": Requesting %u bytes\n", l); */
422 static void send_memblock(struct connection
*c
) {
424 struct record_stream
*r
;
426 start
= PA_IDXSET_INVALID
;
428 struct pa_memchunk chunk
;
430 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
433 if (start
== PA_IDXSET_INVALID
)
434 start
= c
->rrobin_index
;
435 else if (start
== c
->rrobin_index
)
438 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
439 struct pa_memchunk schunk
= chunk
;
441 if (schunk
.length
> r
->fragment_size
)
442 schunk
.length
= r
->fragment_size
;
444 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, &schunk
);
445 pa_memblockq_drop(r
->memblockq
, &chunk
, schunk
.length
);
446 pa_memblock_unref(schunk
.memblock
);
453 static void send_playback_stream_killed(struct playback_stream
*p
) {
454 struct pa_tagstruct
*t
;
457 t
= pa_tagstruct_new(NULL
, 0);
459 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
460 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
461 pa_tagstruct_putu32(t
, p
->index
);
462 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
465 static void send_record_stream_killed(struct record_stream
*r
) {
466 struct pa_tagstruct
*t
;
469 t
= pa_tagstruct_new(NULL
, 0);
471 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
472 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
473 pa_tagstruct_putu32(t
, r
->index
);
474 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
477 /*** sinkinput callbacks ***/
479 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
480 struct playback_stream
*s
;
481 assert(i
&& i
->userdata
&& chunk
);
484 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
490 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
491 struct playback_stream
*s
;
492 assert(i
&& i
->userdata
&& length
);
495 pa_memblockq_drop(s
->memblockq
, chunk
, length
);
498 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
499 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
500 s
->drain_request
= 0;
503 /* pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq)); */
506 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
507 assert(i
&& i
->userdata
);
508 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
509 playback_stream_free((struct playback_stream
*) i
->userdata
);
512 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
513 struct playback_stream
*s
;
514 assert(i
&& i
->userdata
);
517 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
519 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
522 /*** source_output callbacks ***/
524 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
525 struct record_stream
*s
;
526 assert(o
&& o
->userdata
&& chunk
);
529 pa_memblockq_push_align(s
->memblockq
, chunk
, 0);
530 if (!pa_pstream_is_pending(s
->connection
->pstream
))
531 send_memblock(s
->connection
);
534 static void source_output_kill_cb(struct pa_source_output
*o
) {
535 assert(o
&& o
->userdata
);
536 send_record_stream_killed((struct record_stream
*) o
->userdata
);
537 record_stream_free((struct record_stream
*) o
->userdata
);
540 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
) {
541 struct record_stream
*s
;
542 assert(o
&& o
->userdata
);
545 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
547 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &o
->sample_spec
);
550 /*** pdispatch callbacks ***/
552 static void protocol_error(struct connection
*c
) {
553 pa_log(__FILE__
": protocol error, kicking client\n");
557 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
558 struct connection
*c
= userdata
;
559 struct playback_stream
*s
;
560 size_t maxlength
, tlength
, prebuf
, minreq
;
562 const char *name
, *sink_name
;
563 struct pa_sample_spec ss
;
564 struct pa_tagstruct
*reply
;
565 struct pa_sink
*sink
;
568 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
570 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
571 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
572 pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
573 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
574 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
575 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
576 pa_tagstruct_getu32(t
, &tlength
) < 0 ||
577 pa_tagstruct_getu32(t
, &prebuf
) < 0 ||
578 pa_tagstruct_getu32(t
, &minreq
) < 0 ||
579 pa_tagstruct_getu32(t
, &volume
) < 0 ||
580 !pa_tagstruct_eof(t
)) {
586 if (!c
->authorized
) {
587 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
591 if (sink_index
!= (uint32_t) -1)
592 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
594 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
597 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
601 if (!(s
= playback_stream_new(c
, sink
, &ss
, name
, maxlength
, tlength
, prebuf
, minreq
, volume
))) {
602 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
606 pa_sink_input_cork(s
->sink_input
, corked
);
608 reply
= pa_tagstruct_new(NULL
, 0);
610 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
611 pa_tagstruct_putu32(reply
, tag
);
612 pa_tagstruct_putu32(reply
, s
->index
);
613 assert(s
->sink_input
);
614 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
615 pa_tagstruct_putu32(reply
, s
->requested_bytes
= pa_memblockq_missing(s
->memblockq
));
616 pa_pstream_send_tagstruct(c
->pstream
, reply
);
620 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
621 struct connection
*c
= userdata
;
625 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
626 !pa_tagstruct_eof(t
)) {
631 if (!c
->authorized
) {
632 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
636 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
637 struct playback_stream
*s
;
638 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
639 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
643 playback_stream_free(s
);
644 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
645 struct record_stream
*s
;
646 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
647 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
651 record_stream_free(s
);
653 struct upload_stream
*s
;
654 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
655 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
656 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
660 upload_stream_free(s
);
663 pa_pstream_send_simple_ack(c
->pstream
, tag
);
666 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
667 struct connection
*c
= userdata
;
668 struct record_stream
*s
;
669 size_t maxlength
, fragment_size
;
670 uint32_t source_index
;
671 const char *name
, *source_name
;
672 struct pa_sample_spec ss
;
673 struct pa_tagstruct
*reply
;
674 struct pa_source
*source
;
676 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
678 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
679 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
680 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
681 pa_tagstruct_gets(t
, &source_name
) < 0 ||
682 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
683 pa_tagstruct_get_boolean(t
, &corked
) < 0 ||
684 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
685 !pa_tagstruct_eof(t
)) {
690 if (!c
->authorized
) {
691 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
695 if (source_index
!= (uint32_t) -1)
696 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
698 source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
, 1);
701 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
705 if (!(s
= record_stream_new(c
, source
, &ss
, name
, maxlength
, fragment_size
))) {
706 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
710 pa_source_output_cork(s
->source_output
, corked
);
712 reply
= pa_tagstruct_new(NULL
, 0);
714 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
715 pa_tagstruct_putu32(reply
, tag
);
716 pa_tagstruct_putu32(reply
, s
->index
);
717 assert(s
->source_output
);
718 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
719 pa_pstream_send_tagstruct(c
->pstream
, reply
);
722 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
723 struct connection
*c
= userdata
;
726 if (!pa_tagstruct_eof(t
)) {
731 if (!c
->authorized
) {
732 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
736 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
737 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
738 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
742 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
743 struct connection
*c
= userdata
;
747 if (pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
748 !pa_tagstruct_eof(t
)) {
753 if (!c
->authorized
) {
754 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) != 0) {
755 pa_log(__FILE__
": Denied access to client with invalid authorization key.\n");
756 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
761 if (c
->auth_timeout_event
) {
762 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
763 c
->auth_timeout_event
= NULL
;
767 pa_pstream_send_simple_ack(c
->pstream
, tag
);
771 static void command_set_client_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
772 struct connection
*c
= userdata
;
776 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
777 !pa_tagstruct_eof(t
)) {
782 pa_client_set_name(c
->client
, name
);
783 pa_pstream_send_simple_ack(c
->pstream
, tag
);
787 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
788 struct connection
*c
= userdata
;
790 uint32_t index
= PA_IDXSET_INVALID
;
793 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
794 !pa_tagstruct_eof(t
)) {
799 if (!c
->authorized
) {
800 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
804 if (command
== PA_COMMAND_LOOKUP_SINK
) {
805 struct pa_sink
*sink
;
806 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
809 struct pa_source
*source
;
810 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
811 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
812 index
= source
->index
;
815 if (index
== PA_IDXSET_INVALID
)
816 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
818 struct pa_tagstruct
*reply
;
819 reply
= pa_tagstruct_new(NULL
, 0);
821 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
822 pa_tagstruct_putu32(reply
, tag
);
823 pa_tagstruct_putu32(reply
, index
);
824 pa_pstream_send_tagstruct(c
->pstream
, reply
);
828 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
829 struct connection
*c
= userdata
;
831 struct playback_stream
*s
;
834 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
835 !pa_tagstruct_eof(t
)) {
840 if (!c
->authorized
) {
841 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
845 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
846 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
850 s
->drain_request
= 0;
852 pa_memblockq_prebuf_disable(s
->memblockq
);
854 if (!pa_memblockq_is_readable(s
->memblockq
)) {
855 /* pa_log("immediate drain: %u\n", pa_memblockq_get_length(s->memblockq)); */
856 pa_pstream_send_simple_ack(c
->pstream
, tag
);
858 /* pa_log("slow drain triggered\n"); */
859 s
->drain_request
= 1;
862 pa_sink_notify(s
->sink_input
->sink
);
866 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
867 struct connection
*c
= userdata
;
868 struct pa_tagstruct
*reply
;
871 if (!pa_tagstruct_eof(t
)) {
876 if (!c
->authorized
) {
877 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
881 reply
= pa_tagstruct_new(NULL
, 0);
883 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
884 pa_tagstruct_putu32(reply
, tag
);
885 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total
);
886 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total_size
);
887 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated
);
888 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated_size
);
889 pa_tagstruct_putu32(reply
, pa_scache_total_size(c
->protocol
->core
));
890 pa_pstream_send_tagstruct(c
->pstream
, reply
);
893 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
894 struct connection
*c
= userdata
;
895 struct pa_tagstruct
*reply
;
896 struct playback_stream
*s
;
897 struct timeval tv
, now
;
902 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
903 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
904 pa_tagstruct_getu64(t
, &counter
) < 0 ||
905 !pa_tagstruct_eof(t
)) {
910 if (!c
->authorized
) {
911 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
915 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
916 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
920 reply
= pa_tagstruct_new(NULL
, 0);
922 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
923 pa_tagstruct_putu32(reply
, tag
);
924 pa_tagstruct_put_usec(reply
, pa_sink_input_get_latency(s
->sink_input
));
925 pa_tagstruct_put_usec(reply
, pa_sink_get_latency(s
->sink_input
->sink
));
926 pa_tagstruct_put_usec(reply
, 0);
927 pa_tagstruct_put_boolean(reply
, pa_memblockq_is_readable(s
->memblockq
));
928 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
929 pa_tagstruct_put_timeval(reply
, &tv
);
930 gettimeofday(&now
, NULL
);
931 pa_tagstruct_put_timeval(reply
, &now
);
932 pa_tagstruct_putu64(reply
, counter
);
933 pa_pstream_send_tagstruct(c
->pstream
, reply
);
936 static void command_get_record_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
937 struct connection
*c
= userdata
;
938 struct pa_tagstruct
*reply
;
939 struct record_stream
*s
;
940 struct timeval tv
, now
;
945 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
946 pa_tagstruct_get_timeval(t
, &tv
) < 0 ||
947 pa_tagstruct_getu64(t
, &counter
) < 0 ||
948 !pa_tagstruct_eof(t
)) {
953 if (!c
->authorized
) {
954 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
958 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
959 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
963 reply
= pa_tagstruct_new(NULL
, 0);
965 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
966 pa_tagstruct_putu32(reply
, tag
);
967 pa_tagstruct_put_usec(reply
, pa_source_output_get_latency(s
->source_output
));
968 pa_tagstruct_put_usec(reply
, s
->source_output
->source
->monitor_of
? pa_sink_get_latency(s
->source_output
->source
->monitor_of
) : 0);
969 pa_tagstruct_put_usec(reply
, pa_source_get_latency(s
->source_output
->source
));
970 pa_tagstruct_put_boolean(reply
, 0);
971 pa_tagstruct_putu32(reply
, pa_memblockq_get_length(s
->memblockq
));
972 pa_tagstruct_put_timeval(reply
, &tv
);
973 gettimeofday(&now
, NULL
);
974 pa_tagstruct_put_timeval(reply
, &now
);
975 pa_tagstruct_putu64(reply
, counter
);
976 pa_pstream_send_tagstruct(c
->pstream
, reply
);
980 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
981 struct connection
*c
= userdata
;
982 struct upload_stream
*s
;
985 struct pa_sample_spec ss
;
986 struct pa_tagstruct
*reply
;
987 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
989 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
990 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
991 pa_tagstruct_getu32(t
, &length
) < 0 ||
992 !pa_tagstruct_eof(t
)) {
997 if (!c
->authorized
) {
998 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1002 if ((length
% pa_frame_size(&ss
)) != 0 || length
<= 0 || !*name
) {
1003 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
1007 if (!(s
= upload_stream_new(c
, &ss
, name
, length
))) {
1008 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
1012 reply
= pa_tagstruct_new(NULL
, 0);
1014 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1015 pa_tagstruct_putu32(reply
, tag
);
1016 pa_tagstruct_putu32(reply
, s
->index
);
1017 pa_tagstruct_putu32(reply
, length
);
1018 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1021 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1022 struct connection
*c
= userdata
;
1024 struct upload_stream
*s
;
1028 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
1029 !pa_tagstruct_eof(t
)) {
1034 if (!c
->authorized
) {
1035 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1039 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
1040 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1044 pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->memchunk
, &index
);
1045 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1046 upload_stream_free(s
);
1049 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1050 struct connection
*c
= userdata
;
1051 uint32_t sink_index
, volume
;
1052 struct pa_sink
*sink
;
1053 const char *name
, *sink_name
;
1056 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
1057 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
1058 pa_tagstruct_getu32(t
, &volume
) < 0 ||
1059 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1060 !pa_tagstruct_eof(t
)) {
1065 if (!c
->authorized
) {
1066 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1070 if (sink_index
!= (uint32_t) -1)
1071 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
1073 sink
= pa_namereg_get(c
->protocol
->core
, sink_name
, PA_NAMEREG_SINK
, 1);
1076 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1080 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
1081 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1085 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1088 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1089 struct connection
*c
= userdata
;
1093 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1094 !pa_tagstruct_eof(t
)) {
1099 if (!c
->authorized
) {
1100 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1104 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
1105 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1109 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1112 static void sink_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink
*sink
) {
1114 pa_tagstruct_putu32(t
, sink
->index
);
1115 pa_tagstruct_puts(t
, sink
->name
);
1116 pa_tagstruct_puts(t
, sink
->description
);
1117 pa_tagstruct_put_sample_spec(t
, &sink
->sample_spec
);
1118 pa_tagstruct_putu32(t
, sink
->owner
? sink
->owner
->index
: (uint32_t) -1);
1119 pa_tagstruct_putu32(t
, sink
->volume
);
1120 pa_tagstruct_putu32(t
, sink
->monitor_source
->index
);
1121 pa_tagstruct_puts(t
, sink
->monitor_source
->name
);
1122 pa_tagstruct_put_usec(t
, pa_sink_get_latency(sink
));
1123 pa_tagstruct_putu32(t
, sink
->typeid);
1126 static void source_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source
*source
) {
1127 assert(t
&& source
);
1128 pa_tagstruct_putu32(t
, source
->index
);
1129 pa_tagstruct_puts(t
, source
->name
);
1130 pa_tagstruct_puts(t
, source
->description
);
1131 pa_tagstruct_put_sample_spec(t
, &source
->sample_spec
);
1132 pa_tagstruct_putu32(t
, source
->owner
? source
->owner
->index
: (uint32_t) -1);
1133 pa_tagstruct_putu32(t
, source
->monitor_of
? source
->monitor_of
->index
: (uint32_t) -1);
1134 pa_tagstruct_puts(t
, source
->monitor_of
? source
->monitor_of
->name
: NULL
);
1135 pa_tagstruct_put_usec(t
, pa_source_get_latency(source
));
1136 pa_tagstruct_putu32(t
, source
->typeid);
1139 static void client_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_client
*client
) {
1140 assert(t
&& client
);
1141 pa_tagstruct_putu32(t
, client
->index
);
1142 pa_tagstruct_puts(t
, client
->name
);
1143 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: (uint32_t) -1);
1144 pa_tagstruct_putu32(t
, client
->typeid);
1147 static void module_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_module
*module
) {
1148 assert(t
&& module
);
1149 pa_tagstruct_putu32(t
, module
->index
);
1150 pa_tagstruct_puts(t
, module
->name
);
1151 pa_tagstruct_puts(t
, module
->argument
);
1152 pa_tagstruct_putu32(t
, module
->n_used
);
1153 pa_tagstruct_put_boolean(t
, module
->auto_unload
);
1156 static void sink_input_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink_input
*s
) {
1158 pa_tagstruct_putu32(t
, s
->index
);
1159 pa_tagstruct_puts(t
, s
->name
);
1160 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1161 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1162 pa_tagstruct_putu32(t
, s
->sink
->index
);
1163 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1164 pa_tagstruct_putu32(t
, s
->volume
);
1165 pa_tagstruct_put_usec(t
, pa_sink_input_get_latency(s
));
1166 pa_tagstruct_put_usec(t
, pa_sink_get_latency(s
->sink
));
1167 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_sink_input_get_resample_method(s
)));
1168 pa_tagstruct_putu32(t
, s
->typeid);
1171 static void source_output_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source_output
*s
) {
1173 pa_tagstruct_putu32(t
, s
->index
);
1174 pa_tagstruct_puts(t
, s
->name
);
1175 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1176 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1177 pa_tagstruct_putu32(t
, s
->source
->index
);
1178 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1179 pa_tagstruct_put_usec(t
, pa_source_output_get_latency(s
));
1180 pa_tagstruct_put_usec(t
, pa_source_get_latency(s
->source
));
1181 pa_tagstruct_puts(t
, pa_resample_method_to_string(pa_source_output_get_resample_method(s
)));
1182 pa_tagstruct_putu32(t
, s
->typeid);
1185 static void scache_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_scache_entry
*e
) {
1187 pa_tagstruct_putu32(t
, e
->index
);
1188 pa_tagstruct_puts(t
, e
->name
);
1189 pa_tagstruct_putu32(t
, e
->volume
);
1190 pa_tagstruct_put_usec(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1191 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1192 pa_tagstruct_putu32(t
, e
->memchunk
.length
);
1193 pa_tagstruct_put_boolean(t
, e
->lazy
);
1194 pa_tagstruct_puts(t
, e
->filename
);
1197 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1198 struct connection
*c
= userdata
;
1200 struct pa_sink
*sink
= NULL
;
1201 struct pa_source
*source
= NULL
;
1202 struct pa_client
*client
= NULL
;
1203 struct pa_module
*module
= NULL
;
1204 struct pa_sink_input
*si
= NULL
;
1205 struct pa_source_output
*so
= NULL
;
1206 struct pa_scache_entry
*sce
= NULL
;
1208 struct pa_tagstruct
*reply
;
1212 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1213 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1214 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1215 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1216 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1217 pa_tagstruct_gets(t
, &name
) < 0) ||
1218 !pa_tagstruct_eof(t
)) {
1223 if (!c
->authorized
) {
1224 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1228 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1229 if (index
!= (uint32_t) -1)
1230 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1232 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1233 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1234 if (index
!= (uint32_t) -1)
1235 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, index
);
1237 source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1);
1238 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1239 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
);
1240 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1241 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
);
1242 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1243 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1244 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1245 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
);
1247 assert(command
== PA_COMMAND_GET_SAMPLE_INFO
);
1248 if (index
!= (uint32_t) -1)
1249 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, index
);
1251 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1254 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1255 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1259 reply
= pa_tagstruct_new(NULL
, 0);
1261 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1262 pa_tagstruct_putu32(reply
, tag
);
1264 sink_fill_tagstruct(reply
, sink
);
1266 source_fill_tagstruct(reply
, source
);
1268 client_fill_tagstruct(reply
, client
);
1270 module_fill_tagstruct(reply
, module
);
1272 sink_input_fill_tagstruct(reply
, si
);
1274 source_output_fill_tagstruct(reply
, so
);
1276 scache_fill_tagstruct(reply
, sce
);
1277 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1280 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1281 struct connection
*c
= userdata
;
1282 struct pa_idxset
*i
;
1285 struct pa_tagstruct
*reply
;
1288 if (!pa_tagstruct_eof(t
)) {
1293 if (!c
->authorized
) {
1294 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1298 reply
= pa_tagstruct_new(NULL
, 0);
1300 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1301 pa_tagstruct_putu32(reply
, tag
);
1303 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1304 i
= c
->protocol
->core
->sinks
;
1305 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1306 i
= c
->protocol
->core
->sources
;
1307 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1308 i
= c
->protocol
->core
->clients
;
1309 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1310 i
= c
->protocol
->core
->modules
;
1311 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1312 i
= c
->protocol
->core
->sink_inputs
;
1313 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1314 i
= c
->protocol
->core
->source_outputs
;
1316 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1317 i
= c
->protocol
->core
->scache
;
1321 for (p
= pa_idxset_first(i
, &index
); p
; p
= pa_idxset_next(i
, &index
)) {
1322 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1323 sink_fill_tagstruct(reply
, p
);
1324 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1325 source_fill_tagstruct(reply
, p
);
1326 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1327 client_fill_tagstruct(reply
, p
);
1328 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1329 module_fill_tagstruct(reply
, p
);
1330 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1331 sink_input_fill_tagstruct(reply
, p
);
1332 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1333 source_output_fill_tagstruct(reply
, p
);
1335 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1336 scache_fill_tagstruct(reply
, p
);
1341 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1344 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1345 struct connection
*c
= userdata
;
1346 struct pa_tagstruct
*reply
;
1351 if (!pa_tagstruct_eof(t
)) {
1356 if (!c
->authorized
) {
1357 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1361 reply
= pa_tagstruct_new(NULL
, 0);
1363 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1364 pa_tagstruct_putu32(reply
, tag
);
1365 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1366 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1367 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1368 pa_tagstruct_puts(reply
, pa_get_fqdn(txt
, sizeof(txt
)));
1369 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1371 n
= pa_namereg_get_default_sink_name(c
->protocol
->core
);
1372 pa_tagstruct_puts(reply
, n
);
1373 n
= pa_namereg_get_default_source_name(c
->protocol
->core
);
1374 pa_tagstruct_puts(reply
, n
);
1376 pa_tagstruct_putu32(reply
, c
->protocol
->core
->cookie
);
1378 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1381 static void subscription_cb(struct pa_core
*core
, enum pa_subscription_event_type e
, uint32_t index
, void *userdata
) {
1382 struct pa_tagstruct
*t
;
1383 struct connection
*c
= userdata
;
1386 t
= pa_tagstruct_new(NULL
, 0);
1388 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
1389 pa_tagstruct_putu32(t
, (uint32_t) -1);
1390 pa_tagstruct_putu32(t
, e
);
1391 pa_tagstruct_putu32(t
, index
);
1392 pa_pstream_send_tagstruct(c
->pstream
, t
);
1395 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1396 struct connection
*c
= userdata
;
1397 enum pa_subscription_mask m
;
1400 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
1401 !pa_tagstruct_eof(t
)) {
1406 if (!c
->authorized
) {
1407 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1411 if (c
->subscription
)
1412 pa_subscription_free(c
->subscription
);
1415 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
1416 assert(c
->subscription
);
1418 c
->subscription
= NULL
;
1420 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1423 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1424 struct connection
*c
= userdata
;
1425 uint32_t index
, volume
;
1426 struct pa_sink
*sink
= NULL
;
1427 struct pa_sink_input
*si
= NULL
;
1428 const char *name
= NULL
;
1431 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1432 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1433 pa_tagstruct_getu32(t
, &volume
) ||
1434 !pa_tagstruct_eof(t
)) {
1439 if (!c
->authorized
) {
1440 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1444 if (command
== PA_COMMAND_SET_SINK_VOLUME
) {
1445 if (index
!= (uint32_t) -1)
1446 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1448 sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1);
1450 assert(command
== PA_COMMAND_SET_SINK_INPUT_VOLUME
);
1451 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1455 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1460 pa_sink_set_volume(sink
, volume
);
1462 pa_sink_input_set_volume(si
, volume
);
1464 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1467 static void command_cork_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1468 struct connection
*c
= userdata
;
1471 struct playback_stream
*s
;
1474 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1475 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1476 !pa_tagstruct_eof(t
)) {
1481 if (!c
->authorized
) {
1482 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1486 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1487 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1491 pa_sink_input_cork(s
->sink_input
, b
);
1492 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1495 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1496 struct connection
*c
= userdata
;
1498 struct playback_stream
*s
;
1501 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1502 !pa_tagstruct_eof(t
)) {
1507 if (!c
->authorized
) {
1508 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1512 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1513 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1517 if (command
== PA_COMMAND_PREBUF_PLAYBACK_STREAM
)
1518 pa_memblockq_prebuf_reenable(s
->memblockq
);
1519 else if (command
== PA_COMMAND_TRIGGER_PLAYBACK_STREAM
)
1520 pa_memblockq_prebuf_disable(s
->memblockq
);
1522 assert(command
== PA_COMMAND_FLUSH_PLAYBACK_STREAM
);
1523 pa_memblockq_flush(s
->memblockq
);
1524 /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1527 pa_sink_notify(s
->sink_input
->sink
);
1528 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1532 static void command_cork_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1533 struct connection
*c
= userdata
;
1535 struct record_stream
*s
;
1539 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1540 pa_tagstruct_get_boolean(t
, &b
) < 0 ||
1541 !pa_tagstruct_eof(t
)) {
1546 if (!c
->authorized
) {
1547 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1551 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1552 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1556 pa_source_output_cork(s
->source_output
, b
);
1557 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1560 static void command_flush_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1561 struct connection
*c
= userdata
;
1563 struct record_stream
*s
;
1566 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1567 !pa_tagstruct_eof(t
)) {
1572 if (!c
->authorized
) {
1573 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1577 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1578 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1582 pa_memblockq_flush(s
->memblockq
);
1583 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1586 static void command_set_default_sink_or_source(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1587 struct connection
*c
= userdata
;
1592 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1593 pa_tagstruct_gets(t
, &s
) < 0 || !s
||
1594 !pa_tagstruct_eof(t
)) {
1599 if (!c
->authorized
) {
1600 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1604 pa_namereg_set_default(c
->protocol
->core
, s
, command
== PA_COMMAND_SET_DEFAULT_SOURCE
? PA_NAMEREG_SOURCE
: PA_NAMEREG_SINK
);
1605 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1608 static void command_set_stream_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1609 struct connection
*c
= userdata
;
1614 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1615 pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1616 !pa_tagstruct_eof(t
)) {
1621 if (!c
->authorized
) {
1622 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1626 if (command
== PA_COMMAND_SET_PLAYBACK_STREAM_NAME
) {
1627 struct playback_stream
*s
;
1629 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
1630 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1634 pa_sink_input_set_name(s
->sink_input
, name
);
1637 struct record_stream
*s
;
1639 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, index
))) {
1640 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1644 pa_source_output_set_name(s
->source_output
, name
);
1647 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1650 static void command_kill(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1651 struct connection
*c
= userdata
;
1655 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1656 !pa_tagstruct_eof(t
)) {
1661 if (!c
->authorized
) {
1662 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1666 if (command
== PA_COMMAND_KILL_CLIENT
) {
1667 struct pa_client
*client
;
1669 if (!(client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
))) {
1670 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1674 pa_client_kill(client
);
1675 } else if (command
== PA_COMMAND_KILL_SINK_INPUT
) {
1676 struct pa_sink_input
*s
;
1678 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
))) {
1679 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1683 pa_sink_input_kill(s
);
1685 struct pa_source_output
*s
;
1687 assert(command
== PA_COMMAND_KILL_SOURCE_OUTPUT
);
1689 if (!(s
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
))) {
1690 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1694 pa_source_output_kill(s
);
1697 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1700 static void command_load_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1701 struct connection
*c
= userdata
;
1702 struct pa_module
*m
;
1703 const char *name
, *argument
;
1704 struct pa_tagstruct
*reply
;
1707 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1708 pa_tagstruct_gets(t
, &argument
) < 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_module_load(c
->protocol
->core
, name
, argument
))) {
1720 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INITFAILED
);
1724 reply
= pa_tagstruct_new(NULL
, 0);
1725 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1726 pa_tagstruct_putu32(reply
, tag
);
1727 pa_tagstruct_putu32(reply
, m
->index
);
1728 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1731 static void command_unload_module(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1732 struct connection
*c
= userdata
;
1734 struct pa_module
*m
;
1737 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1738 !pa_tagstruct_eof(t
)) {
1743 if (!c
->authorized
) {
1744 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1748 if (!(m
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
))) {
1749 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1753 pa_module_unload_request(m
);
1754 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1757 static void command_add_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1758 struct connection
*c
= userdata
;
1759 const char *name
, *module
, *argument
;
1762 struct pa_tagstruct
*reply
;
1765 if (pa_tagstruct_gets(t
, &name
) < 0 || !name
||
1766 pa_tagstruct_getu32(t
, &type
) < 0 || type
> 1 ||
1767 pa_tagstruct_gets(t
, &module
) < 0 || !module
||
1768 pa_tagstruct_gets(t
, &argument
) < 0 ||
1769 !pa_tagstruct_eof(t
)) {
1774 if (!c
->authorized
) {
1775 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1779 if (pa_autoload_add(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, module
, argument
, &index
) < 0) {
1780 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
1784 reply
= pa_tagstruct_new(NULL
, 0);
1785 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1786 pa_tagstruct_putu32(reply
, tag
);
1787 pa_tagstruct_putu32(reply
, index
);
1788 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1791 static void command_remove_autoload(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1792 struct connection
*c
= userdata
;
1793 const char *name
= NULL
;
1794 uint32_t type
, index
= PA_IDXSET_INVALID
;
1798 if ((pa_tagstruct_getu32(t
, &index
) < 0 &&
1799 (pa_tagstruct_gets(t
, &name
) < 0 ||
1800 pa_tagstruct_getu32(t
, &type
) < 0)) ||
1801 (!name
&& index
== PA_IDXSET_INVALID
) ||
1802 (name
&& type
> 1) ||
1803 !pa_tagstruct_eof(t
)) {
1808 if (!c
->authorized
) {
1809 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1814 r
= pa_autoload_remove_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
1816 r
= pa_autoload_remove_by_index(c
->protocol
->core
, index
);
1819 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1823 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1826 static void autoload_fill_tagstruct(struct pa_tagstruct
*t
, const struct pa_autoload_entry
*e
) {
1829 pa_tagstruct_putu32(t
, e
->index
);
1830 pa_tagstruct_puts(t
, e
->name
);
1831 pa_tagstruct_putu32(t
, e
->type
== PA_NAMEREG_SINK
? 0 : 1);
1832 pa_tagstruct_puts(t
, e
->module
);
1833 pa_tagstruct_puts(t
, e
->argument
);
1836 static void command_get_autoload_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1837 struct connection
*c
= userdata
;
1838 const struct pa_autoload_entry
*a
= NULL
;
1839 uint32_t type
, index
;
1841 struct pa_tagstruct
*reply
;
1844 if ((pa_tagstruct_getu32(t
, &index
) < 0 &&
1845 (pa_tagstruct_gets(t
, &name
) < 0 ||
1846 pa_tagstruct_getu32(t
, &type
) < 0)) ||
1847 (!name
&& index
== PA_IDXSET_INVALID
) ||
1848 (name
&& type
> 1) ||
1849 !pa_tagstruct_eof(t
)) {
1854 if (!c
->authorized
) {
1855 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1861 a
= pa_autoload_get_by_name(c
->protocol
->core
, name
, type
== 0 ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
);
1863 a
= pa_autoload_get_by_index(c
->protocol
->core
, index
);
1866 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1870 reply
= pa_tagstruct_new(NULL
, 0);
1872 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1873 pa_tagstruct_putu32(reply
, tag
);
1874 autoload_fill_tagstruct(reply
, a
);
1875 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1878 static void command_get_autoload_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1879 struct connection
*c
= userdata
;
1880 struct pa_tagstruct
*reply
;
1883 if (!pa_tagstruct_eof(t
)) {
1888 if (!c
->authorized
) {
1889 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1893 reply
= pa_tagstruct_new(NULL
, 0);
1894 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1895 pa_tagstruct_putu32(reply
, tag
);
1897 if (c
->protocol
->core
->autoload_hashmap
) {
1898 struct pa_autoload_entry
*a
;
1901 while ((a
= pa_hashmap_iterate(c
->protocol
->core
->autoload_hashmap
, &state
, NULL
)))
1902 autoload_fill_tagstruct(reply
, a
);
1905 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1908 /*** pstream callbacks ***/
1910 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
1911 struct connection
*c
= userdata
;
1912 assert(p
&& packet
&& packet
->data
&& c
);
1914 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
1915 pa_log(__FILE__
": invalid packet.\n");
1920 static void pstream_memblock_callback(struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
) {
1921 struct connection
*c
= userdata
;
1922 struct output_stream
*stream
;
1923 assert(p
&& chunk
&& userdata
);
1925 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
1926 pa_log(__FILE__
": client sent block for invalid stream.\n");
1931 if (stream
->type
== PLAYBACK_STREAM
) {
1932 struct playback_stream
*p
= (struct playback_stream
*) stream
;
1933 if (chunk
->length
>= p
->requested_bytes
)
1934 p
->requested_bytes
= 0;
1936 p
->requested_bytes
-= chunk
->length
;
1938 pa_memblockq_push_align(p
->memblockq
, chunk
, delta
);
1939 assert(p
->sink_input
);
1940 /* pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq)); */
1942 pa_sink_notify(p
->sink_input
->sink
);
1943 /* pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1946 struct upload_stream
*u
= (struct upload_stream
*) stream
;
1948 assert(u
->type
== UPLOAD_STREAM
);
1950 if (!u
->memchunk
.memblock
) {
1951 if (u
->length
== chunk
->length
) {
1952 u
->memchunk
= *chunk
;
1953 pa_memblock_ref(u
->memchunk
.memblock
);
1956 u
->memchunk
.memblock
= pa_memblock_new(u
->length
, c
->protocol
->core
->memblock_stat
);
1957 u
->memchunk
.index
= u
->memchunk
.length
= 0;
1961 assert(u
->memchunk
.memblock
);
1964 if (l
> chunk
->length
)
1968 memcpy((uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
,
1969 (uint8_t*) chunk
->memblock
->data
+chunk
->index
, l
);
1970 u
->memchunk
.length
+= l
;
1976 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
1977 struct connection
*c
= userdata
;
1981 /* pa_log(__FILE__": connection died.\n");*/
1985 static void pstream_drain_callback(struct pa_pstream
*p
, void *userdata
) {
1986 struct connection
*c
= userdata
;
1992 /*** client callbacks ***/
1994 static void client_kill_cb(struct pa_client
*c
) {
1995 assert(c
&& c
->userdata
);
1996 connection_free(c
->userdata
);
1999 /*** socket server callbacks ***/
2001 static void auth_timeout(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
2002 struct connection
*c
= userdata
;
2003 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
2009 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
2010 struct pa_protocol_native
*p
= userdata
;
2011 struct connection
*c
;
2014 if (pa_idxset_ncontents(p
->connections
)+1 > MAX_CONNECTIONS
) {
2015 pa_log_warn(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
2016 pa_iochannel_free(io
);
2020 c
= pa_xmalloc(sizeof(struct connection
));
2022 c
->authorized
=!! p
->public;
2024 if (!c
->authorized
) {
2026 gettimeofday(&tv
, NULL
);
2027 tv
.tv_sec
+= AUTH_TIMEOUT
;
2028 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
2030 c
->auth_timeout_event
= NULL
;
2034 c
->client
= pa_client_new(p
->core
, PA_TYPEID_NATIVE
, "Client");
2036 c
->client
->kill
= client_kill_cb
;
2037 c
->client
->userdata
= c
;
2038 c
->client
->owner
= p
->module
;
2040 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->memblock_stat
);
2043 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
2044 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
2045 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
2046 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
2048 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
2049 assert(c
->pdispatch
);
2051 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
2052 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
2053 assert(c
->record_streams
&& c
->output_streams
);
2055 c
->rrobin_index
= PA_IDXSET_INVALID
;
2056 c
->subscription
= NULL
;
2058 pa_idxset_put(p
->connections
, c
, NULL
);
2061 /*** module entry points ***/
2063 static int load_key(struct pa_protocol_native
*p
, const char*fn
) {
2066 p
->auth_cookie_in_property
= 0;
2068 if (!fn
&& pa_authkey_prop_get(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0) {
2069 pa_log_info(__FILE__
": using already loaded auth cookie.\n");
2070 pa_authkey_prop_ref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2071 p
->auth_cookie_in_property
= 1;
2076 fn
= PA_NATIVE_COOKIE_FILE
;
2078 if (pa_authkey_load_auto(fn
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0)
2081 pa_log_info(__FILE__
": loading cookie from disk.\n");
2083 if (pa_authkey_prop_put(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
, p
->auth_cookie
, sizeof(p
->auth_cookie
)) >= 0)
2084 p
->auth_cookie_in_property
= 1;
2089 static struct pa_protocol_native
* protocol_new_internal(struct pa_core
*c
, struct pa_module
*m
, struct pa_modargs
*ma
) {
2090 struct pa_protocol_native
*p
;
2094 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
2095 pa_log(__FILE__
": public= expects a boolean argument.\n");
2099 p
= pa_xmalloc(sizeof(struct pa_protocol_native
));
2105 if (load_key(p
, pa_modargs_get_value(ma
, "cookie", NULL
)) < 0) {
2110 p
->connections
= pa_idxset_new(NULL
, NULL
);
2111 assert(p
->connections
);
2116 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
) {
2118 struct pa_protocol_native
*p
;
2120 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2124 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
2126 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2127 struct pa_strlist
*l
;
2128 l
= pa_property_get(core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2129 l
= pa_strlist_prepend(l
, t
);
2130 pa_property_replace(core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2136 void pa_protocol_native_free(struct pa_protocol_native
*p
) {
2137 struct connection
*c
;
2140 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
2142 pa_idxset_free(p
->connections
, NULL
, NULL
);
2147 if (pa_socket_server_get_address(p
->server
, t
, sizeof(t
))) {
2148 struct pa_strlist
*l
;
2149 l
= pa_property_get(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2150 l
= pa_strlist_remove(l
, t
);
2153 pa_property_replace(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
, l
);
2155 pa_property_remove(p
->core
, PA_NATIVE_SERVER_PROPERTY_NAME
);
2158 pa_socket_server_unref(p
->server
);
2161 if (p
->auth_cookie_in_property
)
2162 pa_authkey_prop_unref(p
->core
, PA_NATIVE_COOKIE_PROPERTY_NAME
);
2167 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
) {
2168 struct pa_protocol_native
*p
;
2170 if (!(p
= protocol_new_internal(core
, m
, ma
)))
2173 on_connection(NULL
, io
, p
);