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