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