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