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