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