]> code.delx.au - pulseaudio/blob - polyp/protocol-esound.c
add new module "module-x11-bell"
[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 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 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 "debug.h"
46
47 #define DEFAULT_COOKIE_FILE ".esd_auth"
48
49 #define PLAYBACK_BUFFER_SECONDS (.5)
50 #define PLAYBACK_BUFFER_FRAGMENTS (10)
51 #define RECORD_BUFFER_SECONDS (5)
52 #define RECORD_BUFFER_FRAGMENTS (100)
53
54 #define MAX_CACHE_SAMPLE_SIZE (1024000)
55
56 #define SCACHE_PREFIX "esound~"
57
58 /* This is heavily based on esound's code */
59
60 struct connection {
61 uint32_t index;
62 struct pa_protocol_esound *protocol;
63 struct pa_iochannel *io;
64 struct pa_client *client;
65 int authorized, swap_byte_order;
66 void *write_data;
67 size_t write_data_alloc, write_data_index, write_data_length;
68 void *read_data;
69 size_t read_data_alloc, read_data_length;
70 esd_proto_t request;
71 esd_client_state_t state;
72 struct pa_sink_input *sink_input;
73 struct pa_source_output *source_output;
74 struct pa_memblockq *input_memblockq, *output_memblockq;
75 void *fixed_source;
76 struct {
77 struct pa_memblock *current_memblock;
78 size_t memblock_index, fragment_size;
79 } playback;
80
81
82 struct pa_memchunk scache_memchunk;
83 char *scache_name;
84 struct pa_sample_spec scache_sample_spec;
85 };
86
87 struct pa_protocol_esound {
88 int public;
89 struct pa_module *module;
90 struct pa_core *core;
91 struct pa_socket_server *server;
92 struct pa_idxset *connections;
93 uint32_t sink_index, source_index;
94 unsigned n_player;
95 uint8_t esd_key[ESD_KEY_LEN];
96 };
97
98 typedef struct proto_handler {
99 size_t data_length;
100 int (*proc)(struct connection *c, esd_proto_t request, const void *data, size_t length);
101 const char *description;
102 } esd_proto_handler_info_t;
103
104 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length);
105 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
106 static void sink_input_kill_cb(struct pa_sink_input *i);
107 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);
108
109 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
110 static void source_output_kill_cb(struct pa_source_output *o);
111
112 static int esd_proto_connect(struct connection *c, esd_proto_t request, const void *data, size_t length);
113 static int esd_proto_stream_play(struct connection *c, esd_proto_t request, const void *data, size_t length);
114 static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length);
115 static int esd_proto_get_latency(struct connection *c, esd_proto_t request, const void *data, size_t length);
116 static int esd_proto_server_info(struct connection *c, esd_proto_t request, const void *data, size_t length);
117 static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length);
118 static int esd_proto_stream_pan(struct connection *c, esd_proto_t request, const void *data, size_t length);
119 static int esd_proto_sample_cache(struct connection *c, esd_proto_t request, const void *data, size_t length);
120 static int esd_proto_sample_free_or_play(struct connection *c, esd_proto_t request, const void *data, size_t length);
121 static int esd_proto_sample_get_id(struct connection *c, esd_proto_t request, const void *data, size_t length);
122
123 /* the big map of protocol handler info */
124 static struct proto_handler proto_map[ESD_PROTO_MAX] = {
125 { ESD_KEY_LEN + sizeof(int), esd_proto_connect, "connect" },
126 { ESD_KEY_LEN + sizeof(int), NULL, "lock" },
127 { ESD_KEY_LEN + sizeof(int), NULL, "unlock" },
128
129 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_play, "stream play" },
130 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream rec" },
131 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream mon" },
132
133 { ESD_NAME_MAX + 3 * sizeof(int), esd_proto_sample_cache, "sample cache" },
134 { sizeof(int), esd_proto_sample_free_or_play, "sample free" },
135 { sizeof(int), esd_proto_sample_free_or_play, "sample play" },
136 { sizeof(int), NULL, "sample loop" },
137 { sizeof(int), NULL, "sample stop" },
138 { -1, NULL, "TODO: sample kill" },
139
140 { ESD_KEY_LEN + sizeof(int), NULL, "standby" },
141 { ESD_KEY_LEN + sizeof(int), NULL, "resume" },
142
143 { ESD_NAME_MAX, esd_proto_sample_get_id, "sample getid" },
144 { ESD_NAME_MAX + 2 * sizeof(int), NULL, "stream filter" },
145
146 { sizeof(int), esd_proto_server_info, "server info" },
147 { sizeof(int), esd_proto_all_info, "all info" },
148 { -1, NULL, "TODO: subscribe" },
149 { -1, NULL, "TODO: unsubscribe" },
150
151 { 3 * sizeof(int), esd_proto_stream_pan, "stream pan"},
152 { 3 * sizeof(int), NULL, "sample pan" },
153
154 { sizeof(int), NULL, "standby mode" },
155 { 0, esd_proto_get_latency, "get latency" }
156 };
157
158
159 static void connection_free(struct connection *c) {
160 assert(c);
161 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
162
163 if (c->state == ESD_STREAMING_DATA)
164 c->protocol->n_player--;
165
166 pa_client_free(c->client);
167
168 if (c->sink_input)
169 pa_sink_input_free(c->sink_input);
170 if (c->source_output)
171 pa_source_output_free(c->source_output);
172 if (c->input_memblockq)
173 pa_memblockq_free(c->input_memblockq);
174 if (c->output_memblockq)
175 pa_memblockq_free(c->output_memblockq);
176
177 if (c->playback.current_memblock)
178 pa_memblock_unref(c->playback.current_memblock);
179
180 free(c->read_data);
181 free(c->write_data);
182
183 pa_iochannel_free(c->io);
184
185 if (c->fixed_source)
186 c->protocol->core->mainloop->cancel_fixed(c->protocol->core->mainloop, c->fixed_source);
187
188 if (c->scache_memchunk.memblock)
189 pa_memblock_unref(c->scache_memchunk.memblock);
190 free(c->scache_name);
191
192 free(c);
193 }
194
195 static struct pa_sink* get_output_sink(struct pa_protocol_esound *p) {
196 struct pa_sink *s;
197 assert(p);
198
199 if (!(s = pa_idxset_get_by_index(p->core->sinks, p->sink_index)))
200 s = pa_sink_get_default(p->core);
201
202 p->sink_index = s ? s->index : PA_IDXSET_INVALID;
203 return s;
204 }
205
206 static struct pa_source* get_input_source(struct pa_protocol_esound *p) {
207 struct pa_source *s;
208 assert(p);
209
210 if (!(s = pa_idxset_get_by_index(p->core->sources, p->sink_index)))
211 s = pa_source_get_default(p->core);
212
213 p->source_index = s ? s->index : PA_IDXSET_INVALID;
214 return s;
215 }
216
217 static void* connection_write(struct connection *c, size_t length) {
218 size_t t, i;
219 assert(c);
220
221 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
222 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
223
224 t = c->write_data_length+length;
225
226 if (c->write_data_alloc < t)
227 c->write_data = realloc(c->write_data, c->write_data_alloc = t);
228
229 assert(c->write_data);
230
231 i = c->write_data_length;
232 c->write_data_length += length;
233
234 return c->write_data+i;
235 }
236
237 static void format_esd2native(int format, struct pa_sample_spec *ss) {
238 assert(ss);
239
240 ss->channels = ((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1;
241 ss->format = ((format & ESD_MASK_BITS) == ESD_BITS16) ? PA_SAMPLE_S16NE : PA_SAMPLE_U8;
242 }
243
244 static int format_native2esd(struct pa_sample_spec *ss) {
245 int format = 0;
246
247 format = (ss->format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
248 format |= (ss->channels >= 2) ? ESD_STEREO : ESD_MONO;
249
250 return format;
251 }
252
253 /*** esound commands ***/
254
255 static int esd_proto_connect(struct connection *c, esd_proto_t request, const void *data, size_t length) {
256 uint32_t ekey;
257 int *ok;
258 assert(length == (ESD_KEY_LEN + sizeof(uint32_t)));
259
260 if (!c->authorized) {
261 if (memcmp(data, c->protocol->esd_key, ESD_KEY_LEN) != 0) {
262 fprintf(stderr, __FILE__": kicked client with invalid authorization key.\n");
263 return -1;
264 }
265
266 c->authorized = 1;
267 }
268
269 ekey = *(uint32_t*)(data+ESD_KEY_LEN);
270 if (ekey == ESD_ENDIAN_KEY)
271 c->swap_byte_order = 0;
272 else if (ekey == ESD_SWAP_ENDIAN_KEY)
273 c->swap_byte_order = 1;
274 else {
275 fprintf(stderr, __FILE__": client sent invalid endian key\n");
276 return -1;
277 }
278
279 ok = connection_write(c, sizeof(int));
280 assert(ok);
281 *ok = 1;
282 return 0;
283 }
284
285 static int esd_proto_stream_play(struct connection *c, esd_proto_t request, const void *data, size_t length) {
286 char name[ESD_NAME_MAX];
287 int format, rate;
288 struct pa_sink *sink;
289 struct pa_sample_spec ss;
290 size_t l;
291 assert(c && length == (sizeof(int)*2+ESD_NAME_MAX));
292
293 format = maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
294 rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
295
296 ss.rate = rate;
297 format_esd2native(format, &ss);
298
299 if (!pa_sample_spec_valid(&ss))
300 return -1;
301
302 if (!(sink = get_output_sink(c->protocol)))
303 return -1;
304
305 strncpy(name, data + sizeof(int)*2, sizeof(name));
306 name[sizeof(name)-1] = 0;
307
308 pa_client_rename(c->client, name);
309
310 assert(!c->input_memblockq);
311
312 l = (size_t) (pa_bytes_per_second(&ss)*PLAYBACK_BUFFER_SECONDS);
313 c->input_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&ss), l/2, l/PLAYBACK_BUFFER_FRAGMENTS);
314 assert(c->input_memblockq);
315 pa_iochannel_socket_set_rcvbuf(c->io, l/PLAYBACK_BUFFER_FRAGMENTS*2);
316 c->playback.fragment_size = l/10;
317
318 assert(!c->sink_input);
319 c->sink_input = pa_sink_input_new(sink, name, &ss);
320 assert(c->sink_input);
321
322 c->sink_input->owner = c->protocol->module;
323 c->sink_input->client = c->client;
324 c->sink_input->peek = sink_input_peek_cb;
325 c->sink_input->drop = sink_input_drop_cb;
326 c->sink_input->kill = sink_input_kill_cb;
327 c->sink_input->get_latency = sink_input_get_latency_cb;
328 c->sink_input->userdata = c;
329
330 c->state = ESD_STREAMING_DATA;
331
332 c->protocol->n_player++;
333
334 return 0;
335 }
336
337 static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length) {
338 char name[ESD_NAME_MAX];
339 int format, rate;
340 struct pa_source *source;
341 struct pa_sample_spec ss;
342 size_t l;
343 assert(c && length == (sizeof(int)*2+ESD_NAME_MAX));
344
345 format = maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
346 rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
347
348 ss.rate = rate;
349 format_esd2native(format, &ss);
350
351 if (!pa_sample_spec_valid(&ss))
352 return -1;
353
354 if (request == ESD_PROTO_STREAM_MON) {
355 struct pa_sink* sink;
356
357 if (!(sink = get_output_sink(c->protocol)))
358 return -1;
359
360 if (!(source = sink->monitor_source))
361 return -1;
362 } else {
363 assert(request == ESD_PROTO_STREAM_REC);
364
365 if (!(source = get_input_source(c->protocol)))
366 return -1;
367 }
368
369 strncpy(name, data + sizeof(int)*2, sizeof(name));
370 name[sizeof(name)-1] = 0;
371
372 pa_client_rename(c->client, name);
373
374 assert(!c->output_memblockq);
375
376 l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS);
377 c->output_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&ss), 0, 0);
378 assert(c->output_memblockq);
379 pa_iochannel_socket_set_sndbuf(c->io, l/RECORD_BUFFER_FRAGMENTS*2);
380
381 assert(!c->source_output);
382 c->source_output = pa_source_output_new(source, name, &ss);
383 assert(c->source_output);
384
385 c->source_output->owner = c->protocol->module;
386 c->source_output->client = c->client;
387 c->source_output->push = source_output_push_cb;
388 c->source_output->kill = source_output_kill_cb;
389 c->source_output->userdata = c;
390
391 c->state = ESD_STREAMING_DATA;
392
393 c->protocol->n_player++;
394
395 return 0;
396 }
397
398 static int esd_proto_get_latency(struct connection *c, esd_proto_t request, const void *data, size_t length) {
399 struct pa_sink *sink;
400 int latency, *lag;
401 assert(c && !data && length == 0);
402
403 if (!(sink = get_output_sink(c->protocol)))
404 latency = 0;
405 else {
406 float usec = pa_sink_get_latency(sink);
407 usec += PLAYBACK_BUFFER_SECONDS*1000000; /* A better estimation would be a good idea! */
408 latency = (int) ((usec*44100)/1000000);
409 }
410
411 lag = connection_write(c, sizeof(int));
412 assert(lag);
413 *lag = c->swap_byte_order ? swap_endian_32(latency) : latency;
414 return 0;
415 }
416
417 static int esd_proto_server_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
418 int rate = 44100, format = ESD_STEREO|ESD_BITS16;
419 int *response;
420 struct pa_sink *sink;
421 assert(c && data && length == sizeof(int));
422
423 if ((sink = get_output_sink(c->protocol))) {
424 rate = sink->sample_spec.rate;
425 format = format_native2esd(&sink->sample_spec);
426 }
427
428 response = connection_write(c, sizeof(int)*3);
429 assert(response);
430 *(response++) = 0;
431 *(response++) = maybe_swap_endian_32(c->swap_byte_order, rate);
432 *(response++) = maybe_swap_endian_32(c->swap_byte_order, format);
433 return 0;
434 }
435
436 static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
437 void *response;
438 size_t t, k, s;
439 struct connection *conn;
440 size_t index = PA_IDXSET_INVALID;
441 assert(c && data && length == sizeof(int));
442
443 if (esd_proto_server_info(c, request, data, length) < 0)
444 return -1;
445
446 k = sizeof(int)*5+ESD_NAME_MAX;
447 s = sizeof(int)*6+ESD_NAME_MAX;
448 response = connection_write(c, (t = s+k*(c->protocol->n_player+1)));
449 assert(k);
450
451 for (conn = pa_idxset_first(c->protocol->connections, &index); conn; conn = pa_idxset_next(c->protocol->connections, &index)) {
452 int format = ESD_BITS16 | ESD_STEREO, rate = 44100, volume = 0xFF;
453
454 if (conn->state != ESD_STREAMING_DATA)
455 continue;
456
457 assert(t >= s+k+k);
458
459 if (conn->sink_input) {
460 rate = conn->sink_input->sample_spec.rate;
461 volume = (conn->sink_input->volume*0xFF)/0x100;
462 format = format_native2esd(&conn->sink_input->sample_spec);
463 }
464
465 /* id */
466 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, (int) conn->index);
467 response += sizeof(int);
468
469 /* name */
470 assert(conn->client);
471 strncpy(response, conn->client->name, ESD_NAME_MAX);
472 response += ESD_NAME_MAX;
473
474 /* rate */
475 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, rate);
476 response += sizeof(int);
477
478 /* left */
479 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, volume);
480 response += sizeof(int);
481
482 /*right*/
483 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, volume);
484 response += sizeof(int);
485
486 /*format*/
487 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, format);
488 response += sizeof(int);
489
490 t-= k;
491 }
492
493 assert(t == s+k);
494 memset(response, 0, t);
495 return 0;
496 }
497
498 static int esd_proto_stream_pan(struct connection *c, esd_proto_t request, const void *data, size_t length) {
499 int *ok;
500 uint32_t index, volume;
501 struct connection *conn;
502 assert(c && data && length == sizeof(int)*3);
503
504 index = (uint32_t) maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
505 volume = (uint32_t) maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
506 volume = (volume*0x100)/0xFF;
507
508 ok = connection_write(c, sizeof(int));
509 assert(ok);
510
511 if ((conn = pa_idxset_get_by_index(c->protocol->connections, index))) {
512 assert(conn->sink_input);
513 conn->sink_input->volume = volume;
514 *ok = 1;
515 } else
516 *ok = 0;
517
518 return 0;
519 }
520
521 static int esd_proto_sample_cache(struct connection *c, esd_proto_t request, const void *data, size_t length) {
522 struct pa_sample_spec ss;
523 int format, rate;
524 size_t sc_length;
525 uint32_t index;
526 int *ok;
527 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
528 assert(c && data && length == (ESD_NAME_MAX+3*sizeof(int)));
529
530 format = maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
531 rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
532
533 ss.rate = rate;
534 format_esd2native(format, &ss);
535
536 sc_length = (size_t) maybe_swap_endian_32(c->swap_byte_order, (*((int*)data + 2)));
537
538 if (sc_length >= MAX_CACHE_SAMPLE_SIZE)
539 return -1;
540
541 strcpy(name, SCACHE_PREFIX);
542 strncpy(name+sizeof(SCACHE_PREFIX)-1, data+3*sizeof(int), ESD_NAME_MAX);
543 name[sizeof(name)-1] = 0;
544
545 assert(!c->scache_memchunk.memblock);
546 c->scache_memchunk.memblock = pa_memblock_new(sc_length);
547 c->scache_memchunk.index = 0;
548 c->scache_memchunk.length = sc_length;
549 c->scache_sample_spec = ss;
550 assert(!c->scache_name);
551 c->scache_name = strdup(name);
552 assert(c->scache_name);
553
554 c->state = ESD_CACHING_SAMPLE;
555
556 pa_scache_add_item(c->protocol->core, c->scache_name, NULL, NULL, &index);
557
558 ok = connection_write(c, sizeof(int));
559 assert(ok);
560
561 *ok = index+1;
562
563 return 0;
564 }
565
566 static int esd_proto_sample_get_id(struct connection *c, esd_proto_t request, const void *data, size_t length) {
567 int *ok;
568 uint32_t index;
569 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
570 assert(c && data && length == ESD_NAME_MAX);
571
572 ok = connection_write(c, sizeof(int));
573 assert(ok);
574
575 *ok = -1;
576
577 strcpy(name, SCACHE_PREFIX);
578 strncpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
579 name[sizeof(name)-1] = 0;
580
581 if ((index = pa_scache_get_id_by_name(c->protocol->core, name)) != PA_IDXSET_INVALID)
582 *ok = (int) index +1;
583
584 return 0;
585 }
586
587 static int esd_proto_sample_free_or_play(struct connection *c, esd_proto_t request, const void *data, size_t length) {
588 int *ok;
589 const char *name;
590 uint32_t index;
591 assert(c && data && length == sizeof(int));
592
593 index = (uint32_t) maybe_swap_endian_32(c->swap_byte_order, *(int*)data)-1;
594
595 ok = connection_write(c, sizeof(int));
596 assert(ok);
597
598 *ok = 0;
599
600 if ((name = pa_scache_get_name_by_id(c->protocol->core, index))) {
601 if (request == ESD_PROTO_SAMPLE_PLAY) {
602 struct pa_sink *sink;
603
604 if ((sink = get_output_sink(c->protocol)))
605 if (pa_scache_play_item(c->protocol->core, name, sink, PA_VOLUME_NORM) >= 0)
606 *ok = (int) index+1;
607 } else {
608 assert(request == ESD_PROTO_SAMPLE_FREE);
609
610 if (pa_scache_remove_item(c->protocol->core, name) >= 0)
611 *ok = (int) index+1;
612 }
613 }
614
615 return 0;
616 }
617
618 /*** client callbacks ***/
619
620 static void client_kill_cb(struct pa_client *c) {
621 assert(c && c->userdata);
622 connection_free(c->userdata);
623 }
624
625 /*** pa_iochannel callbacks ***/
626
627 static int do_read(struct connection *c) {
628 assert(c && c->io);
629
630 if (c->state == ESD_NEXT_REQUEST) {
631 ssize_t r;
632 assert(c->read_data_length < sizeof(c->request));
633
634 if ((r = pa_iochannel_read(c->io, ((void*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
635 fprintf(stderr, "protocol-esound.c: read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
636 return -1;
637 }
638
639 if ((c->read_data_length+= r) >= sizeof(c->request)) {
640 struct proto_handler *handler;
641
642 if (c->swap_byte_order)
643 c->request = swap_endian_32(c->request);
644
645 if (c->request < ESD_PROTO_CONNECT || c->request > ESD_PROTO_MAX) {
646 fprintf(stderr, "protocol-esound.c: recieved invalid request.\n");
647 return -1;
648 }
649
650 handler = proto_map+c->request;
651
652 if (!handler->proc) {
653 fprintf(stderr, "protocol-sound.c: recieved unimplemented request.\n");
654 return -1;
655 }
656
657 if (handler->data_length == 0) {
658 c->read_data_length = 0;
659
660 if (handler->proc(c, c->request, NULL, 0) < 0)
661 return -1;
662
663 } else {
664 if (c->read_data_alloc < handler->data_length)
665 c->read_data = realloc(c->read_data, c->read_data_alloc = handler->data_length);
666 assert(c->read_data);
667
668 c->state = ESD_NEEDS_REQDATA;
669 c->read_data_length = 0;
670 }
671 }
672
673 } else if (c->state == ESD_NEEDS_REQDATA) {
674 ssize_t r;
675 struct proto_handler *handler = proto_map+c->request;
676
677 assert(handler->proc);
678
679 assert(c->read_data && c->read_data_length < handler->data_length);
680
681 if ((r = pa_iochannel_read(c->io, c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
682 fprintf(stderr, "protocol-esound.c: read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
683 return -1;
684 }
685
686 if ((c->read_data_length+= r) >= handler->data_length) {
687 size_t l = c->read_data_length;
688 assert(handler->proc);
689
690 c->state = ESD_NEXT_REQUEST;
691 c->read_data_length = 0;
692
693 if (handler->proc(c, c->request, c->read_data, l) < 0)
694 return -1;
695 }
696 } else if (c->state == ESD_CACHING_SAMPLE) {
697 ssize_t r;
698
699 assert(c->scache_memchunk.memblock && c->scache_name && c->scache_memchunk.index < c->scache_memchunk.length);
700
701 if ((r = pa_iochannel_read(c->io, c->scache_memchunk.memblock->data+c->scache_memchunk.index, c->scache_memchunk.length-c->scache_memchunk.index)) <= 0) {
702 fprintf(stderr, __FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
703 return -1;
704 }
705
706 c->scache_memchunk.index += r;
707 assert(c->scache_memchunk.index <= c->scache_memchunk.length);
708
709 if (c->scache_memchunk.index == c->scache_memchunk.length) {
710 uint32_t index;
711 int *ok;
712
713 c->scache_memchunk.index = 0;
714 pa_scache_add_item(c->protocol->core, c->scache_name, &c->scache_sample_spec, &c->scache_memchunk, &index);
715
716 pa_memblock_unref(c->scache_memchunk.memblock);
717 c->scache_memchunk.memblock = NULL;
718 c->scache_memchunk.index = c->scache_memchunk.length = 0;
719
720 free(c->scache_name);
721 c->scache_name = NULL;
722
723 c->state = ESD_NEXT_REQUEST;
724
725 ok = connection_write(c, sizeof(int));
726 assert(ok);
727 *ok = index+1;
728 }
729
730 } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
731 struct pa_memchunk chunk;
732 ssize_t r;
733 size_t l;
734
735 assert(c->input_memblockq);
736
737 if (!(l = pa_memblockq_missing(c->input_memblockq)))
738 return 0;
739
740 if (l > c->playback.fragment_size)
741 l = c->playback.fragment_size;
742
743 if (c->playback.current_memblock)
744 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
745 pa_memblock_unref(c->playback.current_memblock);
746 c->playback.current_memblock = NULL;
747 c->playback.memblock_index = 0;
748 }
749
750 if (!c->playback.current_memblock) {
751 c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2);
752 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
753 c->playback.memblock_index = 0;
754 }
755
756 if ((r = pa_iochannel_read(c->io, c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
757 fprintf(stderr, __FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
758 return -1;
759 }
760
761 chunk.memblock = c->playback.current_memblock;
762 chunk.index = c->playback.memblock_index;
763 chunk.length = r;
764 assert(chunk.memblock);
765
766 c->playback.memblock_index += r;
767
768 assert(c->input_memblockq);
769 pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
770 assert(c->sink_input);
771 pa_sink_notify(c->sink_input->sink);
772 }
773
774 return 0;
775 }
776
777 static int do_write(struct connection *c) {
778 assert(c && c->io);
779
780 if (c->write_data_length) {
781 ssize_t r;
782
783 assert(c->write_data_index < c->write_data_length);
784 if ((r = pa_iochannel_write(c->io, c->write_data+c->write_data_index, c->write_data_length-c->write_data_index)) < 0) {
785 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
786 return -1;
787 }
788
789 if ((c->write_data_index +=r) >= c->write_data_length)
790 c->write_data_length = c->write_data_index = 0;
791
792 } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
793 struct pa_memchunk chunk;
794 ssize_t r;
795
796 assert(c->output_memblockq);
797 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
798 return 0;
799
800 assert(chunk.memblock && chunk.length);
801
802 if ((r = pa_iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
803 pa_memblock_unref(chunk.memblock);
804 fprintf(stderr, __FILE__": write(): %s\n", strerror(errno));
805 return -1;
806 }
807
808 pa_memblockq_drop(c->output_memblockq, r);
809 pa_memblock_unref(chunk.memblock);
810 }
811
812 return 0;
813 }
814
815 static void do_work(struct connection *c) {
816 assert(c);
817
818 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
819 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);
820
821 if (pa_iochannel_is_hungup(c->io))
822 goto fail;
823
824 if (pa_iochannel_is_writable(c->io))
825 if (do_write(c) < 0)
826 goto fail;
827
828 if (pa_iochannel_is_readable(c->io))
829 if (do_read(c) < 0)
830 goto fail;
831
832 return;
833
834 fail:
835 connection_free(c);
836 }
837
838 static void io_callback(struct pa_iochannel*io, void *userdata) {
839 struct connection *c = userdata;
840 assert(io && c && c->io == io);
841
842 do_work(c);
843 }
844
845 /*** fixed callback ***/
846
847 static void fixed_callback(struct pa_mainloop_api*a, void *id, void *userdata) {
848 struct connection *c = userdata;
849 assert(a && c && c->fixed_source == id);
850
851 do_work(c);
852 }
853
854 /*** sink_input callbacks ***/
855
856 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
857 struct connection*c;
858 assert(i && i->userdata && chunk);
859 c = i->userdata;
860
861 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
862 return -1;
863
864 return 0;
865 }
866
867 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
868 struct connection*c = i->userdata;
869 assert(i && c && length);
870
871 pa_memblockq_drop(c->input_memblockq, length);
872
873 /* do something */
874 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
875 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
876 }
877
878 static void sink_input_kill_cb(struct pa_sink_input *i) {
879 assert(i && i->userdata);
880 connection_free((struct connection *) i->userdata);
881 }
882
883
884 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
885 struct connection*c = i->userdata;
886 assert(i && c);
887 return pa_samples_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
888 }
889
890 /*** source_output callbacks ***/
891
892 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
893 struct connection *c = o->userdata;
894 assert(o && c && chunk);
895
896 pa_memblockq_push(c->output_memblockq, chunk, 0);
897
898 /* do something */
899 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
900 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
901 }
902
903 static void source_output_kill_cb(struct pa_source_output *o) {
904 assert(o && o->userdata);
905 connection_free((struct connection *) o->userdata);
906 }
907
908 /*** socket server callback ***/
909
910 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
911 struct connection *c;
912 char cname[256];
913 assert(s && io && userdata);
914
915 c = malloc(sizeof(struct connection));
916 assert(c);
917 c->protocol = userdata;
918 c->io = io;
919 pa_iochannel_set_callback(c->io, io_callback, c);
920
921 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
922 assert(c->protocol->core);
923 c->client = pa_client_new(c->protocol->core, "ESOUND", cname);
924 assert(c->client);
925 c->client->owner = c->protocol->module;
926 c->client->kill = client_kill_cb;
927 c->client->userdata = c;
928
929 c->authorized = c->protocol->public;
930 c->swap_byte_order = 0;
931
932 c->read_data_length = 0;
933 c->read_data = malloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
934 assert(c->read_data);
935
936 c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
937 c->write_data = NULL;
938
939 c->state = ESD_NEEDS_REQDATA;
940 c->request = ESD_PROTO_CONNECT;
941
942 c->sink_input = NULL;
943 c->input_memblockq = NULL;
944
945 c->source_output = NULL;
946 c->output_memblockq = NULL;
947
948 c->playback.current_memblock = NULL;
949 c->playback.memblock_index = 0;
950 c->playback.fragment_size = 0;
951
952 c->scache_memchunk.length = c->scache_memchunk.index = 0;
953 c->scache_memchunk.memblock = NULL;
954 c->scache_name = NULL;
955
956 c->fixed_source = c->protocol->core->mainloop->source_fixed(c->protocol->core->mainloop, fixed_callback, c);
957 assert(c->fixed_source);
958 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);
959
960 pa_idxset_put(c->protocol->connections, c, &c->index);
961 }
962
963 /*** entry points ***/
964
965 struct pa_protocol_esound* pa_protocol_esound_new(struct pa_core*core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) {
966 uint32_t source_index, sink_index;
967 struct pa_protocol_esound *p;
968 assert(core && server && ma);
969
970 if (pa_modargs_get_source_index(ma, core, &source_index) < 0) {
971 fprintf(stderr, __FILE__": source does not exist.\n");
972 return NULL;
973 }
974
975 if (pa_modargs_get_sink_index(ma, core, &sink_index) < 0) {
976 fprintf(stderr, __FILE__": sink does not exist.\n");
977 return NULL;
978 }
979 p = malloc(sizeof(struct pa_protocol_esound));
980 assert(p);
981
982 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", DEFAULT_COOKIE_FILE), p->esd_key, sizeof(p->esd_key)) < 0) {
983 free(p);
984 return NULL;
985 }
986
987 p->module = m;
988 p->public = 0;
989 p->server = server;
990 pa_socket_server_set_callback(p->server, on_connection, p);
991 p->core = core;
992 p->connections = pa_idxset_new(NULL, NULL);
993 assert(p->connections);
994 p->sink_index = sink_index;
995 p->source_index = source_index;
996 p->n_player = 0;
997
998 return p;
999 }
1000
1001 void pa_protocol_esound_free(struct pa_protocol_esound *p) {
1002 struct connection *c;
1003 assert(p);
1004
1005 while ((c = pa_idxset_first(p->connections, NULL)))
1006 connection_free(c);
1007
1008 pa_idxset_free(p->connections, NULL, NULL);
1009 pa_socket_server_free(p->server);
1010 free(p);
1011 }