]> code.delx.au - pulseaudio/blob - src/polypcore/protocol-esound.c
remove left-over log line in protocol-esound.c
[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 /*** esound commands ***/
264
265 static int esd_proto_connect(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
266 uint32_t ekey;
267 int *ok;
268 assert(length == (ESD_KEY_LEN + sizeof(uint32_t)));
269
270 if (!c->authorized) {
271 if (memcmp(data, c->protocol->esd_key, ESD_KEY_LEN) != 0) {
272 pa_log(__FILE__": kicked client with invalid authorization key.\n");
273 return -1;
274 }
275
276 c->authorized = 1;
277 if (c->auth_timeout_event) {
278 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
279 c->auth_timeout_event = NULL;
280 }
281 }
282
283 ekey = *(const uint32_t*)((const uint8_t*) data+ESD_KEY_LEN);
284 if (ekey == ESD_ENDIAN_KEY)
285 c->swap_byte_order = 0;
286 else if (ekey == ESD_SWAP_ENDIAN_KEY)
287 c->swap_byte_order = 1;
288 else {
289 pa_log(__FILE__": client sent invalid endian key\n");
290 return -1;
291 }
292
293 ok = connection_write(c, sizeof(int));
294 assert(ok);
295 *ok = 1;
296 return 0;
297 }
298
299 static int esd_proto_stream_play(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
300 char name[ESD_NAME_MAX];
301 int format, rate;
302 pa_sink *sink;
303 pa_sample_spec ss;
304 size_t l;
305 assert(c && length == (sizeof(int)*2+ESD_NAME_MAX));
306
307 format = MAYBE_INT32_SWAP(c->swap_byte_order, *(const int*)data);
308 rate = MAYBE_INT32_SWAP(c->swap_byte_order, *((const int*)data + 1));
309
310 ss.rate = rate;
311 format_esd2native(format, c->swap_byte_order, &ss);
312
313 if (!pa_sample_spec_valid(&ss)) {
314 pa_log(__FILE__": invalid sample specification\n");
315 return -1;
316 }
317
318 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
319 pa_log(__FILE__": no such sink\n");
320 return -1;
321 }
322
323 strncpy(name, (const char*) data + sizeof(int)*2, sizeof(name));
324 name[sizeof(name)-1] = 0;
325
326 pa_client_set_name(c->client, name);
327
328 assert(!c->sink_input && !c->input_memblockq);
329
330 if (!(c->sink_input = pa_sink_input_new(sink, __FILE__, name, &ss, NULL, 0, -1))) {
331 pa_log(__FILE__": failed to create sink input.\n");
332 return -1;
333 }
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 if (!pa_sample_spec_valid(&ss)) {
378 pa_log(__FILE__": invalid sample specification.\n");
379 return -1;
380 }
381
382 if (request == ESD_PROTO_STREAM_MON) {
383 pa_sink* sink;
384
385 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
386 pa_log(__FILE__": no such sink.\n");
387 return -1;
388 }
389
390 if (!(source = sink->monitor_source)) {
391 pa_log(__FILE__": no such monitor source.\n");
392 return -1;
393 }
394 } else {
395 assert(request == ESD_PROTO_STREAM_REC);
396
397 if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, 1))) {
398 pa_log(__FILE__": no such source.\n");
399 return -1;
400 }
401 }
402
403 strncpy(name, (const char*) data + sizeof(int)*2, sizeof(name));
404 name[sizeof(name)-1] = 0;
405
406 pa_client_set_name(c->client, name);
407
408 assert(!c->output_memblockq && !c->source_output);
409
410 if (!(c->source_output = pa_source_output_new(source, __FILE__, name, &ss, NULL, -1))) {
411 pa_log(__FILE__": failed to create source output\n");
412 return -1;
413 }
414
415 l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS);
416 c->output_memblockq = pa_memblockq_new(
417 0,
418 l,
419 0,
420 pa_frame_size(&ss),
421 1,
422 0,
423 NULL,
424 c->protocol->core->memblock_stat);
425 pa_iochannel_socket_set_sndbuf(c->io, l/RECORD_BUFFER_FRAGMENTS*2);
426
427 c->source_output->owner = c->protocol->module;
428 c->source_output->client = c->client;
429 c->source_output->push = source_output_push_cb;
430 c->source_output->kill = source_output_kill_cb;
431 c->source_output->get_latency = source_output_get_latency_cb;
432 c->source_output->userdata = c;
433
434 c->state = ESD_STREAMING_DATA;
435
436 c->protocol->n_player++;
437
438 return 0;
439 }
440
441 static int esd_proto_get_latency(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
442 pa_sink *sink;
443 int latency, *lag;
444 assert(c && !data && length == 0);
445
446 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
447 latency = 0;
448 else {
449 double usec = pa_sink_get_latency(sink);
450 latency = (int) ((usec*44100)/1000000);
451 }
452
453 lag = connection_write(c, sizeof(int));
454 assert(lag);
455 *lag = MAYBE_INT32_SWAP(c->swap_byte_order, latency);
456 return 0;
457 }
458
459 static int esd_proto_server_info(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
460 int rate = 44100, format = ESD_STEREO|ESD_BITS16;
461 int *response;
462 pa_sink *sink;
463 assert(c && data && length == sizeof(int));
464
465 if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
466 rate = sink->sample_spec.rate;
467 format = format_native2esd(&sink->sample_spec);
468 }
469
470 response = connection_write(c, sizeof(int)*3);
471 assert(response);
472 *(response++) = 0;
473 *(response++) = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
474 *(response++) = MAYBE_INT32_SWAP(c->swap_byte_order, format);
475 return 0;
476 }
477
478 static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
479 uint8_t *response;
480 size_t t, k, s;
481 struct connection *conn;
482 size_t idx = PA_IDXSET_INVALID;
483 unsigned nsamples;
484 assert(c && data && length == sizeof(int));
485
486 if (esd_proto_server_info(c, request, data, length) < 0)
487 return -1;
488
489 k = sizeof(int)*5+ESD_NAME_MAX;
490 s = sizeof(int)*6+ESD_NAME_MAX;
491 nsamples = c->protocol->core->scache ? pa_idxset_size(c->protocol->core->scache) : 0;
492 response = connection_write(c, (t = s*(nsamples+1) + k*(c->protocol->n_player+1)));
493 assert(k);
494
495 for (conn = pa_idxset_first(c->protocol->connections, &idx); conn; conn = pa_idxset_next(c->protocol->connections, &idx)) {
496 int format = ESD_BITS16 | ESD_STEREO, rate = 44100, lvolume = ESD_VOLUME_BASE, rvolume = ESD_VOLUME_BASE;
497
498 if (conn->state != ESD_STREAMING_DATA)
499 continue;
500
501 assert(t >= s+k+k);
502
503 if (conn->sink_input) {
504 rate = conn->sink_input->sample_spec.rate;
505 lvolume = (conn->sink_input->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
506 rvolume = (conn->sink_input->volume.values[1]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
507 format = format_native2esd(&conn->sink_input->sample_spec);
508 }
509
510 /* id */
511 *((int*) response) = MAYBE_INT32_SWAP(c->swap_byte_order, (int) (conn->index+1));
512 response += sizeof(int);
513
514 /* name */
515 assert(conn->client);
516 strncpy((char*) response, conn->client->name, ESD_NAME_MAX);
517 response += ESD_NAME_MAX;
518
519 /* rate */
520 *((int*) response) = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
521 response += sizeof(int);
522
523 /* left */
524 *((int*) response) = MAYBE_INT32_SWAP(c->swap_byte_order, lvolume);
525 response += sizeof(int);
526
527 /*right*/
528 *((int*) response) = MAYBE_INT32_SWAP(c->swap_byte_order, rvolume);
529 response += sizeof(int);
530
531 /*format*/
532 *((int*) response) = MAYBE_INT32_SWAP(c->swap_byte_order, format);
533 response += sizeof(int);
534
535 t-= k;
536 }
537
538 assert(t == s*(nsamples+1)+k);
539 memset(response, 0, k);
540 response += k;
541 t -= k;
542
543 if (nsamples) {
544 pa_scache_entry *ce;
545
546 idx = PA_IDXSET_INVALID;
547 for (ce = pa_idxset_first(c->protocol->core->scache, &idx); ce; ce = pa_idxset_next(c->protocol->core->scache, &idx)) {
548 assert(t >= s*2);
549
550 /* id */
551 *((int*) response) = MAYBE_INT32_SWAP(c->swap_byte_order, (int) (ce->index+1));
552 response += sizeof(int);
553
554 /* name */
555 if (strncmp(ce->name, SCACHE_PREFIX, sizeof(SCACHE_PREFIX)-1) == 0)
556 strncpy((char*) response, ce->name+sizeof(SCACHE_PREFIX)-1, ESD_NAME_MAX);
557 else
558 snprintf((char*) response, ESD_NAME_MAX, "native.%s", ce->name);
559 response += ESD_NAME_MAX;
560
561 /* rate */
562 *((uint32_t*) response) = MAYBE_UINT32_SWAP(c->swap_byte_order, ce->sample_spec.rate);
563 response += sizeof(int);
564
565 /* left */
566 *((uint32_t*) response) = MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
567 response += sizeof(int);
568
569 /*right*/
570 *((uint32_t*) response) = MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
571 response += sizeof(int);
572
573 /*format*/
574 *((int*) response) = MAYBE_INT32_SWAP(c->swap_byte_order, format_native2esd(&ce->sample_spec));
575 response += sizeof(int);
576
577 /*length*/
578 *((int*) response) = MAYBE_INT32_SWAP(c->swap_byte_order, (int) ce->memchunk.length);
579 response += sizeof(int);
580
581 t -= s;
582 }
583 }
584
585 assert(t == s);
586 memset(response, 0, s);
587
588 return 0;
589 }
590
591 static int esd_proto_stream_pan(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
592 int *ok;
593 uint32_t idx;
594 pa_volume_t lvolume, rvolume;
595 struct connection *conn;
596 assert(c && data && length == sizeof(int)*3);
597
598 idx = MAYBE_UINT32_SWAP(c->swap_byte_order, *(const uint32_t*)data)-1;
599 lvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, *((const uint32_t*)data + 1));
600 lvolume = (lvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
601 rvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, *((const uint32_t*)data + 2));
602 rvolume = (rvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
603
604 ok = connection_write(c, sizeof(int));
605 assert(ok);
606
607 if ((conn = pa_idxset_get_by_index(c->protocol->connections, idx))) {
608 assert(conn->sink_input);
609 conn->sink_input->volume.values[0] = lvolume;
610 conn->sink_input->volume.values[1] = rvolume;
611 conn->sink_input->volume.channels = 2;
612 *ok = 1;
613 } else
614 *ok = 0;
615
616 return 0;
617 }
618
619 static int esd_proto_sample_cache(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
620 pa_sample_spec ss;
621 int format, rate;
622 size_t sc_length;
623 uint32_t idx;
624 int *ok;
625 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
626 assert(c && data && length == (ESD_NAME_MAX+3*sizeof(int)));
627
628 format = MAYBE_INT32_SWAP(c->swap_byte_order, *(const int*)data);
629 rate = MAYBE_INT32_SWAP(c->swap_byte_order, *((const int*)data + 1));
630
631 ss.rate = rate;
632 format_esd2native(format, c->swap_byte_order, &ss);
633
634 sc_length = (size_t) MAYBE_INT32_SWAP(c->swap_byte_order, (*((const int*)data + 2)));
635
636 if (sc_length >= MAX_CACHE_SAMPLE_SIZE)
637 return -1;
638
639 strcpy(name, SCACHE_PREFIX);
640 strncpy(name+sizeof(SCACHE_PREFIX)-1, (const char*) data+3*sizeof(int), ESD_NAME_MAX);
641 name[sizeof(name)-1] = 0;
642
643 assert(!c->scache.memchunk.memblock);
644 c->scache.memchunk.memblock = pa_memblock_new(sc_length, c->protocol->core->memblock_stat);
645 c->scache.memchunk.index = 0;
646 c->scache.memchunk.length = sc_length;
647 c->scache.sample_spec = ss;
648 assert(!c->scache.name);
649 c->scache.name = pa_xstrdup(name);
650
651 c->state = ESD_CACHING_SAMPLE;
652
653 pa_scache_add_item(c->protocol->core, c->scache.name, NULL, NULL, NULL, &idx);
654
655 ok = connection_write(c, sizeof(int));
656 assert(ok);
657
658 *ok = idx+1;
659
660 return 0;
661 }
662
663 static int esd_proto_sample_get_id(struct connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
664 int *ok;
665 uint32_t idx;
666 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
667 assert(c && data && length == ESD_NAME_MAX);
668
669 ok = connection_write(c, sizeof(int));
670 assert(ok);
671
672 *ok = -1;
673
674 strcpy(name, SCACHE_PREFIX);
675 strncpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
676 name[sizeof(name)-1] = 0;
677
678 if ((idx = pa_scache_get_id_by_name(c->protocol->core, name)) != PA_IDXSET_INVALID)
679 *ok = (int) idx +1;
680
681 return 0;
682 }
683
684 static int esd_proto_sample_free_or_play(struct connection *c, esd_proto_t request, const void *data, size_t length) {
685 int *ok;
686 const char *name;
687 uint32_t idx;
688 assert(c && data && length == sizeof(int));
689
690 idx = (uint32_t) MAYBE_INT32_SWAP(c->swap_byte_order, *(const int*)data)-1;
691
692 ok = connection_write(c, sizeof(int));
693 assert(ok);
694
695 *ok = 0;
696
697 if ((name = pa_scache_get_name_by_id(c->protocol->core, idx))) {
698 if (request == ESD_PROTO_SAMPLE_PLAY) {
699 pa_sink *sink;
700
701 if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
702 if (pa_scache_play_item(c->protocol->core, name, sink, NULL) >= 0)
703 *ok = (int) idx+1;
704 } else {
705 assert(request == ESD_PROTO_SAMPLE_FREE);
706
707 if (pa_scache_remove_item(c->protocol->core, name) >= 0)
708 *ok = (int) idx+1;
709 }
710 }
711
712 return 0;
713 }
714
715 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) {
716 int *ok;
717 ok = connection_write(c, sizeof(int)*2);
718 assert(ok);
719 ok[0] = 1;
720 ok[1] = 1;
721 return 0;
722 }
723
724 /*** client callbacks ***/
725
726 static void client_kill_cb(pa_client *c) {
727 assert(c && c->userdata);
728 connection_free(c->userdata);
729 }
730
731 /*** pa_iochannel callbacks ***/
732
733 static int do_read(struct connection *c) {
734 assert(c && c->io);
735
736 /* pa_log("READ\n"); */
737
738 if (c->state == ESD_NEXT_REQUEST) {
739 ssize_t r;
740 assert(c->read_data_length < sizeof(c->request));
741
742 if ((r = pa_iochannel_read(c->io, ((uint8_t*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
743 pa_log_debug(__FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
744 return -1;
745 }
746
747 if ((c->read_data_length+= r) >= sizeof(c->request)) {
748 struct proto_handler *handler;
749
750 c->request = MAYBE_INT32_SWAP(c->swap_byte_order, c->request);
751
752 if (c->request < ESD_PROTO_CONNECT || c->request > ESD_PROTO_MAX) {
753 pa_log(__FILE__": recieved invalid request.\n");
754 return -1;
755 }
756
757 handler = proto_map+c->request;
758
759 /* pa_log(__FILE__": executing request #%u\n", c->request); */
760
761 if (!handler->proc) {
762 pa_log(__FILE__": recieved unimplemented request #%u.\n", c->request);
763 return -1;
764 }
765
766 if (handler->data_length == 0) {
767 c->read_data_length = 0;
768
769 if (handler->proc(c, c->request, NULL, 0) < 0)
770 return -1;
771
772 } else {
773 if (c->read_data_alloc < handler->data_length)
774 c->read_data = pa_xrealloc(c->read_data, c->read_data_alloc = handler->data_length);
775 assert(c->read_data);
776
777 c->state = ESD_NEEDS_REQDATA;
778 c->read_data_length = 0;
779 }
780 }
781
782 } else if (c->state == ESD_NEEDS_REQDATA) {
783 ssize_t r;
784 struct proto_handler *handler = proto_map+c->request;
785
786 assert(handler->proc);
787
788 assert(c->read_data && c->read_data_length < handler->data_length);
789
790 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
791 pa_log_debug(__FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
792 return -1;
793 }
794
795 if ((c->read_data_length+= r) >= handler->data_length) {
796 size_t l = c->read_data_length;
797 assert(handler->proc);
798
799 c->state = ESD_NEXT_REQUEST;
800 c->read_data_length = 0;
801
802 if (handler->proc(c, c->request, c->read_data, l) < 0)
803 return -1;
804 }
805 } else if (c->state == ESD_CACHING_SAMPLE) {
806 ssize_t r;
807
808 assert(c->scache.memchunk.memblock && c->scache.name && c->scache.memchunk.index < c->scache.memchunk.length);
809
810 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) {
811 pa_log_debug(__FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
812 return -1;
813 }
814
815 c->scache.memchunk.index += r;
816 assert(c->scache.memchunk.index <= c->scache.memchunk.length);
817
818 if (c->scache.memchunk.index == c->scache.memchunk.length) {
819 uint32_t idx;
820 int *ok;
821
822 c->scache.memchunk.index = 0;
823 pa_scache_add_item(c->protocol->core, c->scache.name, &c->scache.sample_spec, NULL, &c->scache.memchunk, &idx);
824
825 pa_memblock_unref(c->scache.memchunk.memblock);
826 c->scache.memchunk.memblock = NULL;
827 c->scache.memchunk.index = c->scache.memchunk.length = 0;
828
829 pa_xfree(c->scache.name);
830 c->scache.name = NULL;
831
832 c->state = ESD_NEXT_REQUEST;
833
834 ok = connection_write(c, sizeof(int));
835 assert(ok);
836 *ok = idx+1;
837 }
838
839 } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
840 pa_memchunk chunk;
841 ssize_t r;
842 size_t l;
843
844 assert(c->input_memblockq);
845
846 /* pa_log("STREAMING_DATA\n"); */
847
848 if (!(l = pa_memblockq_missing(c->input_memblockq)))
849 return 0;
850
851 if (l > c->playback.fragment_size)
852 l = c->playback.fragment_size;
853
854 if (c->playback.current_memblock)
855 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
856 pa_memblock_unref(c->playback.current_memblock);
857 c->playback.current_memblock = NULL;
858 c->playback.memblock_index = 0;
859 }
860
861 if (!c->playback.current_memblock) {
862 c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2, c->protocol->core->memblock_stat);
863 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
864 c->playback.memblock_index = 0;
865 }
866
867 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
868 pa_log_debug(__FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
869 return -1;
870 }
871
872 chunk.memblock = c->playback.current_memblock;
873 chunk.index = c->playback.memblock_index;
874 chunk.length = r;
875 assert(chunk.memblock);
876
877 c->playback.memblock_index += r;
878
879 assert(c->input_memblockq);
880 pa_memblockq_push_align(c->input_memblockq, &chunk);
881 assert(c->sink_input);
882 pa_sink_notify(c->sink_input->sink);
883 }
884
885 return 0;
886 }
887
888 static int do_write(struct connection *c) {
889 assert(c && c->io);
890
891 /* pa_log("WRITE\n"); */
892
893 if (c->write_data_length) {
894 ssize_t r;
895
896 assert(c->write_data_index < c->write_data_length);
897 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) {
898 pa_log(__FILE__": write() failed: %s\n", strerror(errno));
899 return -1;
900 }
901
902 if ((c->write_data_index +=r) >= c->write_data_length)
903 c->write_data_length = c->write_data_index = 0;
904
905 } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
906 pa_memchunk chunk;
907 ssize_t r;
908
909 assert(c->output_memblockq);
910 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
911 return 0;
912
913 assert(chunk.memblock && chunk.length);
914
915 if ((r = pa_iochannel_write(c->io, (uint8_t*) chunk.memblock->data+chunk.index, chunk.length)) < 0) {
916 pa_memblock_unref(chunk.memblock);
917 pa_log(__FILE__": write(): %s\n", strerror(errno));
918 return -1;
919 }
920
921 pa_memblockq_drop(c->output_memblockq, &chunk, r);
922 pa_memblock_unref(chunk.memblock);
923
924 pa_source_notify(c->source_output->source);
925 }
926
927 return 0;
928 }
929
930 static void do_work(struct connection *c) {
931 assert(c);
932
933 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
934 c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
935
936 if (c->dead)
937 return;
938
939 if (pa_iochannel_is_readable(c->io)) {
940 if (do_read(c) < 0)
941 goto fail;
942 } else if (pa_iochannel_is_hungup(c->io))
943 goto fail;
944
945 if (pa_iochannel_is_writable(c->io))
946 if (do_write(c) < 0)
947 goto fail;
948
949 return;
950
951 fail:
952
953 if (c->state == ESD_STREAMING_DATA && c->sink_input) {
954 c->dead = 1;
955
956 pa_iochannel_free(c->io);
957 c->io = NULL;
958
959 pa_memblockq_prebuf_disable(c->input_memblockq);
960 pa_sink_notify(c->sink_input->sink);
961 } else
962 connection_free(c);
963 }
964
965
966 static void io_callback(pa_iochannel*io, void *userdata) {
967 struct connection *c = userdata;
968 assert(io && c && c->io == io);
969
970 /* pa_log("IO\n"); */
971
972 do_work(c);
973 }
974
975 /*** defer callback ***/
976
977 static void defer_callback(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
978 struct connection *c = userdata;
979 assert(a && c && c->defer_event == e);
980
981 /* pa_log("DEFER\n"); */
982
983 do_work(c);
984 }
985
986 /*** sink_input callbacks ***/
987
988 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
989 struct connection*c;
990 assert(i && i->userdata && chunk);
991 c = i->userdata;
992
993 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
994
995 if (c->dead)
996 connection_free(c);
997
998 return -1;
999 }
1000
1001 return 0;
1002 }
1003
1004 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
1005 struct connection*c = i->userdata;
1006 assert(i && c && length);
1007
1008 /* pa_log("DROP\n"); */
1009
1010 pa_memblockq_drop(c->input_memblockq, chunk, length);
1011
1012 /* do something */
1013 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
1014
1015 if (!c->dead)
1016 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
1017
1018 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1019 }
1020
1021 static void sink_input_kill_cb(pa_sink_input *i) {
1022 assert(i && i->userdata);
1023 connection_free((struct connection *) i->userdata);
1024 }
1025
1026 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
1027 struct connection*c = i->userdata;
1028 assert(i && c);
1029 return pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
1030 }
1031
1032 /*** source_output callbacks ***/
1033
1034 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
1035 struct connection *c = o->userdata;
1036 assert(o && c && chunk);
1037
1038 pa_memblockq_push(c->output_memblockq, chunk);
1039
1040 /* do something */
1041 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
1042
1043 if (!c->dead)
1044 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
1045 }
1046
1047 static void source_output_kill_cb(pa_source_output *o) {
1048 assert(o && o->userdata);
1049 connection_free((struct connection *) o->userdata);
1050 }
1051
1052 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
1053 struct connection*c = o->userdata;
1054 assert(o && c);
1055 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
1056 }
1057
1058 /*** socket server callback ***/
1059
1060 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
1061 struct connection *c = userdata;
1062 assert(m && tv && c && c->auth_timeout_event == e);
1063
1064 if (!c->authorized)
1065 connection_free(c);
1066 }
1067
1068 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
1069 struct connection *c;
1070 pa_protocol_esound *p = userdata;
1071 char cname[256];
1072 assert(s && io && p);
1073
1074 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
1075 pa_log(__FILE__": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS);
1076 pa_iochannel_free(io);
1077 return;
1078 }
1079
1080 c = pa_xmalloc(sizeof(struct connection));
1081 c->protocol = p;
1082 c->io = io;
1083 pa_iochannel_set_callback(c->io, io_callback, c);
1084
1085 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
1086 assert(p->core);
1087 c->client = pa_client_new(p->core, __FILE__, cname);
1088 assert(c->client);
1089 c->client->owner = p->module;
1090 c->client->kill = client_kill_cb;
1091 c->client->userdata = c;
1092
1093 c->authorized = p->public;
1094 c->swap_byte_order = 0;
1095 c->dead = 0;
1096
1097 c->read_data_length = 0;
1098 c->read_data = pa_xmalloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
1099
1100 c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
1101 c->write_data = NULL;
1102
1103 c->state = ESD_NEEDS_REQDATA;
1104 c->request = ESD_PROTO_CONNECT;
1105
1106 c->sink_input = NULL;
1107 c->input_memblockq = NULL;
1108
1109 c->source_output = NULL;
1110 c->output_memblockq = NULL;
1111
1112 c->playback.current_memblock = NULL;
1113 c->playback.memblock_index = 0;
1114 c->playback.fragment_size = 0;
1115
1116 c->scache.memchunk.length = c->scache.memchunk.index = 0;
1117 c->scache.memchunk.memblock = NULL;
1118 c->scache.name = NULL;
1119
1120 if (!c->authorized) {
1121 struct timeval tv;
1122 pa_gettimeofday(&tv);
1123 tv.tv_sec += AUTH_TIMEOUT;
1124 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
1125 } else
1126 c->auth_timeout_event = NULL;
1127
1128 c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
1129 assert(c->defer_event);
1130 p->core->mainloop->defer_enable(c->defer_event, 0);
1131
1132 pa_idxset_put(p->connections, c, &c->index);
1133 }
1134
1135 /*** entry points ***/
1136
1137 pa_protocol_esound* pa_protocol_esound_new(pa_core*core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
1138 pa_protocol_esound *p;
1139 int public = 0;
1140 assert(core && server && ma);
1141
1142 p = pa_xmalloc(sizeof(pa_protocol_esound));
1143
1144 if (pa_modargs_get_value_boolean(ma, "public", &public) < 0) {
1145 pa_log(__FILE__": public= expects a boolean argument.\n");
1146 return NULL;
1147 }
1148
1149 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", DEFAULT_COOKIE_FILE), p->esd_key, sizeof(p->esd_key)) < 0) {
1150 pa_xfree(p);
1151 return NULL;
1152 }
1153
1154 p->module = m;
1155 p->public = public;
1156 p->server = server;
1157 pa_socket_server_set_callback(p->server, on_connection, p);
1158 p->core = core;
1159 p->connections = pa_idxset_new(NULL, NULL);
1160 assert(p->connections);
1161
1162 p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
1163 p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
1164 p->n_player = 0;
1165
1166 return p;
1167 }
1168
1169 void pa_protocol_esound_free(pa_protocol_esound *p) {
1170 struct connection *c;
1171 assert(p);
1172
1173 while ((c = pa_idxset_first(p->connections, NULL)))
1174 connection_free(c);
1175
1176 pa_idxset_free(p->connections, NULL, NULL);
1177 pa_socket_server_unref(p->server);
1178 pa_xfree(p);
1179 }