]> code.delx.au - pulseaudio/blob - src/protocol-esound.c
fc4444c357891201ad32a74acf9a380b6d224176
[pulseaudio] / src / protocol-esound.c
1 #include <errno.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <limits.h>
7
8 #include "protocol-esound.h"
9 #include "esound-spec.h"
10 #include "memblock.h"
11 #include "client.h"
12 #include "sinkinput.h"
13 #include "sink.h"
14 #include "sourceoutput.h"
15 #include "source.h"
16 #include "sample.h"
17
18 #include "authkey.h"
19
20 #define COOKIE_FILE ".esd_auth"
21
22 #define PLAYBACK_BUFFER_SECONDS (.5)
23 #define PLAYBACK_BUFFER_FRAGMENTS (10)
24 #define RECORD_BUFFER_SECONDS (5)
25 #define RECORD_BUFFER_FRAGMENTS (100)
26
27 /* This is heavily based on esound's code */
28
29 struct connection {
30 uint32_t index;
31 struct pa_protocol_esound *protocol;
32 struct pa_iochannel *io;
33 struct pa_client *client;
34 int authorized, swap_byte_order;
35 void *write_data;
36 size_t write_data_alloc, write_data_index, write_data_length;
37 void *read_data;
38 size_t read_data_alloc, read_data_length;
39 esd_proto_t request;
40 esd_client_state_t state;
41 struct pa_sink_input *sink_input;
42 struct pa_source_output *source_output;
43 struct pa_memblockq *input_memblockq, *output_memblockq;
44 void *fixed_source;
45 struct {
46 struct pa_memblock *current_memblock;
47 size_t memblock_index, fragment_size;
48 } playback;
49 };
50
51 struct pa_protocol_esound {
52 int public;
53 struct pa_module *module;
54 struct pa_core *core;
55 struct pa_socket_server *server;
56 struct pa_idxset *connections;
57 uint32_t sink_index, source_index;
58 unsigned n_player;
59 uint8_t esd_key[ESD_KEY_LEN];
60 };
61
62 typedef struct proto_handler {
63 size_t data_length;
64 int (*proc)(struct connection *c, esd_proto_t request, const void *data, size_t length);
65 const char *description;
66 } esd_proto_handler_info_t;
67
68 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length);
69 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
70 static void sink_input_kill_cb(struct pa_sink_input *i);
71 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);
72
73 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
74 static void source_output_kill_cb(struct pa_source_output *o);
75
76 static int esd_proto_connect(struct connection *c, esd_proto_t request, const void *data, size_t length);
77 static int esd_proto_stream_play(struct connection *c, esd_proto_t request, const void *data, size_t length);
78 static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length);
79 static int esd_proto_get_latency(struct connection *c, esd_proto_t request, const void *data, size_t length);
80 static int esd_proto_server_info(struct connection *c, esd_proto_t request, const void *data, size_t length);
81 static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length);
82 static int esd_proto_stream_pan(struct connection *c, esd_proto_t request, const void *data, size_t length);
83
84 /* the big map of protocol handler info */
85 static struct proto_handler proto_map[ESD_PROTO_MAX] = {
86 { ESD_KEY_LEN + sizeof(int), esd_proto_connect, "connect" },
87 { ESD_KEY_LEN + sizeof(int), NULL, "lock" },
88 { ESD_KEY_LEN + sizeof(int), NULL, "unlock" },
89
90 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_play, "stream play" },
91 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream rec" },
92 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream mon" },
93
94 { ESD_NAME_MAX + 3 * sizeof(int), NULL, "sample cache" },
95 { sizeof(int), NULL, "sample free" },
96 { sizeof(int), NULL, "sample play" },
97 { sizeof(int), NULL, "sample loop" },
98 { sizeof(int), NULL, "sample stop" },
99 { -1, NULL, "TODO: sample kill" },
100
101 { ESD_KEY_LEN + sizeof(int), NULL, "standby" },
102 { ESD_KEY_LEN + sizeof(int), NULL, "resume" },
103
104 { ESD_NAME_MAX, NULL, "sample getid" },
105 { ESD_NAME_MAX + 2 * sizeof(int), NULL, "stream filter" },
106
107 { sizeof(int), esd_proto_server_info, "server info" },
108 { sizeof(int), esd_proto_all_info, "all info" },
109 { -1, NULL, "TODO: subscribe" },
110 { -1, NULL, "TODO: unsubscribe" },
111
112 { 3 * sizeof(int), esd_proto_stream_pan, "stream pan"},
113 { 3 * sizeof(int), NULL, "sample pan" },
114
115 { sizeof(int), NULL, "standby mode" },
116 { 0, esd_proto_get_latency, "get latency" }
117 };
118
119
120 static void connection_free(struct connection *c) {
121 assert(c);
122 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
123
124 if (c->state == ESD_STREAMING_DATA)
125 c->protocol->n_player--;
126
127 pa_client_free(c->client);
128
129 if (c->sink_input)
130 pa_sink_input_free(c->sink_input);
131 if (c->source_output)
132 pa_source_output_free(c->source_output);
133 if (c->input_memblockq)
134 pa_memblockq_free(c->input_memblockq);
135 if (c->output_memblockq)
136 pa_memblockq_free(c->output_memblockq);
137
138 if (c->playback.current_memblock)
139 pa_memblock_unref(c->playback.current_memblock);
140
141 free(c->read_data);
142 free(c->write_data);
143
144 pa_iochannel_free(c->io);
145
146 if (c->fixed_source)
147 c->protocol->core->mainloop->cancel_fixed(c->protocol->core->mainloop, c->fixed_source);
148
149 free(c);
150 }
151
152 static struct pa_sink* get_output_sink(struct pa_protocol_esound *p) {
153 struct pa_sink *s;
154 assert(p);
155
156 if (!(s = pa_idxset_get_by_index(p->core->sinks, p->sink_index)))
157 s = pa_sink_get_default(p->core);
158
159 p->sink_index = s ? s->index : PA_IDXSET_INVALID;
160 return s;
161 }
162
163 static struct pa_source* get_input_source(struct pa_protocol_esound *p) {
164 struct pa_source *s;
165 assert(p);
166
167 if (!(s = pa_idxset_get_by_index(p->core->sources, p->sink_index)))
168 s = pa_source_get_default(p->core);
169
170 p->source_index = s ? s->index : PA_IDXSET_INVALID;
171 return s;
172 }
173
174 static void* connection_write(struct connection *c, size_t length) {
175 size_t t, i;
176 assert(c);
177
178 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
179 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
180
181 t = c->write_data_length+length;
182
183 if (c->write_data_alloc < t)
184 c->write_data = realloc(c->write_data, c->write_data_alloc = t);
185
186 assert(c->write_data);
187
188 i = c->write_data_length;
189 c->write_data_length += length;
190
191 return c->write_data+i;
192 }
193
194 /*** esound commands ***/
195
196 static int esd_proto_connect(struct connection *c, esd_proto_t request, const void *data, size_t length) {
197 uint32_t ekey;
198 int *ok;
199 assert(length == (ESD_KEY_LEN + sizeof(uint32_t)));
200
201 if (!c->authorized) {
202 if (memcmp(data, c->protocol->esd_key, ESD_KEY_LEN) != 0) {
203 fprintf(stderr, __FILE__": kicked client with invalid authorization key.\n");
204 return -1;
205 }
206
207 c->authorized = 1;
208 }
209
210 ekey = *(uint32_t*)(data+ESD_KEY_LEN);
211 if (ekey == ESD_ENDIAN_KEY)
212 c->swap_byte_order = 0;
213 else if (ekey == ESD_SWAP_ENDIAN_KEY)
214 c->swap_byte_order = 1;
215 else {
216 fprintf(stderr, __FILE__": client sent invalid endian key\n");
217 return -1;
218 }
219
220 ok = connection_write(c, sizeof(int));
221 assert(ok);
222 *ok = 1;
223 return 0;
224 }
225
226 static int esd_proto_stream_play(struct connection *c, esd_proto_t request, const void *data, size_t length) {
227 char name[ESD_NAME_MAX];
228 int format, rate;
229 struct pa_sink *sink;
230 struct pa_sample_spec ss;
231 size_t l;
232 assert(c && length == (sizeof(int)*2+ESD_NAME_MAX));
233
234 format = maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
235 rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
236
237 ss.rate = rate;
238 ss.channels = ((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1;
239 ss.format = ((format & ESD_MASK_BITS) == ESD_BITS16) ? PA_SAMPLE_S16NE : PA_SAMPLE_U8;
240
241 if (!pa_sample_spec_valid(&ss))
242 return -1;
243
244 if (!(sink = get_output_sink(c->protocol)))
245 return -1;
246
247 strncpy(name, data + sizeof(int)*2, sizeof(name));
248 name[sizeof(name)-1] = 0;
249
250 pa_client_rename(c->client, name);
251
252 assert(!c->input_memblockq);
253
254 l = (size_t) (pa_bytes_per_second(&ss)*PLAYBACK_BUFFER_SECONDS);
255 c->input_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&ss), l/2, l/PLAYBACK_BUFFER_FRAGMENTS);
256 assert(c->input_memblockq);
257 pa_iochannel_socket_set_rcvbuf(c->io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
258 c->playback.fragment_size = l/10;
259
260 assert(!c->sink_input);
261 c->sink_input = pa_sink_input_new(sink, name, &ss);
262 assert(c->sink_input);
263
264 c->sink_input->owner = c->protocol->module;
265 c->sink_input->client = c->client;
266 c->sink_input->peek = sink_input_peek_cb;
267 c->sink_input->drop = sink_input_drop_cb;
268 c->sink_input->kill = sink_input_kill_cb;
269 c->sink_input->get_latency = sink_input_get_latency_cb;
270 c->sink_input->userdata = c;
271
272 c->state = ESD_STREAMING_DATA;
273
274 c->protocol->n_player++;
275
276 return 0;
277 }
278
279 static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length) {
280 char name[ESD_NAME_MAX];
281 int format, rate;
282 struct pa_source *source;
283 struct pa_sample_spec ss;
284 size_t l;
285 assert(c && length == (sizeof(int)*2+ESD_NAME_MAX));
286
287 format = maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
288 rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
289
290 ss.rate = rate;
291 ss.channels = ((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1;
292 ss.format = ((format & ESD_MASK_BITS) == ESD_BITS16) ? PA_SAMPLE_S16NE : PA_SAMPLE_U8;
293
294 if (!pa_sample_spec_valid(&ss))
295 return -1;
296
297 if (request == ESD_PROTO_STREAM_MON) {
298 struct pa_sink* sink;
299
300 if (!(sink = get_output_sink(c->protocol)))
301 return -1;
302
303 if (!(source = sink->monitor_source))
304 return -1;
305 } else {
306 assert(request == ESD_PROTO_STREAM_REC);
307
308 if (!(source = get_input_source(c->protocol)))
309 return -1;
310 }
311
312 strncpy(name, data + sizeof(int)*2, sizeof(name));
313 name[sizeof(name)-1] = 0;
314
315 pa_client_rename(c->client, name);
316
317 assert(!c->output_memblockq);
318
319 l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS);
320 c->output_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&ss), 0, 0);
321 assert(c->output_memblockq);
322 pa_iochannel_socket_set_sndbuf(c->io, l/RECORD_BUFFER_FRAGMENTS*2);
323
324 assert(!c->source_output);
325 c->source_output = pa_source_output_new(source, name, &ss);
326 assert(c->source_output);
327
328 c->source_output->owner = c->protocol->module;
329 c->source_output->client = c->client;
330 c->source_output->push = source_output_push_cb;
331 c->source_output->kill = source_output_kill_cb;
332 c->source_output->userdata = c;
333
334 c->state = ESD_STREAMING_DATA;
335
336 c->protocol->n_player++;
337
338 return 0;
339 }
340
341 static int esd_proto_get_latency(struct connection *c, esd_proto_t request, const void *data, size_t length) {
342 struct pa_sink *sink;
343 int latency, *lag;
344 assert(c && !data && length == 0);
345
346 if (!(sink = get_output_sink(c->protocol)))
347 latency = 0;
348 else {
349 float usec = pa_sink_get_latency(sink);
350 usec += PLAYBACK_BUFFER_SECONDS*1000000*.9; /* A better estimation would be a good idea! */
351 latency = (int) ((usec*44100)/1000000);
352 }
353
354 lag = connection_write(c, sizeof(int));
355 assert(lag);
356 *lag = c->swap_byte_order ? swap_endian_32(latency) : latency;
357 return 0;
358 }
359
360 static int esd_proto_server_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
361 int rate = 44100, format = ESD_STEREO|ESD_BITS16;
362 int *response;
363 struct pa_sink *sink;
364 assert(c && data && length == sizeof(int));
365
366 if ((sink = get_output_sink(c->protocol))) {
367 rate = sink->sample_spec.rate;
368 format = (sink->sample_spec.format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
369 format |= (sink->sample_spec.channels >= 2) ? ESD_STEREO : ESD_MONO;
370 }
371
372 response = connection_write(c, sizeof(int)*3);
373 assert(response);
374 *(response++) = 0;
375 *(response++) = maybe_swap_endian_32(c->swap_byte_order, rate);
376 *(response++) = maybe_swap_endian_32(c->swap_byte_order, format);
377 return 0;
378 }
379
380 static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
381 void *response;
382 size_t t, k, s;
383 struct connection *conn;
384 size_t index = PA_IDXSET_INVALID;
385 assert(c && data && length == sizeof(int));
386
387 if (esd_proto_server_info(c, request, data, length) < 0)
388 return -1;
389
390 k = sizeof(int)*5+ESD_NAME_MAX;
391 s = sizeof(int)*6+ESD_NAME_MAX;
392 response = connection_write(c, (t = s+k*(c->protocol->n_player+1)));
393 assert(k);
394
395 for (conn = pa_idxset_first(c->protocol->connections, &index); conn; conn = pa_idxset_next(c->protocol->connections, &index)) {
396 int format = ESD_BITS16 | ESD_STEREO, rate = 44100, volume = 0xFF;
397
398 if (conn->state != ESD_STREAMING_DATA)
399 continue;
400
401 assert(t >= s+k+k);
402
403 if (conn->sink_input) {
404 rate = conn->sink_input->sample_spec.rate;
405 volume = (conn->sink_input->volume*0xFF)/0x100;
406 format = (conn->sink_input->sample_spec.format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
407 format |= (conn->sink_input->sample_spec.channels >= 2) ? ESD_STEREO : ESD_MONO;
408 }
409
410 /* id */
411 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, (int) conn->index);
412 response += sizeof(int);
413
414 /* name */
415 assert(conn->client);
416 strncpy(response, conn->client->name, ESD_NAME_MAX);
417 response += ESD_NAME_MAX;
418
419 /* rate */
420 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, rate);
421 response += sizeof(int);
422
423 /* left */
424 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, volume);
425 response += sizeof(int);
426
427 /*right*/
428 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, volume);
429 response += sizeof(int);
430
431 /*format*/
432 *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, format);
433 response += sizeof(int);
434
435 t-= k;
436 }
437
438 assert(t == s+k);
439 memset(response, 0, t);
440 return 0;
441 }
442
443 static int esd_proto_stream_pan(struct connection *c, esd_proto_t request, const void *data, size_t length) {
444 int *ok;
445 uint32_t index, volume;
446 struct connection *conn;
447 assert(c && data && length == sizeof(int)*3);
448
449 index = (uint32_t) maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
450 volume = (uint32_t) maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
451 volume = (volume*0x100)/0xFF;
452
453 ok = connection_write(c, sizeof(int));
454 assert(ok);
455
456 if ((conn = pa_idxset_get_by_index(c->protocol->connections, index))) {
457 assert(conn->sink_input);
458 conn->sink_input->volume = volume;
459 *ok = 1;
460 } else
461 *ok = 0;
462
463 return 0;
464 }
465
466 /*** client callbacks ***/
467
468 static void client_kill_cb(struct pa_client *c) {
469 assert(c && c->userdata);
470 connection_free(c->userdata);
471 }
472
473 /*** pa_iochannel callbacks ***/
474
475 static int do_read(struct connection *c) {
476 assert(c && c->io);
477
478 if (c->state == ESD_NEXT_REQUEST) {
479 ssize_t r;
480 assert(c->read_data_length < sizeof(c->request));
481
482 if ((r = pa_iochannel_read(c->io, ((void*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
483 fprintf(stderr, "protocol-esound.c: read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
484 return -1;
485 }
486
487 if ((c->read_data_length+= r) >= sizeof(c->request)) {
488 struct proto_handler *handler;
489
490 if (c->swap_byte_order)
491 c->request = swap_endian_32(c->request);
492
493 if (c->request < ESD_PROTO_CONNECT || c->request > ESD_PROTO_MAX) {
494 fprintf(stderr, "protocol-esound.c: recieved invalid request.\n");
495 return -1;
496 }
497
498 handler = proto_map+c->request;
499
500 if (!handler->proc) {
501 fprintf(stderr, "protocol-sound.c: recieved unimplemented request.\n");
502 return -1;
503 }
504
505 if (handler->data_length == 0) {
506 c->read_data_length = 0;
507
508 if (handler->proc(c, c->request, NULL, 0) < 0)
509 return -1;
510
511 } else {
512 if (c->read_data_alloc < handler->data_length)
513 c->read_data = realloc(c->read_data, c->read_data_alloc = handler->data_length);
514 assert(c->read_data);
515
516 c->state = ESD_NEEDS_REQDATA;
517 c->read_data_length = 0;
518 }
519 }
520
521 } else if (c->state == ESD_NEEDS_REQDATA) {
522 ssize_t r;
523 struct proto_handler *handler = proto_map+c->request;
524
525 assert(handler->proc);
526
527 assert(c->read_data && c->read_data_length < handler->data_length);
528
529 if ((r = pa_iochannel_read(c->io, c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
530 fprintf(stderr, "protocol-esound.c: read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
531 return -1;
532 }
533
534 if ((c->read_data_length+= r) >= handler->data_length) {
535 size_t l = c->read_data_length;
536 assert(handler->proc);
537
538 c->state = ESD_NEXT_REQUEST;
539 c->read_data_length = 0;
540
541 if (handler->proc(c, c->request, c->read_data, l) < 0)
542 return -1;
543 }
544 } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
545 struct pa_memchunk chunk;
546 ssize_t r;
547 size_t l;
548
549 assert(c->input_memblockq);
550
551 if (!(l = pa_memblockq_missing(c->input_memblockq)))
552 return 0;
553
554 if (l > c->playback.fragment_size)
555 l = c->playback.fragment_size;
556
557 if (c->playback.current_memblock)
558 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
559 pa_memblock_unref(c->playback.current_memblock);
560 c->playback.current_memblock = NULL;
561 c->playback.memblock_index = 0;
562 }
563
564
565 if (!c->playback.current_memblock) {
566 c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2);
567 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
568 c->playback.memblock_index = 0;
569 }
570
571 if ((r = pa_iochannel_read(c->io, c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
572 fprintf(stderr, __FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
573 return -1;
574 }
575
576 chunk.memblock = c->playback.current_memblock;
577 chunk.index = c->playback.memblock_index;
578 chunk.length = r;
579 assert(chunk.memblock);
580
581 c->playback.memblock_index += r;
582
583 assert(c->input_memblockq);
584 pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
585 assert(c->sink_input);
586 pa_sink_notify(c->sink_input->sink);
587
588 }
589
590 return 0;
591 }
592
593 static int do_write(struct connection *c) {
594 assert(c && c->io);
595
596 if (c->write_data_length) {
597 ssize_t r;
598
599 assert(c->write_data_index < c->write_data_length);
600 if ((r = pa_iochannel_write(c->io, c->write_data+c->write_data_index, c->write_data_length-c->write_data_index)) < 0) {
601 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
602 return -1;
603 }
604
605 if ((c->write_data_index +=r) >= c->write_data_length)
606 c->write_data_length = c->write_data_index = 0;
607
608 } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
609 struct pa_memchunk chunk;
610 ssize_t r;
611
612 assert(c->output_memblockq);
613 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
614 return 0;
615
616 assert(chunk.memblock && chunk.length);
617
618 if ((r = pa_iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
619 pa_memblock_unref(chunk.memblock);
620 fprintf(stderr, __FILE__": write(): %s\n", strerror(errno));
621 return -1;
622 }
623
624 pa_memblockq_drop(c->output_memblockq, r);
625 pa_memblock_unref(chunk.memblock);
626 }
627
628 return 0;
629 }
630
631 static void do_work(struct connection *c) {
632 assert(c);
633
634 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
635 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);
636
637 if (pa_iochannel_is_hungup(c->io))
638 goto fail;
639
640 if (pa_iochannel_is_writable(c->io))
641 if (do_write(c) < 0)
642 goto fail;
643
644 if (pa_iochannel_is_readable(c->io))
645 if (do_read(c) < 0)
646 goto fail;
647
648 return;
649
650 fail:
651 connection_free(c);
652 }
653
654 static void io_callback(struct pa_iochannel*io, void *userdata) {
655 struct connection *c = userdata;
656 assert(io && c && c->io == io);
657
658 do_work(c);
659 }
660
661 /*** fixed callback ***/
662
663 void fixed_callback(struct pa_mainloop_api*a, void *id, void *userdata) {
664 struct connection *c = userdata;
665 assert(a && c && c->fixed_source == id);
666
667 do_work(c);
668 }
669
670 /*** sink_input callbacks ***/
671
672 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
673 struct connection*c;
674 assert(i && i->userdata && chunk);
675 c = i->userdata;
676
677 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
678 return -1;
679
680 return 0;
681 }
682
683 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
684 struct connection*c = i->userdata;
685 assert(i && c && length);
686
687 pa_memblockq_drop(c->input_memblockq, length);
688
689 /* do something */
690 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
691 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
692 }
693
694 static void sink_input_kill_cb(struct pa_sink_input *i) {
695 assert(i && i->userdata);
696 connection_free((struct connection *) i->userdata);
697 }
698
699
700 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
701 struct connection*c = i->userdata;
702 assert(i && c);
703 return pa_samples_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
704 }
705
706 /*** source_output callbacks ***/
707
708 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
709 struct connection *c = o->userdata;
710 assert(o && c && chunk);
711
712 pa_memblockq_push(c->output_memblockq, chunk, 0);
713
714 /* do something */
715 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
716 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
717 }
718
719 static void source_output_kill_cb(struct pa_source_output *o) {
720 assert(o && o->userdata);
721 connection_free((struct connection *) o->userdata);
722 }
723
724 /*** socket server callback ***/
725
726 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
727 struct connection *c;
728 char cname[256];
729 assert(s && io && userdata);
730
731 c = malloc(sizeof(struct connection));
732 assert(c);
733 c->protocol = userdata;
734 c->io = io;
735 pa_iochannel_set_callback(c->io, io_callback, c);
736
737 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
738 assert(c->protocol->core);
739 c->client = pa_client_new(c->protocol->core, "ESOUND", cname);
740 assert(c->client);
741 c->client->owner = c->protocol->module;
742 c->client->kill = client_kill_cb;
743 c->client->userdata = c;
744
745 c->authorized = c->protocol->public;
746 c->swap_byte_order = 0;
747
748 c->read_data_length = 0;
749 c->read_data = malloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
750 assert(c->read_data);
751
752 c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
753 c->write_data = NULL;
754
755 c->state = ESD_NEEDS_REQDATA;
756 c->request = ESD_PROTO_CONNECT;
757
758 c->sink_input = NULL;
759 c->input_memblockq = NULL;
760
761 c->source_output = NULL;
762 c->output_memblockq = NULL;
763
764 c->playback.current_memblock = NULL;
765 c->playback.memblock_index = 0;
766 c->playback.fragment_size = 0;
767
768 c->fixed_source = c->protocol->core->mainloop->source_fixed(c->protocol->core->mainloop, fixed_callback, c);
769 assert(c->fixed_source);
770 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);
771
772 pa_idxset_put(c->protocol->connections, c, &c->index);
773 }
774
775 /*** entry points ***/
776
777 struct pa_protocol_esound* pa_protocol_esound_new(struct pa_core*core, struct pa_socket_server *server, struct pa_module *m) {
778 struct pa_protocol_esound *p;
779 assert(core && server);
780
781 p = malloc(sizeof(struct pa_protocol_esound));
782 assert(p);
783
784 if (pa_authkey_load_from_home(COOKIE_FILE, p->esd_key, sizeof(p->esd_key)) < 0) {
785 free(p);
786 return NULL;
787 }
788
789 p->module = m;
790 p->public = 0;
791 p->server = server;
792 pa_socket_server_set_callback(p->server, on_connection, p);
793 p->core = core;
794 p->connections = pa_idxset_new(NULL, NULL);
795 assert(p->connections);
796 p->sink_index = p->source_index = PA_IDXSET_INVALID;
797 p->n_player = 0;
798
799 return p;
800 }
801
802 void pa_protocol_esound_free(struct pa_protocol_esound *p) {
803 struct connection *c;
804 assert(p);
805
806 while ((c = pa_idxset_first(p->connections, NULL)))
807 connection_free(c);
808
809 pa_idxset_free(p->connections, NULL, NULL);
810 pa_socket_server_free(p->server);
811 free(p);
812 }