]> code.delx.au - pulseaudio/blob - polyp/protocol-native.c
move sample cache to namereg
[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
48 struct connection;
49 struct pa_protocol_native;
50
51 struct record_stream {
52 struct connection *connection;
53 uint32_t index;
54 struct pa_source_output *source_output;
55 struct pa_memblockq *memblockq;
56 size_t fragment_size;
57 };
58
59 struct playback_stream {
60 int type;
61 struct connection *connection;
62 uint32_t index;
63 struct pa_sink_input *sink_input;
64 struct pa_memblockq *memblockq;
65 size_t requested_bytes;
66 int drain_request;
67 uint32_t drain_tag;
68 };
69
70 struct upload_stream {
71 int type;
72 struct connection *connection;
73 uint32_t index;
74 struct pa_memchunk memchunk;
75 size_t length;
76 char *name;
77 struct pa_sample_spec sample_spec;
78 };
79
80 struct output_stream {
81 int type;
82 };
83
84 enum {
85 UPLOAD_STREAM,
86 PLAYBACK_STREAM
87 };
88
89 struct connection {
90 int authorized;
91 struct pa_protocol_native *protocol;
92 struct pa_client *client;
93 struct pa_pstream *pstream;
94 struct pa_pdispatch *pdispatch;
95 struct pa_idxset *record_streams, *output_streams;
96 uint32_t rrobin_index;
97 struct pa_subscription *subscription;
98 };
99
100 struct pa_protocol_native {
101 struct pa_module *module;
102 int public;
103 struct pa_core *core;
104 struct pa_socket_server *server;
105 struct pa_idxset *connections;
106 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
107 };
108
109 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
110 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length);
111 static void sink_input_kill_cb(struct pa_sink_input *i);
112 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);
113
114 static void request_bytes(struct playback_stream*s);
115
116 static void source_output_kill_cb(struct pa_source_output *o);
117 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
118
119 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
120 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
121 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
122 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
123 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
124 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
125 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
126 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
127 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
128 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
129 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
130 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
131 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
132 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
133 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
134 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
135 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
136 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
137 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
138
139 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
140 [PA_COMMAND_ERROR] = { NULL },
141 [PA_COMMAND_TIMEOUT] = { NULL },
142 [PA_COMMAND_REPLY] = { NULL },
143 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
144 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_stream },
145 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
146 [PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
147 [PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_stream },
148 [PA_COMMAND_AUTH] = { command_auth },
149 [PA_COMMAND_REQUEST] = { NULL },
150 [PA_COMMAND_EXIT] = { command_exit },
151 [PA_COMMAND_SET_NAME] = { command_set_name },
152 [PA_COMMAND_LOOKUP_SINK] = { command_lookup },
153 [PA_COMMAND_LOOKUP_SOURCE] = { command_lookup },
154 [PA_COMMAND_STAT] = { command_stat },
155 [PA_COMMAND_GET_PLAYBACK_LATENCY] = { command_get_playback_latency },
156 [PA_COMMAND_CREATE_UPLOAD_STREAM] = { command_create_upload_stream },
157 [PA_COMMAND_DELETE_UPLOAD_STREAM] = { command_delete_stream },
158 [PA_COMMAND_FINISH_UPLOAD_STREAM] = { command_finish_upload_stream },
159 [PA_COMMAND_PLAY_SAMPLE] = { command_play_sample },
160 [PA_COMMAND_REMOVE_SAMPLE] = { command_remove_sample },
161 [PA_COMMAND_GET_SINK_INFO] = { command_get_info },
162 [PA_COMMAND_GET_SOURCE_INFO] = { command_get_info },
163 [PA_COMMAND_GET_CLIENT_INFO] = { command_get_info },
164 [PA_COMMAND_GET_MODULE_INFO] = { command_get_info },
165 [PA_COMMAND_GET_SINK_INPUT_INFO] = { command_get_info },
166 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = { command_get_info },
167 [PA_COMMAND_GET_SAMPLE_INFO] = { command_get_info },
168 [PA_COMMAND_GET_SINK_INFO_LIST] = { command_get_info_list },
169 [PA_COMMAND_GET_SOURCE_INFO_LIST] = { command_get_info_list },
170 [PA_COMMAND_GET_MODULE_INFO_LIST] = { command_get_info_list },
171 [PA_COMMAND_GET_CLIENT_INFO_LIST] = { command_get_info_list },
172 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = { command_get_info_list },
173 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = { command_get_info_list },
174 [PA_COMMAND_GET_SAMPLE_INFO_LIST] = { command_get_info_list },
175 [PA_COMMAND_GET_SERVER_INFO] = { command_get_server_info },
176 [PA_COMMAND_SUBSCRIBE] = { command_subscribe },
177 [PA_COMMAND_SET_SINK_VOLUME] = { command_set_volume },
178 [PA_COMMAND_SET_SINK_INPUT_VOLUME] = { command_set_volume },
179 };
180
181 /* structure management */
182
183 static struct upload_stream* upload_stream_new(struct connection *c, const struct pa_sample_spec *ss, const char *name, size_t length) {
184 struct upload_stream *s;
185 assert(c && ss && name && length);
186
187 s = pa_xmalloc(sizeof(struct upload_stream));
188 s->type = UPLOAD_STREAM;
189 s->connection = c;
190 s->sample_spec = *ss;
191 s->name = pa_xstrdup(name);
192
193 s->memchunk.memblock = NULL;
194 s->memchunk.index = 0;
195 s->memchunk.length = 0;
196
197 s->length = length;
198
199 pa_idxset_put(c->output_streams, s, &s->index);
200 return s;
201 }
202
203 static void upload_stream_free(struct upload_stream *o) {
204 assert(o && o->connection);
205
206 pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
207
208 pa_xfree(o->name);
209
210 if (o->memchunk.memblock)
211 pa_memblock_unref(o->memchunk.memblock);
212
213 pa_xfree(o);
214 }
215
216 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) {
217 struct record_stream *s;
218 struct pa_source_output *source_output;
219 size_t base;
220 assert(c && source && ss && name && maxlength);
221
222 if (!(source_output = pa_source_output_new(source, name, ss)))
223 return NULL;
224
225 s = pa_xmalloc(sizeof(struct record_stream));
226 s->connection = c;
227 s->source_output = source_output;
228 s->source_output->push = source_output_push_cb;
229 s->source_output->kill = source_output_kill_cb;
230 s->source_output->userdata = s;
231 s->source_output->owner = c->protocol->module;
232 s->source_output->client = c->client;
233
234 s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0, c->protocol->core->memblock_stat);
235 assert(s->memblockq);
236
237 s->fragment_size = (fragment_size/base)*base;
238 if (!s->fragment_size)
239 s->fragment_size = base;
240
241 pa_idxset_put(c->record_streams, s, &s->index);
242 return s;
243 }
244
245 static void record_stream_free(struct record_stream* r) {
246 assert(r && r->connection);
247
248 pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
249 pa_source_output_free(r->source_output);
250 pa_memblockq_free(r->memblockq);
251 pa_xfree(r);
252 }
253
254 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name,
255 size_t maxlength,
256 size_t tlength,
257 size_t prebuf,
258 size_t minreq) {
259 struct playback_stream *s;
260 struct pa_sink_input *sink_input;
261 assert(c && sink && ss && name && maxlength);
262
263 if (!(sink_input = pa_sink_input_new(sink, name, ss)))
264 return NULL;
265
266 s = pa_xmalloc(sizeof(struct playback_stream));
267 s->type = PLAYBACK_STREAM;
268 s->connection = c;
269 s->sink_input = sink_input;
270
271 s->sink_input->peek = sink_input_peek_cb;
272 s->sink_input->drop = sink_input_drop_cb;
273 s->sink_input->kill = sink_input_kill_cb;
274 s->sink_input->get_latency = sink_input_get_latency_cb;
275 s->sink_input->userdata = s;
276 s->sink_input->owner = c->protocol->module;
277 s->sink_input->client = c->client;
278
279 s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq, c->protocol->core->memblock_stat);
280 assert(s->memblockq);
281
282 s->requested_bytes = 0;
283 s->drain_request = 0;
284
285 pa_idxset_put(c->output_streams, s, &s->index);
286 return s;
287 }
288
289 static void playback_stream_free(struct playback_stream* p) {
290 assert(p && p->connection);
291
292 if (p->drain_request)
293 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
294
295 pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
296 pa_sink_input_free(p->sink_input);
297 pa_memblockq_free(p->memblockq);
298 pa_xfree(p);
299 }
300
301 static void connection_free(struct connection *c) {
302 struct record_stream *r;
303 struct output_stream *o;
304 assert(c && c->protocol);
305
306 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
307 while ((r = pa_idxset_first(c->record_streams, NULL)))
308 record_stream_free(r);
309 pa_idxset_free(c->record_streams, NULL, NULL);
310
311 while ((o = pa_idxset_first(c->output_streams, NULL)))
312 if (o->type == PLAYBACK_STREAM)
313 playback_stream_free((struct playback_stream*) o);
314 else
315 upload_stream_free((struct upload_stream*) o);
316 pa_idxset_free(c->output_streams, NULL, NULL);
317
318 pa_pdispatch_unref(c->pdispatch);
319 pa_pstream_close(c->pstream);
320 pa_pstream_unref(c->pstream);
321 pa_client_free(c->client);
322
323 if (c->subscription)
324 pa_subscription_free(c->subscription);
325
326 pa_xfree(c);
327 }
328
329 static void request_bytes(struct playback_stream *s) {
330 struct pa_tagstruct *t;
331 size_t l;
332 assert(s);
333
334 if (!(l = pa_memblockq_missing(s->memblockq)))
335 return;
336
337 if (l <= s->requested_bytes)
338 return;
339
340 l -= s->requested_bytes;
341
342 if (l < pa_memblockq_get_minreq(s->memblockq))
343 return;
344
345 s->requested_bytes += l;
346
347 t = pa_tagstruct_new(NULL, 0);
348 assert(t);
349 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
350 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
351 pa_tagstruct_putu32(t, s->index);
352 pa_tagstruct_putu32(t, l);
353 pa_pstream_send_tagstruct(s->connection->pstream, t);
354
355 /*fprintf(stderr, "Requesting %u bytes\n", l);*/
356 }
357
358 static void send_memblock(struct connection *c) {
359 uint32_t start;
360 struct record_stream *r;
361
362 start = PA_IDXSET_INVALID;
363 for (;;) {
364 struct pa_memchunk chunk;
365
366 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
367 return;
368
369 if (start == PA_IDXSET_INVALID)
370 start = c->rrobin_index;
371 else if (start == c->rrobin_index)
372 return;
373
374 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
375 if (chunk.length > r->fragment_size)
376 chunk.length = r->fragment_size;
377
378 pa_pstream_send_memblock(c->pstream, r->index, 0, &chunk);
379 pa_memblockq_drop(r->memblockq, chunk.length);
380 pa_memblock_unref(chunk.memblock);
381
382 return;
383 }
384 }
385 }
386
387 static void send_playback_stream_killed(struct playback_stream *p) {
388 struct pa_tagstruct *t;
389 assert(p);
390
391 t = pa_tagstruct_new(NULL, 0);
392 assert(t);
393 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
394 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
395 pa_tagstruct_putu32(t, p->index);
396 pa_pstream_send_tagstruct(p->connection->pstream, t);
397 }
398
399 static void send_record_stream_killed(struct record_stream *r) {
400 struct pa_tagstruct *t;
401 assert(r);
402
403 t = pa_tagstruct_new(NULL, 0);
404 assert(t);
405 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
406 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
407 pa_tagstruct_putu32(t, r->index);
408 pa_pstream_send_tagstruct(r->connection->pstream, t);
409 }
410
411
412 /*** sinkinput callbacks ***/
413
414 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
415 struct playback_stream *s;
416 assert(i && i->userdata && chunk);
417 s = i->userdata;
418
419 if (pa_memblockq_peek(s->memblockq, chunk) < 0)
420 return -1;
421
422 return 0;
423 }
424
425 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
426 struct playback_stream *s;
427 assert(i && i->userdata && length);
428 s = i->userdata;
429
430 pa_memblockq_drop(s->memblockq, length);
431 request_bytes(s);
432
433 if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
434 pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
435 s->drain_request = 0;
436 }
437 }
438
439 static void sink_input_kill_cb(struct pa_sink_input *i) {
440 assert(i && i->userdata);
441 send_playback_stream_killed((struct playback_stream *) i->userdata);
442 playback_stream_free((struct playback_stream *) i->userdata);
443 }
444
445 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
446 struct playback_stream *s;
447 assert(i && i->userdata);
448 s = i->userdata;
449
450 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
451 }
452
453 /*** source_output callbacks ***/
454
455 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
456 struct record_stream *s;
457 assert(o && o->userdata && chunk);
458 s = o->userdata;
459
460 pa_memblockq_push(s->memblockq, chunk, 0);
461 if (!pa_pstream_is_pending(s->connection->pstream))
462 send_memblock(s->connection);
463 }
464
465 static void source_output_kill_cb(struct pa_source_output *o) {
466 assert(o && o->userdata);
467 send_record_stream_killed((struct record_stream *) o->userdata);
468 record_stream_free((struct record_stream *) o->userdata);
469 }
470
471 /*** pdispatch callbacks ***/
472
473 static void protocol_error(struct connection *c) {
474 fprintf(stderr, __FILE__": protocol error, kicking client\n");
475 connection_free(c);
476 }
477
478 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
479 struct connection *c = userdata;
480 struct playback_stream *s;
481 size_t maxlength, tlength, prebuf, minreq;
482 uint32_t sink_index;
483 const char *name, *sink_name;
484 struct pa_sample_spec ss;
485 struct pa_tagstruct *reply;
486 struct pa_sink *sink;
487 assert(c && t && c->protocol && c->protocol->core);
488
489 if (pa_tagstruct_gets(t, &name) < 0 ||
490 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
491 pa_tagstruct_getu32(t, &sink_index) < 0 ||
492 pa_tagstruct_gets(t, &sink_name) < 0 ||
493 pa_tagstruct_getu32(t, &maxlength) < 0 ||
494 pa_tagstruct_getu32(t, &tlength) < 0 ||
495 pa_tagstruct_getu32(t, &prebuf) < 0 ||
496 pa_tagstruct_getu32(t, &minreq) < 0 ||
497 !pa_tagstruct_eof(t)) {
498 protocol_error(c);
499 return;
500 }
501
502 if (!c->authorized) {
503 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
504 return;
505 }
506
507 if (sink_index != (uint32_t) -1)
508 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
509 else
510 sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
511
512 if (!sink) {
513 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
514 return;
515 }
516
517 if (!(s = playback_stream_new(c, sink, &ss, name, maxlength, tlength, prebuf, minreq))) {
518 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
519 return;
520 }
521
522 reply = pa_tagstruct_new(NULL, 0);
523 assert(reply);
524 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
525 pa_tagstruct_putu32(reply, tag);
526 pa_tagstruct_putu32(reply, s->index);
527 assert(s->sink_input);
528 pa_tagstruct_putu32(reply, s->sink_input->index);
529 pa_pstream_send_tagstruct(c->pstream, reply);
530 request_bytes(s);
531 }
532
533 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
534 struct connection *c = userdata;
535 uint32_t channel;
536 assert(c && t);
537
538 if (pa_tagstruct_getu32(t, &channel) < 0 ||
539 !pa_tagstruct_eof(t)) {
540 protocol_error(c);
541 return;
542 }
543
544 if (!c->authorized) {
545 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
546 return;
547 }
548
549 if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
550 struct playback_stream *s;
551 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
552 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
553 return;
554 }
555
556 playback_stream_free(s);
557 } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
558 struct record_stream *s;
559 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
560 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
561 return;
562 }
563
564 record_stream_free(s);
565 } else {
566 struct upload_stream *s;
567 assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
568 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
569 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
570 return;
571 }
572
573 upload_stream_free(s);
574 }
575
576 pa_pstream_send_simple_ack(c->pstream, tag);
577 }
578
579 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
580 struct connection *c = userdata;
581 struct record_stream *s;
582 size_t maxlength, fragment_size;
583 uint32_t source_index;
584 const char *name, *source_name;
585 struct pa_sample_spec ss;
586 struct pa_tagstruct *reply;
587 struct pa_source *source;
588 assert(c && t && c->protocol && c->protocol->core);
589
590 if (pa_tagstruct_gets(t, &name) < 0 ||
591 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
592 pa_tagstruct_getu32(t, &source_index) < 0 ||
593 pa_tagstruct_gets(t, &source_name) < 0 ||
594 pa_tagstruct_getu32(t, &maxlength) < 0 ||
595 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
596 !pa_tagstruct_eof(t)) {
597 protocol_error(c);
598 return;
599 }
600
601 if (!c->authorized) {
602 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
603 return;
604 }
605
606 if (source_index != (uint32_t) -1)
607 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
608 else
609 source = pa_namereg_get(c->protocol->core, *source_name ? source_name : NULL, PA_NAMEREG_SOURCE, 1);
610
611 if (!source) {
612 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
613 return;
614 }
615
616 if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
617 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
618 return;
619 }
620
621 reply = pa_tagstruct_new(NULL, 0);
622 assert(reply);
623 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
624 pa_tagstruct_putu32(reply, tag);
625 pa_tagstruct_putu32(reply, s->index);
626 assert(s->source_output);
627 pa_tagstruct_putu32(reply, s->source_output->index);
628 pa_pstream_send_tagstruct(c->pstream, reply);
629 }
630
631 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
632 struct connection *c = userdata;
633 assert(c && t);
634
635 if (!pa_tagstruct_eof(t)) {
636 protocol_error(c);
637 return;
638 }
639
640 if (!c->authorized) {
641 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
642 return;
643 }
644
645 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
646 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
647 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
648 return;
649 }
650
651 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
652 struct connection *c = userdata;
653 const void*cookie;
654 assert(c && t);
655
656 if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
657 !pa_tagstruct_eof(t)) {
658 protocol_error(c);
659 return;
660 }
661
662 if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
663 fprintf(stderr, "protocol-native.c: Denied access to client with invalid authorization key.\n");
664 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
665 return;
666 }
667
668 c->authorized = 1;
669 pa_pstream_send_simple_ack(c->pstream, tag);
670 return;
671 }
672
673 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
674 struct connection *c = userdata;
675 const char *name;
676 assert(c && t);
677
678 if (pa_tagstruct_gets(t, &name) < 0 ||
679 !pa_tagstruct_eof(t)) {
680 protocol_error(c);
681 return;
682 }
683
684 pa_client_rename(c->client, name);
685 pa_pstream_send_simple_ack(c->pstream, tag);
686 return;
687 }
688
689 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
690 struct connection *c = userdata;
691 const char *name;
692 uint32_t index = PA_IDXSET_INVALID;
693 assert(c && t);
694
695 if (pa_tagstruct_gets(t, &name) < 0 ||
696 !pa_tagstruct_eof(t)) {
697 protocol_error(c);
698 return;
699 }
700
701 if (!c->authorized) {
702 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
703 return;
704 }
705
706 if (command == PA_COMMAND_LOOKUP_SINK) {
707 struct pa_sink *sink;
708 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
709 index = sink->index;
710 } else {
711 struct pa_source *source;
712 assert(command == PA_COMMAND_LOOKUP_SOURCE);
713 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
714 index = source->index;
715 }
716
717 if (index == PA_IDXSET_INVALID)
718 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
719 else {
720 struct pa_tagstruct *reply;
721 reply = pa_tagstruct_new(NULL, 0);
722 assert(reply);
723 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
724 pa_tagstruct_putu32(reply, tag);
725 pa_tagstruct_putu32(reply, index);
726 pa_pstream_send_tagstruct(c->pstream, reply);
727 }
728 }
729
730 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
731 struct connection *c = userdata;
732 uint32_t index;
733 struct playback_stream *s;
734 assert(c && t);
735
736 if (pa_tagstruct_getu32(t, &index) < 0 ||
737 !pa_tagstruct_eof(t)) {
738 protocol_error(c);
739 return;
740 }
741
742 if (!c->authorized) {
743 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
744 return;
745 }
746
747 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
748 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
749 return;
750 }
751
752 s->drain_request = 0;
753
754 pa_memblockq_prebuf_disable(s->memblockq);
755
756 if (!pa_memblockq_is_readable(s->memblockq))
757 pa_pstream_send_simple_ack(c->pstream, tag);
758 else {
759 s->drain_request = 1;
760 s->drain_tag = tag;
761
762 pa_sink_notify(s->sink_input->sink);
763 }
764 }
765
766 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
767 struct connection *c = userdata;
768 struct pa_tagstruct *reply;
769 assert(c && t);
770
771 if (!pa_tagstruct_eof(t)) {
772 protocol_error(c);
773 return;
774 }
775
776 if (!c->authorized) {
777 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
778 return;
779 }
780
781 reply = pa_tagstruct_new(NULL, 0);
782 assert(reply);
783 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
784 pa_tagstruct_putu32(reply, tag);
785 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total);
786 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total_size);
787 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated);
788 pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated_size);
789 pa_pstream_send_tagstruct(c->pstream, reply);
790 }
791
792 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
793 struct connection *c = userdata;
794 struct pa_tagstruct *reply;
795 struct playback_stream *s;
796 uint32_t index, latency;
797 assert(c && t);
798
799 if (pa_tagstruct_getu32(t, &index) < 0 ||
800 !pa_tagstruct_eof(t)) {
801 protocol_error(c);
802 return;
803 }
804
805 if (!c->authorized) {
806 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
807 return;
808 }
809
810 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
811 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
812 return;
813 }
814
815 latency = pa_sink_input_get_latency(s->sink_input);
816 reply = pa_tagstruct_new(NULL, 0);
817 assert(reply);
818 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
819 pa_tagstruct_putu32(reply, tag);
820 pa_tagstruct_putu32(reply, latency);
821 pa_pstream_send_tagstruct(c->pstream, reply);
822 }
823
824 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
825 struct connection *c = userdata;
826 struct upload_stream *s;
827 size_t length;
828 const char *name;
829 struct pa_sample_spec ss;
830 struct pa_tagstruct *reply;
831 assert(c && t && c->protocol && c->protocol->core);
832
833 if (pa_tagstruct_gets(t, &name) < 0 ||
834 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
835 pa_tagstruct_getu32(t, &length) < 0 ||
836 !pa_tagstruct_eof(t)) {
837 protocol_error(c);
838 return;
839 }
840
841 if (!c->authorized) {
842 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
843 return;
844 }
845
846 if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
847 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
848 return;
849 }
850
851 if (!(s = upload_stream_new(c, &ss, name, length))) {
852 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
853 return;
854 }
855
856 reply = pa_tagstruct_new(NULL, 0);
857 assert(reply);
858 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
859 pa_tagstruct_putu32(reply, tag);
860 pa_tagstruct_putu32(reply, s->index);
861 pa_pstream_send_tagstruct(c->pstream, reply);
862
863 reply = pa_tagstruct_new(NULL, 0);
864 assert(reply);
865 pa_tagstruct_putu32(reply, PA_COMMAND_REQUEST);
866 pa_tagstruct_putu32(reply, (uint32_t) -1); /* tag */
867 pa_tagstruct_putu32(reply, s->index);
868 pa_tagstruct_putu32(reply, length);
869 pa_pstream_send_tagstruct(c->pstream, reply);
870 }
871
872 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
873 struct connection *c = userdata;
874 uint32_t channel;
875 struct upload_stream *s;
876 uint32_t index;
877 assert(c && t);
878
879 if (pa_tagstruct_getu32(t, &channel) < 0 ||
880 !pa_tagstruct_eof(t)) {
881 protocol_error(c);
882 return;
883 }
884
885 if (!c->authorized) {
886 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
887 return;
888 }
889
890 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
891 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
892 return;
893 }
894
895 pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
896 pa_pstream_send_simple_ack(c->pstream, tag);
897 upload_stream_free(s);
898 }
899
900 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
901 struct connection *c = userdata;
902 uint32_t sink_index, volume;
903 struct pa_sink *sink;
904 const char *name, *sink_name;
905 assert(c && t);
906
907 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
908 pa_tagstruct_gets(t, &sink_name) < 0 ||
909 pa_tagstruct_getu32(t, &volume) < 0 ||
910 pa_tagstruct_gets(t, &name) < 0 ||
911 !pa_tagstruct_eof(t)) {
912 protocol_error(c);
913 return;
914 }
915
916 if (!c->authorized) {
917 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
918 return;
919 }
920
921 if (sink_index != (uint32_t) -1)
922 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
923 else
924 sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
925
926 if (!sink) {
927 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
928 return;
929 }
930
931 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
932 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
933 return;
934 }
935
936 pa_pstream_send_simple_ack(c->pstream, tag);
937 }
938
939 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
940 struct connection *c = userdata;
941 const char *name;
942 assert(c && t);
943
944 if (pa_tagstruct_gets(t, &name) < 0 ||
945 !pa_tagstruct_eof(t)) {
946 protocol_error(c);
947 return;
948 }
949
950 if (!c->authorized) {
951 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
952 return;
953 }
954
955 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
956 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
957 return;
958 }
959
960 pa_pstream_send_simple_ack(c->pstream, tag);
961 }
962
963 static void sink_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink *sink) {
964 assert(t && sink);
965 pa_tagstruct_putu32(t, sink->index);
966 pa_tagstruct_puts(t, sink->name);
967 pa_tagstruct_puts(t, sink->description ? sink->description : "");
968 pa_tagstruct_put_sample_spec(t, &sink->sample_spec);
969 pa_tagstruct_putu32(t, sink->owner ? sink->owner->index : (uint32_t) -1);
970 pa_tagstruct_putu32(t, sink->volume);
971 pa_tagstruct_putu32(t, sink->monitor_source->index);
972 pa_tagstruct_puts(t, sink->monitor_source->name);
973 pa_tagstruct_putu32(t, pa_sink_get_latency(sink));
974 }
975
976 static void source_fill_tagstruct(struct pa_tagstruct *t, struct pa_source *source) {
977 assert(t && source);
978 pa_tagstruct_putu32(t, source->index);
979 pa_tagstruct_puts(t, source->name);
980 pa_tagstruct_puts(t, source->description ? source->description : "");
981 pa_tagstruct_put_sample_spec(t, &source->sample_spec);
982 pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
983 pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
984 pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : "");
985 }
986
987 static void client_fill_tagstruct(struct pa_tagstruct *t, struct pa_client *client) {
988 assert(t && client);
989 pa_tagstruct_putu32(t, client->index);
990 pa_tagstruct_puts(t, client->name);
991 pa_tagstruct_puts(t, client->protocol_name);
992 pa_tagstruct_putu32(t, client->owner ? client->owner->index : (uint32_t) -1);
993 }
994
995 static void module_fill_tagstruct(struct pa_tagstruct *t, struct pa_module *module) {
996 assert(t && module);
997 pa_tagstruct_putu32(t, module->index);
998 pa_tagstruct_puts(t, module->name);
999 pa_tagstruct_puts(t, module->argument ? module->argument : "");
1000 pa_tagstruct_putu32(t, module->n_used);
1001 pa_tagstruct_putu32(t, module->auto_unload);
1002 }
1003
1004 static void sink_input_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink_input *s) {
1005 assert(t && s);
1006 pa_tagstruct_putu32(t, s->index);
1007 pa_tagstruct_puts(t, s->name ? s->name : "");
1008 pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1009 pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1010 pa_tagstruct_putu32(t, s->sink->index);
1011 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1012 pa_tagstruct_putu32(t, s->volume);
1013 pa_tagstruct_putu32(t, pa_sink_input_get_latency(s));
1014 }
1015
1016 static void source_output_fill_tagstruct(struct pa_tagstruct *t, struct pa_source_output *s) {
1017 assert(t && s);
1018 pa_tagstruct_putu32(t, s->index);
1019 pa_tagstruct_puts(t, s->name ? s->name : "");
1020 pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1021 pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1022 pa_tagstruct_putu32(t, s->source->index);
1023 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1024 }
1025
1026 static void scache_fill_tagstruct(struct pa_tagstruct *t, struct pa_scache_entry *e) {
1027 assert(t && e);
1028 pa_tagstruct_putu32(t, e->index);
1029 pa_tagstruct_puts(t, e->name);
1030 pa_tagstruct_putu32(t, e->volume);
1031 pa_tagstruct_putu32(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1032 pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1033 }
1034
1035 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1036 struct connection *c = userdata;
1037 uint32_t index;
1038 struct pa_sink *sink = NULL;
1039 struct pa_source *source = NULL;
1040 struct pa_client *client = NULL;
1041 struct pa_module *module = NULL;
1042 struct pa_sink_input *si = NULL;
1043 struct pa_source_output *so = NULL;
1044 struct pa_scache_entry *sce = NULL;
1045 const char *name;
1046 struct pa_tagstruct *reply;
1047 assert(c && t);
1048
1049
1050 if (pa_tagstruct_getu32(t, &index) < 0 ||
1051 (command != PA_COMMAND_GET_CLIENT_INFO &&
1052 command != PA_COMMAND_GET_MODULE_INFO &&
1053 command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1054 command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1055 pa_tagstruct_gets(t, &name) < 0) ||
1056 !pa_tagstruct_eof(t)) {
1057 protocol_error(c);
1058 return;
1059 }
1060
1061 if (!c->authorized) {
1062 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1063 return;
1064 }
1065
1066 if (command == PA_COMMAND_GET_SINK_INFO) {
1067 if (index != (uint32_t) -1)
1068 sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1069 else
1070 sink = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SINK, 1);
1071 } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1072 if (index != (uint32_t) -1)
1073 source = pa_idxset_get_by_index(c->protocol->core->sources, index);
1074 else
1075 source = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SOURCE, 1);
1076 } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1077 client = pa_idxset_get_by_index(c->protocol->core->clients, index);
1078 else if (command == PA_COMMAND_GET_MODULE_INFO)
1079 module = pa_idxset_get_by_index(c->protocol->core->modules, index);
1080 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1081 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1082 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1083 so = pa_idxset_get_by_index(c->protocol->core->source_outputs, index);
1084 else {
1085 assert(command == PA_COMMAND_GET_SAMPLE_INFO && name);
1086 if (index != (uint32_t) -1)
1087 sce = pa_idxset_get_by_index(c->protocol->core->scache, index);
1088 else
1089 sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1090 }
1091
1092 if (!sink && !source && !client && !module && !si && !so && !sce) {
1093 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1094 return;
1095 }
1096
1097 reply = pa_tagstruct_new(NULL, 0);
1098 assert(reply);
1099 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1100 pa_tagstruct_putu32(reply, tag);
1101 if (sink)
1102 sink_fill_tagstruct(reply, sink);
1103 else if (source)
1104 source_fill_tagstruct(reply, source);
1105 else if (client)
1106 client_fill_tagstruct(reply, client);
1107 else if (module)
1108 module_fill_tagstruct(reply, module);
1109 else if (si)
1110 sink_input_fill_tagstruct(reply, si);
1111 else if (so)
1112 source_output_fill_tagstruct(reply, so);
1113 else
1114 scache_fill_tagstruct(reply, sce);
1115 pa_pstream_send_tagstruct(c->pstream, reply);
1116 }
1117
1118 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1119 struct connection *c = userdata;
1120 struct pa_idxset *i;
1121 uint32_t index;
1122 void *p;
1123 struct pa_tagstruct *reply;
1124 assert(c && t);
1125
1126 if (!pa_tagstruct_eof(t)) {
1127 protocol_error(c);
1128 return;
1129 }
1130
1131 if (!c->authorized) {
1132 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1133 return;
1134 }
1135
1136 reply = pa_tagstruct_new(NULL, 0);
1137 assert(reply);
1138 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1139 pa_tagstruct_putu32(reply, tag);
1140
1141 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1142 i = c->protocol->core->sinks;
1143 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1144 i = c->protocol->core->sources;
1145 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1146 i = c->protocol->core->clients;
1147 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1148 i = c->protocol->core->modules;
1149 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1150 i = c->protocol->core->sink_inputs;
1151 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1152 i = c->protocol->core->source_outputs;
1153 else {
1154 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1155 i = c->protocol->core->scache;
1156 }
1157
1158 for (p = pa_idxset_first(i, &index); p; p = pa_idxset_next(i, &index)) {
1159 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1160 sink_fill_tagstruct(reply, p);
1161 else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1162 source_fill_tagstruct(reply, p);
1163 else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1164 client_fill_tagstruct(reply, p);
1165 else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1166 module_fill_tagstruct(reply, p);
1167 else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1168 sink_input_fill_tagstruct(reply, p);
1169 else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1170 source_output_fill_tagstruct(reply, p);
1171 else {
1172 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1173 scache_fill_tagstruct(reply, p);
1174 }
1175 }
1176
1177 pa_pstream_send_tagstruct(c->pstream, reply);
1178 }
1179
1180 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1181 struct connection *c = userdata;
1182 struct pa_tagstruct *reply;
1183 char txt[256];
1184 assert(c && t);
1185
1186 if (!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 reply = pa_tagstruct_new(NULL, 0);
1197 assert(reply);
1198 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1199 pa_tagstruct_putu32(reply, tag);
1200 pa_tagstruct_puts(reply, PACKAGE_NAME);
1201 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1202 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1203 pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
1204 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1205 pa_pstream_send_tagstruct(c->pstream, reply);
1206 }
1207
1208 static void subscription_cb(struct pa_core *core, enum pa_subscription_event_type e, uint32_t index, void *userdata) {
1209 struct pa_tagstruct *t;
1210 struct connection *c = userdata;
1211 assert(c && core);
1212
1213 t = pa_tagstruct_new(NULL, 0);
1214 assert(t);
1215 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1216 pa_tagstruct_putu32(t, (uint32_t) -1);
1217 pa_tagstruct_putu32(t, e);
1218 pa_tagstruct_putu32(t, index);
1219 pa_pstream_send_tagstruct(c->pstream, t);
1220 }
1221
1222 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1223 struct connection *c = userdata;
1224 enum pa_subscription_mask m;
1225 assert(c && t);
1226
1227 if (pa_tagstruct_getu32(t, &m) < 0 ||
1228 !pa_tagstruct_eof(t)) {
1229 protocol_error(c);
1230 return;
1231 }
1232
1233 if (!c->authorized) {
1234 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1235 return;
1236 }
1237
1238 if (c->subscription)
1239 pa_subscription_free(c->subscription);
1240
1241 if (m != 0) {
1242 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1243 assert(c->subscription);
1244 } else
1245 c->subscription = NULL;
1246
1247 pa_pstream_send_simple_ack(c->pstream, tag);
1248 }
1249
1250 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1251 struct connection *c = userdata;
1252 uint32_t index, volume;
1253 struct pa_sink *sink = NULL;
1254 struct pa_sink_input *si = NULL;
1255 const char *name = NULL;
1256 assert(c && t);
1257
1258 if (pa_tagstruct_getu32(t, &index) < 0 ||
1259 (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1260 pa_tagstruct_getu32(t, &volume) ||
1261 !pa_tagstruct_eof(t)) {
1262 protocol_error(c);
1263 return;
1264 }
1265
1266 if (!c->authorized) {
1267 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1268 return;
1269 }
1270
1271 if (command == PA_COMMAND_SET_SINK_VOLUME) {
1272 if (index != (uint32_t) -1)
1273 sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1274 else
1275 sink = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SINK, 1);
1276 } else {
1277 assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1278 si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1279 }
1280
1281 if (!si && !sink) {
1282 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1283 return;
1284 }
1285
1286 if (sink)
1287 pa_sink_set_volume(sink, volume);
1288 else if (si)
1289 pa_sink_input_set_volume(si, volume);
1290
1291 pa_pstream_send_simple_ack(c->pstream, tag);
1292 }
1293
1294
1295 /*** pstream callbacks ***/
1296
1297 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
1298 struct connection *c = userdata;
1299 assert(p && packet && packet->data && c);
1300
1301 if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1302 fprintf(stderr, "protocol-native: invalid packet.\n");
1303 connection_free(c);
1304 }
1305 }
1306
1307 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) {
1308 struct connection *c = userdata;
1309 struct output_stream *stream;
1310 assert(p && chunk && userdata);
1311
1312 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1313 fprintf(stderr, "protocol-native: client sent block for invalid stream.\n");
1314 connection_free(c);
1315 return;
1316 }
1317
1318 if (stream->type == PLAYBACK_STREAM) {
1319 struct playback_stream *p = (struct playback_stream*) stream;
1320 if (chunk->length >= p->requested_bytes)
1321 p->requested_bytes = 0;
1322 else
1323 p->requested_bytes -= chunk->length;
1324
1325 pa_memblockq_push_align(p->memblockq, chunk, delta);
1326 assert(p->sink_input);
1327 pa_sink_notify(p->sink_input->sink);
1328 /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
1329 } else {
1330 struct upload_stream *u = (struct upload_stream*) stream;
1331 size_t l;
1332 assert(u->type == UPLOAD_STREAM);
1333
1334 if (!u->memchunk.memblock) {
1335 if (u->length == chunk->length) {
1336 u->memchunk = *chunk;
1337 pa_memblock_ref(u->memchunk.memblock);
1338 u->length = 0;
1339 fprintf(stderr, "COPY\n");
1340 } else {
1341 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
1342 u->memchunk.index = u->memchunk.length = 0;
1343 }
1344 }
1345
1346 assert(u->memchunk.memblock);
1347
1348 l = u->length;
1349 if (l > chunk->length)
1350 l = chunk->length;
1351
1352 if (l > 0) {
1353 memcpy(u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length, chunk->memblock->data+chunk->index, l);
1354 u->memchunk.length += l;
1355 u->length -= l;
1356 }
1357 }
1358 }
1359
1360 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
1361 struct connection *c = userdata;
1362 assert(p && c);
1363 connection_free(c);
1364
1365 fprintf(stderr, "protocol-native: connection died.\n");
1366 }
1367
1368
1369 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
1370 struct connection *c = userdata;
1371 assert(p && c);
1372
1373 send_memblock(c);
1374 }
1375
1376 /*** client callbacks ***/
1377
1378 static void client_kill_cb(struct pa_client *c) {
1379 assert(c && c->userdata);
1380 connection_free(c->userdata);
1381 }
1382
1383 /*** socket server callbacks ***/
1384
1385 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
1386 struct pa_protocol_native *p = userdata;
1387 struct connection *c;
1388 assert(s && io && p);
1389
1390 c = pa_xmalloc(sizeof(struct connection));
1391 c->authorized = p->public;
1392 c->protocol = p;
1393 assert(p->core);
1394 c->client = pa_client_new(p->core, "NATIVE", "Client");
1395 assert(c->client);
1396 c->client->kill = client_kill_cb;
1397 c->client->userdata = c;
1398 c->client->owner = p->module;
1399
1400 c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
1401 assert(c->pstream);
1402
1403 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
1404 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
1405 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
1406 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
1407
1408 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
1409 assert(c->pdispatch);
1410
1411 c->record_streams = pa_idxset_new(NULL, NULL);
1412 c->output_streams = pa_idxset_new(NULL, NULL);
1413 assert(c->record_streams && c->output_streams);
1414
1415 c->rrobin_index = PA_IDXSET_INVALID;
1416 c->subscription = NULL;
1417
1418 pa_idxset_put(p->connections, c, NULL);
1419 }
1420
1421 /*** module entry points ***/
1422
1423 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) {
1424 struct pa_protocol_native *p;
1425 uint32_t public;
1426 assert(core && server && ma);
1427
1428 if (pa_modargs_get_value_u32(ma, "public", &public) < 0) {
1429 fprintf(stderr, __FILE__": public= expects numeric argument.\n");
1430 return NULL;
1431 }
1432
1433 p = pa_xmalloc(sizeof(struct pa_protocol_native));
1434
1435 if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
1436 pa_xfree(p);
1437 return NULL;
1438 }
1439
1440 p->module = m;
1441 p->public = public;
1442 p->server = server;
1443 p->core = core;
1444 p->connections = pa_idxset_new(NULL, NULL);
1445 assert(p->connections);
1446
1447 pa_socket_server_set_callback(p->server, on_connection, p);
1448
1449 return p;
1450 }
1451
1452 void pa_protocol_native_free(struct pa_protocol_native *p) {
1453 struct connection *c;
1454 assert(p);
1455
1456 while ((c = pa_idxset_first(p->connections, NULL)))
1457 connection_free(c);
1458 pa_idxset_free(p->connections, NULL, NULL);
1459 pa_socket_server_unref(p->server);
1460 pa_xfree(p);
1461 }