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