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