]> code.delx.au - pulseaudio/blob - polyp/protocol-native.c
renamed module-tunnel to module-tunnel-sink
[pulseaudio] / polyp / protocol-native.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <stdlib.h>
30
31 #include "protocol-native.h"
32 #include "native-common.h"
33 #include "packet.h"
34 #include "client.h"
35 #include "source-output.h"
36 #include "sink-input.h"
37 #include "pstream.h"
38 #include "tagstruct.h"
39 #include "pdispatch.h"
40 #include "pstream-util.h"
41 #include "authkey.h"
42 #include "namereg.h"
43 #include "scache.h"
44 #include "xmalloc.h"
45 #include "util.h"
46 #include "subscribe.h"
47 #include "log.h"
48 #include "autoload.h"
49
50 struct connection;
51 struct pa_protocol_native;
52
53 struct record_stream {
54 struct connection *connection;
55 uint32_t index;
56 struct pa_source_output *source_output;
57 struct pa_memblockq *memblockq;
58 size_t fragment_size;
59 };
60
61 struct playback_stream {
62 int type;
63 struct connection *connection;
64 uint32_t index;
65 struct pa_sink_input *sink_input;
66 struct pa_memblockq *memblockq;
67 size_t requested_bytes;
68 int drain_request;
69 uint32_t drain_tag;
70 };
71
72 struct upload_stream {
73 int type;
74 struct connection *connection;
75 uint32_t index;
76 struct pa_memchunk memchunk;
77 size_t length;
78 char *name;
79 struct pa_sample_spec sample_spec;
80 };
81
82 struct output_stream {
83 int type;
84 };
85
86 enum {
87 UPLOAD_STREAM,
88 PLAYBACK_STREAM
89 };
90
91 struct connection {
92 int authorized;
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;
100 };
101
102 struct pa_protocol_native {
103 struct pa_module *module;
104 int public;
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];
109 };
110
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);
115
116 static void request_bytes(struct playback_stream*s);
117
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);
121
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);
155
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 },
195
196 [PA_COMMAND_SET_SINK_VOLUME] = { command_set_volume },
197 [PA_COMMAND_SET_SINK_INPUT_VOLUME] = { command_set_volume },
198
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 },
203
204 [PA_COMMAND_CORK_RECORD_STREAM] = { command_cork_record_stream },
205 [PA_COMMAND_FLUSH_RECORD_STREAM] = { command_flush_record_stream },
206
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 },
220
221 };
222
223 /* structure management */
224
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);
228
229 s = pa_xmalloc(sizeof(struct upload_stream));
230 s->type = UPLOAD_STREAM;
231 s->connection = c;
232 s->sample_spec = *ss;
233 s->name = pa_xstrdup(name);
234
235 s->memchunk.memblock = NULL;
236 s->memchunk.index = 0;
237 s->memchunk.length = 0;
238
239 s->length = length;
240
241 pa_idxset_put(c->output_streams, s, &s->index);
242 return s;
243 }
244
245 static void upload_stream_free(struct upload_stream *o) {
246 assert(o && o->connection);
247
248 pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
249
250 pa_xfree(o->name);
251
252 if (o->memchunk.memblock)
253 pa_memblock_unref(o->memchunk.memblock);
254
255 pa_xfree(o);
256 }
257
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;
261 size_t base;
262 assert(c && source && ss && name && maxlength);
263
264 if (!(source_output = pa_source_output_new(source, name, ss, -1)))
265 return NULL;
266
267 s = pa_xmalloc(sizeof(struct record_stream));
268 s->connection = c;
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;
276
277 s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0, c->protocol->core->memblock_stat);
278 assert(s->memblockq);
279
280 s->fragment_size = (fragment_size/base)*base;
281 if (!s->fragment_size)
282 s->fragment_size = base;
283
284 pa_idxset_put(c->record_streams, s, &s->index);
285 return s;
286 }
287
288 static void record_stream_free(struct record_stream* r) {
289 assert(r && r->connection);
290
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);
295 pa_xfree(r);
296 }
297
298 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name,
299 size_t maxlength,
300 size_t tlength,
301 size_t prebuf,
302 size_t minreq,
303 pa_volume_t volume) {
304 struct playback_stream *s;
305 struct pa_sink_input *sink_input;
306 assert(c && sink && ss && name && maxlength);
307
308 if (!(sink_input = pa_sink_input_new(sink, name, ss, 0, -1)))
309 return NULL;
310
311 s = pa_xmalloc(sizeof(struct playback_stream));
312 s->type = PLAYBACK_STREAM;
313 s->connection = c;
314 s->sink_input = sink_input;
315
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;
323
324 s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq, c->protocol->core->memblock_stat);
325 assert(s->memblockq);
326
327 s->requested_bytes = 0;
328 s->drain_request = 0;
329
330 s->sink_input->volume = volume;
331
332 pa_idxset_put(c->output_streams, s, &s->index);
333 return s;
334 }
335
336 static void playback_stream_free(struct playback_stream* p) {
337 assert(p && p->connection);
338
339 if (p->drain_request)
340 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
341
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);
346 pa_xfree(p);
347 }
348
349 static void connection_free(struct connection *c) {
350 struct record_stream *r;
351 struct output_stream *o;
352 assert(c && c->protocol);
353
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);
358
359 while ((o = pa_idxset_first(c->output_streams, NULL)))
360 if (o->type == PLAYBACK_STREAM)
361 playback_stream_free((struct playback_stream*) o);
362 else
363 upload_stream_free((struct upload_stream*) o);
364 pa_idxset_free(c->output_streams, NULL, NULL);
365
366 pa_pdispatch_unref(c->pdispatch);
367 pa_pstream_close(c->pstream);
368 pa_pstream_unref(c->pstream);
369 pa_client_free(c->client);
370
371 if (c->subscription)
372 pa_subscription_free(c->subscription);
373
374 pa_xfree(c);
375 }
376
377 static void request_bytes(struct playback_stream *s) {
378 struct pa_tagstruct *t;
379 size_t l;
380 assert(s);
381
382 if (!(l = pa_memblockq_missing(s->memblockq)))
383 return;
384
385 if (l <= s->requested_bytes)
386 return;
387
388 l -= s->requested_bytes;
389
390 if (l < pa_memblockq_get_minreq(s->memblockq))
391 return;
392
393 s->requested_bytes += l;
394
395 t = pa_tagstruct_new(NULL, 0);
396 assert(t);
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);
402
403 /* pa_log(__FILE__": Requesting %u bytes\n", l); */
404 }
405
406 static void send_memblock(struct connection *c) {
407 uint32_t start;
408 struct record_stream *r;
409
410 start = PA_IDXSET_INVALID;
411 for (;;) {
412 struct pa_memchunk chunk;
413
414 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
415 return;
416
417 if (start == PA_IDXSET_INVALID)
418 start = c->rrobin_index;
419 else if (start == c->rrobin_index)
420 return;
421
422 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
423 struct pa_memchunk schunk = chunk;
424
425 if (schunk.length > r->fragment_size)
426 schunk.length = r->fragment_size;
427
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);
431
432 return;
433 }
434 }
435 }
436
437 static void send_playback_stream_killed(struct playback_stream *p) {
438 struct pa_tagstruct *t;
439 assert(p);
440
441 t = pa_tagstruct_new(NULL, 0);
442 assert(t);
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);
447 }
448
449 static void send_record_stream_killed(struct record_stream *r) {
450 struct pa_tagstruct *t;
451 assert(r);
452
453 t = pa_tagstruct_new(NULL, 0);
454 assert(t);
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);
459 }
460
461 /*** sinkinput callbacks ***/
462
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);
466 s = i->userdata;
467
468 if (pa_memblockq_peek(s->memblockq, chunk) < 0)
469 return -1;
470
471 return 0;
472 }
473
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);
477 s = i->userdata;
478
479 pa_memblockq_drop(s->memblockq, chunk, length);
480 request_bytes(s);
481
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;
485 }
486
487 /* pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq)); */
488 }
489
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);
494 }
495
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);
499 s = i->userdata;
500
501 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
502
503 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
504 }
505
506 /*** source_output callbacks ***/
507
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);
511 s = o->userdata;
512
513 pa_memblockq_push_align(s->memblockq, chunk, 0);
514 if (!pa_pstream_is_pending(s->connection->pstream))
515 send_memblock(s->connection);
516 }
517
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);
522 }
523
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);
527 s = o->userdata;
528
529 /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
530
531 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &o->sample_spec);
532 }
533
534 /*** pdispatch callbacks ***/
535
536 static void protocol_error(struct connection *c) {
537 pa_log(__FILE__": protocol error, kicking client\n");
538 connection_free(c);
539 }
540
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;
545 uint32_t sink_index;
546 const char *name, *sink_name;
547 struct pa_sample_spec ss;
548 struct pa_tagstruct *reply;
549 struct pa_sink *sink;
550 pa_volume_t volume;
551 int corked;
552 assert(c && t && c->protocol && c->protocol->core);
553
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)) {
565 protocol_error(c);
566 return;
567 }
568
569
570 if (!c->authorized) {
571 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
572 return;
573 }
574
575 if (sink_index != (uint32_t) -1)
576 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
577 else
578 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
579
580 if (!sink) {
581 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
582 return;
583 }
584
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);
587 return;
588 }
589
590 pa_sink_input_cork(s->sink_input, corked);
591
592 reply = pa_tagstruct_new(NULL, 0);
593 assert(reply);
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);
601 request_bytes(s);
602 }
603
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;
606 uint32_t channel;
607 assert(c && t);
608
609 if (pa_tagstruct_getu32(t, &channel) < 0 ||
610 !pa_tagstruct_eof(t)) {
611 protocol_error(c);
612 return;
613 }
614
615 if (!c->authorized) {
616 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
617 return;
618 }
619
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);
624 return;
625 }
626
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);
632 return;
633 }
634
635 record_stream_free(s);
636 } else {
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);
641 return;
642 }
643
644 upload_stream_free(s);
645 }
646
647 pa_pstream_send_simple_ack(c->pstream, tag);
648 }
649
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;
659 int corked;
660 assert(c && t && c->protocol && c->protocol->core);
661
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)) {
670 protocol_error(c);
671 return;
672 }
673
674 if (!c->authorized) {
675 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
676 return;
677 }
678
679 if (source_index != (uint32_t) -1)
680 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
681 else
682 source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1);
683
684 if (!source) {
685 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
686 return;
687 }
688
689 if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
690 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
691 return;
692 }
693
694 pa_source_output_cork(s->source_output, corked);
695
696 reply = pa_tagstruct_new(NULL, 0);
697 assert(reply);
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);
704 }
705
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;
708 assert(c && t);
709
710 if (!pa_tagstruct_eof(t)) {
711 protocol_error(c);
712 return;
713 }
714
715 if (!c->authorized) {
716 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
717 return;
718 }
719
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 */
723 return;
724 }
725
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;
728 const void*cookie;
729 assert(c && t);
730
731 if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
732 !pa_tagstruct_eof(t)) {
733 protocol_error(c);
734 return;
735 }
736
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);
741 return;
742 }
743
744 c->authorized = 1;
745 }
746
747 pa_pstream_send_simple_ack(c->pstream, tag);
748 return;
749 }
750
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;
753 const char *name;
754 assert(c && t);
755
756 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
757 !pa_tagstruct_eof(t)) {
758 protocol_error(c);
759 return;
760 }
761
762 pa_client_set_name(c->client, name);
763 pa_pstream_send_simple_ack(c->pstream, tag);
764 return;
765 }
766
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;
769 const char *name;
770 uint32_t index = PA_IDXSET_INVALID;
771 assert(c && t);
772
773 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
774 !pa_tagstruct_eof(t)) {
775 protocol_error(c);
776 return;
777 }
778
779 if (!c->authorized) {
780 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
781 return;
782 }
783
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)))
787 index = sink->index;
788 } else {
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;
793 }
794
795 if (index == PA_IDXSET_INVALID)
796 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
797 else {
798 struct pa_tagstruct *reply;
799 reply = pa_tagstruct_new(NULL, 0);
800 assert(reply);
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);
805 }
806 }
807
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;
810 uint32_t index;
811 struct playback_stream *s;
812 assert(c && t);
813
814 if (pa_tagstruct_getu32(t, &index) < 0 ||
815 !pa_tagstruct_eof(t)) {
816 protocol_error(c);
817 return;
818 }
819
820 if (!c->authorized) {
821 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
822 return;
823 }
824
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);
827 return;
828 }
829
830 s->drain_request = 0;
831
832 pa_memblockq_prebuf_disable(s->memblockq);
833
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);
837 } else {
838 /* pa_log("slow drain triggered\n"); */
839 s->drain_request = 1;
840 s->drain_tag = tag;
841
842 pa_sink_notify(s->sink_input->sink);
843 }
844 }
845
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;
849 assert(c && t);
850
851 if (!pa_tagstruct_eof(t)) {
852 protocol_error(c);
853 return;
854 }
855
856 if (!c->authorized) {
857 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
858 return;
859 }
860
861 reply = pa_tagstruct_new(NULL, 0);
862 assert(reply);
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);
871 }
872
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;
878 uint32_t index;
879 assert(c && t);
880
881 if (pa_tagstruct_getu32(t, &index) < 0 ||
882 pa_tagstruct_get_timeval(t, &tv) < 0 ||
883 !pa_tagstruct_eof(t)) {
884 protocol_error(c);
885 return;
886 }
887
888 if (!c->authorized) {
889 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
890 return;
891 }
892
893 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
894 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
895 return;
896 }
897
898 reply = pa_tagstruct_new(NULL, 0);
899 assert(reply);
900 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
901 pa_tagstruct_putu32(reply, tag);
902 pa_tagstruct_put_usec(reply, pa_sink_input_get_latency(s->sink_input));
903 pa_tagstruct_put_usec(reply, pa_sink_get_latency(s->sink_input->sink));
904 pa_tagstruct_put_usec(reply, 0);
905 pa_tagstruct_put_boolean(reply, pa_memblockq_is_readable(s->memblockq));
906 pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
907 pa_tagstruct_put_timeval(reply, &tv);
908 gettimeofday(&now, NULL);
909 pa_tagstruct_put_timeval(reply, &now);
910 pa_pstream_send_tagstruct(c->pstream, reply);
911 }
912
913 static void command_get_record_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
914 struct connection *c = userdata;
915 struct pa_tagstruct *reply;
916 struct record_stream *s;
917 struct timeval tv, now;
918 uint32_t index;
919 assert(c && t);
920
921 if (pa_tagstruct_getu32(t, &index) < 0 ||
922 pa_tagstruct_get_timeval(t, &tv) < 0 ||
923 !pa_tagstruct_eof(t)) {
924 protocol_error(c);
925 return;
926 }
927
928 if (!c->authorized) {
929 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
930 return;
931 }
932
933 if (!(s = pa_idxset_get_by_index(c->record_streams, index))) {
934 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
935 return;
936 }
937
938 reply = pa_tagstruct_new(NULL, 0);
939 assert(reply);
940 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
941 pa_tagstruct_putu32(reply, tag);
942 pa_tagstruct_put_usec(reply, pa_source_output_get_latency(s->source_output));
943 pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
944 pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
945 pa_tagstruct_put_boolean(reply, 0);
946 pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
947 pa_tagstruct_put_timeval(reply, &tv);
948 gettimeofday(&now, NULL);
949 pa_tagstruct_put_timeval(reply, &now);
950 pa_pstream_send_tagstruct(c->pstream, reply);
951 }
952
953
954 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
955 struct connection *c = userdata;
956 struct upload_stream *s;
957 size_t length;
958 const char *name;
959 struct pa_sample_spec ss;
960 struct pa_tagstruct *reply;
961 assert(c && t && c->protocol && c->protocol->core);
962
963 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
964 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
965 pa_tagstruct_getu32(t, &length) < 0 ||
966 !pa_tagstruct_eof(t)) {
967 protocol_error(c);
968 return;
969 }
970
971 if (!c->authorized) {
972 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
973 return;
974 }
975
976 if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
977 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
978 return;
979 }
980
981 if (!(s = upload_stream_new(c, &ss, name, length))) {
982 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
983 return;
984 }
985
986 reply = pa_tagstruct_new(NULL, 0);
987 assert(reply);
988 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
989 pa_tagstruct_putu32(reply, tag);
990 pa_tagstruct_putu32(reply, s->index);
991 pa_tagstruct_putu32(reply, length);
992 pa_pstream_send_tagstruct(c->pstream, reply);
993 }
994
995 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
996 struct connection *c = userdata;
997 uint32_t channel;
998 struct upload_stream *s;
999 uint32_t index;
1000 assert(c && t);
1001
1002 if (pa_tagstruct_getu32(t, &channel) < 0 ||
1003 !pa_tagstruct_eof(t)) {
1004 protocol_error(c);
1005 return;
1006 }
1007
1008 if (!c->authorized) {
1009 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1010 return;
1011 }
1012
1013 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
1014 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
1015 return;
1016 }
1017
1018 pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
1019 pa_pstream_send_simple_ack(c->pstream, tag);
1020 upload_stream_free(s);
1021 }
1022
1023 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1024 struct connection *c = userdata;
1025 uint32_t sink_index, volume;
1026 struct pa_sink *sink;
1027 const char *name, *sink_name;
1028 assert(c && t);
1029
1030 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
1031 pa_tagstruct_gets(t, &sink_name) < 0 ||
1032 pa_tagstruct_getu32(t, &volume) < 0 ||
1033 pa_tagstruct_gets(t, &name) < 0 || !name ||
1034 !pa_tagstruct_eof(t)) {
1035 protocol_error(c);
1036 return;
1037 }
1038
1039 if (!c->authorized) {
1040 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1041 return;
1042 }
1043
1044 if (sink_index != (uint32_t) -1)
1045 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1046 else
1047 sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1048
1049 if (!sink) {
1050 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1051 return;
1052 }
1053
1054 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
1055 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1056 return;
1057 }
1058
1059 pa_pstream_send_simple_ack(c->pstream, tag);
1060 }
1061
1062 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1063 struct connection *c = userdata;
1064 const char *name;
1065 assert(c && t);
1066
1067 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1068 !pa_tagstruct_eof(t)) {
1069 protocol_error(c);
1070 return;
1071 }
1072
1073 if (!c->authorized) {
1074 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1075 return;
1076 }
1077
1078 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
1079 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1080 return;
1081 }
1082
1083 pa_pstream_send_simple_ack(c->pstream, tag);
1084 }
1085
1086 static void sink_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink *sink) {
1087 assert(t && sink);
1088 pa_tagstruct_putu32(t, sink->index);
1089 pa_tagstruct_puts(t, sink->name);
1090 pa_tagstruct_puts(t, sink->description);
1091 pa_tagstruct_put_sample_spec(t, &sink->sample_spec);
1092 pa_tagstruct_putu32(t, sink->owner ? sink->owner->index : (uint32_t) -1);
1093 pa_tagstruct_putu32(t, sink->volume);
1094 pa_tagstruct_putu32(t, sink->monitor_source->index);
1095 pa_tagstruct_puts(t, sink->monitor_source->name);
1096 pa_tagstruct_put_usec(t, pa_sink_get_latency(sink));
1097 }
1098
1099 static void source_fill_tagstruct(struct pa_tagstruct *t, struct pa_source *source) {
1100 assert(t && source);
1101 pa_tagstruct_putu32(t, source->index);
1102 pa_tagstruct_puts(t, source->name);
1103 pa_tagstruct_puts(t, source->description);
1104 pa_tagstruct_put_sample_spec(t, &source->sample_spec);
1105 pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
1106 pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
1107 pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : NULL);
1108 pa_tagstruct_put_usec(t, pa_source_get_latency(source));
1109 }
1110
1111 static void client_fill_tagstruct(struct pa_tagstruct *t, struct pa_client *client) {
1112 assert(t && client);
1113 pa_tagstruct_putu32(t, client->index);
1114 pa_tagstruct_puts(t, client->name);
1115 pa_tagstruct_puts(t, client->protocol_name);
1116 pa_tagstruct_putu32(t, client->owner ? client->owner->index : (uint32_t) -1);
1117 }
1118
1119 static void module_fill_tagstruct(struct pa_tagstruct *t, struct pa_module *module) {
1120 assert(t && module);
1121 pa_tagstruct_putu32(t, module->index);
1122 pa_tagstruct_puts(t, module->name);
1123 pa_tagstruct_puts(t, module->argument);
1124 pa_tagstruct_putu32(t, module->n_used);
1125 pa_tagstruct_put_boolean(t, module->auto_unload);
1126 }
1127
1128 static void sink_input_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink_input *s) {
1129 assert(t && s);
1130 pa_tagstruct_putu32(t, s->index);
1131 pa_tagstruct_puts(t, s->name);
1132 pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1133 pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1134 pa_tagstruct_putu32(t, s->sink->index);
1135 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1136 pa_tagstruct_putu32(t, s->volume);
1137 pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
1138 pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
1139 }
1140
1141 static void source_output_fill_tagstruct(struct pa_tagstruct *t, struct pa_source_output *s) {
1142 assert(t && s);
1143 pa_tagstruct_putu32(t, s->index);
1144 pa_tagstruct_puts(t, s->name);
1145 pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1146 pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1147 pa_tagstruct_putu32(t, s->source->index);
1148 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1149 pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
1150 pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
1151 }
1152
1153 static void scache_fill_tagstruct(struct pa_tagstruct *t, struct pa_scache_entry *e) {
1154 assert(t && e);
1155 pa_tagstruct_putu32(t, e->index);
1156 pa_tagstruct_puts(t, e->name);
1157 pa_tagstruct_putu32(t, e->volume);
1158 pa_tagstruct_put_usec(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1159 pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1160 pa_tagstruct_putu32(t, e->memchunk.length);
1161 pa_tagstruct_put_boolean(t, e->lazy);
1162 pa_tagstruct_puts(t, e->filename);
1163 }
1164
1165 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1166 struct connection *c = userdata;
1167 uint32_t index;
1168 struct pa_sink *sink = NULL;
1169 struct pa_source *source = NULL;
1170 struct pa_client *client = NULL;
1171 struct pa_module *module = NULL;
1172 struct pa_sink_input *si = NULL;
1173 struct pa_source_output *so = NULL;
1174 struct pa_scache_entry *sce = NULL;
1175 const char *name;
1176 struct pa_tagstruct *reply;
1177 assert(c && t);
1178
1179
1180 if (pa_tagstruct_getu32(t, &index) < 0 ||
1181 (command != PA_COMMAND_GET_CLIENT_INFO &&
1182 command != PA_COMMAND_GET_MODULE_INFO &&
1183 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1184 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1185 pa_tagstruct_gets(t, &name) < 0) ||
1186 !pa_tagstruct_eof(t)) {
1187 protocol_error(c);
1188 return;
1189 }
1190
1191 if (!c->authorized) {
1192 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1193 return;
1194 }
1195
1196 if (command == PA_COMMAND_GET_SINK_INFO) {
1197 if (index != (uint32_t) -1)
1198 sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1199 else
1200 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1201 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1202 if (index != (uint32_t) -1)
1203 source = pa_idxset_get_by_index(c->protocol->core->sources, index);
1204 else
1205 source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1206 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1207 client = pa_idxset_get_by_index(c->protocol->core->clients, index);
1208 else if (command == PA_COMMAND_GET_MODULE_INFO)
1209 module = pa_idxset_get_by_index(c->protocol->core->modules, index);
1210 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1211 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1212 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1213 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, index);
1214 else {
1215 assert(command == PA_COMMAND_GET_SAMPLE_INFO);
1216 if (index != (uint32_t) -1)
1217 sce = pa_idxset_get_by_index(c->protocol->core->scache, index);
1218 else
1219 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1220 }
1221
1222 if (!sink && !source && !client && !module && !si && !so && !sce) {
1223 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1224 return;
1225 }
1226
1227 reply = pa_tagstruct_new(NULL, 0);
1228 assert(reply);
1229 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1230 pa_tagstruct_putu32(reply, tag);
1231 if (sink)
1232 sink_fill_tagstruct(reply, sink);
1233 else if (source)
1234 source_fill_tagstruct(reply, source);
1235 else if (client)
1236 client_fill_tagstruct(reply, client);
1237 else if (module)
1238 module_fill_tagstruct(reply, module);
1239 else if (si)
1240 sink_input_fill_tagstruct(reply, si);
1241 else if (so)
1242 source_output_fill_tagstruct(reply, so);
1243 else
1244 scache_fill_tagstruct(reply, sce);
1245 pa_pstream_send_tagstruct(c->pstream, reply);
1246 }
1247
1248 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1249 struct connection *c = userdata;
1250 struct pa_idxset *i;
1251 uint32_t index;
1252 void *p;
1253 struct pa_tagstruct *reply;
1254 assert(c && t);
1255
1256 if (!pa_tagstruct_eof(t)) {
1257 protocol_error(c);
1258 return;
1259 }
1260
1261 if (!c->authorized) {
1262 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1263 return;
1264 }
1265
1266 reply = pa_tagstruct_new(NULL, 0);
1267 assert(reply);
1268 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1269 pa_tagstruct_putu32(reply, tag);
1270
1271 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1272 i = c->protocol->core->sinks;
1273 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1274 i = c->protocol->core->sources;
1275 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1276 i = c->protocol->core->clients;
1277 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1278 i = c->protocol->core->modules;
1279 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1280 i = c->protocol->core->sink_inputs;
1281 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1282 i = c->protocol->core->source_outputs;
1283 else {
1284 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1285 i = c->protocol->core->scache;
1286 }
1287
1288 if (i) {
1289 for (p = pa_idxset_first(i, &index); p; p = pa_idxset_next(i, &index)) {
1290 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1291 sink_fill_tagstruct(reply, p);
1292 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1293 source_fill_tagstruct(reply, p);
1294 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1295 client_fill_tagstruct(reply, p);
1296 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1297 module_fill_tagstruct(reply, p);
1298 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1299 sink_input_fill_tagstruct(reply, p);
1300 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1301 source_output_fill_tagstruct(reply, p);
1302 else {
1303 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1304 scache_fill_tagstruct(reply, p);
1305 }
1306 }
1307 }
1308
1309 pa_pstream_send_tagstruct(c->pstream, reply);
1310 }
1311
1312 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1313 struct connection *c = userdata;
1314 struct pa_tagstruct *reply;
1315 char txt[256];
1316 const char *n;
1317 assert(c && t);
1318
1319 if (!pa_tagstruct_eof(t)) {
1320 protocol_error(c);
1321 return;
1322 }
1323
1324 if (!c->authorized) {
1325 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1326 return;
1327 }
1328
1329 reply = pa_tagstruct_new(NULL, 0);
1330 assert(reply);
1331 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1332 pa_tagstruct_putu32(reply, tag);
1333 pa_tagstruct_puts(reply, PACKAGE_NAME);
1334 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1335 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1336 pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
1337 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1338
1339 n = pa_namereg_get_default_sink_name(c->protocol->core);
1340 pa_tagstruct_puts(reply, n);
1341 n = pa_namereg_get_default_source_name(c->protocol->core);
1342 pa_tagstruct_puts(reply, n);
1343 pa_pstream_send_tagstruct(c->pstream, reply);
1344 }
1345
1346 static void subscription_cb(struct pa_core *core, enum pa_subscription_event_type e, uint32_t index, void *userdata) {
1347 struct pa_tagstruct *t;
1348 struct connection *c = userdata;
1349 assert(c && core);
1350
1351 t = pa_tagstruct_new(NULL, 0);
1352 assert(t);
1353 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1354 pa_tagstruct_putu32(t, (uint32_t) -1);
1355 pa_tagstruct_putu32(t, e);
1356 pa_tagstruct_putu32(t, index);
1357 pa_pstream_send_tagstruct(c->pstream, t);
1358 }
1359
1360 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1361 struct connection *c = userdata;
1362 enum pa_subscription_mask m;
1363 assert(c && t);
1364
1365 if (pa_tagstruct_getu32(t, &m) < 0 ||
1366 !pa_tagstruct_eof(t)) {
1367 protocol_error(c);
1368 return;
1369 }
1370
1371 if (!c->authorized) {
1372 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1373 return;
1374 }
1375
1376 if (c->subscription)
1377 pa_subscription_free(c->subscription);
1378
1379 if (m != 0) {
1380 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1381 assert(c->subscription);
1382 } else
1383 c->subscription = NULL;
1384
1385 pa_pstream_send_simple_ack(c->pstream, tag);
1386 }
1387
1388 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1389 struct connection *c = userdata;
1390 uint32_t index, volume;
1391 struct pa_sink *sink = NULL;
1392 struct pa_sink_input *si = NULL;
1393 const char *name = NULL;
1394 assert(c && t);
1395
1396 if (pa_tagstruct_getu32(t, &index) < 0 ||
1397 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1398 pa_tagstruct_getu32(t, &volume) ||
1399 !pa_tagstruct_eof(t)) {
1400 protocol_error(c);
1401 return;
1402 }
1403
1404 if (!c->authorized) {
1405 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1406 return;
1407 }
1408
1409 if (command == PA_COMMAND_SET_SINK_VOLUME) {
1410 if (index != (uint32_t) -1)
1411 sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1412 else
1413 sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1414 } else {
1415 assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1416 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1417 }
1418
1419 if (!si && !sink) {
1420 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1421 return;
1422 }
1423
1424 if (sink)
1425 pa_sink_set_volume(sink, volume);
1426 else if (si)
1427 pa_sink_input_set_volume(si, volume);
1428
1429 pa_pstream_send_simple_ack(c->pstream, tag);
1430 }
1431
1432 static void command_cork_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1433 struct connection *c = userdata;
1434 uint32_t index;
1435 int b;
1436 struct playback_stream *s;
1437 assert(c && t);
1438
1439 if (pa_tagstruct_getu32(t, &index) < 0 ||
1440 pa_tagstruct_get_boolean(t, &b) < 0 ||
1441 !pa_tagstruct_eof(t)) {
1442 protocol_error(c);
1443 return;
1444 }
1445
1446 if (!c->authorized) {
1447 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1448 return;
1449 }
1450
1451 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1452 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1453 return;
1454 }
1455
1456 pa_sink_input_cork(s->sink_input, b);
1457 pa_pstream_send_simple_ack(c->pstream, tag);
1458 }
1459
1460 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1461 struct connection *c = userdata;
1462 uint32_t index;
1463 struct playback_stream *s;
1464 assert(c && t);
1465
1466 if (pa_tagstruct_getu32(t, &index) < 0 ||
1467 !pa_tagstruct_eof(t)) {
1468 protocol_error(c);
1469 return;
1470 }
1471
1472 if (!c->authorized) {
1473 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1474 return;
1475 }
1476
1477 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1478 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1479 return;
1480 }
1481
1482 if (command == PA_COMMAND_PREBUF_PLAYBACK_STREAM)
1483 pa_memblockq_prebuf_reenable(s->memblockq);
1484 else if (command == PA_COMMAND_TRIGGER_PLAYBACK_STREAM)
1485 pa_memblockq_prebuf_disable(s->memblockq);
1486 else {
1487 assert(command == PA_COMMAND_FLUSH_PLAYBACK_STREAM);
1488 pa_memblockq_flush(s->memblockq);
1489 /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1490 }
1491
1492 pa_sink_notify(s->sink_input->sink);
1493 pa_pstream_send_simple_ack(c->pstream, tag);
1494 request_bytes(s);
1495 }
1496
1497 static void command_cork_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1498 struct connection *c = userdata;
1499 uint32_t index;
1500 struct record_stream *s;
1501 int b;
1502 assert(c && t);
1503
1504 if (pa_tagstruct_getu32(t, &index) < 0 ||
1505 pa_tagstruct_get_boolean(t, &b) < 0 ||
1506 !pa_tagstruct_eof(t)) {
1507 protocol_error(c);
1508 return;
1509 }
1510
1511 if (!c->authorized) {
1512 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1513 return;
1514 }
1515
1516 if (!(s = pa_idxset_get_by_index(c->record_streams, index))) {
1517 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1518 return;
1519 }
1520
1521 pa_source_output_cork(s->source_output, b);
1522 pa_pstream_send_simple_ack(c->pstream, tag);
1523 }
1524
1525 static void command_flush_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1526 struct connection *c = userdata;
1527 uint32_t index;
1528 struct record_stream *s;
1529 assert(c && t);
1530
1531 if (pa_tagstruct_getu32(t, &index) < 0 ||
1532 !pa_tagstruct_eof(t)) {
1533 protocol_error(c);
1534 return;
1535 }
1536
1537 if (!c->authorized) {
1538 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1539 return;
1540 }
1541
1542 if (!(s = pa_idxset_get_by_index(c->record_streams, index))) {
1543 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1544 return;
1545 }
1546
1547 pa_memblockq_flush(s->memblockq);
1548 pa_pstream_send_simple_ack(c->pstream, tag);
1549 }
1550
1551 static void command_set_default_sink_or_source(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1552 struct connection *c = userdata;
1553 uint32_t index;
1554 const char *s;
1555 assert(c && t);
1556
1557 if (pa_tagstruct_getu32(t, &index) < 0 ||
1558 pa_tagstruct_gets(t, &s) < 0 || !s ||
1559 !pa_tagstruct_eof(t)) {
1560 protocol_error(c);
1561 return;
1562 }
1563
1564 if (!c->authorized) {
1565 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1566 return;
1567 }
1568
1569 pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
1570 pa_pstream_send_simple_ack(c->pstream, tag);
1571 }
1572
1573 static void command_set_stream_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1574 struct connection *c = userdata;
1575 uint32_t index;
1576 const char *name;
1577 assert(c && t);
1578
1579 if (pa_tagstruct_getu32(t, &index) < 0 ||
1580 pa_tagstruct_gets(t, &name) < 0 || !name ||
1581 !pa_tagstruct_eof(t)) {
1582 protocol_error(c);
1583 return;
1584 }
1585
1586 if (!c->authorized) {
1587 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1588 return;
1589 }
1590
1591 if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
1592 struct playback_stream *s;
1593
1594 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1595 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1596 return;
1597 }
1598
1599 pa_sink_input_set_name(s->sink_input, name);
1600
1601 } else {
1602 struct record_stream *s;
1603
1604 if (!(s = pa_idxset_get_by_index(c->record_streams, index))) {
1605 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1606 return;
1607 }
1608
1609 pa_source_output_set_name(s->source_output, name);
1610 }
1611
1612 pa_pstream_send_simple_ack(c->pstream, tag);
1613 }
1614
1615 static void command_kill(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1616 struct connection *c = userdata;
1617 uint32_t index;
1618 assert(c && t);
1619
1620 if (pa_tagstruct_getu32(t, &index) < 0 ||
1621 !pa_tagstruct_eof(t)) {
1622 protocol_error(c);
1623 return;
1624 }
1625
1626 if (!c->authorized) {
1627 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1628 return;
1629 }
1630
1631 if (command == PA_COMMAND_KILL_CLIENT) {
1632 struct pa_client *client;
1633
1634 if (!(client = pa_idxset_get_by_index(c->protocol->core->clients, index))) {
1635 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1636 return;
1637 }
1638
1639 pa_client_kill(client);
1640 } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
1641 struct pa_sink_input *s;
1642
1643 if (!(s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index))) {
1644 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1645 return;
1646 }
1647
1648 pa_sink_input_kill(s);
1649 } else {
1650 struct pa_source_output *s;
1651
1652 assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
1653
1654 if (!(s = pa_idxset_get_by_index(c->protocol->core->source_outputs, index))) {
1655 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1656 return;
1657 }
1658
1659 pa_source_output_kill(s);
1660 }
1661
1662 pa_pstream_send_simple_ack(c->pstream, tag);
1663 }
1664
1665 static void command_load_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1666 struct connection *c = userdata;
1667 struct pa_module *m;
1668 const char *name, *argument;
1669 struct pa_tagstruct *reply;
1670 assert(c && t);
1671
1672 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1673 pa_tagstruct_gets(t, &argument) < 0 ||
1674 !pa_tagstruct_eof(t)) {
1675 protocol_error(c);
1676 return;
1677 }
1678
1679 if (!c->authorized) {
1680 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1681 return;
1682 }
1683
1684 if (!(m = pa_module_load(c->protocol->core, name, argument))) {
1685 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INITFAILED);
1686 return;
1687 }
1688
1689 reply = pa_tagstruct_new(NULL, 0);
1690 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1691 pa_tagstruct_putu32(reply, tag);
1692 pa_tagstruct_putu32(reply, m->index);
1693 pa_pstream_send_tagstruct(c->pstream, reply);
1694 }
1695
1696 static void command_unload_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1697 struct connection *c = userdata;
1698 uint32_t index;
1699 struct pa_module *m;
1700 assert(c && t);
1701
1702 if (pa_tagstruct_getu32(t, &index) < 0 ||
1703 !pa_tagstruct_eof(t)) {
1704 protocol_error(c);
1705 return;
1706 }
1707
1708 if (!c->authorized) {
1709 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1710 return;
1711 }
1712
1713 if (!(m = pa_idxset_get_by_index(c->protocol->core->modules, index))) {
1714 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1715 return;
1716 }
1717
1718 pa_module_unload_request(m);
1719 pa_pstream_send_simple_ack(c->pstream, tag);
1720 }
1721
1722 static void command_add_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1723 struct connection *c = userdata;
1724 const char *name, *module, *argument;
1725 uint32_t type;
1726 assert(c && t);
1727
1728 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1729 pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1730 pa_tagstruct_gets(t, &module) < 0 || !module ||
1731 pa_tagstruct_gets(t, &argument) < 0 ||
1732 !pa_tagstruct_eof(t)) {
1733 protocol_error(c);
1734 return;
1735 }
1736
1737 if (!c->authorized) {
1738 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1739 return;
1740 }
1741
1742 if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument) < 0) {
1743 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
1744 return;
1745 }
1746
1747 pa_pstream_send_simple_ack(c->pstream, tag);
1748 }
1749
1750 static void command_remove_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1751 struct connection *c = userdata;
1752 const char *name;
1753 uint32_t type;
1754 assert(c && t);
1755
1756 if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1757 pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1758 !pa_tagstruct_eof(t)) {
1759 protocol_error(c);
1760 return;
1761 }
1762
1763 if (!c->authorized) {
1764 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1765 return;
1766 }
1767
1768 if (pa_autoload_remove(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
1769 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1770 return;
1771 }
1772
1773 pa_pstream_send_simple_ack(c->pstream, tag);
1774 }
1775
1776 static void autoload_fill_tagstruct(struct pa_tagstruct *t, struct pa_autoload_entry *e) {
1777 assert(t && e);
1778 pa_tagstruct_puts(t, e->name);
1779 pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
1780 pa_tagstruct_puts(t, e->module);
1781 pa_tagstruct_puts(t, e->argument);
1782 }
1783
1784 static void command_get_autoload_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1785 struct connection *c = userdata;
1786 struct pa_autoload_entry *a = NULL;
1787 uint32_t type;
1788 const char *name;
1789 struct pa_tagstruct *reply;
1790 assert(c && t);
1791
1792 if (pa_tagstruct_gets(t, &name) < 0 || name ||
1793 pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1794 !pa_tagstruct_eof(t)) {
1795 protocol_error(c);
1796 return;
1797 }
1798
1799 if (!c->authorized) {
1800 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1801 return;
1802 }
1803
1804 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)) {
1805 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1806 return;
1807 }
1808
1809 reply = pa_tagstruct_new(NULL, 0);
1810 assert(reply);
1811 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1812 pa_tagstruct_putu32(reply, tag);
1813 autoload_fill_tagstruct(reply, a);
1814 pa_pstream_send_tagstruct(c->pstream, reply);
1815 }
1816
1817 static void command_get_autoload_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1818 struct connection *c = userdata;
1819 struct pa_tagstruct *reply;
1820 assert(c && t);
1821
1822 if (!pa_tagstruct_eof(t)) {
1823 protocol_error(c);
1824 return;
1825 }
1826
1827 if (!c->authorized) {
1828 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1829 return;
1830 }
1831
1832 reply = pa_tagstruct_new(NULL, 0);
1833 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1834 pa_tagstruct_putu32(reply, tag);
1835
1836 if (c->protocol->core->autoload_hashmap) {
1837 struct pa_autoload_entry *a;
1838 void *state = NULL;
1839
1840 while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state)))
1841 autoload_fill_tagstruct(reply, a);
1842 }
1843
1844 pa_pstream_send_tagstruct(c->pstream, reply);
1845 }
1846
1847 /*** pstream callbacks ***/
1848
1849 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
1850 struct connection *c = userdata;
1851 assert(p && packet && packet->data && c);
1852
1853 if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1854 pa_log(__FILE__": invalid packet.\n");
1855 connection_free(c);
1856 }
1857 }
1858
1859 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata) {
1860 struct connection *c = userdata;
1861 struct output_stream *stream;
1862 assert(p && chunk && userdata);
1863
1864 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1865 pa_log(__FILE__": client sent block for invalid stream.\n");
1866 connection_free(c);
1867 return;
1868 }
1869
1870 if (stream->type == PLAYBACK_STREAM) {
1871 struct playback_stream *p = (struct playback_stream*) stream;
1872 if (chunk->length >= p->requested_bytes)
1873 p->requested_bytes = 0;
1874 else
1875 p->requested_bytes -= chunk->length;
1876
1877 pa_memblockq_push_align(p->memblockq, chunk, delta);
1878 assert(p->sink_input);
1879 /* pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq)); */
1880
1881 pa_sink_notify(p->sink_input->sink);
1882 /* pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1883
1884 } else {
1885 struct upload_stream *u = (struct upload_stream*) stream;
1886 size_t l;
1887 assert(u->type == UPLOAD_STREAM);
1888
1889 if (!u->memchunk.memblock) {
1890 if (u->length == chunk->length) {
1891 u->memchunk = *chunk;
1892 pa_memblock_ref(u->memchunk.memblock);
1893 u->length = 0;
1894 } else {
1895 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
1896 u->memchunk.index = u->memchunk.length = 0;
1897 }
1898 }
1899
1900 assert(u->memchunk.memblock);
1901
1902 l = u->length;
1903 if (l > chunk->length)
1904 l = chunk->length;
1905
1906 if (l > 0) {
1907 memcpy((uint8_t*) u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length,
1908 (uint8_t*) chunk->memblock->data+chunk->index, l);
1909 u->memchunk.length += l;
1910 u->length -= l;
1911 }
1912 }
1913 }
1914
1915 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
1916 struct connection *c = userdata;
1917 assert(p && c);
1918 connection_free(c);
1919
1920 /* pa_log(__FILE__": connection died.\n");*/
1921 }
1922
1923
1924 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
1925 struct connection *c = userdata;
1926 assert(p && c);
1927
1928 send_memblock(c);
1929 }
1930
1931 /*** client callbacks ***/
1932
1933 static void client_kill_cb(struct pa_client *c) {
1934 assert(c && c->userdata);
1935 connection_free(c->userdata);
1936 }
1937
1938 /*** socket server callbacks ***/
1939
1940 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
1941 struct pa_protocol_native *p = userdata;
1942 struct connection *c;
1943 assert(io && p);
1944
1945 c = pa_xmalloc(sizeof(struct connection));
1946
1947 c->authorized =!! p->public;
1948 c->protocol = p;
1949 assert(p->core);
1950 c->client = pa_client_new(p->core, "NATIVE", "Client");
1951 assert(c->client);
1952 c->client->kill = client_kill_cb;
1953 c->client->userdata = c;
1954 c->client->owner = p->module;
1955
1956 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
1957 assert(c->pstream);
1958
1959 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
1960 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
1961 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
1962 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
1963
1964 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
1965 assert(c->pdispatch);
1966
1967 c->record_streams = pa_idxset_new(NULL, NULL);
1968 c->output_streams = pa_idxset_new(NULL, NULL);
1969 assert(c->record_streams && c->output_streams);
1970
1971 c->rrobin_index = PA_IDXSET_INVALID;
1972 c->subscription = NULL;
1973
1974 pa_idxset_put(p->connections, c, NULL);
1975 }
1976
1977 /*** module entry points ***/
1978
1979 static struct pa_protocol_native* protocol_new_internal(struct pa_core *c, struct pa_module *m, struct pa_modargs *ma) {
1980 struct pa_protocol_native *p;
1981 int public = 0;
1982 assert(c && ma);
1983
1984 if (pa_modargs_get_value_boolean(ma, "public", &public) < 0) {
1985 pa_log(__FILE__": public= expects a boolean argument.\n");
1986 return NULL;
1987 }
1988
1989 p = pa_xmalloc(sizeof(struct pa_protocol_native));
1990
1991 if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
1992 pa_xfree(p);
1993 return NULL;
1994 }
1995
1996 p->module = m;
1997 p->public = public;
1998 p->server = NULL;
1999 p->core = c;
2000 p->connections = pa_idxset_new(NULL, NULL);
2001 assert(p->connections);
2002
2003 return p;
2004 }
2005
2006 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) {
2007 struct pa_protocol_native *p;
2008
2009 if (!(p = protocol_new_internal(core, m, ma)))
2010 return NULL;
2011
2012 p->server = server;
2013 pa_socket_server_set_callback(p->server, on_connection, p);
2014
2015 return p;
2016 }
2017
2018 void pa_protocol_native_free(struct pa_protocol_native *p) {
2019 struct connection *c;
2020 assert(p);
2021
2022 while ((c = pa_idxset_first(p->connections, NULL)))
2023 connection_free(c);
2024 pa_idxset_free(p->connections, NULL, NULL);
2025
2026 if (p->server)
2027 pa_socket_server_unref(p->server);
2028
2029 pa_xfree(p);
2030 }
2031
2032 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) {
2033 struct pa_protocol_native *p;
2034
2035 if (!(p = protocol_new_internal(core, m, ma)))
2036 return NULL;
2037
2038 on_connection(NULL, io, p);
2039
2040 return p;
2041 }