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