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