]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-esound.c
49a78d410234d922a58f10e0d04ab90c406c50c6
[pulseaudio] / src / pulsecore / protocol-esound.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <limits.h>
35
36 #include <pulse/sample.h>
37 #include <pulse/timeval.h>
38 #include <pulse/utf8.h>
39 #include <pulse/xmalloc.h>
40
41 #include <pulsecore/esound.h>
42 #include <pulsecore/memblock.h>
43 #include <pulsecore/client.h>
44 #include <pulsecore/sink-input.h>
45 #include <pulsecore/sink.h>
46 #include <pulsecore/source-output.h>
47 #include <pulsecore/source.h>
48 #include <pulsecore/core-scache.h>
49 #include <pulsecore/sample-util.h>
50 #include <pulsecore/authkey.h>
51 #include <pulsecore/namereg.h>
52 #include <pulsecore/log.h>
53 #include <pulsecore/core-util.h>
54 #include <pulsecore/core-error.h>
55 #include <pulsecore/ipacl.h>
56
57 #include "endianmacros.h"
58
59 #include "protocol-esound.h"
60
61 /* Don't accept more connection than this */
62 #define MAX_CONNECTIONS 64
63
64 /* Kick a client if it doesn't authenticate within this time */
65 #define AUTH_TIMEOUT 5
66
67 #define DEFAULT_COOKIE_FILE ".esd_auth"
68
69 #define PLAYBACK_BUFFER_SECONDS (.25)
70 #define PLAYBACK_BUFFER_FRAGMENTS (10)
71 #define RECORD_BUFFER_SECONDS (5)
72 #define RECORD_BUFFER_FRAGMENTS (100)
73
74 #define MAX_CACHE_SAMPLE_SIZE (1024000)
75
76 #define SCACHE_PREFIX "esound."
77
78 /* This is heavily based on esound's code */
79
80 struct connection {
81 uint32_t index;
82 int dead;
83 pa_protocol_esound *protocol;
84 pa_iochannel *io;
85 pa_client *client;
86 int authorized, swap_byte_order;
87 void *write_data;
88 size_t write_data_alloc, write_data_index, write_data_length;
89 void *read_data;
90 size_t read_data_alloc, read_data_length;
91 esd_proto_t request;
92 esd_client_state_t state;
93 pa_sink_input *sink_input;
94 pa_source_output *source_output;
95 pa_memblockq *input_memblockq, *output_memblockq;
96 pa_defer_event *defer_event;
97
98 char *original_name;
99
100 struct {
101 pa_memblock *current_memblock;
102 size_t memblock_index, fragment_size;
103 } playback;
104
105 struct {
106 pa_memchunk memchunk;
107 char *name;
108 pa_sample_spec sample_spec;
109 } scache;
110
111 pa_time_event *auth_timeout_event;
112 };
113
114 struct pa_protocol_esound {
115 int public;
116 pa_module *module;
117 pa_core *core;
118 pa_socket_server *server;
119 pa_idxset *connections;
120 char *sink_name, *source_name;
121 unsigned n_player;
122 uint8_t esd_key[ESD_KEY_LEN];
123 pa_ip_acl *auth_ip_acl;
124 };
125
126 typedef struct proto_handler {
127 size_t data_length;
128 int (*proc)(struct connection *c, esd_proto_t request, const void *data, size_t length);
129 const char *description;
130 } esd_proto_handler_info_t;
131
132 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length);
133 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk);
134 static void sink_input_kill_cb(pa_sink_input *i);
135 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i);
136 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
137
138 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
139 static void source_output_kill_cb(pa_source_output *o);
140
141 static int esd_proto_connect(struct connection *c, esd_proto_t request, const void *data, size_t length);
142 static int esd_proto_stream_play(struct connection *c, esd_proto_t request, const void *data, size_t length);
143 static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length);
144 static int esd_proto_get_latency(struct connection *c, esd_proto_t request, const void *data, size_t length);
145 static int esd_proto_server_info(struct connection *c, esd_proto_t request, const void *data, size_t length);
146 static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length);
147 static int esd_proto_stream_pan(struct connection *c, esd_proto_t request, const void *data, size_t length);
148 static int esd_proto_sample_cache(struct connection *c, esd_proto_t request, const void *data, size_t length);
149 static int esd_proto_sample_free_or_play(struct connection *c, esd_proto_t request, const void *data, size_t length);
150 static int esd_proto_sample_get_id(struct connection *c, esd_proto_t request, const void *data, size_t length);
151 static int esd_proto_standby_or_resume(struct connection *c, esd_proto_t request, const void *data, size_t length);
152
153 /* the big map of protocol handler info */
154 static struct proto_handler proto_map[ESD_PROTO_MAX] = {
155 { ESD_KEY_LEN + sizeof(int), esd_proto_connect, "connect" },
156 { ESD_KEY_LEN + sizeof(int), NULL, "lock" },
157 { ESD_KEY_LEN + sizeof(int), NULL, "unlock" },
158
159 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_play, "stream play" },
160 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream rec" },
161 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream mon" },
162
163 { ESD_NAME_MAX + 3 * sizeof(int), esd_proto_sample_cache, "sample cache" }, /* 6 */
164 { sizeof(int), esd_proto_sample_free_or_play, "sample free" },
165 { sizeof(int), esd_proto_sample_free_or_play, "sample play" }, /* 8 */
166 { sizeof(int), NULL, "sample loop" },
167 { sizeof(int), NULL, "sample stop" },
168 { -1, NULL, "TODO: sample kill" },
169
170 { ESD_KEY_LEN + sizeof(int), esd_proto_standby_or_resume, "standby" }, /* NOOP! */
171 { ESD_KEY_LEN + sizeof(int), esd_proto_standby_or_resume, "resume" }, /* NOOP! */ /* 13 */
172
173 { ESD_NAME_MAX, esd_proto_sample_get_id, "sample getid" }, /* 14 */
174 { ESD_NAME_MAX + 2 * sizeof(int), NULL, "stream filter" },
175
176 { sizeof(int), esd_proto_server_info, "server info" },
177 { sizeof(int), esd_proto_all_info, "all info" },
178 { -1, NULL, "TODO: subscribe" },
179 { -1, NULL, "TODO: unsubscribe" },
180
181 { 3 * sizeof(int), esd_proto_stream_pan, "stream pan"},
182 { 3 * sizeof(int), NULL, "sample pan" },
183
184 { sizeof(int), NULL, "standby mode" },
185 { 0, esd_proto_get_latency, "get latency" }
186 };
187
188 static void connection_free(struct connection *c) {
189 assert(c);
190 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
191
192 if (c->state == ESD_STREAMING_DATA)
193 c->protocol->n_player--;
194
195 pa_client_free(c->client);
196
197 if (c->sink_input) {
198 pa_sink_input_disconnect(c->sink_input);
199 pa_sink_input_unref(c->sink_input);
200 }
201
202 if (c->source_output) {
203 pa_source_output_disconnect(c->source_output);
204 pa_source_output_unref(c->source_output);
205 }
206
207 if (c->input_memblockq)
208 pa_memblockq_free(c->input_memblockq);
209 if (c->output_memblockq)
210 pa_memblockq_free(c->output_memblockq);
211
212 if (c->playback.current_memblock)
213 pa_memblock_unref(c->playback.current_memblock);
214
215 pa_xfree(c->read_data);
216 pa_xfree(c->write_data);
217
218 if (c->io)
219 pa_iochannel_free(c->io);
220
221 if (c->defer_event)
222 c->protocol->core->mainloop->defer_free(c->defer_event);
223
224 if (c->scache.memchunk.memblock)
225 pa_memblock_unref(c->scache.memchunk.memblock);
226 pa_xfree(c->scache.name);
227
228 if (c->auth_timeout_event)
229 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
230
231 pa_xfree(c->original_name);
232 pa_xfree(c);
233 }
234
235 static void connection_write_prepare(struct connection *c, size_t length) {
236 size_t t;
237 assert(c);
238
239 t = c->write_data_length+length;
240
241 if (c->write_data_alloc < t)
242 c->write_data = pa_xrealloc(c->write_data, c->write_data_alloc = t);
243
244 assert(c->write_data);
245 }
246
247 static void connection_write(struct connection *c, const void *data, size_t length) {
248 size_t i;
249 assert(c);
250
251 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
252 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
253
254 connection_write_prepare(c, length);
255
256 assert(c->write_data);
257
258 i = c->write_data_length;
259 c->write_data_length += length;
260
261 memcpy((char*)c->write_data + i, data, length);
262 }
263
264 static void format_esd2native(int format, int swap_bytes, pa_sample_spec *ss) {
265 assert(ss);
266
267 ss->channels = ((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1;
268 if ((format & ESD_MASK_BITS) == ESD_BITS16)
269 ss->format = swap_bytes ? PA_SAMPLE_S16RE : PA_SAMPLE_S16NE;
270 else
271 ss->format = PA_SAMPLE_U8;
272 }
273
274 static int format_native2esd(pa_sample_spec *ss) {
275 int format = 0;
276
277 format = (ss->format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
278 format |= (ss->channels >= 2) ? ESD_STEREO : ESD_MONO;
279
280 return format;
281 }
282
283 #define CHECK_VALIDITY(expression, ...) do { \
284 if (!(expression)) { \
285 pa_log_warn(__FILE__ ": " __VA_ARGS__); \
286 return -1; \
287 } \
288 } while(0);
289
290 /*** esound commands ***/
291
292 static int esd_proto_connect(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
293 uint32_t ekey;
294 int ok;
295
296 assert(length == (ESD_KEY_LEN + sizeof(uint32_t)));
297
298 if (!c->authorized) {
299 if (memcmp(data, c->protocol->esd_key, ESD_KEY_LEN) != 0) {
300 pa_log("kicked client with invalid authorization key.");
301 return -1;
302 }
303
304 c->authorized = 1;
305 if (c->auth_timeout_event) {
306 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
307 c->auth_timeout_event = NULL;
308 }
309 }
310
311 data = (const char*)data + ESD_KEY_LEN;
312
313 memcpy(&ekey, data, sizeof(uint32_t));
314 if (ekey == ESD_ENDIAN_KEY)
315 c->swap_byte_order = 0;
316 else if (ekey == ESD_SWAP_ENDIAN_KEY)
317 c->swap_byte_order = 1;
318 else {
319 pa_log("client sent invalid endian key");
320 return -1;
321 }
322
323 ok = 1;
324 connection_write(c, &ok, sizeof(int));
325 return 0;
326 }
327
328 static int esd_proto_stream_play(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
329 char name[ESD_NAME_MAX], *utf8_name;
330 int32_t format, rate;
331 pa_sample_spec ss;
332 size_t l;
333 pa_sink *sink = NULL;
334 pa_sink_input_new_data sdata;
335
336 assert(c && length == (sizeof(int32_t)*2+ESD_NAME_MAX));
337
338 memcpy(&format, data, sizeof(int32_t));
339 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
340 data = (const char*)data + sizeof(int32_t);
341
342 memcpy(&rate, data, sizeof(int32_t));
343 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
344 data = (const char*)data + sizeof(int32_t);
345
346 ss.rate = rate;
347 format_esd2native(format, c->swap_byte_order, &ss);
348
349 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification");
350
351 if (c->protocol->sink_name) {
352 sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1);
353 CHECK_VALIDITY(sink, "No such sink: %s", c->protocol->sink_name);
354 }
355
356 strncpy(name, data, sizeof(name));
357 name[sizeof(name)-1] = 0;
358
359 utf8_name = pa_utf8_filter(name);
360 pa_client_set_name(c->client, utf8_name);
361 pa_xfree(utf8_name);
362
363 c->original_name = pa_xstrdup(name);
364
365 assert(!c->sink_input && !c->input_memblockq);
366
367 pa_sink_input_new_data_init(&sdata);
368 sdata.sink = sink;
369 sdata.driver = __FILE__;
370 sdata.name = c->client->name;
371 pa_sink_input_new_data_set_sample_spec(&sdata, &ss);
372 sdata.module = c->protocol->module;
373 sdata.client = c->client;
374
375 c->sink_input = pa_sink_input_new(c->protocol->core, &sdata, 0);
376 CHECK_VALIDITY(c->sink_input, "Failed to create sink input.");
377
378 l = (size_t) (pa_bytes_per_second(&ss)*PLAYBACK_BUFFER_SECONDS);
379 c->input_memblockq = pa_memblockq_new(
380 0,
381 l,
382 0,
383 pa_frame_size(&ss),
384 (size_t) -1,
385 l/PLAYBACK_BUFFER_FRAGMENTS,
386 NULL);
387 pa_iochannel_socket_set_rcvbuf(c->io, l/PLAYBACK_BUFFER_FRAGMENTS*2);
388 c->playback.fragment_size = l/10;
389
390 c->sink_input->peek = sink_input_peek_cb;
391 c->sink_input->drop = sink_input_drop_cb;
392 c->sink_input->kill = sink_input_kill_cb;
393 c->sink_input->get_latency = sink_input_get_latency_cb;
394 c->sink_input->userdata = c;
395
396 c->state = ESD_STREAMING_DATA;
397
398 c->protocol->n_player++;
399
400 return 0;
401 }
402
403 static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length) {
404 char name[ESD_NAME_MAX], *utf8_name;
405 int32_t format, rate;
406 pa_source *source = NULL;
407 pa_sample_spec ss;
408 size_t l;
409 pa_source_output_new_data sdata;
410
411 assert(c && length == (sizeof(int32_t)*2+ESD_NAME_MAX));
412
413 memcpy(&format, data, sizeof(int32_t));
414 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
415 data = (const char*)data + sizeof(int32_t);
416
417 memcpy(&rate, data, sizeof(int32_t));
418 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
419 data = (const char*)data + sizeof(int32_t);
420
421 ss.rate = rate;
422 format_esd2native(format, c->swap_byte_order, &ss);
423
424 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
425
426 if (request == ESD_PROTO_STREAM_MON) {
427 pa_sink* sink;
428
429 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
430 pa_log("no such sink.");
431 return -1;
432 }
433
434 if (!(source = sink->monitor_source)) {
435 pa_log("no such monitor source.");
436 return -1;
437 }
438 } else {
439 assert(request == ESD_PROTO_STREAM_REC);
440
441 if (c->protocol->source_name) {
442 if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, 1))) {
443 pa_log("no such source.");
444 return -1;
445 }
446 }
447 }
448
449 strncpy(name, data, sizeof(name));
450 name[sizeof(name)-1] = 0;
451
452 utf8_name = pa_utf8_filter(name);
453 pa_client_set_name(c->client, utf8_name);
454 pa_xfree(utf8_name);
455
456 c->original_name = pa_xstrdup(name);
457
458 assert(!c->output_memblockq && !c->source_output);
459
460 pa_source_output_new_data_init(&sdata);
461 sdata.source = source;
462 sdata.driver = __FILE__;
463 sdata.name = c->client->name;
464 pa_source_output_new_data_set_sample_spec(&sdata, &ss);
465 sdata.module = c->protocol->module;
466 sdata.client = c->client;
467
468 c->source_output = pa_source_output_new(c->protocol->core, &sdata, 9);
469 CHECK_VALIDITY(c->source_output, "Failed to create source_output.");
470
471 l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS);
472 c->output_memblockq = pa_memblockq_new(
473 0,
474 l,
475 0,
476 pa_frame_size(&ss),
477 1,
478 0,
479 NULL);
480 pa_iochannel_socket_set_sndbuf(c->io, l/RECORD_BUFFER_FRAGMENTS*2);
481
482 c->source_output->push = source_output_push_cb;
483 c->source_output->kill = source_output_kill_cb;
484 c->source_output->get_latency = source_output_get_latency_cb;
485 c->source_output->userdata = c;
486
487 c->state = ESD_STREAMING_DATA;
488
489 c->protocol->n_player++;
490
491 return 0;
492 }
493
494 static int esd_proto_get_latency(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
495 pa_sink *sink;
496 int32_t latency;
497
498 assert(c && !data && length == 0);
499
500 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
501 latency = 0;
502 else {
503 double usec = pa_sink_get_latency(sink);
504 latency = (int) ((usec*44100)/1000000);
505 }
506
507 latency = MAYBE_INT32_SWAP(c->swap_byte_order, latency);
508 connection_write(c, &latency, sizeof(int32_t));
509 return 0;
510 }
511
512 static int esd_proto_server_info(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
513 int32_t rate = 44100, format = ESD_STEREO|ESD_BITS16;
514 int32_t response;
515 pa_sink *sink;
516
517 assert(c && data && length == sizeof(int32_t));
518
519 if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
520 rate = sink->sample_spec.rate;
521 format = format_native2esd(&sink->sample_spec);
522 }
523
524 connection_write_prepare(c, sizeof(int32_t) * 3);
525
526 response = 0;
527 connection_write(c, &response, sizeof(int32_t));
528 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
529 connection_write(c, &rate, sizeof(int32_t));
530 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
531 connection_write(c, &format, sizeof(int32_t));
532
533 return 0;
534 }
535
536 static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
537 size_t t, k, s;
538 struct connection *conn;
539 uint32_t idx = PA_IDXSET_INVALID;
540 unsigned nsamples;
541 char terminator[sizeof(int32_t)*6+ESD_NAME_MAX];
542
543 assert(c && data && length == sizeof(int32_t));
544
545 if (esd_proto_server_info(c, request, data, length) < 0)
546 return -1;
547
548 k = sizeof(int32_t)*5+ESD_NAME_MAX;
549 s = sizeof(int32_t)*6+ESD_NAME_MAX;
550 nsamples = c->protocol->core->scache ? pa_idxset_size(c->protocol->core->scache) : 0;
551 t = s*(nsamples+1) + k*(c->protocol->n_player+1);
552
553 connection_write_prepare(c, t);
554
555 memset(terminator, 0, sizeof(terminator));
556
557 for (conn = pa_idxset_first(c->protocol->connections, &idx); conn; conn = pa_idxset_next(c->protocol->connections, &idx)) {
558 int32_t id, format = ESD_BITS16 | ESD_STEREO, rate = 44100, lvolume = ESD_VOLUME_BASE, rvolume = ESD_VOLUME_BASE;
559 char name[ESD_NAME_MAX];
560
561 if (conn->state != ESD_STREAMING_DATA)
562 continue;
563
564 assert(t >= k*2+s);
565
566 if (conn->sink_input) {
567 pa_cvolume volume = *pa_sink_input_get_volume(conn->sink_input);
568 rate = conn->sink_input->sample_spec.rate;
569 lvolume = (volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
570 rvolume = (volume.values[1]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
571 format = format_native2esd(&conn->sink_input->sample_spec);
572 }
573
574 /* id */
575 id = MAYBE_INT32_SWAP(c->swap_byte_order, (int32_t) (conn->index+1));
576 connection_write(c, &id, sizeof(int32_t));
577
578 /* name */
579 memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
580 if (conn->original_name)
581 strncpy(name, conn->original_name, ESD_NAME_MAX);
582 else if (conn->client && conn->client->name)
583 strncpy(name, conn->client->name, ESD_NAME_MAX);
584 connection_write(c, name, ESD_NAME_MAX);
585
586 /* rate */
587 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
588 connection_write(c, &rate, sizeof(int32_t));
589
590 /* left */
591 lvolume = MAYBE_INT32_SWAP(c->swap_byte_order, lvolume);
592 connection_write(c, &lvolume, sizeof(int32_t));
593
594 /*right*/
595 rvolume = MAYBE_INT32_SWAP(c->swap_byte_order, rvolume);
596 connection_write(c, &rvolume, sizeof(int32_t));
597
598 /*format*/
599 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
600 connection_write(c, &format, sizeof(int32_t));
601
602 t -= k;
603 }
604
605 assert(t == s*(nsamples+1)+k);
606 t -= k;
607
608 connection_write(c, terminator, k);
609
610 if (nsamples) {
611 pa_scache_entry *ce;
612
613 idx = PA_IDXSET_INVALID;
614 for (ce = pa_idxset_first(c->protocol->core->scache, &idx); ce; ce = pa_idxset_next(c->protocol->core->scache, &idx)) {
615 int32_t id, rate, lvolume, rvolume, format, len;
616 char name[ESD_NAME_MAX];
617
618 assert(t >= s*2);
619
620 /* id */
621 id = MAYBE_INT32_SWAP(c->swap_byte_order, (int) (ce->index+1));
622 connection_write(c, &id, sizeof(int32_t));
623
624 /* name */
625 memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
626 if (strncmp(ce->name, SCACHE_PREFIX, sizeof(SCACHE_PREFIX)-1) == 0)
627 strncpy(name, ce->name+sizeof(SCACHE_PREFIX)-1, ESD_NAME_MAX);
628 else
629 snprintf(name, ESD_NAME_MAX, "native.%s", ce->name);
630 connection_write(c, name, ESD_NAME_MAX);
631
632 /* rate */
633 rate = MAYBE_UINT32_SWAP(c->swap_byte_order, ce->sample_spec.rate);
634 connection_write(c, &rate, sizeof(int32_t));
635
636 /* left */
637 lvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
638 connection_write(c, &lvolume, sizeof(int32_t));
639
640 /*right*/
641 rvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
642 connection_write(c, &rvolume, sizeof(int32_t));
643
644 /*format*/
645 format = MAYBE_INT32_SWAP(c->swap_byte_order, format_native2esd(&ce->sample_spec));
646 connection_write(c, &format, sizeof(int32_t));
647
648 /*length*/
649 len = MAYBE_INT32_SWAP(c->swap_byte_order, (int) ce->memchunk.length);
650 connection_write(c, &len, sizeof(int32_t));
651
652 t -= s;
653 }
654 }
655
656 assert(t == s);
657
658 connection_write(c, terminator, s);
659
660 return 0;
661 }
662
663 static int esd_proto_stream_pan(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
664 int32_t ok;
665 uint32_t idx, lvolume, rvolume;
666 struct connection *conn;
667
668 assert(c && data && length == sizeof(int32_t)*3);
669
670 memcpy(&idx, data, sizeof(uint32_t));
671 idx = MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
672 data = (const char*)data + sizeof(uint32_t);
673
674 memcpy(&lvolume, data, sizeof(uint32_t));
675 lvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, lvolume);
676 data = (const char*)data + sizeof(uint32_t);
677
678 memcpy(&rvolume, data, sizeof(uint32_t));
679 rvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, rvolume);
680 data = (const char*)data + sizeof(uint32_t);
681
682 if ((conn = pa_idxset_get_by_index(c->protocol->connections, idx)) && conn->sink_input) {
683 pa_cvolume volume;
684 volume.values[0] = (lvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
685 volume.values[1] = (rvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
686 volume.channels = 2;
687 pa_sink_input_set_volume(conn->sink_input, &volume);
688 ok = 1;
689 } else
690 ok = 0;
691
692 connection_write(c, &ok, sizeof(int32_t));
693
694 return 0;
695 }
696
697 static int esd_proto_sample_cache(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
698 pa_sample_spec ss;
699 int32_t format, rate, sc_length;
700 uint32_t idx;
701 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
702
703 assert(c && data && length == (ESD_NAME_MAX+3*sizeof(int32_t)));
704
705 memcpy(&format, data, sizeof(int32_t));
706 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
707 data = (const char*)data + sizeof(int32_t);
708
709 memcpy(&rate, data, sizeof(int32_t));
710 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
711 data = (const char*)data + sizeof(int32_t);
712
713 ss.rate = rate;
714 format_esd2native(format, c->swap_byte_order, &ss);
715
716 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
717
718 memcpy(&sc_length, data, sizeof(int32_t));
719 sc_length = MAYBE_INT32_SWAP(c->swap_byte_order, sc_length);
720 data = (const char*)data + sizeof(int32_t);
721
722 CHECK_VALIDITY(sc_length <= MAX_CACHE_SAMPLE_SIZE, "Sample too large (%d bytes).", (int)sc_length);
723
724 strcpy(name, SCACHE_PREFIX);
725 strncpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
726 name[sizeof(name)-1] = 0;
727
728 CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
729
730 assert(!c->scache.memchunk.memblock);
731 c->scache.memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, sc_length);
732 c->scache.memchunk.index = 0;
733 c->scache.memchunk.length = sc_length;
734 c->scache.sample_spec = ss;
735 assert(!c->scache.name);
736 c->scache.name = pa_xstrdup(name);
737
738 c->state = ESD_CACHING_SAMPLE;
739
740 pa_scache_add_item(c->protocol->core, c->scache.name, NULL, NULL, NULL, &idx);
741
742 idx += 1;
743 connection_write(c, &idx, sizeof(uint32_t));
744
745 return 0;
746 }
747
748 static int esd_proto_sample_get_id(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
749 int32_t ok;
750 uint32_t idx;
751 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
752
753 assert(c && data && length == ESD_NAME_MAX);
754
755 strcpy(name, SCACHE_PREFIX);
756 strncpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
757 name[sizeof(name)-1] = 0;
758
759 CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
760
761 ok = -1;
762 if ((idx = pa_scache_get_id_by_name(c->protocol->core, name)) != PA_IDXSET_INVALID)
763 ok = idx + 1;
764
765 connection_write(c, &ok, sizeof(int32_t));
766
767 return 0;
768 }
769
770 static int esd_proto_sample_free_or_play(struct connection *c, esd_proto_t request, const void *data, size_t length) {
771 int32_t ok;
772 const char *name;
773 uint32_t idx;
774
775 assert(c && data && length == sizeof(int32_t));
776
777 memcpy(&idx, data, sizeof(uint32_t));
778 idx = MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
779
780 ok = 0;
781
782 if ((name = pa_scache_get_name_by_id(c->protocol->core, idx))) {
783 if (request == ESD_PROTO_SAMPLE_PLAY) {
784 pa_sink *sink;
785
786 if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
787 if (pa_scache_play_item(c->protocol->core, name, sink, PA_VOLUME_NORM) >= 0)
788 ok = idx + 1;
789 } else {
790 assert(request == ESD_PROTO_SAMPLE_FREE);
791
792 if (pa_scache_remove_item(c->protocol->core, name) >= 0)
793 ok = idx + 1;
794 }
795 }
796
797 connection_write(c, &ok, sizeof(int32_t));
798
799 return 0;
800 }
801
802 static int esd_proto_standby_or_resume(struct connection *c, PA_GCC_UNUSED esd_proto_t request, PA_GCC_UNUSED const void *data, PA_GCC_UNUSED size_t length) {
803 int32_t ok;
804
805 connection_write_prepare(c, sizeof(int32_t) * 2);
806
807 ok = 1;
808 connection_write(c, &ok, sizeof(int32_t));
809 connection_write(c, &ok, sizeof(int32_t));
810
811 return 0;
812 }
813
814 /*** client callbacks ***/
815
816 static void client_kill_cb(pa_client *c) {
817 assert(c && c->userdata);
818 connection_free(c->userdata);
819 }
820
821 /*** pa_iochannel callbacks ***/
822
823 static int do_read(struct connection *c) {
824 assert(c && c->io);
825
826 /* pa_log("READ"); */
827
828 if (c->state == ESD_NEXT_REQUEST) {
829 ssize_t r;
830 assert(c->read_data_length < sizeof(c->request));
831
832 if ((r = pa_iochannel_read(c->io, ((uint8_t*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
833 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
834 return -1;
835 }
836
837 if ((c->read_data_length+= r) >= sizeof(c->request)) {
838 struct proto_handler *handler;
839
840 c->request = MAYBE_INT32_SWAP(c->swap_byte_order, c->request);
841
842 if (c->request < ESD_PROTO_CONNECT || c->request > ESD_PROTO_MAX) {
843 pa_log("recieved invalid request.");
844 return -1;
845 }
846
847 handler = proto_map+c->request;
848
849 /* pa_log("executing request #%u", c->request); */
850
851 if (!handler->proc) {
852 pa_log("recieved unimplemented request #%u.", c->request);
853 return -1;
854 }
855
856 if (handler->data_length == 0) {
857 c->read_data_length = 0;
858
859 if (handler->proc(c, c->request, NULL, 0) < 0)
860 return -1;
861
862 } else {
863 if (c->read_data_alloc < handler->data_length)
864 c->read_data = pa_xrealloc(c->read_data, c->read_data_alloc = handler->data_length);
865 assert(c->read_data);
866
867 c->state = ESD_NEEDS_REQDATA;
868 c->read_data_length = 0;
869 }
870 }
871
872 } else if (c->state == ESD_NEEDS_REQDATA) {
873 ssize_t r;
874 struct proto_handler *handler = proto_map+c->request;
875
876 assert(handler->proc);
877
878 assert(c->read_data && c->read_data_length < handler->data_length);
879
880 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
881 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
882 return -1;
883 }
884
885 if ((c->read_data_length += r) >= handler->data_length) {
886 size_t l = c->read_data_length;
887 assert(handler->proc);
888
889 c->state = ESD_NEXT_REQUEST;
890 c->read_data_length = 0;
891
892 if (handler->proc(c, c->request, c->read_data, l) < 0)
893 return -1;
894 }
895 } else if (c->state == ESD_CACHING_SAMPLE) {
896 ssize_t r;
897
898 assert(c->scache.memchunk.memblock && c->scache.name && c->scache.memchunk.index < c->scache.memchunk.length);
899
900 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->scache.memchunk.memblock->data+c->scache.memchunk.index, c->scache.memchunk.length-c->scache.memchunk.index)) <= 0) {
901 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
902 return -1;
903 }
904
905 c->scache.memchunk.index += r;
906 assert(c->scache.memchunk.index <= c->scache.memchunk.length);
907
908 if (c->scache.memchunk.index == c->scache.memchunk.length) {
909 uint32_t idx;
910
911 c->scache.memchunk.index = 0;
912 pa_scache_add_item(c->protocol->core, c->scache.name, &c->scache.sample_spec, NULL, &c->scache.memchunk, &idx);
913
914 pa_memblock_unref(c->scache.memchunk.memblock);
915 c->scache.memchunk.memblock = NULL;
916 c->scache.memchunk.index = c->scache.memchunk.length = 0;
917
918 pa_xfree(c->scache.name);
919 c->scache.name = NULL;
920
921 c->state = ESD_NEXT_REQUEST;
922
923 idx += 1;
924 connection_write(c, &idx, sizeof(uint32_t));
925 }
926
927 } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
928 pa_memchunk chunk;
929 ssize_t r;
930 size_t l;
931
932 assert(c->input_memblockq);
933
934 /* pa_log("STREAMING_DATA"); */
935
936 if (!(l = pa_memblockq_missing(c->input_memblockq)))
937 return 0;
938
939 if (l > c->playback.fragment_size)
940 l = c->playback.fragment_size;
941
942 if (c->playback.current_memblock)
943 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
944 pa_memblock_unref(c->playback.current_memblock);
945 c->playback.current_memblock = NULL;
946 c->playback.memblock_index = 0;
947 }
948
949 if (!c->playback.current_memblock) {
950 c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, c->playback.fragment_size*2);
951 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
952 c->playback.memblock_index = 0;
953 }
954
955 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
956 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
957 return -1;
958 }
959
960 chunk.memblock = c->playback.current_memblock;
961 chunk.index = c->playback.memblock_index;
962 chunk.length = r;
963 assert(chunk.memblock);
964
965 c->playback.memblock_index += r;
966
967 assert(c->input_memblockq);
968 pa_memblockq_push_align(c->input_memblockq, &chunk);
969 assert(c->sink_input);
970 pa_sink_notify(c->sink_input->sink);
971 }
972
973 return 0;
974 }
975
976 static int do_write(struct connection *c) {
977 assert(c && c->io);
978
979 /* pa_log("WRITE"); */
980
981 if (c->write_data_length) {
982 ssize_t r;
983
984 assert(c->write_data_index < c->write_data_length);
985 if ((r = pa_iochannel_write(c->io, (uint8_t*) c->write_data+c->write_data_index, c->write_data_length-c->write_data_index)) < 0) {
986 pa_log("write(): %s", pa_cstrerror(errno));
987 return -1;
988 }
989
990 if ((c->write_data_index +=r) >= c->write_data_length)
991 c->write_data_length = c->write_data_index = 0;
992
993 } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
994 pa_memchunk chunk;
995 ssize_t r;
996
997 assert(c->output_memblockq);
998 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
999 return 0;
1000
1001 assert(chunk.memblock && chunk.length);
1002
1003 if ((r = pa_iochannel_write(c->io, (uint8_t*) chunk.memblock->data+chunk.index, chunk.length)) < 0) {
1004 pa_memblock_unref(chunk.memblock);
1005 pa_log("write(): %s", pa_cstrerror(errno));
1006 return -1;
1007 }
1008
1009 pa_memblockq_drop(c->output_memblockq, &chunk, r);
1010 pa_memblock_unref(chunk.memblock);
1011
1012 pa_source_notify(c->source_output->source);
1013 }
1014
1015 return 0;
1016 }
1017
1018 static void do_work(struct connection *c) {
1019 assert(c);
1020
1021 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
1022 c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
1023
1024 if (c->dead)
1025 return;
1026
1027 if (pa_iochannel_is_readable(c->io)) {
1028 if (do_read(c) < 0)
1029 goto fail;
1030 }
1031
1032 if (c->state == ESD_STREAMING_DATA && c->source_output && pa_iochannel_is_hungup(c->io))
1033 /* In case we are in capture mode we will never call read()
1034 * on the socket, hence we need to detect the hangup manually
1035 * here, instead of simply waiting for read() to return 0. */
1036 goto fail;
1037
1038 if (pa_iochannel_is_writable(c->io))
1039 if (do_write(c) < 0)
1040 goto fail;
1041
1042 return;
1043
1044 fail:
1045
1046 if (c->state == ESD_STREAMING_DATA && c->sink_input) {
1047 c->dead = 1;
1048
1049 pa_iochannel_free(c->io);
1050 c->io = NULL;
1051
1052 pa_memblockq_prebuf_disable(c->input_memblockq);
1053 pa_sink_notify(c->sink_input->sink);
1054 } else
1055 connection_free(c);
1056 }
1057
1058 static void io_callback(pa_iochannel*io, void *userdata) {
1059 struct connection *c = userdata;
1060 assert(io && c && c->io == io);
1061
1062 do_work(c);
1063 }
1064
1065 /*** defer callback ***/
1066
1067 static void defer_callback(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
1068 struct connection *c = userdata;
1069 assert(a && c && c->defer_event == e);
1070
1071 /* pa_log("DEFER"); */
1072
1073 do_work(c);
1074 }
1075
1076 /*** sink_input callbacks ***/
1077
1078 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
1079 struct connection*c;
1080 assert(i && i->userdata && chunk);
1081 c = i->userdata;
1082
1083 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
1084
1085 if (c->dead)
1086 connection_free(c);
1087
1088 return -1;
1089 }
1090
1091 return 0;
1092 }
1093
1094 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
1095 struct connection*c = i->userdata;
1096 assert(i && c && length);
1097
1098 /* pa_log("DROP"); */
1099
1100 pa_memblockq_drop(c->input_memblockq, chunk, length);
1101
1102 /* do something */
1103 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
1104
1105 if (!c->dead)
1106 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
1107
1108 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1109 }
1110
1111 static void sink_input_kill_cb(pa_sink_input *i) {
1112 assert(i && i->userdata);
1113 connection_free((struct connection *) i->userdata);
1114 }
1115
1116 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
1117 struct connection*c = i->userdata;
1118 assert(i && c);
1119 return pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
1120 }
1121
1122 /*** source_output callbacks ***/
1123
1124 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
1125 struct connection *c = o->userdata;
1126 assert(o && c && chunk);
1127
1128 pa_memblockq_push(c->output_memblockq, chunk);
1129
1130 /* do something */
1131 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
1132
1133 if (!c->dead)
1134 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
1135 }
1136
1137 static void source_output_kill_cb(pa_source_output *o) {
1138 assert(o && o->userdata);
1139 connection_free((struct connection *) o->userdata);
1140 }
1141
1142 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
1143 struct connection*c = o->userdata;
1144 assert(o && c);
1145 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
1146 }
1147
1148 /*** socket server callback ***/
1149
1150 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
1151 struct connection *c = userdata;
1152 assert(m && tv && c && c->auth_timeout_event == e);
1153
1154 if (!c->authorized)
1155 connection_free(c);
1156 }
1157
1158 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
1159 struct connection *c;
1160 pa_protocol_esound *p = userdata;
1161 char cname[256], pname[128];
1162 assert(s && io && p);
1163
1164 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
1165 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
1166 pa_iochannel_free(io);
1167 return;
1168 }
1169
1170 c = pa_xnew(struct connection, 1);
1171 c->protocol = p;
1172 c->io = io;
1173 pa_iochannel_set_callback(c->io, io_callback, c);
1174
1175 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
1176 snprintf(cname, sizeof(cname), "EsounD client (%s)", pname);
1177 assert(p->core);
1178 c->client = pa_client_new(p->core, __FILE__, cname);
1179 assert(c->client);
1180 c->client->owner = p->module;
1181 c->client->kill = client_kill_cb;
1182 c->client->userdata = c;
1183
1184 c->authorized = !!p->public;
1185 c->swap_byte_order = 0;
1186 c->dead = 0;
1187
1188 c->read_data_length = 0;
1189 c->read_data = pa_xmalloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
1190
1191 c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
1192 c->write_data = NULL;
1193
1194 c->state = ESD_NEEDS_REQDATA;
1195 c->request = ESD_PROTO_CONNECT;
1196
1197 c->sink_input = NULL;
1198 c->input_memblockq = NULL;
1199
1200 c->source_output = NULL;
1201 c->output_memblockq = NULL;
1202
1203 c->playback.current_memblock = NULL;
1204 c->playback.memblock_index = 0;
1205 c->playback.fragment_size = 0;
1206
1207 c->scache.memchunk.length = c->scache.memchunk.index = 0;
1208 c->scache.memchunk.memblock = NULL;
1209 c->scache.name = NULL;
1210
1211 c->original_name = NULL;
1212
1213 if (!c->authorized && p->auth_ip_acl && pa_ip_acl_check(p->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
1214 pa_log_info("Client authenticated by IP ACL.");
1215 c->authorized = 1;
1216 }
1217
1218 if (!c->authorized) {
1219 struct timeval tv;
1220 pa_gettimeofday(&tv);
1221 tv.tv_sec += AUTH_TIMEOUT;
1222 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
1223 } else
1224 c->auth_timeout_event = NULL;
1225
1226 c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
1227 assert(c->defer_event);
1228 p->core->mainloop->defer_enable(c->defer_event, 0);
1229
1230 pa_idxset_put(p->connections, c, &c->index);
1231 }
1232
1233 /*** entry points ***/
1234
1235 pa_protocol_esound* pa_protocol_esound_new(pa_core*core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
1236 pa_protocol_esound *p;
1237 int public = 0;
1238 const char *acl;
1239
1240 assert(core);
1241 assert(server);
1242 assert(m);
1243 assert(ma);
1244
1245 p = pa_xnew(pa_protocol_esound, 1);
1246
1247 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
1248 pa_log("auth-anonymous= expects a boolean argument.");
1249 goto fail;
1250 }
1251
1252 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", DEFAULT_COOKIE_FILE), p->esd_key, sizeof(p->esd_key)) < 0)
1253 goto fail;
1254
1255 if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
1256
1257 if (!(p->auth_ip_acl = pa_ip_acl_new(acl))) {
1258 pa_log("Failed to parse IP ACL '%s'", acl);
1259 goto fail;
1260 }
1261 } else
1262 p->auth_ip_acl = NULL;
1263
1264 p->module = m;
1265 p->public = public;
1266 p->server = server;
1267 pa_socket_server_set_callback(p->server, on_connection, p);
1268 p->core = core;
1269 p->connections = pa_idxset_new(NULL, NULL);
1270 assert(p->connections);
1271
1272 p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
1273 p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
1274 p->n_player = 0;
1275
1276 return p;
1277
1278 fail:
1279 pa_xfree(p);
1280 return NULL;
1281 }
1282
1283 void pa_protocol_esound_free(pa_protocol_esound *p) {
1284 struct connection *c;
1285 assert(p);
1286
1287 while ((c = pa_idxset_first(p->connections, NULL)))
1288 connection_free(c);
1289
1290 pa_idxset_free(p->connections, NULL, NULL);
1291 pa_socket_server_unref(p->server);
1292
1293 if (p->auth_ip_acl)
1294 pa_ip_acl_free(p->auth_ip_acl);
1295
1296 pa_xfree(p);
1297 }