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