4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/iochannel.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/socket-client.h>
46 #include <pulsecore/esound.h>
47 #include <pulsecore/authkey.h>
49 #include "module-esound-sink-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("ESOUND Sink")
53 PA_MODULE_VERSION(PACKAGE_VERSION)
54 PA_MODULE_USAGE("sink_name=<name for the sink> server=<address> cookie=<filename> format=<sample format> channels=<number of channels> rate=<sample rate>")
56 #define DEFAULT_SINK_NAME "esound_output"
63 pa_socket_client *client;
65 pa_defer_event *defer_event;
71 size_t write_length, write_index;
74 size_t read_length, read_index;
76 enum { STATE_AUTH, STATE_LATENCY, STATE_RUNNING, STATE_DEAD } state;
84 static const char* const valid_modargs[] = {
94 static void cancel(struct userdata *u) {
97 u->state = STATE_DEAD;
100 pa_iochannel_free(u->io);
104 if (u->defer_event) {
105 u->core->mainloop->defer_free(u->defer_event);
106 u->defer_event = NULL;
110 pa_sink_disconnect(u->sink);
111 pa_sink_unref(u->sink);
116 pa_module_unload_request(u->module);
121 static int do_write(struct userdata *u) {
125 if (!pa_iochannel_is_writable(u->io))
129 assert(u->write_index < u->write_length);
131 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) {
132 pa_log("write() failed: %s", pa_cstrerror(errno));
137 assert(u->write_index <= u->write_length);
139 if (u->write_index == u->write_length) {
141 u->write_data = NULL;
142 u->write_index = u->write_length = 0;
144 } else if (u->state == STATE_RUNNING) {
145 pa_module_set_used(u->module, pa_sink_used_by(u->sink));
147 if (!u->memchunk.length)
148 if (pa_sink_render(u->sink, 8192, &u->memchunk) < 0)
151 assert(u->memchunk.memblock && u->memchunk.length);
153 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length)) < 0) {
154 pa_log("write() failed: %s", pa_cstrerror(errno));
158 u->memchunk.index += r;
159 u->memchunk.length -= r;
161 if (u->memchunk.length <= 0) {
162 pa_memblock_unref(u->memchunk.memblock);
163 u->memchunk.memblock = NULL;
170 static int handle_response(struct userdata *u) {
175 assert(u->read_length == sizeof(int32_t));
177 /* Process auth data */
178 if (!*(int32_t*) u->read_data) {
179 pa_log("Authentication failed: %s", pa_cstrerror(errno));
183 /* Request latency data */
184 assert(!u->write_data);
185 *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
188 u->state = STATE_LATENCY;
190 /* Space for next response */
191 assert(u->read_length >= sizeof(int32_t));
193 u->read_length = sizeof(int32_t);
197 case STATE_LATENCY: {
199 assert(u->read_length == sizeof(int32_t));
201 /* Process latency info */
202 u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
203 if (u->latency > 10000000) {
204 pa_log("WARNING! Invalid latency information received from server");
209 assert(!u->write_data);
210 p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
211 *(p++) = ESD_PROTO_STREAM_PLAY;
214 pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
217 u->state = STATE_RUNNING;
219 /* Don't read any further */
220 pa_xfree(u->read_data);
222 u->read_index = u->read_length = 0;
234 static int do_read(struct userdata *u) {
237 if (!pa_iochannel_is_readable(u->io))
240 if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
246 assert(u->read_index < u->read_length);
248 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
249 pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
255 assert(u->read_index <= u->read_length);
257 if (u->read_index == u->read_length)
258 return handle_response(u);
264 static void do_work(struct userdata *u) {
267 u->core->mainloop->defer_enable(u->defer_event, 0);
269 if (do_read(u) < 0 || do_write(u) < 0)
273 static void notify_cb(pa_sink*s) {
274 struct userdata *u = s->userdata;
277 if (pa_iochannel_is_writable(u->io))
278 u->core->mainloop->defer_enable(u->defer_event, 1);
281 static pa_usec_t get_latency_cb(pa_sink *s) {
282 struct userdata *u = s->userdata;
287 (u->memchunk.memblock ? pa_bytes_to_usec(u->memchunk.length, &s->sample_spec) : 0);
290 static void defer_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_defer_event*e, void *userdata) {
291 struct userdata *u = userdata;
296 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
297 struct userdata *u = userdata;
302 static void on_connection(PA_GCC_UNUSED pa_socket_client *c, pa_iochannel*io, void *userdata) {
303 struct userdata *u = userdata;
305 pa_socket_client_unref(u->client);
309 pa_log("connection failed: %s", pa_cstrerror(errno));
315 pa_iochannel_set_callback(u->io, io_callback, u);
318 int pa__init(pa_core *c, pa_module*m) {
319 struct userdata *u = NULL;
322 pa_modargs *ma = NULL;
327 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
328 pa_log("failed to parse module arguments");
332 ss = c->default_sample_spec;
333 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
334 pa_log("invalid sample format specification");
338 if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
340 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
344 u = pa_xmalloc0(sizeof(struct userdata));
349 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
350 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);
355 u->read_data = u->write_data = NULL;
356 u->read_index = u->write_index = u->read_length = u->write_length = 0;
357 u->state = STATE_AUTH;
360 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) {
361 pa_log("failed to create sink.");
365 if (!(u->client = pa_socket_client_new_string(u->core->mainloop, p = pa_modargs_get_value(ma, "server", ESD_UNIX_SOCKET_NAME), ESD_DEFAULT_PORT))) {
366 pa_log("failed to connect to server.");
369 pa_socket_client_set_callback(u->client, on_connection, u);
371 /* Prepare the initial request */
372 u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
373 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) {
374 pa_log("failed to load cookie");
377 *(int32_t*) ((uint8_t*) u->write_data + ESD_KEY_LEN) = ESD_ENDIAN_KEY;
379 /* Reserve space for the response */
380 u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
382 u->sink->notify = notify_cb;
383 u->sink->get_latency = get_latency_cb;
384 u->sink->userdata = u;
385 pa_sink_set_owner(u->sink, m);
386 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Esound sink '%s'", p));
389 u->memchunk.memblock = NULL;
390 u->memchunk.length = 0;
392 u->defer_event = c->mainloop->defer_new(c->mainloop, defer_callback, u);
393 c->mainloop->defer_enable(u->defer_event, 0);
409 void pa__done(pa_core *c, pa_module*m) {
413 if (!(u = m->userdata))
419 if (u->memchunk.memblock)
420 pa_memblock_unref(u->memchunk.memblock);
423 pa_socket_client_unref(u->client);
425 pa_xfree(u->read_data);
426 pa_xfree(u->write_data);