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