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