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