]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-esound.c
add a "length" argument to the seek functions, as an optimization to request a certai...
[pulseaudio] / src / pulsecore / protocol-esound.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <limits.h>
34
35 #include <pulse/sample.h>
36 #include <pulse/timeval.h>
37 #include <pulse/utf8.h>
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/esound.h>
41 #include <pulsecore/memblock.h>
42 #include <pulsecore/client.h>
43 #include <pulsecore/sink-input.h>
44 #include <pulsecore/sink.h>
45 #include <pulsecore/source-output.h>
46 #include <pulsecore/source.h>
47 #include <pulsecore/core-scache.h>
48 #include <pulsecore/sample-util.h>
49 #include <pulsecore/authkey.h>
50 #include <pulsecore/namereg.h>
51 #include <pulsecore/log.h>
52 #include <pulsecore/core-util.h>
53 #include <pulsecore/core-error.h>
54 #include <pulsecore/ipacl.h>
55 #include <pulsecore/macro.h>
56 #include <pulsecore/thread-mq.h>
57
58 #include "endianmacros.h"
59
60 #include "protocol-esound.h"
61
62 /* Don't accept more connection than this */
63 #define MAX_CONNECTIONS 64
64
65 /* Kick a client if it doesn't authenticate within this time */
66 #define AUTH_TIMEOUT 5
67
68 #define DEFAULT_COOKIE_FILE ".esd_auth"
69
70 #define PLAYBACK_BUFFER_SECONDS (.25)
71 #define PLAYBACK_BUFFER_FRAGMENTS (10)
72 #define RECORD_BUFFER_SECONDS (5)
73 #define RECORD_BUFFER_FRAGMENTS (100)
74
75 #define MAX_CACHE_SAMPLE_SIZE (1024000)
76
77 #define SCACHE_PREFIX "esound."
78
79 /* This is heavily based on esound's code */
80
81 typedef struct connection {
82 pa_msgobject parent;
83
84 uint32_t index;
85 int dead;
86 pa_protocol_esound *protocol;
87 pa_iochannel *io;
88 pa_client *client;
89 int authorized, swap_byte_order;
90 void *write_data;
91 size_t write_data_alloc, write_data_index, write_data_length;
92 void *read_data;
93 size_t read_data_alloc, read_data_length;
94 esd_proto_t request;
95 esd_client_state_t state;
96 pa_sink_input *sink_input;
97 pa_source_output *source_output;
98 pa_memblockq *input_memblockq, *output_memblockq;
99 pa_defer_event *defer_event;
100
101 char *original_name;
102
103 struct {
104 pa_memblock *current_memblock;
105 size_t memblock_index, fragment_size;
106 pa_atomic_t missing;
107 } playback;
108
109 struct {
110 pa_memchunk memchunk;
111 char *name;
112 pa_sample_spec sample_spec;
113 } scache;
114
115 pa_time_event *auth_timeout_event;
116 } connection;
117
118 PA_DECLARE_CLASS(connection);
119 #define CONNECTION(o) (connection_cast(o))
120 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
121
122 struct pa_protocol_esound {
123 pa_module *module;
124 pa_core *core;
125 int public;
126 pa_socket_server *server;
127 pa_idxset *connections;
128
129 char *sink_name, *source_name;
130 unsigned n_player;
131 uint8_t esd_key[ESD_KEY_LEN];
132 pa_ip_acl *auth_ip_acl;
133 };
134
135 enum {
136 SINK_INPUT_MESSAGE_POST_DATA = PA_SINK_INPUT_MESSAGE_MAX, /* data from main loop to sink input */
137 SINK_INPUT_MESSAGE_DISABLE_PREBUF
138 };
139
140 enum {
141 CONNECTION_MESSAGE_REQUEST_DATA,
142 CONNECTION_MESSAGE_POST_DATA,
143 CONNECTION_MESSAGE_UNLINK_CONNECTION
144 };
145
146 typedef struct proto_handler {
147 size_t data_length;
148 int (*proc)(connection *c, esd_proto_t request, const void *data, size_t length);
149 const char *description;
150 } esd_proto_handler_info_t;
151
152 static void sink_input_drop_cb(pa_sink_input *i, size_t length);
153 static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk);
154 static void sink_input_kill_cb(pa_sink_input *i);
155 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk);
156 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
157
158 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
159 static void source_output_kill_cb(pa_source_output *o);
160
161 static int esd_proto_connect(connection *c, esd_proto_t request, const void *data, size_t length);
162 static int esd_proto_stream_play(connection *c, esd_proto_t request, const void *data, size_t length);
163 static int esd_proto_stream_record(connection *c, esd_proto_t request, const void *data, size_t length);
164 static int esd_proto_get_latency(connection *c, esd_proto_t request, const void *data, size_t length);
165 static int esd_proto_server_info(connection *c, esd_proto_t request, const void *data, size_t length);
166 static int esd_proto_all_info(connection *c, esd_proto_t request, const void *data, size_t length);
167 static int esd_proto_stream_pan(connection *c, esd_proto_t request, const void *data, size_t length);
168 static int esd_proto_sample_cache(connection *c, esd_proto_t request, const void *data, size_t length);
169 static int esd_proto_sample_free_or_play(connection *c, esd_proto_t request, const void *data, size_t length);
170 static int esd_proto_sample_get_id(connection *c, esd_proto_t request, const void *data, size_t length);
171 static int esd_proto_standby_or_resume(connection *c, esd_proto_t request, const void *data, size_t length);
172
173 /* the big map of protocol handler info */
174 static struct proto_handler proto_map[ESD_PROTO_MAX] = {
175 { ESD_KEY_LEN + sizeof(int), esd_proto_connect, "connect" },
176 { ESD_KEY_LEN + sizeof(int), NULL, "lock" },
177 { ESD_KEY_LEN + sizeof(int), NULL, "unlock" },
178
179 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_play, "stream play" },
180 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream rec" },
181 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream mon" },
182
183 { ESD_NAME_MAX + 3 * sizeof(int), esd_proto_sample_cache, "sample cache" }, /* 6 */
184 { sizeof(int), esd_proto_sample_free_or_play, "sample free" },
185 { sizeof(int), esd_proto_sample_free_or_play, "sample play" }, /* 8 */
186 { sizeof(int), NULL, "sample loop" },
187 { sizeof(int), NULL, "sample stop" },
188 { -1, NULL, "TODO: sample kill" },
189
190 { ESD_KEY_LEN + sizeof(int), esd_proto_standby_or_resume, "standby" }, /* NOOP! */
191 { ESD_KEY_LEN + sizeof(int), esd_proto_standby_or_resume, "resume" }, /* NOOP! */ /* 13 */
192
193 { ESD_NAME_MAX, esd_proto_sample_get_id, "sample getid" }, /* 14 */
194 { ESD_NAME_MAX + 2 * sizeof(int), NULL, "stream filter" },
195
196 { sizeof(int), esd_proto_server_info, "server info" },
197 { sizeof(int), esd_proto_all_info, "all info" },
198 { -1, NULL, "TODO: subscribe" },
199 { -1, NULL, "TODO: unsubscribe" },
200
201 { 3 * sizeof(int), esd_proto_stream_pan, "stream pan"},
202 { 3 * sizeof(int), NULL, "sample pan" },
203
204 { sizeof(int), NULL, "standby mode" },
205 { 0, esd_proto_get_latency, "get latency" }
206 };
207
208 static void connection_unlink(connection *c) {
209 pa_assert(c);
210
211 if (!c->protocol)
212 return;
213
214 if (c->sink_input) {
215 pa_sink_input_unlink(c->sink_input);
216 pa_sink_input_unref(c->sink_input);
217 c->sink_input = NULL;
218 }
219
220 if (c->source_output) {
221 pa_source_output_unlink(c->source_output);
222 pa_source_output_unref(c->source_output);
223 c->source_output = NULL;
224 }
225
226 if (c->client) {
227 pa_client_free(c->client);
228 c->client = NULL;
229 }
230
231 if (c->state == ESD_STREAMING_DATA)
232 c->protocol->n_player--;
233
234 if (c->io) {
235 pa_iochannel_free(c->io);
236 c->io = NULL;
237 }
238
239 if (c->defer_event) {
240 c->protocol->core->mainloop->defer_free(c->defer_event);
241 c->defer_event = NULL;
242 }
243
244 if (c->auth_timeout_event) {
245 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
246 c->auth_timeout_event = NULL;
247 }
248
249 pa_assert_se(pa_idxset_remove_by_data(c->protocol->connections, c, NULL) == c);
250 c->protocol = NULL;
251 connection_unref(c);
252 }
253
254 static void connection_free(pa_object *obj) {
255 connection *c = CONNECTION(obj);
256 pa_assert(c);
257
258 if (c->input_memblockq)
259 pa_memblockq_free(c->input_memblockq);
260 if (c->output_memblockq)
261 pa_memblockq_free(c->output_memblockq);
262
263 if (c->playback.current_memblock)
264 pa_memblock_unref(c->playback.current_memblock);
265
266 pa_xfree(c->read_data);
267 pa_xfree(c->write_data);
268
269 if (c->scache.memchunk.memblock)
270 pa_memblock_unref(c->scache.memchunk.memblock);
271 pa_xfree(c->scache.name);
272
273 pa_xfree(c->original_name);
274 pa_xfree(c);
275 }
276
277 static void connection_write_prepare(connection *c, size_t length) {
278 size_t t;
279 pa_assert(c);
280
281 t = c->write_data_length+length;
282
283 if (c->write_data_alloc < t)
284 c->write_data = pa_xrealloc(c->write_data, c->write_data_alloc = t);
285
286 pa_assert(c->write_data);
287 }
288
289 static void connection_write(connection *c, const void *data, size_t length) {
290 size_t i;
291 pa_assert(c);
292
293 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
294
295 connection_write_prepare(c, length);
296
297 pa_assert(c->write_data);
298
299 i = c->write_data_length;
300 c->write_data_length += length;
301
302 memcpy((uint8_t*) c->write_data + i, data, length);
303 }
304
305 static void format_esd2native(int format, int swap_bytes, pa_sample_spec *ss) {
306 pa_assert(ss);
307
308 ss->channels = ((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1;
309 if ((format & ESD_MASK_BITS) == ESD_BITS16)
310 ss->format = swap_bytes ? PA_SAMPLE_S16RE : PA_SAMPLE_S16NE;
311 else
312 ss->format = PA_SAMPLE_U8;
313 }
314
315 static int format_native2esd(pa_sample_spec *ss) {
316 int format = 0;
317
318 format = (ss->format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
319 format |= (ss->channels >= 2) ? ESD_STEREO : ESD_MONO;
320
321 return format;
322 }
323
324 #define CHECK_VALIDITY(expression, ...) do { \
325 if (!(expression)) { \
326 pa_log_warn(__FILE__ ": " __VA_ARGS__); \
327 return -1; \
328 } \
329 } while(0);
330
331 /*** esound commands ***/
332
333 static int esd_proto_connect(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
334 uint32_t ekey;
335 int ok;
336
337 connection_assert_ref(c);
338 pa_assert(data);
339 pa_assert(length == (ESD_KEY_LEN + sizeof(uint32_t)));
340
341 if (!c->authorized) {
342 if (memcmp(data, c->protocol->esd_key, ESD_KEY_LEN) != 0) {
343 pa_log("kicked client with invalid authorization key.");
344 return -1;
345 }
346
347 c->authorized = 1;
348 if (c->auth_timeout_event) {
349 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
350 c->auth_timeout_event = NULL;
351 }
352 }
353
354 data = (const char*)data + ESD_KEY_LEN;
355
356 memcpy(&ekey, data, sizeof(uint32_t));
357 if (ekey == ESD_ENDIAN_KEY)
358 c->swap_byte_order = 0;
359 else if (ekey == ESD_SWAP_ENDIAN_KEY)
360 c->swap_byte_order = 1;
361 else {
362 pa_log_warn("Client sent invalid endian key");
363 return -1;
364 }
365
366 ok = 1;
367 connection_write(c, &ok, sizeof(int));
368 return 0;
369 }
370
371 static int esd_proto_stream_play(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
372 char name[ESD_NAME_MAX], *utf8_name;
373 int32_t format, rate;
374 pa_sample_spec ss;
375 size_t l;
376 pa_sink *sink = NULL;
377 pa_sink_input_new_data sdata;
378
379 connection_assert_ref(c);
380 pa_assert(data);
381 pa_assert(length == (sizeof(int32_t)*2+ESD_NAME_MAX));
382
383 memcpy(&format, data, sizeof(int32_t));
384 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
385 data = (const char*) data + sizeof(int32_t);
386
387 memcpy(&rate, data, sizeof(int32_t));
388 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
389 data = (const char*) data + sizeof(int32_t);
390
391 ss.rate = rate;
392 format_esd2native(format, c->swap_byte_order, &ss);
393
394 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification");
395
396 if (c->protocol->sink_name) {
397 sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1);
398 CHECK_VALIDITY(sink, "No such sink: %s", c->protocol->sink_name);
399 }
400
401 strncpy(name, data, sizeof(name));
402 name[sizeof(name)-1] = 0;
403
404 utf8_name = pa_utf8_filter(name);
405 pa_client_set_name(c->client, utf8_name);
406 pa_xfree(utf8_name);
407
408 c->original_name = pa_xstrdup(name);
409
410 pa_assert(!c->sink_input && !c->input_memblockq);
411
412 pa_sink_input_new_data_init(&sdata);
413 sdata.sink = sink;
414 sdata.driver = __FILE__;
415 sdata.name = c->client->name;
416 pa_sink_input_new_data_set_sample_spec(&sdata, &ss);
417 sdata.module = c->protocol->module;
418 sdata.client = c->client;
419
420 c->sink_input = pa_sink_input_new(c->protocol->core, &sdata, 0);
421 CHECK_VALIDITY(c->sink_input, "Failed to create sink input.");
422
423 l = (size_t) (pa_bytes_per_second(&ss)*PLAYBACK_BUFFER_SECONDS);
424 c->input_memblockq = pa_memblockq_new(
425 0,
426 l,
427 0,
428 pa_frame_size(&ss),
429 (size_t) -1,
430 l/PLAYBACK_BUFFER_FRAGMENTS,
431 NULL);
432 pa_iochannel_socket_set_rcvbuf(c->io, l/PLAYBACK_BUFFER_FRAGMENTS*2);
433 c->playback.fragment_size = l/PLAYBACK_BUFFER_FRAGMENTS;
434
435 c->sink_input->parent.process_msg = sink_input_process_msg;
436 c->sink_input->peek = sink_input_peek_cb;
437 c->sink_input->drop = sink_input_drop_cb;
438 c->sink_input->kill = sink_input_kill_cb;
439 c->sink_input->userdata = c;
440
441 c->state = ESD_STREAMING_DATA;
442
443 c->protocol->n_player++;
444
445 pa_atomic_store(&c->playback.missing, pa_memblockq_missing(c->input_memblockq));
446
447 pa_sink_input_put(c->sink_input);
448
449 return 0;
450 }
451
452 static int esd_proto_stream_record(connection *c, esd_proto_t request, const void *data, size_t length) {
453 char name[ESD_NAME_MAX], *utf8_name;
454 int32_t format, rate;
455 pa_source *source = NULL;
456 pa_sample_spec ss;
457 size_t l;
458 pa_source_output_new_data sdata;
459
460 connection_assert_ref(c);
461 pa_assert(data);
462 pa_assert(length == (sizeof(int32_t)*2+ESD_NAME_MAX));
463
464 memcpy(&format, data, sizeof(int32_t));
465 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
466 data = (const char*) data + sizeof(int32_t);
467
468 memcpy(&rate, data, sizeof(int32_t));
469 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
470 data = (const char*) data + sizeof(int32_t);
471
472 ss.rate = rate;
473 format_esd2native(format, c->swap_byte_order, &ss);
474
475 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
476
477 if (request == ESD_PROTO_STREAM_MON) {
478 pa_sink* sink;
479
480 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
481 pa_log("no such sink.");
482 return -1;
483 }
484
485 if (!(source = sink->monitor_source)) {
486 pa_log("no such monitor source.");
487 return -1;
488 }
489 } else {
490 pa_assert(request == ESD_PROTO_STREAM_REC);
491
492 if (c->protocol->source_name) {
493 if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, 1))) {
494 pa_log("no such source.");
495 return -1;
496 }
497 }
498 }
499
500 strncpy(name, data, sizeof(name));
501 name[sizeof(name)-1] = 0;
502
503 utf8_name = pa_utf8_filter(name);
504 pa_client_set_name(c->client, utf8_name);
505 pa_xfree(utf8_name);
506
507 c->original_name = pa_xstrdup(name);
508
509 pa_assert(!c->output_memblockq && !c->source_output);
510
511 pa_source_output_new_data_init(&sdata);
512 sdata.source = source;
513 sdata.driver = __FILE__;
514 sdata.name = c->client->name;
515 pa_source_output_new_data_set_sample_spec(&sdata, &ss);
516 sdata.module = c->protocol->module;
517 sdata.client = c->client;
518
519 c->source_output = pa_source_output_new(c->protocol->core, &sdata, 9);
520 CHECK_VALIDITY(c->source_output, "Failed to create source_output.");
521
522 l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS);
523 c->output_memblockq = pa_memblockq_new(
524 0,
525 l,
526 0,
527 pa_frame_size(&ss),
528 1,
529 0,
530 NULL);
531 pa_iochannel_socket_set_sndbuf(c->io, l/RECORD_BUFFER_FRAGMENTS*2);
532
533 c->source_output->push = source_output_push_cb;
534 c->source_output->kill = source_output_kill_cb;
535 c->source_output->get_latency = source_output_get_latency_cb;
536 c->source_output->userdata = c;
537
538 c->state = ESD_STREAMING_DATA;
539
540 c->protocol->n_player++;
541
542 pa_source_output_put(c->source_output);
543
544 return 0;
545 }
546
547 static int esd_proto_get_latency(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
548 pa_sink *sink;
549 int32_t latency;
550
551 connection_ref(c);
552 pa_assert(!data);
553 pa_assert(length == 0);
554
555 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
556 latency = 0;
557 else {
558 double usec = pa_sink_get_latency(sink);
559 latency = (int) ((usec*44100)/1000000);
560 }
561
562 latency = MAYBE_INT32_SWAP(c->swap_byte_order, latency);
563 connection_write(c, &latency, sizeof(int32_t));
564 return 0;
565 }
566
567 static int esd_proto_server_info(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
568 int32_t rate = 44100, format = ESD_STEREO|ESD_BITS16;
569 int32_t response;
570 pa_sink *sink;
571
572 connection_ref(c);
573 pa_assert(data);
574 pa_assert(length == sizeof(int32_t));
575
576 if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
577 rate = sink->sample_spec.rate;
578 format = format_native2esd(&sink->sample_spec);
579 }
580
581 connection_write_prepare(c, sizeof(int32_t) * 3);
582
583 response = 0;
584 connection_write(c, &response, sizeof(int32_t));
585 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
586 connection_write(c, &rate, sizeof(int32_t));
587 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
588 connection_write(c, &format, sizeof(int32_t));
589
590 return 0;
591 }
592
593 static int esd_proto_all_info(connection *c, esd_proto_t request, const void *data, size_t length) {
594 size_t t, k, s;
595 connection *conn;
596 uint32_t idx = PA_IDXSET_INVALID;
597 unsigned nsamples;
598 char terminator[sizeof(int32_t)*6+ESD_NAME_MAX];
599
600 connection_ref(c);
601 pa_assert(data);
602 pa_assert(length == sizeof(int32_t));
603
604 if (esd_proto_server_info(c, request, data, length) < 0)
605 return -1;
606
607 k = sizeof(int32_t)*5+ESD_NAME_MAX;
608 s = sizeof(int32_t)*6+ESD_NAME_MAX;
609 nsamples = c->protocol->core->scache ? pa_idxset_size(c->protocol->core->scache) : 0;
610 t = s*(nsamples+1) + k*(c->protocol->n_player+1);
611
612 connection_write_prepare(c, t);
613
614 memset(terminator, 0, sizeof(terminator));
615
616 for (conn = pa_idxset_first(c->protocol->connections, &idx); conn; conn = pa_idxset_next(c->protocol->connections, &idx)) {
617 int32_t id, format = ESD_BITS16 | ESD_STEREO, rate = 44100, lvolume = ESD_VOLUME_BASE, rvolume = ESD_VOLUME_BASE;
618 char name[ESD_NAME_MAX];
619
620 if (conn->state != ESD_STREAMING_DATA)
621 continue;
622
623 pa_assert(t >= k*2+s);
624
625 if (conn->sink_input) {
626 pa_cvolume volume = *pa_sink_input_get_volume(conn->sink_input);
627 rate = conn->sink_input->sample_spec.rate;
628 lvolume = (volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
629 rvolume = (volume.values[1]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
630 format = format_native2esd(&conn->sink_input->sample_spec);
631 }
632
633 /* id */
634 id = MAYBE_INT32_SWAP(c->swap_byte_order, (int32_t) (conn->index+1));
635 connection_write(c, &id, sizeof(int32_t));
636
637 /* name */
638 memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
639 if (conn->original_name)
640 strncpy(name, conn->original_name, ESD_NAME_MAX);
641 else if (conn->client && conn->client->name)
642 strncpy(name, conn->client->name, ESD_NAME_MAX);
643 connection_write(c, name, ESD_NAME_MAX);
644
645 /* rate */
646 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
647 connection_write(c, &rate, sizeof(int32_t));
648
649 /* left */
650 lvolume = MAYBE_INT32_SWAP(c->swap_byte_order, lvolume);
651 connection_write(c, &lvolume, sizeof(int32_t));
652
653 /*right*/
654 rvolume = MAYBE_INT32_SWAP(c->swap_byte_order, rvolume);
655 connection_write(c, &rvolume, sizeof(int32_t));
656
657 /*format*/
658 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
659 connection_write(c, &format, sizeof(int32_t));
660
661 t -= k;
662 }
663
664 pa_assert(t == s*(nsamples+1)+k);
665 t -= k;
666
667 connection_write(c, terminator, k);
668
669 if (nsamples) {
670 pa_scache_entry *ce;
671
672 idx = PA_IDXSET_INVALID;
673 for (ce = pa_idxset_first(c->protocol->core->scache, &idx); ce; ce = pa_idxset_next(c->protocol->core->scache, &idx)) {
674 int32_t id, rate, lvolume, rvolume, format, len;
675 char name[ESD_NAME_MAX];
676
677 pa_assert(t >= s*2);
678
679 /* id */
680 id = MAYBE_INT32_SWAP(c->swap_byte_order, (int) (ce->index+1));
681 connection_write(c, &id, sizeof(int32_t));
682
683 /* name */
684 memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
685 if (strncmp(ce->name, SCACHE_PREFIX, sizeof(SCACHE_PREFIX)-1) == 0)
686 strncpy(name, ce->name+sizeof(SCACHE_PREFIX)-1, ESD_NAME_MAX);
687 else
688 pa_snprintf(name, ESD_NAME_MAX, "native.%s", ce->name);
689 connection_write(c, name, ESD_NAME_MAX);
690
691 /* rate */
692 rate = MAYBE_UINT32_SWAP(c->swap_byte_order, ce->sample_spec.rate);
693 connection_write(c, &rate, sizeof(int32_t));
694
695 /* left */
696 lvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
697 connection_write(c, &lvolume, sizeof(int32_t));
698
699 /*right*/
700 rvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
701 connection_write(c, &rvolume, sizeof(int32_t));
702
703 /*format*/
704 format = MAYBE_INT32_SWAP(c->swap_byte_order, format_native2esd(&ce->sample_spec));
705 connection_write(c, &format, sizeof(int32_t));
706
707 /*length*/
708 len = MAYBE_INT32_SWAP(c->swap_byte_order, (int) ce->memchunk.length);
709 connection_write(c, &len, sizeof(int32_t));
710
711 t -= s;
712 }
713 }
714
715 pa_assert(t == s);
716
717 connection_write(c, terminator, s);
718
719 return 0;
720 }
721
722 static int esd_proto_stream_pan(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
723 int32_t ok;
724 uint32_t idx, lvolume, rvolume;
725 connection *conn;
726
727 connection_assert_ref(c);
728 pa_assert(data);
729 pa_assert(length == sizeof(int32_t)*3);
730
731 memcpy(&idx, data, sizeof(uint32_t));
732 idx = MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
733 data = (const char*)data + sizeof(uint32_t);
734
735 memcpy(&lvolume, data, sizeof(uint32_t));
736 lvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, lvolume);
737 data = (const char*)data + sizeof(uint32_t);
738
739 memcpy(&rvolume, data, sizeof(uint32_t));
740 rvolume = MAYBE_UINT32_SWAP(c->swap_byte_order, rvolume);
741 data = (const char*)data + sizeof(uint32_t);
742
743 if ((conn = pa_idxset_get_by_index(c->protocol->connections, idx)) && conn->sink_input) {
744 pa_cvolume volume;
745 volume.values[0] = (lvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
746 volume.values[1] = (rvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
747 volume.channels = 2;
748 pa_sink_input_set_volume(conn->sink_input, &volume);
749 ok = 1;
750 } else
751 ok = 0;
752
753 connection_write(c, &ok, sizeof(int32_t));
754
755 return 0;
756 }
757
758 static int esd_proto_sample_cache(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
759 pa_sample_spec ss;
760 int32_t format, rate, sc_length;
761 uint32_t idx;
762 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
763
764 connection_assert_ref(c);
765 pa_assert(data);
766 pa_assert(length == (ESD_NAME_MAX+3*sizeof(int32_t)));
767
768 memcpy(&format, data, sizeof(int32_t));
769 format = MAYBE_INT32_SWAP(c->swap_byte_order, format);
770 data = (const char*)data + sizeof(int32_t);
771
772 memcpy(&rate, data, sizeof(int32_t));
773 rate = MAYBE_INT32_SWAP(c->swap_byte_order, rate);
774 data = (const char*)data + sizeof(int32_t);
775
776 ss.rate = rate;
777 format_esd2native(format, c->swap_byte_order, &ss);
778
779 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
780
781 memcpy(&sc_length, data, sizeof(int32_t));
782 sc_length = MAYBE_INT32_SWAP(c->swap_byte_order, sc_length);
783 data = (const char*)data + sizeof(int32_t);
784
785 CHECK_VALIDITY(sc_length <= MAX_CACHE_SAMPLE_SIZE, "Sample too large (%d bytes).", (int)sc_length);
786
787 strcpy(name, SCACHE_PREFIX);
788 strncpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
789 name[sizeof(name)-1] = 0;
790
791 CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
792
793 pa_assert(!c->scache.memchunk.memblock);
794 c->scache.memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, sc_length);
795 c->scache.memchunk.index = 0;
796 c->scache.memchunk.length = sc_length;
797 c->scache.sample_spec = ss;
798 pa_assert(!c->scache.name);
799 c->scache.name = pa_xstrdup(name);
800
801 c->state = ESD_CACHING_SAMPLE;
802
803 pa_scache_add_item(c->protocol->core, c->scache.name, NULL, NULL, NULL, &idx);
804
805 idx += 1;
806 connection_write(c, &idx, sizeof(uint32_t));
807
808 return 0;
809 }
810
811 static int esd_proto_sample_get_id(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
812 int32_t ok;
813 uint32_t idx;
814 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
815
816 connection_assert_ref(c);
817 pa_assert(data);
818 pa_assert(length == ESD_NAME_MAX);
819
820 strcpy(name, SCACHE_PREFIX);
821 strncpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
822 name[sizeof(name)-1] = 0;
823
824 CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
825
826 ok = -1;
827 if ((idx = pa_scache_get_id_by_name(c->protocol->core, name)) != PA_IDXSET_INVALID)
828 ok = idx + 1;
829
830 connection_write(c, &ok, sizeof(int32_t));
831
832 return 0;
833 }
834
835 static int esd_proto_sample_free_or_play(connection *c, esd_proto_t request, const void *data, size_t length) {
836 int32_t ok;
837 const char *name;
838 uint32_t idx;
839
840 connection_assert_ref(c);
841 pa_assert(data);
842 pa_assert(length == sizeof(int32_t));
843
844 memcpy(&idx, data, sizeof(uint32_t));
845 idx = MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
846
847 ok = 0;
848
849 if ((name = pa_scache_get_name_by_id(c->protocol->core, idx))) {
850 if (request == ESD_PROTO_SAMPLE_PLAY) {
851 pa_sink *sink;
852
853 if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
854 if (pa_scache_play_item(c->protocol->core, name, sink, PA_VOLUME_NORM) >= 0)
855 ok = idx + 1;
856 } else {
857 pa_assert(request == ESD_PROTO_SAMPLE_FREE);
858
859 if (pa_scache_remove_item(c->protocol->core, name) >= 0)
860 ok = idx + 1;
861 }
862 }
863
864 connection_write(c, &ok, sizeof(int32_t));
865
866 return 0;
867 }
868
869 static int esd_proto_standby_or_resume(connection *c, PA_GCC_UNUSED esd_proto_t request, PA_GCC_UNUSED const void *data, PA_GCC_UNUSED size_t length) {
870 int32_t ok;
871
872 connection_assert_ref(c);
873
874 connection_write_prepare(c, sizeof(int32_t) * 2);
875
876 ok = 1;
877 connection_write(c, &ok, sizeof(int32_t));
878 connection_write(c, &ok, sizeof(int32_t));
879
880 return 0;
881 }
882
883 /*** client callbacks ***/
884
885 static void client_kill_cb(pa_client *c) {
886 pa_assert(c);
887
888 connection_unlink(CONNECTION(c->userdata));
889 }
890
891 /*** pa_iochannel callbacks ***/
892
893 static int do_read(connection *c) {
894 connection_assert_ref(c);
895
896 /* pa_log("READ"); */
897
898 if (c->state == ESD_NEXT_REQUEST) {
899 ssize_t r;
900 pa_assert(c->read_data_length < sizeof(c->request));
901
902 if ((r = pa_iochannel_read(c->io, ((uint8_t*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
903 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
904 return -1;
905 }
906
907 if ((c->read_data_length+= r) >= sizeof(c->request)) {
908 struct proto_handler *handler;
909
910 c->request = MAYBE_INT32_SWAP(c->swap_byte_order, c->request);
911
912 if (c->request < ESD_PROTO_CONNECT || c->request > ESD_PROTO_MAX) {
913 pa_log("recieved invalid request.");
914 return -1;
915 }
916
917 handler = proto_map+c->request;
918
919 /* pa_log("executing request #%u", c->request); */
920
921 if (!handler->proc) {
922 pa_log("recieved unimplemented request #%u.", c->request);
923 return -1;
924 }
925
926 if (handler->data_length == 0) {
927 c->read_data_length = 0;
928
929 if (handler->proc(c, c->request, NULL, 0) < 0)
930 return -1;
931
932 } else {
933 if (c->read_data_alloc < handler->data_length)
934 c->read_data = pa_xrealloc(c->read_data, c->read_data_alloc = handler->data_length);
935 pa_assert(c->read_data);
936
937 c->state = ESD_NEEDS_REQDATA;
938 c->read_data_length = 0;
939 }
940 }
941
942 } else if (c->state == ESD_NEEDS_REQDATA) {
943 ssize_t r;
944 struct proto_handler *handler = proto_map+c->request;
945
946 pa_assert(handler->proc);
947
948 pa_assert(c->read_data && c->read_data_length < handler->data_length);
949
950 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
951 if (errno == EINTR || errno == EAGAIN)
952 return 0;
953
954 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
955 return -1;
956 }
957
958 if ((c->read_data_length += r) >= handler->data_length) {
959 size_t l = c->read_data_length;
960 pa_assert(handler->proc);
961
962 c->state = ESD_NEXT_REQUEST;
963 c->read_data_length = 0;
964
965 if (handler->proc(c, c->request, c->read_data, l) < 0)
966 return -1;
967 }
968 } else if (c->state == ESD_CACHING_SAMPLE) {
969 ssize_t r;
970 void *p;
971
972 pa_assert(c->scache.memchunk.memblock);
973 pa_assert(c->scache.name);
974 pa_assert(c->scache.memchunk.index < c->scache.memchunk.length);
975
976 p = pa_memblock_acquire(c->scache.memchunk.memblock);
977 r = pa_iochannel_read(c->io, (uint8_t*) p+c->scache.memchunk.index, c->scache.memchunk.length-c->scache.memchunk.index);
978 pa_memblock_release(c->scache.memchunk.memblock);
979
980 if (r <= 0) {
981 if (errno == EINTR || errno == EAGAIN)
982 return 0;
983
984 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
985 return -1;
986 }
987
988 c->scache.memchunk.index += r;
989 pa_assert(c->scache.memchunk.index <= c->scache.memchunk.length);
990
991 if (c->scache.memchunk.index == c->scache.memchunk.length) {
992 uint32_t idx;
993
994 c->scache.memchunk.index = 0;
995 pa_scache_add_item(c->protocol->core, c->scache.name, &c->scache.sample_spec, NULL, &c->scache.memchunk, &idx);
996
997 pa_memblock_unref(c->scache.memchunk.memblock);
998 c->scache.memchunk.memblock = NULL;
999 c->scache.memchunk.index = c->scache.memchunk.length = 0;
1000
1001 pa_xfree(c->scache.name);
1002 c->scache.name = NULL;
1003
1004 c->state = ESD_NEXT_REQUEST;
1005
1006 idx += 1;
1007 connection_write(c, &idx, sizeof(uint32_t));
1008 }
1009
1010 } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
1011 pa_memchunk chunk;
1012 ssize_t r;
1013 size_t l;
1014 void *p;
1015
1016 pa_assert(c->input_memblockq);
1017
1018 /* pa_log("STREAMING_DATA"); */
1019
1020 if (!(l = pa_atomic_load(&c->playback.missing)))
1021 return 0;
1022
1023 if (l > c->playback.fragment_size)
1024 l = c->playback.fragment_size;
1025
1026 if (c->playback.current_memblock)
1027 if (pa_memblock_get_length(c->playback.current_memblock) - c->playback.memblock_index < l) {
1028 pa_memblock_unref(c->playback.current_memblock);
1029 c->playback.current_memblock = NULL;
1030 c->playback.memblock_index = 0;
1031 }
1032
1033 if (!c->playback.current_memblock) {
1034 pa_assert_se(c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, c->playback.fragment_size*2));
1035 c->playback.memblock_index = 0;
1036 }
1037
1038 p = pa_memblock_acquire(c->playback.current_memblock);
1039 r = pa_iochannel_read(c->io, (uint8_t*) p+c->playback.memblock_index, l);
1040 pa_memblock_release(c->playback.current_memblock);
1041
1042 if (r <= 0) {
1043
1044 if (errno == EINTR || errno == EAGAIN)
1045 return 0;
1046
1047 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
1048 return -1;
1049 }
1050
1051 chunk.memblock = c->playback.current_memblock;
1052 chunk.index = c->playback.memblock_index;
1053 chunk.length = r;
1054
1055 c->playback.memblock_index += r;
1056
1057 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, &chunk, NULL);
1058 pa_atomic_sub(&c->playback.missing, r);
1059 }
1060
1061 return 0;
1062 }
1063
1064 static int do_write(connection *c) {
1065 connection_assert_ref(c);
1066
1067 /* pa_log("WRITE"); */
1068
1069 if (c->write_data_length) {
1070 ssize_t r;
1071
1072 pa_assert(c->write_data_index < c->write_data_length);
1073 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) {
1074
1075 if (errno == EINTR || errno == EAGAIN)
1076 return 0;
1077
1078 pa_log("write(): %s", pa_cstrerror(errno));
1079 return -1;
1080 }
1081
1082 if ((c->write_data_index +=r) >= c->write_data_length)
1083 c->write_data_length = c->write_data_index = 0;
1084
1085 } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
1086 pa_memchunk chunk;
1087 ssize_t r;
1088 void *p;
1089
1090 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
1091 return 0;
1092
1093 pa_assert(chunk.memblock);
1094 pa_assert(chunk.length);
1095
1096 p = pa_memblock_acquire(chunk.memblock);
1097 r = pa_iochannel_write(c->io, (uint8_t*) p+chunk.index, chunk.length);
1098 pa_memblock_release(chunk.memblock);
1099
1100 pa_memblock_unref(chunk.memblock);
1101
1102 if (r < 0) {
1103
1104 if (errno == EINTR || errno == EAGAIN)
1105 return 0;
1106
1107 pa_log("write(): %s", pa_cstrerror(errno));
1108 return -1;
1109 }
1110
1111 pa_memblockq_drop(c->output_memblockq, r);
1112 }
1113
1114 return 0;
1115 }
1116
1117 static void do_work(connection *c) {
1118 connection_assert_ref(c);
1119
1120 c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
1121
1122 if (c->dead)
1123 return;
1124
1125 if (pa_iochannel_is_readable(c->io)) {
1126 if (do_read(c) < 0)
1127 goto fail;
1128 }
1129
1130 if (c->state == ESD_STREAMING_DATA && c->source_output && pa_iochannel_is_hungup(c->io))
1131 /* In case we are in capture mode we will never call read()
1132 * on the socket, hence we need to detect the hangup manually
1133 * here, instead of simply waiting for read() to return 0. */
1134 goto fail;
1135
1136 if (pa_iochannel_is_writable(c->io))
1137 if (do_write(c) < 0)
1138 goto fail;
1139
1140 return;
1141
1142 fail:
1143
1144 if (c->state == ESD_STREAMING_DATA && c->sink_input) {
1145 c->dead = 1;
1146
1147 pa_iochannel_free(c->io);
1148 c->io = NULL;
1149
1150 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_DISABLE_PREBUF, NULL, 0, NULL, NULL);
1151 } else
1152 connection_unlink(c);
1153 }
1154
1155 static void io_callback(pa_iochannel*io, void *userdata) {
1156 connection *c = CONNECTION(userdata);
1157
1158 connection_assert_ref(c);
1159 pa_assert(io);
1160
1161 do_work(c);
1162 }
1163
1164 static void defer_callback(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
1165 connection *c = CONNECTION(userdata);
1166
1167 connection_assert_ref(c);
1168 pa_assert(e);
1169
1170 do_work(c);
1171 }
1172
1173 static int connection_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
1174 connection *c = CONNECTION(o);
1175 connection_assert_ref(c);
1176
1177 switch (code) {
1178 case CONNECTION_MESSAGE_REQUEST_DATA:
1179 do_work(c);
1180 break;
1181
1182 case CONNECTION_MESSAGE_POST_DATA:
1183 /* pa_log("got data %u", chunk->length); */
1184 pa_memblockq_push_align(c->output_memblockq, chunk);
1185 do_work(c);
1186 break;
1187
1188 case CONNECTION_MESSAGE_UNLINK_CONNECTION:
1189 connection_unlink(c);
1190 break;
1191 }
1192
1193 return 0;
1194 }
1195
1196 /*** sink_input callbacks ***/
1197
1198 /* Called from thread context */
1199 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
1200 pa_sink_input *i = PA_SINK_INPUT(o);
1201 connection*c;
1202
1203 pa_sink_input_assert_ref(i);
1204 c = CONNECTION(i->userdata);
1205 connection_assert_ref(c);
1206
1207 switch (code) {
1208
1209 case SINK_INPUT_MESSAGE_POST_DATA: {
1210 pa_assert(chunk);
1211
1212 /* New data from the main loop */
1213 pa_memblockq_push_align(c->input_memblockq, chunk);
1214
1215 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
1216
1217 return 0;
1218 }
1219
1220 case SINK_INPUT_MESSAGE_DISABLE_PREBUF: {
1221 pa_memblockq_prebuf_disable(c->input_memblockq);
1222 return 0;
1223 }
1224
1225 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
1226 pa_usec_t *r = userdata;
1227
1228 *r = pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
1229
1230 /* Fall through, the default handler will add in the extra
1231 * latency added by the resampler */
1232 }
1233
1234 default:
1235 return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
1236 }
1237 }
1238
1239 /* Called from thread context */
1240 static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
1241 connection*c;
1242 int r;
1243
1244 pa_assert(i);
1245 c = CONNECTION(i->userdata);
1246 connection_assert_ref(c);
1247 pa_assert(chunk);
1248
1249 if ((r = pa_memblockq_peek(c->input_memblockq, chunk)) < 0 && c->dead)
1250 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_UNLINK_CONNECTION, NULL, 0, NULL, NULL);
1251
1252 return r;
1253 }
1254
1255 /* Called from thread context */
1256 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
1257 connection*c;
1258 size_t old, new;
1259
1260 pa_assert(i);
1261 c = CONNECTION(i->userdata);
1262 connection_assert_ref(c);
1263 pa_assert(length);
1264
1265 /* pa_log("DROP"); */
1266
1267 old = pa_memblockq_missing(c->input_memblockq);
1268 pa_memblockq_drop(c->input_memblockq, length);
1269 new = pa_memblockq_missing(c->input_memblockq);
1270
1271 if (new > old) {
1272 if (pa_atomic_add(&c->playback.missing, new - old) <= 0)
1273 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
1274 }
1275 }
1276
1277 static void sink_input_kill_cb(pa_sink_input *i) {
1278 pa_sink_input_assert_ref(i);
1279
1280 connection_unlink(CONNECTION(i->userdata));
1281 }
1282
1283 /*** source_output callbacks ***/
1284
1285 /* Called from thread context */
1286 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
1287 connection *c;
1288
1289 pa_assert(o);
1290 c = CONNECTION(o->userdata);
1291 pa_assert(c);
1292 pa_assert(chunk);
1293
1294 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
1295 }
1296
1297 static void source_output_kill_cb(pa_source_output *o) {
1298 pa_source_output_assert_ref(o);
1299
1300 connection_unlink(CONNECTION(o->userdata));
1301 }
1302
1303 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
1304 connection*c;
1305
1306 pa_assert(o);
1307 c = CONNECTION(o->userdata);
1308 pa_assert(c);
1309
1310 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
1311 }
1312
1313 /*** socket server callback ***/
1314
1315 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
1316 connection *c = CONNECTION(userdata);
1317
1318 pa_assert(m);
1319 pa_assert(tv);
1320 connection_assert_ref(c);
1321 pa_assert(c->auth_timeout_event == e);
1322
1323 if (!c->authorized)
1324 connection_unlink(c);
1325 }
1326
1327 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
1328 connection *c;
1329 pa_protocol_esound *p = userdata;
1330 char cname[256], pname[128];
1331
1332 pa_assert(s);
1333 pa_assert(io);
1334 pa_assert(p);
1335
1336 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
1337 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
1338 pa_iochannel_free(io);
1339 return;
1340 }
1341
1342 c = pa_msgobject_new(connection);
1343 c->parent.parent.free = connection_free;
1344 c->parent.process_msg = connection_process_msg;
1345 c->protocol = p;
1346 c->io = io;
1347 pa_iochannel_set_callback(c->io, io_callback, c);
1348
1349 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
1350 pa_snprintf(cname, sizeof(cname), "EsounD client (%s)", pname);
1351 c->client = pa_client_new(p->core, __FILE__, cname);
1352 c->client->owner = p->module;
1353 c->client->kill = client_kill_cb;
1354 c->client->userdata = c;
1355
1356 c->authorized = !!p->public;
1357 c->swap_byte_order = 0;
1358 c->dead = 0;
1359
1360 c->read_data_length = 0;
1361 c->read_data = pa_xmalloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
1362
1363 c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
1364 c->write_data = NULL;
1365
1366 c->state = ESD_NEEDS_REQDATA;
1367 c->request = ESD_PROTO_CONNECT;
1368
1369 c->sink_input = NULL;
1370 c->input_memblockq = NULL;
1371
1372 c->source_output = NULL;
1373 c->output_memblockq = NULL;
1374
1375 c->playback.current_memblock = NULL;
1376 c->playback.memblock_index = 0;
1377 c->playback.fragment_size = 0;
1378 pa_atomic_store(&c->playback.missing, 0);
1379
1380 c->scache.memchunk.length = c->scache.memchunk.index = 0;
1381 c->scache.memchunk.memblock = NULL;
1382 c->scache.name = NULL;
1383
1384 c->original_name = NULL;
1385
1386 if (!c->authorized && p->auth_ip_acl && pa_ip_acl_check(p->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
1387 pa_log_info("Client authenticated by IP ACL.");
1388 c->authorized = 1;
1389 }
1390
1391 if (!c->authorized) {
1392 struct timeval tv;
1393 pa_gettimeofday(&tv);
1394 tv.tv_sec += AUTH_TIMEOUT;
1395 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
1396 } else
1397 c->auth_timeout_event = NULL;
1398
1399 c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
1400 p->core->mainloop->defer_enable(c->defer_event, 0);
1401
1402 pa_idxset_put(p->connections, c, &c->index);
1403 }
1404
1405 /*** entry points ***/
1406
1407 pa_protocol_esound* pa_protocol_esound_new(pa_core*core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
1408 pa_protocol_esound *p = NULL;
1409 int public = 0;
1410 const char *acl;
1411
1412 pa_assert(core);
1413 pa_assert(server);
1414 pa_assert(m);
1415 pa_assert(ma);
1416
1417 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
1418 pa_log("auth-anonymous= expects a boolean argument.");
1419 goto fail;
1420 }
1421
1422 p = pa_xnew(pa_protocol_esound, 1);
1423
1424 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", DEFAULT_COOKIE_FILE), p->esd_key, sizeof(p->esd_key)) < 0)
1425 goto fail;
1426
1427 if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
1428
1429 if (!(p->auth_ip_acl = pa_ip_acl_new(acl))) {
1430 pa_log("Failed to parse IP ACL '%s'", acl);
1431 goto fail;
1432 }
1433 } else
1434 p->auth_ip_acl = NULL;
1435
1436 p->core = core;
1437 p->module = m;
1438 p->public = public;
1439 p->server = server;
1440 pa_socket_server_set_callback(p->server, on_connection, p);
1441 p->connections = pa_idxset_new(NULL, NULL);
1442
1443 p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
1444 p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
1445 p->n_player = 0;
1446
1447 return p;
1448
1449 fail:
1450 pa_xfree(p);
1451 return NULL;
1452 }
1453
1454 void pa_protocol_esound_free(pa_protocol_esound *p) {
1455 connection *c;
1456 pa_assert(p);
1457
1458 while ((c = pa_idxset_first(p->connections, NULL)))
1459 connection_unlink(c);
1460 pa_idxset_free(p->connections, NULL, NULL);
1461
1462 pa_socket_server_unref(p->server);
1463
1464 if (p->auth_ip_acl)
1465 pa_ip_acl_free(p->auth_ip_acl);
1466
1467 pa_xfree(p->sink_name);
1468 pa_xfree(p->source_name);
1469
1470 pa_xfree(p);
1471 }