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