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