Merge HUGE set of changes temporarily into a branch, to allow me to move them from...
[profile/ivi/pulseaudio-panda.git] / 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         void *p;
148
149         pa_module_set_used(u->module, pa_sink_used_by(u->sink));
150
151         if (!u->memchunk.length)
152             if (pa_sink_render(u->sink, 8192, &u->memchunk) < 0)
153                 return 0;
154
155         assert(u->memchunk.memblock);
156         assert(u->memchunk.length);
157
158         p = pa_memblock_acquire(u->memchunk.memblock);
159
160         if ((r = pa_iochannel_write(u->io, (uint8_t*) p + u->memchunk.index, u->memchunk.length)) < 0) {
161             pa_memblock_release(u->memchunk.memblock);
162             pa_log("write() failed: %s", pa_cstrerror(errno));
163             return -1;
164         }
165         pa_memblock_release(u->memchunk.memblock);
166
167         u->memchunk.index += r;
168         u->memchunk.length -= r;
169
170         if (u->memchunk.length <= 0) {
171             pa_memblock_unref(u->memchunk.memblock);
172             u->memchunk.memblock = NULL;
173         }
174     }
175
176     return 0;
177 }
178
179 static int handle_response(struct userdata *u) {
180     assert(u);
181
182     switch (u->state) {
183         case STATE_AUTH:
184             assert(u->read_length == sizeof(int32_t));
185
186             /* Process auth data */
187             if (!*(int32_t*) u->read_data) {
188                 pa_log("Authentication failed: %s", pa_cstrerror(errno));
189                 return -1;
190             }
191
192             /* Request latency data */
193             assert(!u->write_data);
194             *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
195
196             u->write_index = 0;
197             u->state = STATE_LATENCY;
198
199             /* Space for next response */
200             assert(u->read_length >= sizeof(int32_t));
201             u->read_index = 0;
202             u->read_length = sizeof(int32_t);
203
204             break;
205
206         case STATE_LATENCY: {
207             int32_t *p;
208             assert(u->read_length == sizeof(int32_t));
209
210             /* Process latency info */
211             u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
212             if (u->latency > 10000000) {
213                 pa_log("WARNING! Invalid latency information received from server");
214                 u->latency = 0;
215             }
216
217             /* Create stream */
218             assert(!u->write_data);
219             p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
220             *(p++) = ESD_PROTO_STREAM_PLAY;
221             *(p++) = u->format;
222             *(p++) = u->rate;
223             pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
224
225             u->write_index = 0;
226             u->state = STATE_RUNNING;
227
228             /* Don't read any further */
229             pa_xfree(u->read_data);
230             u->read_data = NULL;
231             u->read_index = u->read_length = 0;
232
233             break;
234         }
235
236         default:
237             abort();
238     }
239
240     return 0;
241 }
242
243 static int do_read(struct userdata *u) {
244     assert(u);
245
246     if (!pa_iochannel_is_readable(u->io))
247         return 0;
248
249     if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
250         ssize_t r;
251
252         if (!u->read_data)
253             return 0;
254
255         assert(u->read_index < u->read_length);
256
257         if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
258             pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
259             cancel(u);
260             return -1;
261         }
262
263         u->read_index += r;
264         assert(u->read_index <= u->read_length);
265
266         if (u->read_index == u->read_length)
267             return handle_response(u);
268     }
269
270     return 0;
271 }
272
273 static void do_work(struct userdata *u) {
274     assert(u);
275
276     u->core->mainloop->defer_enable(u->defer_event, 0);
277
278     if (do_read(u) < 0 || do_write(u) < 0)
279         cancel(u);
280 }
281
282 static void notify_cb(pa_sink*s) {
283     struct userdata *u = s->userdata;
284     assert(s && u);
285
286     if (pa_iochannel_is_writable(u->io))
287         u->core->mainloop->defer_enable(u->defer_event, 1);
288 }
289
290 static pa_usec_t get_latency_cb(pa_sink *s) {
291     struct userdata *u = s->userdata;
292     assert(s && u);
293
294     return
295         u->latency +
296         (u->memchunk.memblock ? pa_bytes_to_usec(u->memchunk.length, &s->sample_spec) : 0);
297 }
298
299 static void defer_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_defer_event*e, void *userdata) {
300     struct userdata *u = userdata;
301     assert(u);
302     do_work(u);
303 }
304
305 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
306     struct userdata *u = userdata;
307     assert(u);
308     do_work(u);
309 }
310
311 static void on_connection(PA_GCC_UNUSED pa_socket_client *c, pa_iochannel*io, void *userdata) {
312     struct userdata *u = userdata;
313
314     pa_socket_client_unref(u->client);
315     u->client = NULL;
316
317     if (!io) {
318         pa_log("connection failed: %s", pa_cstrerror(errno));
319         cancel(u);
320         return;
321     }
322
323     u->io = io;
324     pa_iochannel_set_callback(u->io, io_callback, u);
325 }
326
327 int pa__init(pa_core *c, pa_module*m) {
328     struct userdata *u = NULL;
329     const char *p;
330     pa_sample_spec ss;
331     pa_modargs *ma = NULL;
332     char *t;
333
334     assert(c && m);
335
336     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
337         pa_log("failed to parse module arguments");
338         goto fail;
339     }
340
341     ss = c->default_sample_spec;
342     if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
343         pa_log("invalid sample format specification");
344         goto fail;
345     }
346
347     if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
348         (ss.channels > 2)) {
349         pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
350         goto fail;
351     }
352
353     u = pa_xmalloc0(sizeof(struct userdata));
354     u->core = c;
355     u->module = m;
356     m->userdata = u;
357     u->format =
358         (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
359         (ss.channels == 2 ? ESD_STEREO : ESD_MONO);
360     u->rate = ss.rate;
361     u->sink = NULL;
362     u->client = NULL;
363     u->io = NULL;
364     u->read_data = u->write_data = NULL;
365     u->read_index = u->write_index = u->read_length = u->write_length = 0;
366     u->state = STATE_AUTH;
367     u->latency = 0;
368
369     if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) {
370         pa_log("failed to create sink.");
371         goto fail;
372     }
373
374     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))) {
375         pa_log("failed to connect to server.");
376         goto fail;
377     }
378     pa_socket_client_set_callback(u->client, on_connection, u);
379
380     /* Prepare the initial request */
381     u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
382     if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) {
383         pa_log("failed to load cookie");
384         goto fail;
385     }
386     *(int32_t*) ((uint8_t*) u->write_data + ESD_KEY_LEN) = ESD_ENDIAN_KEY;
387
388     /* Reserve space for the response */
389     u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
390
391     u->sink->notify = notify_cb;
392     u->sink->get_latency = get_latency_cb;
393     u->sink->userdata = u;
394     pa_sink_set_owner(u->sink, m);
395     pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Esound sink '%s'", p));
396     pa_xfree(t);
397
398     u->memchunk.memblock = NULL;
399     u->memchunk.length = 0;
400
401     u->defer_event = c->mainloop->defer_new(c->mainloop, defer_callback, u);
402     c->mainloop->defer_enable(u->defer_event, 0);
403
404
405     pa_modargs_free(ma);
406
407     return 0;
408
409 fail:
410     if (ma)
411         pa_modargs_free(ma);
412
413     pa__done(c, m);
414
415     return -1;
416 }
417
418 void pa__done(pa_core *c, pa_module*m) {
419     struct userdata *u;
420     assert(c && m);
421
422     if (!(u = m->userdata))
423         return;
424
425     u->module = NULL;
426     cancel(u);
427
428     if (u->memchunk.memblock)
429         pa_memblock_unref(u->memchunk.memblock);
430
431     if (u->client)
432         pa_socket_client_unref(u->client);
433
434     pa_xfree(u->read_data);
435     pa_xfree(u->write_data);
436
437     pa_xfree(u);
438 }
439
440
441