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