2fadeca303e58c17d3775559ae4baefacb7b577a
[profile/ivi/pulseaudio.git] / 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     pa_iochannel_socket_set_rcvbuf(c->io, l/PLAYBACK_BUFFER_FRAGMENTS*2);
382     c->playback.fragment_size = l/10;
383
384     c->sink_input->peek = sink_input_peek_cb;
385     c->sink_input->drop = sink_input_drop_cb;
386     c->sink_input->kill = sink_input_kill_cb;
387     c->sink_input->get_latency = sink_input_get_latency_cb;
388     c->sink_input->userdata = c;
389
390     c->state = ESD_STREAMING_DATA;
391
392     c->protocol->n_player++;
393     
394     return 0;
395 }
396
397 static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length) {
398     char name[ESD_NAME_MAX], *utf8_name;
399     int32_t format, rate;
400     pa_source *source;
401     pa_sample_spec ss;
402     size_t l;
403     pa_source_output_new_data sdata;
404
405     assert(c && length == (sizeof(int32_t)*2+ESD_NAME_MAX));
406     
407     memcpy(&format, data, sizeof(int32_t));
408     format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
409     data = (const char*)data + sizeof(int32_t);
410
411     memcpy(&rate, data, sizeof(int32_t));
412     rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
413     data = (const char*)data + sizeof(int32_t);
414
415     ss.rate = rate;
416     format_esd2native(format, c->swap_byte_order, &ss);
417
418     CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
419
420     if (request == ESD_PROTO_STREAM_MON) {
421         pa_sink* sink;
422
423         if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
424             pa_log(__FILE__": no such sink.");
425             return -1;
426         }
427
428         if (!(source = sink->monitor_source)) {
429             pa_log(__FILE__": no such monitor source.");
430             return -1;
431         }
432     } else {
433         assert(request == ESD_PROTO_STREAM_REC);
434         
435         if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, 1))) {
436             pa_log(__FILE__": no such source.");
437             return -1;
438         }
439     }
440     
441     strncpy(name, data, sizeof(name));
442     name[sizeof(name)-1] = 0;
443
444     utf8_name = pa_utf8_filter(name);
445     pa_client_set_name(c->client, utf8_name);
446     pa_xfree(utf8_name);
447     
448     c->original_name = pa_xstrdup(name);
449
450     assert(!c->output_memblockq && !c->source_output);
451
452     pa_source_output_new_data_init(&sdata);
453     sdata.source = source;
454     sdata.driver = __FILE__;
455     sdata.name = c->client->name;
456     pa_source_output_new_data_set_sample_spec(&sdata, &ss);
457     sdata.module = c->protocol->module;
458     sdata.client = c->client;
459     
460     c->source_output = pa_source_output_new(c->protocol->core, &sdata, 9);
461     CHECK_VALIDITY(c->sink_input, "Failed to create source_output.");
462
463     l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS); 
464     c->output_memblockq = pa_memblockq_new(
465             0,
466             l,
467             0,
468             pa_frame_size(&ss),
469             1,
470             0,
471             NULL);
472     pa_iochannel_socket_set_sndbuf(c->io, l/RECORD_BUFFER_FRAGMENTS*2);
473     
474     c->source_output->push = source_output_push_cb;
475     c->source_output->kill = source_output_kill_cb;
476     c->source_output->get_latency = source_output_get_latency_cb;
477     c->source_output->userdata = c;
478
479     c->state = ESD_STREAMING_DATA;
480
481     c->protocol->n_player++;
482     
483     return 0;
484 }
485
486 static int esd_proto_get_latency(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
487     pa_sink *sink;
488     int32_t latency;
489
490     assert(c && !data && length == 0);
491
492     if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
493         latency = 0;
494     else {
495         double usec = pa_sink_get_latency(sink);
496         latency = (int) ((usec*44100)/1000000);
497     }
498     
499     latency = MAYBE_INT32_SWAP(c->swap_byte_order, latency);
500     connection_write(c, &latency, sizeof(int32_t));
501     return 0;
502 }
503
504 static int esd_proto_server_info(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
505     int32_t rate = 44100, format = ESD_STEREO|ESD_BITS16;
506     int32_t response;
507     pa_sink *sink;
508
509     assert(c && data && length == sizeof(int32_t));
510
511     if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
512         rate = sink->sample_spec.rate;
513         format = format_native2esd(&sink->sample_spec);
514     }
515
516     connection_write_prepare(c, sizeof(int32_t) * 3);
517
518     response = 0;
519     connection_write(c, &response, sizeof(int32_t));
520     rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
521     connection_write(c, &rate, sizeof(int32_t));
522     format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
523     connection_write(c, &format, sizeof(int32_t));
524
525     return 0;
526 }
527
528 static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
529     size_t t, k, s;
530     struct connection *conn;
531     uint32_t idx = PA_IDXSET_INVALID;
532     unsigned nsamples;
533     char terminator[sizeof(int32_t)*6+ESD_NAME_MAX];
534
535     assert(c && data && length == sizeof(int32_t));
536     
537     if (esd_proto_server_info(c, request, data, length) < 0)
538         return -1;
539
540     k = sizeof(int32_t)*5+ESD_NAME_MAX;
541     s = sizeof(int32_t)*6+ESD_NAME_MAX;
542     nsamples = c->protocol->core->scache ? pa_idxset_size(c->protocol->core->scache) : 0;
543     t = s*(nsamples+1) + k*(c->protocol->n_player+1);
544
545     connection_write_prepare(c, t);
546
547     memset(terminator, 0, sizeof(terminator));
548
549     for (conn = pa_idxset_first(c->protocol->connections, &idx); conn; conn = pa_idxset_next(c->protocol->connections, &idx)) {
550         int32_t id, format = ESD_BITS16 | ESD_STEREO, rate = 44100, lvolume = ESD_VOLUME_BASE, rvolume = ESD_VOLUME_BASE;
551         char name[ESD_NAME_MAX];
552
553         if (conn->state != ESD_STREAMING_DATA)
554             continue;
555
556         assert(t >= k*2+s);
557         
558         if (conn->sink_input) {
559             pa_cvolume volume = *pa_sink_input_get_volume(conn->sink_input);
560             rate = conn->sink_input->sample_spec.rate;
561             lvolume = (volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
562             rvolume = (volume.values[1]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
563             format = format_native2esd(&conn->sink_input->sample_spec);
564         }
565         
566         /* id */
567         id = MAYBE_INT32_SWAP(c->swap_byte_order, (int32_t) (conn->index+1));
568         connection_write(c, &id, sizeof(int32_t));
569
570         /* name */
571         memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
572         if (conn->original_name)
573             strncpy(name, conn->original_name, ESD_NAME_MAX);
574         else if (conn->client && conn->client->name)
575             strncpy(name, conn->client->name, ESD_NAME_MAX);
576         connection_write(c, name, ESD_NAME_MAX);
577
578         /* rate */
579         rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
580         connection_write(c, &rate, sizeof(int32_t));
581
582         /* left */
583         lvolume = MAYBE_INT32_SWAP(c->swap_byte_order, lvolume);
584         connection_write(c, &lvolume, sizeof(int32_t));
585
586         /*right*/
587         rvolume = MAYBE_INT32_SWAP(c->swap_byte_order, rvolume);
588         connection_write(c, &rvolume, sizeof(int32_t));
589
590         /*format*/
591         format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
592         connection_write(c, &format, sizeof(int32_t));
593
594         t -= k;
595     }
596
597     assert(t == s*(nsamples+1)+k);
598     t -= k;
599
600     connection_write(c, terminator, k);
601
602     if (nsamples) {
603         pa_scache_entry *ce;
604         
605         idx = PA_IDXSET_INVALID;
606         for (ce = pa_idxset_first(c->protocol->core->scache, &idx); ce; ce = pa_idxset_next(c->protocol->core->scache, &idx)) {
607             int32_t id, rate, lvolume, rvolume, format, len;
608             char name[ESD_NAME_MAX];
609
610             assert(t >= s*2);
611
612             /* id */
613             id = MAYBE_INT32_SWAP(c->swap_byte_order, (int) (ce->index+1));
614             connection_write(c, &id, sizeof(int32_t));
615             
616             /* name */
617             memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
618             if (strncmp(ce->name, SCACHE_PREFIX, sizeof(SCACHE_PREFIX)-1) == 0)
619                 strncpy(name, ce->name+sizeof(SCACHE_PREFIX)-1, ESD_NAME_MAX);
620             else
621                 snprintf(name, ESD_NAME_MAX, "native.%s", ce->name);
622             connection_write(c, name, ESD_NAME_MAX);
623             
624             /* rate */
625             rate = MAYBE_UINT32_SWAP(c->swap_byte_order, ce->sample_spec.rate);
626             connection_write(c, &rate, sizeof(int32_t));
627             
628             /* left */
629             lvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
630             connection_write(c, &lvolume, sizeof(int32_t));
631             
632             /*right*/
633             rvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
634             connection_write(c, &rvolume, sizeof(int32_t));
635             
636             /*format*/
637             format = MAYBE_INT32_SWAP(c->swap_byte_order, format_native2esd(&ce->sample_spec));
638             connection_write(c, &format, sizeof(int32_t));
639
640             /*length*/
641             len = MAYBE_INT32_SWAP(c->swap_byte_order, (int) ce->memchunk.length);
642             connection_write(c, &len, sizeof(int32_t));
643
644             t -= s;
645         }
646     }
647
648     assert(t == s);
649
650     connection_write(c, terminator, s);
651
652     return 0;
653 }
654
655 static int esd_proto_stream_pan(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
656     int32_t ok;
657     uint32_t idx, lvolume, rvolume;
658     struct connection *conn;
659
660     assert(c && data && length == sizeof(int32_t)*3);
661     
662     memcpy(&idx, data, sizeof(uint32_t));
663     idx = MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
664     data = (const char*)data + sizeof(uint32_t);
665
666     memcpy(&lvolume, data, sizeof(uint32_t));
667     lvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, lvolume);
668     data = (const char*)data + sizeof(uint32_t);
669
670     memcpy(&rvolume, data, sizeof(uint32_t));
671     rvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, rvolume);
672     data = (const char*)data + sizeof(uint32_t);
673
674     if ((conn = pa_idxset_get_by_index(c->protocol->connections, idx)) && conn->sink_input) {
675         pa_cvolume volume;
676         volume.values[0] = (lvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
677         volume.values[1] = (rvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
678         volume.channels = 2;
679         pa_sink_input_set_volume(conn->sink_input, &volume);
680         ok = 1;
681     } else
682         ok = 0;
683
684     connection_write(c, &ok, sizeof(int32_t));
685     
686     return 0;
687 }
688
689 static int esd_proto_sample_cache(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
690     pa_sample_spec ss;
691     int32_t format, rate, sc_length;
692     uint32_t idx;
693     char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
694
695     assert(c && data && length == (ESD_NAME_MAX+3*sizeof(int32_t)));
696
697     memcpy(&format, data, sizeof(int32_t));
698     format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
699     data = (const char*)data + sizeof(int32_t);
700
701     memcpy(&rate, data, sizeof(int32_t));
702     rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
703     data = (const char*)data + sizeof(int32_t);
704     
705     ss.rate = rate;
706     format_esd2native(format, c->swap_byte_order, &ss);
707
708     CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
709
710     memcpy(&sc_length, data, sizeof(int32_t));
711     sc_length = MAYBE_INT32_SWAP(c->swap_byte_order, sc_length);
712     data = (const char*)data + sizeof(int32_t);
713
714     CHECK_VALIDITY(sc_length <= MAX_CACHE_SAMPLE_SIZE, "Sample too large.");
715
716     strcpy(name, SCACHE_PREFIX);
717     strncpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
718     name[sizeof(name)-1] = 0;
719
720     CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
721     
722     assert(!c->scache.memchunk.memblock);
723     c->scache.memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, sc_length);
724     c->scache.memchunk.index = 0;
725     c->scache.memchunk.length = sc_length;
726     c->scache.sample_spec = ss;
727     assert(!c->scache.name);
728     c->scache.name = pa_xstrdup(name);
729     
730     c->state = ESD_CACHING_SAMPLE;
731
732     pa_scache_add_item(c->protocol->core, c->scache.name, NULL, NULL, NULL, &idx);
733
734     idx += 1;
735     connection_write(c, &idx, sizeof(uint32_t));
736     
737     return 0;
738 }
739
740 static int esd_proto_sample_get_id(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
741     int32_t ok;
742     uint32_t idx;
743     char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
744
745     assert(c && data && length == ESD_NAME_MAX);
746
747     strcpy(name, SCACHE_PREFIX);
748     strncpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
749     name[sizeof(name)-1] = 0;
750
751     CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
752
753     ok = -1;
754     if ((idx = pa_scache_get_id_by_name(c->protocol->core, name)) != PA_IDXSET_INVALID)
755         ok = idx + 1;
756
757     connection_write(c, &ok, sizeof(int32_t));
758
759     return 0;
760 }
761
762 static int esd_proto_sample_free_or_play(struct connection *c, esd_proto_t request, const void *data, size_t length) {
763     int32_t ok;
764     const char *name;
765     uint32_t idx;
766
767     assert(c && data && length == sizeof(int32_t));
768
769     memcpy(&idx, data, sizeof(uint32_t));
770     idx = MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
771
772     ok = 0;
773     
774     if ((name = pa_scache_get_name_by_id(c->protocol->core, idx))) {
775         if (request == ESD_PROTO_SAMPLE_PLAY) {
776             pa_sink *sink;
777         
778             if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
779                 if (pa_scache_play_item(c->protocol->core, name, sink, PA_VOLUME_NORM) >= 0)
780                     ok = idx + 1;
781         } else {
782             assert(request == ESD_PROTO_SAMPLE_FREE);
783
784             if (pa_scache_remove_item(c->protocol->core, name) >= 0)
785                 ok = idx + 1;
786         }
787     }
788     
789     connection_write(c, &ok, sizeof(int32_t));
790
791     return 0;
792 }
793
794 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) {
795     int32_t ok;
796
797     connection_write_prepare(c, sizeof(int32_t) * 2);
798
799     ok = 1;
800     connection_write(c, &ok, sizeof(int32_t));
801     connection_write(c, &ok, sizeof(int32_t));
802
803     return 0;
804 }
805
806 /*** client callbacks ***/
807
808 static void client_kill_cb(pa_client *c) {
809     assert(c && c->userdata);
810     connection_free(c->userdata);
811 }
812
813 /*** pa_iochannel callbacks ***/
814
815 static int do_read(struct connection *c) {
816     assert(c && c->io);
817
818 /*      pa_log("READ");  */
819     
820     if (c->state == ESD_NEXT_REQUEST) {
821         ssize_t r;
822         assert(c->read_data_length < sizeof(c->request));
823
824         if ((r = pa_iochannel_read(c->io, ((uint8_t*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
825             pa_log_debug(__FILE__": read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
826             return -1;
827         }
828
829         if ((c->read_data_length+= r) >= sizeof(c->request)) {
830             struct proto_handler *handler;
831             
832             c->request = MAYBE_INT32_SWAP(c->swap_byte_order, c->request);
833
834             if (c->request < ESD_PROTO_CONNECT || c->request > ESD_PROTO_MAX) {
835                 pa_log(__FILE__": recieved invalid request.");
836                 return -1;
837             }
838
839             handler = proto_map+c->request;
840
841 /*             pa_log(__FILE__": executing request #%u", c->request); */
842
843             if (!handler->proc) {
844                 pa_log(__FILE__": recieved unimplemented request #%u.", c->request);
845                 return -1;
846             }
847             
848             if (handler->data_length == 0) {
849                 c->read_data_length = 0;
850
851                 if (handler->proc(c, c->request, NULL, 0) < 0)
852                     return -1;
853                 
854             } else {
855                 if (c->read_data_alloc < handler->data_length)
856                     c->read_data = pa_xrealloc(c->read_data, c->read_data_alloc = handler->data_length);
857                 assert(c->read_data);
858                 
859                 c->state = ESD_NEEDS_REQDATA;
860                 c->read_data_length = 0;
861             }
862         }
863
864     } else if (c->state == ESD_NEEDS_REQDATA) {
865         ssize_t r;
866         struct proto_handler *handler = proto_map+c->request;
867
868         assert(handler->proc);
869         
870         assert(c->read_data && c->read_data_length < handler->data_length);
871
872         if ((r = pa_iochannel_read(c->io, (uint8_t*) c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
873             pa_log_debug(__FILE__": read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
874             return -1;
875         }
876
877         if ((c->read_data_length += r) >= handler->data_length) {
878             size_t l = c->read_data_length;
879             assert(handler->proc);
880
881             c->state = ESD_NEXT_REQUEST;
882             c->read_data_length = 0;
883             
884             if (handler->proc(c, c->request, c->read_data, l) < 0)
885                 return -1;
886         }
887     } else if (c->state == ESD_CACHING_SAMPLE) {
888         ssize_t r;
889
890         assert(c->scache.memchunk.memblock && c->scache.name && c->scache.memchunk.index < c->scache.memchunk.length);
891         
892         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) {
893             pa_log_debug(__FILE__": read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
894             return -1;
895         }
896
897         c->scache.memchunk.index += r;
898         assert(c->scache.memchunk.index <= c->scache.memchunk.length);
899         
900         if (c->scache.memchunk.index == c->scache.memchunk.length) {
901             uint32_t idx;
902             
903             c->scache.memchunk.index = 0;
904             pa_scache_add_item(c->protocol->core, c->scache.name, &c->scache.sample_spec, NULL, &c->scache.memchunk, &idx);
905
906             pa_memblock_unref(c->scache.memchunk.memblock);
907             c->scache.memchunk.memblock = NULL;
908             c->scache.memchunk.index = c->scache.memchunk.length = 0;
909
910             pa_xfree(c->scache.name);
911             c->scache.name = NULL;
912
913             c->state = ESD_NEXT_REQUEST;
914
915             idx += 1;
916             connection_write(c, &idx, sizeof(uint32_t));
917         }
918         
919     } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
920         pa_memchunk chunk;
921         ssize_t r;
922         size_t l;
923
924         assert(c->input_memblockq);
925
926 /*         pa_log("STREAMING_DATA"); */
927
928         if (!(l = pa_memblockq_missing(c->input_memblockq)))
929             return 0;
930
931         if (l > c->playback.fragment_size)
932             l = c->playback.fragment_size;
933
934         if (c->playback.current_memblock) 
935             if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
936                 pa_memblock_unref(c->playback.current_memblock);
937                 c->playback.current_memblock = NULL;
938                 c->playback.memblock_index = 0;
939             }
940         
941         if (!c->playback.current_memblock) {
942             c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, c->playback.fragment_size*2);
943             assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
944             c->playback.memblock_index = 0;
945         }
946
947         if ((r = pa_iochannel_read(c->io, (uint8_t*) c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
948             pa_log_debug(__FILE__": read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
949             return -1;
950         }
951         
952         chunk.memblock = c->playback.current_memblock;
953         chunk.index = c->playback.memblock_index;
954         chunk.length = r;
955         assert(chunk.memblock);
956
957         c->playback.memblock_index += r;
958         
959         assert(c->input_memblockq);
960         pa_memblockq_push_align(c->input_memblockq, &chunk);
961         assert(c->sink_input);
962         pa_sink_notify(c->sink_input->sink);
963     }
964     
965     return 0;
966 }
967
968 static int do_write(struct connection *c) {
969     assert(c && c->io);
970
971 /*     pa_log("WRITE"); */
972     
973     if (c->write_data_length) {
974         ssize_t r;
975         
976         assert(c->write_data_index < c->write_data_length);
977         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) {
978             pa_log(__FILE__": write(): %s", pa_cstrerror(errno));
979             return -1;
980         }
981         
982         if ((c->write_data_index +=r) >= c->write_data_length)
983             c->write_data_length = c->write_data_index = 0;
984         
985     } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
986         pa_memchunk chunk;
987         ssize_t r;
988
989         assert(c->output_memblockq);
990         if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
991             return 0;
992         
993         assert(chunk.memblock && chunk.length);
994         
995         if ((r = pa_iochannel_write(c->io, (uint8_t*) chunk.memblock->data+chunk.index, chunk.length)) < 0) {
996             pa_memblock_unref(chunk.memblock);
997             pa_log(__FILE__": write(): %s", pa_cstrerror(errno));
998             return -1;
999         }
1000
1001         pa_memblockq_drop(c->output_memblockq, &chunk, r);
1002         pa_memblock_unref(chunk.memblock);
1003
1004         pa_source_notify(c->source_output->source);
1005     }
1006     
1007     return 0;
1008 }
1009
1010 static void do_work(struct connection *c) {
1011     assert(c);
1012
1013     assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
1014     c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
1015
1016     if (c->dead)
1017         return;
1018
1019     if (pa_iochannel_is_readable(c->io)) {
1020         if (do_read(c) < 0)
1021             goto fail;
1022     }
1023
1024     if (c->state == ESD_STREAMING_DATA && c->source_output && pa_iochannel_is_hungup(c->io))
1025         /* In case we are in capture mode we will never call read()
1026          * on the socket, hence we need to detect the hangup manually
1027          * here, instead of simply waiting for read() to return 0. */
1028         goto fail;
1029
1030     if (pa_iochannel_is_writable(c->io))
1031         if (do_write(c) < 0)
1032             goto fail;
1033     
1034     return;
1035
1036 fail:
1037
1038     if (c->state == ESD_STREAMING_DATA && c->sink_input) {
1039         c->dead = 1;
1040
1041         pa_iochannel_free(c->io);
1042         c->io = NULL;
1043
1044         pa_memblockq_prebuf_disable(c->input_memblockq);
1045         pa_sink_notify(c->sink_input->sink);
1046     } else
1047         connection_free(c);
1048 }
1049
1050 static void io_callback(pa_iochannel*io, void *userdata) {
1051     struct connection *c = userdata;
1052     assert(io && c && c->io == io);
1053
1054     do_work(c);
1055 }
1056
1057 /*** defer callback ***/
1058
1059 static void defer_callback(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
1060     struct connection *c = userdata;
1061     assert(a && c && c->defer_event == e);
1062
1063 /*     pa_log("DEFER"); */
1064     
1065     do_work(c);
1066 }
1067
1068 /*** sink_input callbacks ***/
1069
1070 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
1071     struct connection*c;
1072     assert(i && i->userdata && chunk);
1073     c = i->userdata;
1074     
1075     if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
1076
1077         if (c->dead)
1078             connection_free(c);
1079         
1080         return -1;
1081     }
1082
1083     return 0;
1084 }
1085
1086 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
1087     struct connection*c = i->userdata;
1088     assert(i && c && length);
1089
1090 /*     pa_log("DROP"); */
1091     
1092     pa_memblockq_drop(c->input_memblockq, chunk, length);
1093
1094     /* do something */
1095     assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
1096
1097     if (!c->dead)
1098         c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
1099
1100 /*     assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1101 }
1102
1103 static void sink_input_kill_cb(pa_sink_input *i) {
1104     assert(i && i->userdata);
1105     connection_free((struct connection *) i->userdata);
1106 }
1107
1108 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
1109     struct connection*c = i->userdata;
1110     assert(i && c);
1111     return pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
1112 }
1113
1114 /*** source_output callbacks ***/
1115
1116 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
1117     struct connection *c = o->userdata;
1118     assert(o && c && chunk);
1119
1120     pa_memblockq_push(c->output_memblockq, chunk);
1121
1122     /* do something */
1123     assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
1124
1125     if (!c->dead)
1126         c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
1127 }
1128
1129 static void source_output_kill_cb(pa_source_output *o) {
1130     assert(o && o->userdata);
1131     connection_free((struct connection *) o->userdata);
1132 }
1133
1134 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
1135     struct connection*c = o->userdata;
1136     assert(o && c);
1137     return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
1138 }
1139
1140 /*** socket server callback ***/
1141
1142 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
1143     struct connection *c = userdata;
1144     assert(m && tv && c && c->auth_timeout_event == e);
1145
1146     if (!c->authorized)
1147         connection_free(c);
1148 }
1149
1150 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
1151     struct connection *c;
1152     pa_protocol_esound *p = userdata;
1153     char cname[256], pname[128];
1154     assert(s && io && p);
1155
1156     if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
1157         pa_log(__FILE__": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
1158         pa_iochannel_free(io);
1159         return;
1160     }
1161     
1162     c = pa_xnew(struct connection, 1);
1163     c->protocol = p;
1164     c->io = io;
1165     pa_iochannel_set_callback(c->io, io_callback, c);
1166
1167     pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
1168     snprintf(cname, sizeof(cname), "EsounD client (%s)", pname);
1169     assert(p->core);
1170     c->client = pa_client_new(p->core, __FILE__, cname);
1171     assert(c->client);
1172     c->client->owner = p->module;
1173     c->client->kill = client_kill_cb;
1174     c->client->userdata = c;
1175     
1176     c->authorized = !!p->public;
1177     c->swap_byte_order = 0;
1178     c->dead = 0;
1179
1180     c->read_data_length = 0;
1181     c->read_data = pa_xmalloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
1182
1183     c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
1184     c->write_data = NULL;
1185
1186     c->state = ESD_NEEDS_REQDATA;
1187     c->request = ESD_PROTO_CONNECT;
1188
1189     c->sink_input = NULL;
1190     c->input_memblockq = NULL;
1191
1192     c->source_output = NULL;
1193     c->output_memblockq = NULL;
1194
1195     c->playback.current_memblock = NULL;
1196     c->playback.memblock_index = 0;
1197     c->playback.fragment_size = 0;
1198
1199     c->scache.memchunk.length = c->scache.memchunk.index = 0;
1200     c->scache.memchunk.memblock = NULL;
1201     c->scache.name = NULL;
1202
1203     c->original_name = NULL;
1204
1205     if (!c->authorized && p->auth_ip_acl && pa_ip_acl_check(p->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
1206         pa_log_info(__FILE__": Client authenticated by IP ACL.");
1207         c->authorized = 1;
1208     }
1209
1210     if (!c->authorized) {
1211         struct timeval tv;
1212         pa_gettimeofday(&tv);
1213         tv.tv_sec += AUTH_TIMEOUT;
1214         c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
1215     } else
1216         c->auth_timeout_event = NULL;
1217     
1218     c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
1219     assert(c->defer_event);
1220     p->core->mainloop->defer_enable(c->defer_event, 0);
1221
1222     pa_idxset_put(p->connections, c, &c->index);
1223 }
1224
1225 /*** entry points ***/
1226
1227 pa_protocol_esound* pa_protocol_esound_new(pa_core*core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
1228     pa_protocol_esound *p;
1229     int public = 0;
1230     const char *acl;
1231     
1232     assert(core);
1233     assert(server);
1234     assert(m);
1235     assert(ma);
1236
1237     p = pa_xnew(pa_protocol_esound, 1);
1238
1239     if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
1240         pa_log(__FILE__": auth-anonymous= expects a boolean argument.");
1241         goto fail;
1242     }
1243
1244     if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", DEFAULT_COOKIE_FILE), p->esd_key, sizeof(p->esd_key)) < 0)
1245         goto fail;
1246
1247     if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
1248
1249         if (!(p->auth_ip_acl = pa_ip_acl_new(acl))) {
1250             pa_log(__FILE__": Failed to parse IP ACL '%s'", acl);
1251             goto fail;
1252         }
1253     } else
1254         p->auth_ip_acl = NULL;
1255     
1256     p->module = m;
1257     p->public = public;
1258     p->server = server;
1259     pa_socket_server_set_callback(p->server, on_connection, p);
1260     p->core = core;
1261     p->connections = pa_idxset_new(NULL, NULL);
1262     assert(p->connections);
1263
1264     p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
1265     p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
1266     p->n_player = 0;
1267
1268     return p;
1269
1270 fail:
1271     pa_xfree(p);
1272     return NULL;
1273 }
1274
1275 void pa_protocol_esound_free(pa_protocol_esound *p) {
1276     struct connection *c;
1277     assert(p);
1278
1279     while ((c = pa_idxset_first(p->connections, NULL)))
1280         connection_free(c);
1281
1282     pa_idxset_free(p->connections, NULL, NULL);
1283     pa_socket_server_unref(p->server);
1284
1285     if (p->auth_ip_acl)
1286         pa_ip_acl_free(p->auth_ip_acl);
1287
1288     pa_xfree(p);
1289 }