]> code.delx.au - pulseaudio/blob - src/modules/module-esound-sink.c
26638d9dc31445f9d492c3a7a454d9dfca6f25de
[pulseaudio] / src / modules / module-esound-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/iochannel.h>
42 #include <pulsecore/sink.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/socket-client.h>
48 #include <pulsecore/esound.h>
49 #include <pulsecore/authkey.h>
50
51 #include "module-esound-sink-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering")
54 PA_MODULE_DESCRIPTION("ESOUND Sink")
55 PA_MODULE_VERSION(PACKAGE_VERSION)
56 PA_MODULE_USAGE("sink_name=<name for the sink> server=<address> cookie=<filename> format=<sample format> channels=<number of channels> rate=<sample rate>")
57
58 #define DEFAULT_SINK_NAME "esound_output"
59
60 struct userdata {
61 pa_core *core;
62
63 pa_sink *sink;
64 pa_iochannel *io;
65 pa_socket_client *client;
66
67 pa_defer_event *defer_event;
68
69 pa_memchunk memchunk;
70 pa_module *module;
71
72 void *write_data;
73 size_t write_length, write_index;
74
75 void *read_data;
76 size_t read_length, read_index;
77
78 enum { STATE_AUTH, STATE_LATENCY, STATE_RUNNING, STATE_DEAD } state;
79
80 pa_usec_t latency;
81
82 esd_format_t format;
83 int32_t rate;
84 };
85
86 static const char* const valid_modargs[] = {
87 "server",
88 "cookie",
89 "rate",
90 "format",
91 "channels",
92 "sink_name",
93 NULL
94 };
95
96 static void cancel(struct userdata *u) {
97 assert(u);
98
99 u->state = STATE_DEAD;
100
101 if (u->io) {
102 pa_iochannel_free(u->io);
103 u->io = NULL;
104 }
105
106 if (u->defer_event) {
107 u->core->mainloop->defer_free(u->defer_event);
108 u->defer_event = NULL;
109 }
110
111 if (u->sink) {
112 pa_sink_disconnect(u->sink);
113 pa_sink_unref(u->sink);
114 u->sink = NULL;
115 }
116
117 if (u->module) {
118 pa_module_unload_request(u->module);
119 u->module = NULL;
120 }
121 }
122
123 static int do_write(struct userdata *u) {
124 ssize_t r;
125 assert(u);
126
127 if (!pa_iochannel_is_writable(u->io))
128 return 0;
129
130 if (u->write_data) {
131 assert(u->write_index < u->write_length);
132
133 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) {
134 pa_log("write() failed: %s", pa_cstrerror(errno));
135 return -1;
136 }
137
138 u->write_index += r;
139 assert(u->write_index <= u->write_length);
140
141 if (u->write_index == u->write_length) {
142 free(u->write_data);
143 u->write_data = NULL;
144 u->write_index = u->write_length = 0;
145 }
146 } else if (u->state == STATE_RUNNING) {
147 pa_module_set_used(u->module, pa_sink_used_by(u->sink));
148
149 if (!u->memchunk.length)
150 if (pa_sink_render(u->sink, 8192, &u->memchunk) < 0)
151 return 0;
152
153 assert(u->memchunk.memblock && u->memchunk.length);
154
155 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length)) < 0) {
156 pa_log("write() failed: %s", pa_cstrerror(errno));
157 return -1;
158 }
159
160 u->memchunk.index += r;
161 u->memchunk.length -= r;
162
163 if (u->memchunk.length <= 0) {
164 pa_memblock_unref(u->memchunk.memblock);
165 u->memchunk.memblock = NULL;
166 }
167 }
168
169 return 0;
170 }
171
172 static int handle_response(struct userdata *u) {
173 assert(u);
174
175 switch (u->state) {
176 case STATE_AUTH:
177 assert(u->read_length == sizeof(int32_t));
178
179 /* Process auth data */
180 if (!*(int32_t*) u->read_data) {
181 pa_log("Authentication failed: %s", pa_cstrerror(errno));
182 return -1;
183 }
184
185 /* Request latency data */
186 assert(!u->write_data);
187 *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
188
189 u->write_index = 0;
190 u->state = STATE_LATENCY;
191
192 /* Space for next response */
193 assert(u->read_length >= sizeof(int32_t));
194 u->read_index = 0;
195 u->read_length = sizeof(int32_t);
196
197 break;
198
199 case STATE_LATENCY: {
200 int32_t *p;
201 assert(u->read_length == sizeof(int32_t));
202
203 /* Process latency info */
204 u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
205 if (u->latency > 10000000) {
206 pa_log("WARNING! Invalid latency information received from server");
207 u->latency = 0;
208 }
209
210 /* Create stream */
211 assert(!u->write_data);
212 p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
213 *(p++) = ESD_PROTO_STREAM_PLAY;
214 *(p++) = u->format;
215 *(p++) = u->rate;
216 pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
217
218 u->write_index = 0;
219 u->state = STATE_RUNNING;
220
221 /* Don't read any further */
222 pa_xfree(u->read_data);
223 u->read_data = NULL;
224 u->read_index = u->read_length = 0;
225
226 break;
227 }
228
229 default:
230 abort();
231 }
232
233 return 0;
234 }
235
236 static int do_read(struct userdata *u) {
237 assert(u);
238
239 if (!pa_iochannel_is_readable(u->io))
240 return 0;
241
242 if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
243 ssize_t r;
244
245 if (!u->read_data)
246 return 0;
247
248 assert(u->read_index < u->read_length);
249
250 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
251 pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
252 cancel(u);
253 return -1;
254 }
255
256 u->read_index += r;
257 assert(u->read_index <= u->read_length);
258
259 if (u->read_index == u->read_length)
260 return handle_response(u);
261 }
262
263 return 0;
264 }
265
266 static void do_work(struct userdata *u) {
267 assert(u);
268
269 u->core->mainloop->defer_enable(u->defer_event, 0);
270
271 if (do_read(u) < 0 || do_write(u) < 0)
272 cancel(u);
273 }
274
275 static void notify_cb(pa_sink*s) {
276 struct userdata *u = s->userdata;
277 assert(s && u);
278
279 if (pa_iochannel_is_writable(u->io))
280 u->core->mainloop->defer_enable(u->defer_event, 1);
281 }
282
283 static pa_usec_t get_latency_cb(pa_sink *s) {
284 struct userdata *u = s->userdata;
285 assert(s && u);
286
287 return
288 u->latency +
289 (u->memchunk.memblock ? pa_bytes_to_usec(u->memchunk.length, &s->sample_spec) : 0);
290 }
291
292 static void defer_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_defer_event*e, void *userdata) {
293 struct userdata *u = userdata;
294 assert(u);
295 do_work(u);
296 }
297
298 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
299 struct userdata *u = userdata;
300 assert(u);
301 do_work(u);
302 }
303
304 static void on_connection(PA_GCC_UNUSED pa_socket_client *c, pa_iochannel*io, void *userdata) {
305 struct userdata *u = userdata;
306
307 pa_socket_client_unref(u->client);
308 u->client = NULL;
309
310 if (!io) {
311 pa_log("connection failed: %s", pa_cstrerror(errno));
312 cancel(u);
313 return;
314 }
315
316 u->io = io;
317 pa_iochannel_set_callback(u->io, io_callback, u);
318 }
319
320 int pa__init(pa_core *c, pa_module*m) {
321 struct userdata *u = NULL;
322 const char *p;
323 pa_sample_spec ss;
324 pa_modargs *ma = NULL;
325 char *t;
326
327 assert(c && m);
328
329 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
330 pa_log("failed to parse module arguments");
331 goto fail;
332 }
333
334 ss = c->default_sample_spec;
335 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
336 pa_log("invalid sample format specification");
337 goto fail;
338 }
339
340 if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
341 (ss.channels > 2)) {
342 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
343 goto fail;
344 }
345
346 u = pa_xmalloc0(sizeof(struct userdata));
347 u->core = c;
348 u->module = m;
349 m->userdata = u;
350 u->format =
351 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
352 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);
353 u->rate = ss.rate;
354 u->sink = NULL;
355 u->client = NULL;
356 u->io = NULL;
357 u->read_data = u->write_data = NULL;
358 u->read_index = u->write_index = u->read_length = u->write_length = 0;
359 u->state = STATE_AUTH;
360 u->latency = 0;
361
362 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) {
363 pa_log("failed to create sink.");
364 goto fail;
365 }
366
367 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))) {
368 pa_log("failed to connect to server.");
369 goto fail;
370 }
371 pa_socket_client_set_callback(u->client, on_connection, u);
372
373 /* Prepare the initial request */
374 u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
375 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) {
376 pa_log("failed to load cookie");
377 goto fail;
378 }
379 *(int32_t*) ((uint8_t*) u->write_data + ESD_KEY_LEN) = ESD_ENDIAN_KEY;
380
381 /* Reserve space for the response */
382 u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
383
384 u->sink->notify = notify_cb;
385 u->sink->get_latency = get_latency_cb;
386 u->sink->userdata = u;
387 pa_sink_set_owner(u->sink, m);
388 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Esound sink '%s'", p));
389 pa_xfree(t);
390
391 u->memchunk.memblock = NULL;
392 u->memchunk.length = 0;
393
394 u->defer_event = c->mainloop->defer_new(c->mainloop, defer_callback, u);
395 c->mainloop->defer_enable(u->defer_event, 0);
396
397
398 pa_modargs_free(ma);
399
400 return 0;
401
402 fail:
403 if (ma)
404 pa_modargs_free(ma);
405
406 pa__done(c, m);
407
408 return -1;
409 }
410
411 void pa__done(pa_core *c, pa_module*m) {
412 struct userdata *u;
413 assert(c && m);
414
415 if (!(u = m->userdata))
416 return;
417
418 u->module = NULL;
419 cancel(u);
420
421 if (u->memchunk.memblock)
422 pa_memblock_unref(u->memchunk.memblock);
423
424 if (u->client)
425 pa_socket_client_unref(u->client);
426
427 pa_xfree(u->read_data);
428 pa_xfree(u->write_data);
429
430 pa_xfree(u);
431 }
432
433
434