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