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