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