4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include <pulse/sample.h>
36 #include <pulse/timeval.h>
37 #include <pulse/utf8.h>
38 #include <pulse/xmalloc.h>
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>
58 #include "endianmacros.h"
60 #include "protocol-esound.h"
62 /* Don't accept more connection than this */
63 #define MAX_CONNECTIONS 64
65 /* Kick a client if it doesn't authenticate within this time */
66 #define AUTH_TIMEOUT 5
68 #define DEFAULT_COOKIE_FILE ".esd_auth"
70 #define PLAYBACK_BUFFER_SECONDS (.25)
71 #define PLAYBACK_BUFFER_FRAGMENTS (10)
72 #define RECORD_BUFFER_SECONDS (5)
74 #define MAX_CACHE_SAMPLE_SIZE (2048000)
76 #define DEFAULT_SINK_LATENCY (150*PA_USEC_PER_MSEC)
77 #define DEFAULT_SOURCE_LATENCY (150*PA_USEC_PER_MSEC)
79 #define SCACHE_PREFIX "esound."
81 /* This is heavily based on esound's code */
83 typedef struct connection {
88 pa_protocol_esound *protocol;
91 pa_bool_t authorized, swap_byte_order;
93 size_t write_data_alloc, write_data_index, write_data_length;
95 size_t read_data_alloc, read_data_length;
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;
106 pa_memblock *current_memblock;
107 size_t memblock_index;
113 pa_memchunk memchunk;
115 pa_sample_spec sample_spec;
118 pa_time_event *auth_timeout_event;
121 PA_DECLARE_CLASS(connection);
122 #define CONNECTION(o) (connection_cast(o))
123 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
125 struct pa_protocol_esound {
129 pa_socket_server *server;
130 pa_idxset *connections;
132 char *sink_name, *source_name;
134 uint8_t esd_key[ESD_KEY_LEN];
135 pa_ip_acl *auth_ip_acl;
139 SINK_INPUT_MESSAGE_POST_DATA = PA_SINK_INPUT_MESSAGE_MAX, /* data from main loop to sink input */
140 SINK_INPUT_MESSAGE_DISABLE_PREBUF
144 CONNECTION_MESSAGE_REQUEST_DATA,
145 CONNECTION_MESSAGE_POST_DATA,
146 CONNECTION_MESSAGE_UNLINK_CONNECTION
149 typedef struct proto_handler {
151 int (*proc)(connection *c, esd_proto_t request, const void *data, size_t length);
152 const char *description;
153 } esd_proto_handler_info_t;
155 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk);
156 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes);
157 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes);
158 static void sink_input_kill_cb(pa_sink_input *i);
159 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk);
160 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
162 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
163 static void source_output_kill_cb(pa_source_output *o);
165 static int esd_proto_connect(connection *c, esd_proto_t request, const void *data, size_t length);
166 static int esd_proto_stream_play(connection *c, esd_proto_t request, const void *data, size_t length);
167 static int esd_proto_stream_record(connection *c, esd_proto_t request, const void *data, size_t length);
168 static int esd_proto_get_latency(connection *c, esd_proto_t request, const void *data, size_t length);
169 static int esd_proto_server_info(connection *c, esd_proto_t request, const void *data, size_t length);
170 static int esd_proto_all_info(connection *c, esd_proto_t request, const void *data, size_t length);
171 static int esd_proto_stream_pan(connection *c, esd_proto_t request, const void *data, size_t length);
172 static int esd_proto_sample_cache(connection *c, esd_proto_t request, const void *data, size_t length);
173 static int esd_proto_sample_free_or_play(connection *c, esd_proto_t request, const void *data, size_t length);
174 static int esd_proto_sample_get_id(connection *c, esd_proto_t request, const void *data, size_t length);
175 static int esd_proto_standby_or_resume(connection *c, esd_proto_t request, const void *data, size_t length);
177 /* the big map of protocol handler info */
178 static struct proto_handler proto_map[ESD_PROTO_MAX] = {
179 { ESD_KEY_LEN + sizeof(int), esd_proto_connect, "connect" },
180 { ESD_KEY_LEN + sizeof(int), NULL, "lock" },
181 { ESD_KEY_LEN + sizeof(int), NULL, "unlock" },
183 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_play, "stream play" },
184 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream rec" },
185 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream mon" },
187 { ESD_NAME_MAX + 3 * sizeof(int), esd_proto_sample_cache, "sample cache" }, /* 6 */
188 { sizeof(int), esd_proto_sample_free_or_play, "sample free" },
189 { sizeof(int), esd_proto_sample_free_or_play, "sample play" }, /* 8 */
190 { sizeof(int), NULL, "sample loop" },
191 { sizeof(int), NULL, "sample stop" },
192 { -1, NULL, "TODO: sample kill" },
194 { ESD_KEY_LEN + sizeof(int), esd_proto_standby_or_resume, "standby" }, /* NOOP! */
195 { ESD_KEY_LEN + sizeof(int), esd_proto_standby_or_resume, "resume" }, /* NOOP! */ /* 13 */
197 { ESD_NAME_MAX, esd_proto_sample_get_id, "sample getid" }, /* 14 */
198 { ESD_NAME_MAX + 2 * sizeof(int), NULL, "stream filter" },
200 { sizeof(int), esd_proto_server_info, "server info" },
201 { sizeof(int), esd_proto_all_info, "all info" },
202 { -1, NULL, "TODO: subscribe" },
203 { -1, NULL, "TODO: unsubscribe" },
205 { 3 * sizeof(int), esd_proto_stream_pan, "stream pan"},
206 { 3 * sizeof(int), NULL, "sample pan" },
208 { sizeof(int), NULL, "standby mode" },
209 { 0, esd_proto_get_latency, "get latency" }
212 static void connection_unlink(connection *c) {
219 pa_sink_input_unlink(c->sink_input);
220 pa_sink_input_unref(c->sink_input);
221 c->sink_input = NULL;
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;
231 pa_client_free(c->client);
235 if (c->state == ESD_STREAMING_DATA)
236 c->protocol->n_player--;
239 pa_iochannel_free(c->io);
243 if (c->defer_event) {
244 c->protocol->core->mainloop->defer_free(c->defer_event);
245 c->defer_event = NULL;
248 if (c->auth_timeout_event) {
249 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
250 c->auth_timeout_event = NULL;
253 pa_assert_se(pa_idxset_remove_by_data(c->protocol->connections, c, NULL) == c);
258 static void connection_free(pa_object *obj) {
259 connection *c = CONNECTION(obj);
262 if (c->input_memblockq)
263 pa_memblockq_free(c->input_memblockq);
264 if (c->output_memblockq)
265 pa_memblockq_free(c->output_memblockq);
267 if (c->playback.current_memblock)
268 pa_memblock_unref(c->playback.current_memblock);
270 pa_xfree(c->read_data);
271 pa_xfree(c->write_data);
273 if (c->scache.memchunk.memblock)
274 pa_memblock_unref(c->scache.memchunk.memblock);
275 pa_xfree(c->scache.name);
277 pa_xfree(c->original_name);
281 static void connection_write_prepare(connection *c, size_t length) {
285 t = c->write_data_length+length;
287 if (c->write_data_alloc < t)
288 c->write_data = pa_xrealloc(c->write_data, c->write_data_alloc = t);
290 pa_assert(c->write_data);
293 static void connection_write(connection *c, const void *data, size_t length) {
297 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
299 connection_write_prepare(c, length);
301 pa_assert(c->write_data);
303 i = c->write_data_length;
304 c->write_data_length += length;
306 memcpy((uint8_t*) c->write_data + i, data, length);
309 static void format_esd2native(int format, pa_bool_t swap_bytes, pa_sample_spec *ss) {
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;
316 ss->format = PA_SAMPLE_U8;
319 static int format_native2esd(pa_sample_spec *ss) {
322 format = (ss->format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
323 format |= (ss->channels >= 2) ? ESD_STEREO : ESD_MONO;
328 #define CHECK_VALIDITY(expression, ...) do { \
329 if (!(expression)) { \
330 pa_log_warn(__FILE__ ": " __VA_ARGS__); \
335 /*** esound commands ***/
337 static int esd_proto_connect(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
341 connection_assert_ref(c);
343 pa_assert(length == (ESD_KEY_LEN + sizeof(uint32_t)));
345 if (!c->authorized) {
346 if (memcmp(data, c->protocol->esd_key, ESD_KEY_LEN) != 0) {
347 pa_log("kicked client with invalid authorization key.");
351 c->authorized = TRUE;
352 if (c->auth_timeout_event) {
353 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
354 c->auth_timeout_event = NULL;
358 data = (const char*)data + ESD_KEY_LEN;
360 memcpy(&ekey, data, sizeof(uint32_t));
361 if (ekey == ESD_ENDIAN_KEY)
362 c->swap_byte_order = FALSE;
363 else if (ekey == ESD_SWAP_ENDIAN_KEY)
364 c->swap_byte_order = TRUE;
366 pa_log_warn("Client sent invalid endian key");
371 connection_write(c, &ok, sizeof(int));
375 static int esd_proto_stream_play(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
376 char name[ESD_NAME_MAX], *utf8_name;
377 int32_t format, rate;
380 pa_sink *sink = NULL;
381 pa_sink_input_new_data sdata;
383 connection_assert_ref(c);
385 pa_assert(length == (sizeof(int32_t)*2+ESD_NAME_MAX));
387 memcpy(&format, data, sizeof(int32_t));
388 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
389 data = (const char*) data + sizeof(int32_t);
391 memcpy(&rate, data, sizeof(int32_t));
392 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
393 data = (const char*) data + sizeof(int32_t);
396 format_esd2native(format, c->swap_byte_order, &ss);
398 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification");
400 if (c->protocol->sink_name) {
401 sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1);
402 CHECK_VALIDITY(sink, "No such sink: %s", c->protocol->sink_name);
405 pa_strlcpy(name, data, sizeof(name));
407 utf8_name = pa_utf8_filter(name);
408 pa_client_set_name(c->client, utf8_name);
411 c->original_name = pa_xstrdup(name);
413 pa_assert(!c->sink_input && !c->input_memblockq);
415 pa_sink_input_new_data_init(&sdata);
416 sdata.driver = __FILE__;
417 sdata.module = c->protocol->module;
418 sdata.client = c->client;
420 pa_proplist_update(sdata.proplist, PA_UPDATE_MERGE, c->client->proplist);
421 pa_sink_input_new_data_set_sample_spec(&sdata, &ss);
423 c->sink_input = pa_sink_input_new(c->protocol->core, &sdata, 0);
424 pa_sink_input_new_data_done(&sdata);
426 CHECK_VALIDITY(c->sink_input, "Failed to create sink input.");
428 l = (size_t) (pa_bytes_per_second(&ss)*PLAYBACK_BUFFER_SECONDS);
429 c->input_memblockq = pa_memblockq_new(
435 l/PLAYBACK_BUFFER_FRAGMENTS,
438 pa_iochannel_socket_set_rcvbuf(c->io, l);
440 c->sink_input->parent.process_msg = sink_input_process_msg;
441 c->sink_input->pop = sink_input_pop_cb;
442 c->sink_input->process_rewind = sink_input_process_rewind_cb;
443 c->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
444 c->sink_input->kill = sink_input_kill_cb;
445 c->sink_input->userdata = c;
447 pa_sink_input_set_requested_latency(c->sink_input, DEFAULT_SINK_LATENCY);
449 c->state = ESD_STREAMING_DATA;
451 c->protocol->n_player++;
453 pa_atomic_store(&c->playback.missing, pa_memblockq_missing(c->input_memblockq));
455 pa_sink_input_put(c->sink_input);
460 static int esd_proto_stream_record(connection *c, esd_proto_t request, const void *data, size_t length) {
461 char name[ESD_NAME_MAX], *utf8_name;
462 int32_t format, rate;
463 pa_source *source = NULL;
466 pa_source_output_new_data sdata;
468 connection_assert_ref(c);
470 pa_assert(length == (sizeof(int32_t)*2+ESD_NAME_MAX));
472 memcpy(&format, data, sizeof(int32_t));
473 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
474 data = (const char*) data + sizeof(int32_t);
476 memcpy(&rate, data, sizeof(int32_t));
477 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
478 data = (const char*) data + sizeof(int32_t);
481 format_esd2native(format, c->swap_byte_order, &ss);
483 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
485 if (request == ESD_PROTO_STREAM_MON) {
488 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
489 pa_log("no such sink.");
493 if (!(source = sink->monitor_source)) {
494 pa_log("no such monitor source.");
498 pa_assert(request == ESD_PROTO_STREAM_REC);
500 if (c->protocol->source_name) {
501 if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, 1))) {
502 pa_log("no such source.");
508 pa_strlcpy(name, data, sizeof(name));
510 utf8_name = pa_utf8_filter(name);
511 pa_client_set_name(c->client, utf8_name);
514 c->original_name = pa_xstrdup(name);
516 pa_assert(!c->output_memblockq && !c->source_output);
518 pa_source_output_new_data_init(&sdata);
519 sdata.driver = __FILE__;
520 sdata.module = c->protocol->module;
521 sdata.client = c->client;
522 sdata.source = source;
523 pa_proplist_update(sdata.proplist, PA_UPDATE_MERGE, c->client->proplist);
524 pa_source_output_new_data_set_sample_spec(&sdata, &ss);
526 c->source_output = pa_source_output_new(c->protocol->core, &sdata, 0);
527 pa_source_output_new_data_done(&sdata);
529 CHECK_VALIDITY(c->source_output, "Failed to create source output.");
531 l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS);
532 c->output_memblockq = pa_memblockq_new(
541 pa_iochannel_socket_set_sndbuf(c->io, l);
543 c->source_output->push = source_output_push_cb;
544 c->source_output->kill = source_output_kill_cb;
545 c->source_output->get_latency = source_output_get_latency_cb;
546 c->source_output->userdata = c;
548 pa_source_output_set_requested_latency(c->source_output, DEFAULT_SOURCE_LATENCY);
550 c->state = ESD_STREAMING_DATA;
552 c->protocol->n_player++;
554 pa_source_output_put(c->source_output);
559 static int esd_proto_get_latency(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
565 pa_assert(length == 0);
567 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
570 double usec = pa_sink_get_latency(sink);
571 latency = (int) ((usec*44100)/1000000);
574 latency = PA_MAYBE_INT32_SWAP(c->swap_byte_order, latency);
575 connection_write(c, &latency, sizeof(int32_t));
579 static int esd_proto_server_info(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
580 int32_t rate = 44100, format = ESD_STEREO|ESD_BITS16;
586 pa_assert(length == sizeof(int32_t));
588 if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) {
589 rate = sink->sample_spec.rate;
590 format = format_native2esd(&sink->sample_spec);
593 connection_write_prepare(c, sizeof(int32_t) * 3);
596 connection_write(c, &response, sizeof(int32_t));
597 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
598 connection_write(c, &rate, sizeof(int32_t));
599 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
600 connection_write(c, &format, sizeof(int32_t));
605 static int esd_proto_all_info(connection *c, esd_proto_t request, const void *data, size_t length) {
608 uint32_t idx = PA_IDXSET_INVALID;
610 char terminator[sizeof(int32_t)*6+ESD_NAME_MAX];
614 pa_assert(length == sizeof(int32_t));
616 if (esd_proto_server_info(c, request, data, length) < 0)
619 k = sizeof(int32_t)*5+ESD_NAME_MAX;
620 s = sizeof(int32_t)*6+ESD_NAME_MAX;
621 nsamples = c->protocol->core->scache ? pa_idxset_size(c->protocol->core->scache) : 0;
622 t = s*(nsamples+1) + k*(c->protocol->n_player+1);
624 connection_write_prepare(c, t);
626 memset(terminator, 0, sizeof(terminator));
628 for (conn = pa_idxset_first(c->protocol->connections, &idx); conn; conn = pa_idxset_next(c->protocol->connections, &idx)) {
629 int32_t id, format = ESD_BITS16 | ESD_STEREO, rate = 44100, lvolume = ESD_VOLUME_BASE, rvolume = ESD_VOLUME_BASE;
630 char name[ESD_NAME_MAX];
632 if (conn->state != ESD_STREAMING_DATA)
635 pa_assert(t >= k*2+s);
637 if (conn->sink_input) {
638 pa_cvolume volume = *pa_sink_input_get_volume(conn->sink_input);
639 rate = conn->sink_input->sample_spec.rate;
640 lvolume = (volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
641 rvolume = (volume.values[1]*ESD_VOLUME_BASE)/PA_VOLUME_NORM;
642 format = format_native2esd(&conn->sink_input->sample_spec);
646 id = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int32_t) (conn->index+1));
647 connection_write(c, &id, sizeof(int32_t));
650 memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
651 if (conn->original_name)
652 strncpy(name, conn->original_name, ESD_NAME_MAX);
653 else if (conn->client && pa_proplist_gets(conn->client->proplist, PA_PROP_APPLICATION_NAME))
654 strncpy(name, pa_proplist_gets(conn->client->proplist, PA_PROP_APPLICATION_NAME), ESD_NAME_MAX);
655 connection_write(c, name, ESD_NAME_MAX);
658 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
659 connection_write(c, &rate, sizeof(int32_t));
662 lvolume = PA_MAYBE_INT32_SWAP(c->swap_byte_order, lvolume);
663 connection_write(c, &lvolume, sizeof(int32_t));
666 rvolume = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rvolume);
667 connection_write(c, &rvolume, sizeof(int32_t));
670 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
671 connection_write(c, &format, sizeof(int32_t));
676 pa_assert(t == s*(nsamples+1)+k);
679 connection_write(c, terminator, k);
684 idx = PA_IDXSET_INVALID;
685 for (ce = pa_idxset_first(c->protocol->core->scache, &idx); ce; ce = pa_idxset_next(c->protocol->core->scache, &idx)) {
686 int32_t id, rate, lvolume, rvolume, format, len;
687 char name[ESD_NAME_MAX];
692 id = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int) (ce->index+1));
693 connection_write(c, &id, sizeof(int32_t));
696 memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
697 if (strncmp(ce->name, SCACHE_PREFIX, sizeof(SCACHE_PREFIX)-1) == 0)
698 strncpy(name, ce->name+sizeof(SCACHE_PREFIX)-1, ESD_NAME_MAX);
700 pa_snprintf(name, ESD_NAME_MAX, "native.%s", ce->name);
701 connection_write(c, name, ESD_NAME_MAX);
704 rate = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, ce->sample_spec.rate);
705 connection_write(c, &rate, sizeof(int32_t));
708 lvolume = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
709 connection_write(c, &lvolume, sizeof(int32_t));
712 rvolume = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, (ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
713 connection_write(c, &rvolume, sizeof(int32_t));
716 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format_native2esd(&ce->sample_spec));
717 connection_write(c, &format, sizeof(int32_t));
720 len = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int) ce->memchunk.length);
721 connection_write(c, &len, sizeof(int32_t));
729 connection_write(c, terminator, s);
734 static int esd_proto_stream_pan(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
736 uint32_t idx, lvolume, rvolume;
739 connection_assert_ref(c);
741 pa_assert(length == sizeof(int32_t)*3);
743 memcpy(&idx, data, sizeof(uint32_t));
744 idx = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
745 data = (const char*)data + sizeof(uint32_t);
747 memcpy(&lvolume, data, sizeof(uint32_t));
748 lvolume = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, lvolume);
749 data = (const char*)data + sizeof(uint32_t);
751 memcpy(&rvolume, data, sizeof(uint32_t));
752 rvolume = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, rvolume);
753 data = (const char*)data + sizeof(uint32_t);
755 if ((conn = pa_idxset_get_by_index(c->protocol->connections, idx)) && conn->sink_input) {
757 volume.values[0] = (lvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
758 volume.values[1] = (rvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
760 pa_sink_input_set_volume(conn->sink_input, &volume);
765 connection_write(c, &ok, sizeof(int32_t));
770 static int esd_proto_sample_cache(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
772 int32_t format, rate, sc_length;
774 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
776 connection_assert_ref(c);
778 pa_assert(length == (ESD_NAME_MAX+3*sizeof(int32_t)));
780 memcpy(&format, data, sizeof(int32_t));
781 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
782 data = (const char*)data + sizeof(int32_t);
784 memcpy(&rate, data, sizeof(int32_t));
785 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
786 data = (const char*)data + sizeof(int32_t);
789 format_esd2native(format, c->swap_byte_order, &ss);
791 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
793 memcpy(&sc_length, data, sizeof(int32_t));
794 sc_length = PA_MAYBE_INT32_SWAP(c->swap_byte_order, sc_length);
795 data = (const char*)data + sizeof(int32_t);
797 CHECK_VALIDITY(sc_length <= MAX_CACHE_SAMPLE_SIZE, "Sample too large (%d bytes).", (int)sc_length);
799 strcpy(name, SCACHE_PREFIX);
800 pa_strlcpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
802 CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
804 pa_assert(!c->scache.memchunk.memblock);
805 c->scache.memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, sc_length);
806 c->scache.memchunk.index = 0;
807 c->scache.memchunk.length = sc_length;
808 c->scache.sample_spec = ss;
809 pa_assert(!c->scache.name);
810 c->scache.name = pa_xstrdup(name);
812 c->state = ESD_CACHING_SAMPLE;
814 pa_scache_add_item(c->protocol->core, c->scache.name, NULL, NULL, NULL, c->client->proplist, &idx);
817 connection_write(c, &idx, sizeof(uint32_t));
822 static int esd_proto_sample_get_id(connection *c, PA_GCC_UNUSED esd_proto_t request, const void *data, size_t length) {
825 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
827 connection_assert_ref(c);
829 pa_assert(length == ESD_NAME_MAX);
831 strcpy(name, SCACHE_PREFIX);
832 pa_strlcpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
834 CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
837 if ((idx = pa_scache_get_id_by_name(c->protocol->core, name)) != PA_IDXSET_INVALID)
840 connection_write(c, &ok, sizeof(int32_t));
845 static int esd_proto_sample_free_or_play(connection *c, esd_proto_t request, const void *data, size_t length) {
850 connection_assert_ref(c);
852 pa_assert(length == sizeof(int32_t));
854 memcpy(&idx, data, sizeof(uint32_t));
855 idx = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
859 if ((name = pa_scache_get_name_by_id(c->protocol->core, idx))) {
860 if (request == ESD_PROTO_SAMPLE_PLAY) {
863 if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1)))
864 if (pa_scache_play_item(c->protocol->core, name, sink, PA_VOLUME_NORM, c->client->proplist, NULL) >= 0)
867 pa_assert(request == ESD_PROTO_SAMPLE_FREE);
869 if (pa_scache_remove_item(c->protocol->core, name) >= 0)
874 connection_write(c, &ok, sizeof(int32_t));
879 static int esd_proto_standby_or_resume(connection *c, PA_GCC_UNUSED esd_proto_t request, PA_GCC_UNUSED const void *data, PA_GCC_UNUSED size_t length) {
882 connection_assert_ref(c);
884 connection_write_prepare(c, sizeof(int32_t) * 2);
887 connection_write(c, &ok, sizeof(int32_t));
888 connection_write(c, &ok, sizeof(int32_t));
893 /*** client callbacks ***/
895 static void client_kill_cb(pa_client *c) {
898 connection_unlink(CONNECTION(c->userdata));
901 /*** pa_iochannel callbacks ***/
903 static int do_read(connection *c) {
904 connection_assert_ref(c);
906 /* pa_log("READ"); */
908 if (c->state == ESD_NEXT_REQUEST) {
910 pa_assert(c->read_data_length < sizeof(c->request));
912 if ((r = pa_iochannel_read(c->io, ((uint8_t*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
913 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
917 if ((c->read_data_length+= r) >= sizeof(c->request)) {
918 struct proto_handler *handler;
920 c->request = PA_MAYBE_INT32_SWAP(c->swap_byte_order, c->request);
922 if (c->request < ESD_PROTO_CONNECT || c->request > ESD_PROTO_MAX) {
923 pa_log("recieved invalid request.");
927 handler = proto_map+c->request;
929 /* pa_log("executing request #%u", c->request); */
931 if (!handler->proc) {
932 pa_log("recieved unimplemented request #%u.", c->request);
936 if (handler->data_length == 0) {
937 c->read_data_length = 0;
939 if (handler->proc(c, c->request, NULL, 0) < 0)
943 if (c->read_data_alloc < handler->data_length)
944 c->read_data = pa_xrealloc(c->read_data, c->read_data_alloc = handler->data_length);
945 pa_assert(c->read_data);
947 c->state = ESD_NEEDS_REQDATA;
948 c->read_data_length = 0;
952 } else if (c->state == ESD_NEEDS_REQDATA) {
954 struct proto_handler *handler = proto_map+c->request;
956 pa_assert(handler->proc);
958 pa_assert(c->read_data && c->read_data_length < handler->data_length);
960 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
961 if (r < 0 && (errno == EINTR || errno == EAGAIN))
964 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
968 if ((c->read_data_length += r) >= handler->data_length) {
969 size_t l = c->read_data_length;
970 pa_assert(handler->proc);
972 c->state = ESD_NEXT_REQUEST;
973 c->read_data_length = 0;
975 if (handler->proc(c, c->request, c->read_data, l) < 0)
978 } else if (c->state == ESD_CACHING_SAMPLE) {
982 pa_assert(c->scache.memchunk.memblock);
983 pa_assert(c->scache.name);
984 pa_assert(c->scache.memchunk.index < c->scache.memchunk.length);
986 p = pa_memblock_acquire(c->scache.memchunk.memblock);
987 r = pa_iochannel_read(c->io, (uint8_t*) p+c->scache.memchunk.index, c->scache.memchunk.length-c->scache.memchunk.index);
988 pa_memblock_release(c->scache.memchunk.memblock);
991 if (r < 0 && (errno == EINTR || errno == EAGAIN))
994 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
998 c->scache.memchunk.index += r;
999 pa_assert(c->scache.memchunk.index <= c->scache.memchunk.length);
1001 if (c->scache.memchunk.index == c->scache.memchunk.length) {
1004 c->scache.memchunk.index = 0;
1005 pa_scache_add_item(c->protocol->core, c->scache.name, &c->scache.sample_spec, NULL, &c->scache.memchunk, c->client->proplist, &idx);
1007 pa_memblock_unref(c->scache.memchunk.memblock);
1008 c->scache.memchunk.memblock = NULL;
1009 c->scache.memchunk.index = c->scache.memchunk.length = 0;
1011 pa_xfree(c->scache.name);
1012 c->scache.name = NULL;
1014 c->state = ESD_NEXT_REQUEST;
1017 connection_write(c, &idx, sizeof(uint32_t));
1020 } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
1027 pa_assert(c->input_memblockq);
1029 /* pa_log("STREAMING_DATA"); */
1031 if (!(l = pa_atomic_load(&c->playback.missing)))
1034 if (c->playback.current_memblock) {
1036 space = pa_memblock_get_length(c->playback.current_memblock) - c->playback.memblock_index;
1039 pa_memblock_unref(c->playback.current_memblock);
1040 c->playback.current_memblock = NULL;
1044 if (!c->playback.current_memblock) {
1045 pa_assert_se(c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, 0));
1046 c->playback.memblock_index = 0;
1048 space = pa_memblock_get_length(c->playback.current_memblock);
1054 p = pa_memblock_acquire(c->playback.current_memblock);
1055 r = pa_iochannel_read(c->io, (uint8_t*) p+c->playback.memblock_index, l);
1056 pa_memblock_release(c->playback.current_memblock);
1060 if (r < 0 && (errno == EINTR || errno == EAGAIN))
1063 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
1067 chunk.memblock = c->playback.current_memblock;
1068 chunk.index = c->playback.memblock_index;
1071 c->playback.memblock_index += r;
1073 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, &chunk, NULL);
1074 pa_atomic_sub(&c->playback.missing, r);
1080 static int do_write(connection *c) {
1081 connection_assert_ref(c);
1083 /* pa_log("WRITE"); */
1085 if (c->write_data_length) {
1088 pa_assert(c->write_data_index < c->write_data_length);
1089 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) {
1091 if (r < 0 && (errno == EINTR || errno == EAGAIN))
1094 pa_log("write(): %s", pa_cstrerror(errno));
1098 if ((c->write_data_index +=r) >= c->write_data_length)
1099 c->write_data_length = c->write_data_index = 0;
1101 } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
1106 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
1109 pa_assert(chunk.memblock);
1110 pa_assert(chunk.length);
1112 p = pa_memblock_acquire(chunk.memblock);
1113 r = pa_iochannel_write(c->io, (uint8_t*) p+chunk.index, chunk.length);
1114 pa_memblock_release(chunk.memblock);
1116 pa_memblock_unref(chunk.memblock);
1120 if (r < 0 && (errno == EINTR || errno == EAGAIN))
1123 pa_log("write(): %s", pa_cstrerror(errno));
1127 pa_memblockq_drop(c->output_memblockq, r);
1133 static void do_work(connection *c) {
1134 connection_assert_ref(c);
1136 c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
1141 if (pa_iochannel_is_readable(c->io))
1145 if (c->state == ESD_STREAMING_DATA && !c->sink_input && pa_iochannel_is_hungup(c->io))
1146 /* In case we are in capture mode we will never call read()
1147 * on the socket, hence we need to detect the hangup manually
1148 * here, instead of simply waiting for read() to return 0. */
1151 if (pa_iochannel_is_writable(c->io))
1152 if (do_write(c) < 0)
1159 if (c->state == ESD_STREAMING_DATA && c->sink_input) {
1162 pa_iochannel_free(c->io);
1165 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_DISABLE_PREBUF, NULL, 0, NULL, NULL);
1167 connection_unlink(c);
1170 static void io_callback(pa_iochannel*io, void *userdata) {
1171 connection *c = CONNECTION(userdata);
1173 connection_assert_ref(c);
1179 static void defer_callback(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
1180 connection *c = CONNECTION(userdata);
1182 connection_assert_ref(c);
1188 static int connection_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
1189 connection *c = CONNECTION(o);
1190 connection_assert_ref(c);
1193 case CONNECTION_MESSAGE_REQUEST_DATA:
1197 case CONNECTION_MESSAGE_POST_DATA:
1198 /* pa_log("got data %u", chunk->length); */
1199 pa_memblockq_push_align(c->output_memblockq, chunk);
1203 case CONNECTION_MESSAGE_UNLINK_CONNECTION:
1204 connection_unlink(c);
1211 /*** sink_input callbacks ***/
1213 /* Called from thread context */
1214 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
1215 pa_sink_input *i = PA_SINK_INPUT(o);
1218 pa_sink_input_assert_ref(i);
1219 c = CONNECTION(i->userdata);
1220 connection_assert_ref(c);
1224 case SINK_INPUT_MESSAGE_POST_DATA: {
1227 /* New data from the main loop */
1228 pa_memblockq_push_align(c->input_memblockq, chunk);
1230 if (pa_memblockq_is_readable(c->input_memblockq) && c->playback.underrun) {
1231 pa_log_debug("Requesting rewind due to end of underrun.");
1232 pa_sink_input_request_rewind(c->sink_input, 0, FALSE, TRUE);
1235 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
1240 case SINK_INPUT_MESSAGE_DISABLE_PREBUF:
1241 pa_memblockq_prebuf_disable(c->input_memblockq);
1244 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
1245 pa_usec_t *r = userdata;
1247 *r = pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
1249 /* Fall through, the default handler will add in the extra
1250 * latency added by the resampler */
1254 return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
1258 /* Called from thread context */
1259 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
1262 pa_sink_input_assert_ref(i);
1263 c = CONNECTION(i->userdata);
1264 connection_assert_ref(c);
1267 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
1269 c->playback.underrun = TRUE;
1271 if (c->dead && pa_sink_input_safe_to_remove(i))
1272 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_UNLINK_CONNECTION, NULL, 0, NULL, NULL);
1278 c->playback.underrun = FALSE;
1280 pa_memblockq_drop(c->input_memblockq, chunk->length);
1281 m = pa_memblockq_pop_missing(c->input_memblockq);
1284 if (pa_atomic_add(&c->playback.missing, m) <= 0)
1285 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
1291 /* Called from thread context */
1292 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
1295 pa_sink_input_assert_ref(i);
1296 c = CONNECTION(i->userdata);
1297 connection_assert_ref(c);
1299 /* If we are in an underrun, then we don't rewind */
1300 if (i->thread_info.underrun_for > 0)
1303 pa_memblockq_rewind(c->input_memblockq, nbytes);
1306 /* Called from thread context */
1307 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
1310 pa_sink_input_assert_ref(i);
1311 c = CONNECTION(i->userdata);
1312 connection_assert_ref(c);
1314 pa_memblockq_set_maxrewind(c->input_memblockq, nbytes);
1317 static void sink_input_kill_cb(pa_sink_input *i) {
1318 pa_sink_input_assert_ref(i);
1320 connection_unlink(CONNECTION(i->userdata));
1323 /*** source_output callbacks ***/
1325 /* Called from thread context */
1326 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
1329 pa_source_output_assert_ref(o);
1330 c = CONNECTION(o->userdata);
1334 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
1337 static void source_output_kill_cb(pa_source_output *o) {
1338 pa_source_output_assert_ref(o);
1340 connection_unlink(CONNECTION(o->userdata));
1343 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
1346 pa_source_output_assert_ref(o);
1347 c = CONNECTION(o->userdata);
1350 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
1353 /*** socket server callback ***/
1355 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
1356 connection *c = CONNECTION(userdata);
1360 connection_assert_ref(c);
1361 pa_assert(c->auth_timeout_event == e);
1364 connection_unlink(c);
1367 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
1369 pa_protocol_esound *p = userdata;
1370 char cname[256], pname[128];
1376 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
1377 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
1378 pa_iochannel_free(io);
1382 c = pa_msgobject_new(connection);
1383 c->parent.parent.free = connection_free;
1384 c->parent.process_msg = connection_process_msg;
1387 pa_iochannel_set_callback(c->io, io_callback, c);
1389 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
1390 pa_snprintf(cname, sizeof(cname), "EsounD client (%s)", pname);
1391 c->client = pa_client_new(p->core, __FILE__, cname);
1392 pa_proplist_sets(c->client->proplist, "esound-protocol.peer", pname);
1393 c->client->module = p->module;
1394 c->client->kill = client_kill_cb;
1395 c->client->userdata = c;
1397 c->authorized = !!p->public;
1398 c->swap_byte_order = FALSE;
1401 c->read_data_length = 0;
1402 c->read_data = pa_xmalloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
1404 c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
1405 c->write_data = NULL;
1407 c->state = ESD_NEEDS_REQDATA;
1408 c->request = ESD_PROTO_CONNECT;
1410 c->sink_input = NULL;
1411 c->input_memblockq = NULL;
1413 c->source_output = NULL;
1414 c->output_memblockq = NULL;
1416 c->playback.current_memblock = NULL;
1417 c->playback.memblock_index = 0;
1418 c->playback.underrun = TRUE;
1419 pa_atomic_store(&c->playback.missing, 0);
1421 pa_memchunk_reset(&c->scache.memchunk);
1422 c->scache.name = NULL;
1424 c->original_name = NULL;
1426 if (!c->authorized && p->auth_ip_acl && pa_ip_acl_check(p->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
1427 pa_log_info("Client authenticated by IP ACL.");
1428 c->authorized = TRUE;
1431 if (!c->authorized) {
1433 pa_gettimeofday(&tv);
1434 tv.tv_sec += AUTH_TIMEOUT;
1435 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
1437 c->auth_timeout_event = NULL;
1439 c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
1440 p->core->mainloop->defer_enable(c->defer_event, 0);
1442 pa_idxset_put(p->connections, c, &c->index);
1445 /*** entry points ***/
1447 pa_protocol_esound* pa_protocol_esound_new(pa_core*core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
1448 pa_protocol_esound *p = NULL;
1449 pa_bool_t public = FALSE;
1457 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &public) < 0) {
1458 pa_log("auth-anonymous= expects a boolean argument.");
1462 p = pa_xnew(pa_protocol_esound, 1);
1464 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", DEFAULT_COOKIE_FILE), p->esd_key, sizeof(p->esd_key)) < 0)
1467 if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
1469 if (!(p->auth_ip_acl = pa_ip_acl_new(acl))) {
1470 pa_log("Failed to parse IP ACL '%s'", acl);
1474 p->auth_ip_acl = NULL;
1479 p->server = pa_socket_server_ref(server);
1480 pa_socket_server_set_callback(p->server, on_connection, p);
1481 p->connections = pa_idxset_new(NULL, NULL);
1483 p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
1484 p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
1494 void pa_protocol_esound_free(pa_protocol_esound *p) {
1498 while ((c = pa_idxset_first(p->connections, NULL)))
1499 connection_unlink(c);
1500 pa_idxset_free(p->connections, NULL, NULL);
1503 pa_socket_server_unref(p->server);
1506 pa_ip_acl_free(p->auth_ip_acl);
1508 pa_xfree(p->sink_name);
1509 pa_xfree(p->source_name);