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