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