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