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