]> code.delx.au - pulseaudio/blob - src/modules/module-esound-sink.c
ignore network sinks/sources
[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 <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #include <poll.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <sys/ioctl.h>
41
42 #ifdef HAVE_LINUX_SOCKIOS_H
43 #include <linux/sockios.h>
44 #endif
45
46 #include <pulse/xmalloc.h>
47 #include <pulse/timeval.h>
48
49 #include <pulsecore/core-error.h>
50 #include <pulsecore/iochannel.h>
51 #include <pulsecore/sink.h>
52 #include <pulsecore/module.h>
53 #include <pulsecore/core-util.h>
54 #include <pulsecore/modargs.h>
55 #include <pulsecore/log.h>
56 #include <pulsecore/socket-client.h>
57 #include <pulsecore/esound.h>
58 #include <pulsecore/authkey.h>
59 #include <pulsecore/thread-mq.h>
60 #include <pulsecore/thread.h>
61 #include <pulsecore/time-smoother.h>
62 #include <pulsecore/rtclock.h>
63 #include <pulsecore/socket-util.h>
64
65 #include "module-esound-sink-symdef.h"
66
67 PA_MODULE_AUTHOR("Lennart Poettering")
68 PA_MODULE_DESCRIPTION("ESOUND Sink")
69 PA_MODULE_VERSION(PACKAGE_VERSION)
70 PA_MODULE_USAGE(
71 "sink_name=<name for the sink> "
72 "server=<address> cookie=<filename> "
73 "format=<sample format> "
74 "channels=<number of channels> "
75 "rate=<sample rate>")
76
77 #define DEFAULT_SINK_NAME "esound_out"
78
79 struct userdata {
80 pa_core *core;
81 pa_module *module;
82 pa_sink *sink;
83
84 pa_thread_mq thread_mq;
85 pa_rtpoll *rtpoll;
86 pa_rtpoll_item *rtpoll_item;
87 pa_thread *thread;
88
89 pa_memchunk memchunk;
90
91 void *write_data;
92 size_t write_length, write_index;
93
94 void *read_data;
95 size_t read_length, read_index;
96
97 enum {
98 STATE_AUTH,
99 STATE_LATENCY,
100 STATE_PREPARE,
101 STATE_RUNNING,
102 STATE_DEAD
103 } state;
104
105 pa_usec_t latency;
106
107 esd_format_t format;
108 int32_t rate;
109
110 pa_smoother *smoother;
111 int fd;
112
113 int64_t offset;
114
115 pa_iochannel *io;
116 pa_socket_client *client;
117
118 size_t block_size;
119 };
120
121 static const char* const valid_modargs[] = {
122 "server",
123 "cookie",
124 "rate",
125 "format",
126 "channels",
127 "sink_name",
128 NULL
129 };
130
131 enum {
132 SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX
133 };
134
135 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
136 struct userdata *u = PA_SINK(o)->userdata;
137
138 switch (code) {
139
140 case PA_SINK_MESSAGE_SET_STATE:
141
142 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
143
144 case PA_SINK_SUSPENDED:
145 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
146
147 pa_smoother_pause(u->smoother, pa_rtclock_usec());
148 break;
149
150 case PA_SINK_IDLE:
151 case PA_SINK_RUNNING:
152
153 if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
154 pa_smoother_resume(u->smoother, pa_rtclock_usec());
155
156 break;
157
158 case PA_SINK_UNLINKED:
159 case PA_SINK_INIT:
160 ;
161 }
162
163 break;
164
165 case PA_SINK_MESSAGE_GET_LATENCY: {
166 pa_usec_t w, r;
167
168 r = pa_smoother_get(u->smoother, pa_rtclock_usec());
169 w = pa_bytes_to_usec(u->offset + u->memchunk.length, &u->sink->sample_spec);
170
171 *((pa_usec_t*) data) = w > r ? w - r : 0;
172 break;
173 }
174
175 case SINK_MESSAGE_PASS_SOCKET: {
176 struct pollfd *pollfd;
177
178 pa_assert(!u->rtpoll_item);
179
180 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
181 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
182 pollfd->fd = u->fd;
183 pollfd->events = pollfd->revents = 0;
184
185 return 0;
186 }
187 }
188
189 return pa_sink_process_msg(o, code, data, offset, chunk);
190 }
191
192 static void thread_func(void *userdata) {
193 struct userdata *u = userdata;
194 int write_type = 0;
195
196 pa_assert(u);
197
198 pa_log_debug("Thread starting up");
199
200 pa_thread_mq_install(&u->thread_mq);
201 pa_rtpoll_install(u->rtpoll);
202
203 pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
204
205 for (;;) {
206 int ret;
207
208 if (u->rtpoll_item) {
209 struct pollfd *pollfd;
210 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
211
212 /* Render some data and write it to the fifo */
213 if (PA_SINK_OPENED(u->sink->thread_info.state) && pollfd->revents) {
214 pa_usec_t usec;
215 int64_t n;
216
217 for (;;) {
218 ssize_t l;
219 void *p;
220
221 if (u->memchunk.length <= 0)
222 pa_sink_render(u->sink, u->block_size, &u->memchunk);
223
224 pa_assert(u->memchunk.length > 0);
225
226 p = pa_memblock_acquire(u->memchunk.memblock);
227 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
228 pa_memblock_release(u->memchunk.memblock);
229
230 pa_assert(l != 0);
231
232 if (l < 0) {
233
234 if (errno == EINTR)
235 continue;
236 else if (errno == EAGAIN) {
237
238 /* OK, we filled all socket buffers up
239 * now. */
240 goto filled_up;
241
242 } else {
243 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
244 goto fail;
245 }
246
247 } else {
248 u->offset += l;
249
250 u->memchunk.index += l;
251 u->memchunk.length -= l;
252
253 if (u->memchunk.length <= 0) {
254 pa_memblock_unref(u->memchunk.memblock);
255 pa_memchunk_reset(&u->memchunk);
256 }
257
258 pollfd->revents = 0;
259
260 if (u->memchunk.length > 0)
261
262 /* OK, we wrote less that we asked for,
263 * hence we can assume that the socket
264 * buffers are full now */
265 goto filled_up;
266 }
267 }
268
269 filled_up:
270
271 /* At this spot we know that the socket buffers are
272 * fully filled up. This is the best time to estimate
273 * the playback position of the server */
274
275 n = u->offset;
276
277 #ifdef SIOCOUTQ
278 {
279 int l;
280 if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
281 n -= l;
282 }
283 #endif
284
285 usec = pa_bytes_to_usec(n, &u->sink->sample_spec);
286
287 if (usec > u->latency)
288 usec -= u->latency;
289 else
290 usec = 0;
291
292 pa_smoother_put(u->smoother, pa_rtclock_usec(), usec);
293 }
294
295 /* Hmm, nothing to do. Let's sleep */
296 pollfd->events = PA_SINK_OPENED(u->sink->thread_info.state) ? POLLOUT : 0;
297 }
298
299 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
300 goto fail;
301
302 if (ret == 0)
303 goto finish;
304
305 if (u->rtpoll_item) {
306 struct pollfd* pollfd;
307
308 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
309
310 if (pollfd->revents & ~POLLOUT) {
311 pa_log("FIFO shutdown.");
312 goto fail;
313 }
314 }
315 }
316
317 fail:
318 /* If this was no regular exit from the loop we have to continue
319 * processing messages until we received PA_MESSAGE_SHUTDOWN */
320 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
321 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
322
323 finish:
324 pa_log_debug("Thread shutting down");
325 }
326
327 static int do_write(struct userdata *u) {
328 ssize_t r;
329 pa_assert(u);
330
331 if (!pa_iochannel_is_writable(u->io))
332 return 0;
333
334 if (u->write_data) {
335 pa_assert(u->write_index < u->write_length);
336
337 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) {
338 pa_log("write() failed: %s", pa_cstrerror(errno));
339 return -1;
340 }
341
342 u->write_index += r;
343 pa_assert(u->write_index <= u->write_length);
344
345 if (u->write_index == u->write_length) {
346 pa_xfree(u->write_data);
347 u->write_data = NULL;
348 u->write_index = u->write_length = 0;
349 }
350 }
351
352 if (!u->write_data && u->state == STATE_PREPARE) {
353 /* OK, we're done with sending all control data we need to, so
354 * let's hand the socket over to the IO thread now */
355
356 pa_assert(u->fd < 0);
357 u->fd = pa_iochannel_get_send_fd(u->io);
358
359 pa_iochannel_set_noclose(u->io, TRUE);
360 pa_iochannel_free(u->io);
361 u->io = NULL;
362
363 pa_make_tcp_socket_low_delay(u->fd);
364
365 pa_log_debug("Connection authenticated, handing fd to IO thread...");
366
367 pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
368 u->state = STATE_RUNNING;
369 }
370
371 return 0;
372 }
373
374 static int handle_response(struct userdata *u) {
375 pa_assert(u);
376
377 switch (u->state) {
378
379 case STATE_AUTH:
380 pa_assert(u->read_length == sizeof(int32_t));
381
382 /* Process auth data */
383 if (!*(int32_t*) u->read_data) {
384 pa_log("Authentication failed: %s", pa_cstrerror(errno));
385 return -1;
386 }
387
388 /* Request latency data */
389 pa_assert(!u->write_data);
390 *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
391
392 u->write_index = 0;
393 u->state = STATE_LATENCY;
394
395 /* Space for next response */
396 pa_assert(u->read_length >= sizeof(int32_t));
397 u->read_index = 0;
398 u->read_length = sizeof(int32_t);
399
400 break;
401
402 case STATE_LATENCY: {
403 int32_t *p;
404 pa_assert(u->read_length == sizeof(int32_t));
405
406 /* Process latency info */
407 u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
408 if (u->latency > 10000000) {
409 pa_log_warn("Invalid latency information received from server");
410 u->latency = 0;
411 }
412
413 /* Create stream */
414 pa_assert(!u->write_data);
415 p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
416 *(p++) = ESD_PROTO_STREAM_PLAY;
417 *(p++) = u->format;
418 *(p++) = u->rate;
419 pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
420
421 u->write_index = 0;
422 u->state = STATE_PREPARE;
423
424 /* Don't read any further */
425 pa_xfree(u->read_data);
426 u->read_data = NULL;
427 u->read_index = u->read_length = 0;
428
429 break;
430 }
431
432 default:
433 pa_assert_not_reached();
434 }
435
436 return 0;
437 }
438
439 static int do_read(struct userdata *u) {
440 pa_assert(u);
441
442 if (!pa_iochannel_is_readable(u->io))
443 return 0;
444
445 if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
446 ssize_t r;
447
448 if (!u->read_data)
449 return 0;
450
451 pa_assert(u->read_index < u->read_length);
452
453 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
454 pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
455 return -1;
456 }
457
458 u->read_index += r;
459 pa_assert(u->read_index <= u->read_length);
460
461 if (u->read_index == u->read_length)
462 return handle_response(u);
463 }
464
465 return 0;
466 }
467
468 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
469 struct userdata *u = userdata;
470 pa_assert(u);
471
472 if (do_read(u) < 0 || do_write(u) < 0) {
473
474 if (u->io) {
475 pa_iochannel_free(u->io);
476 u->io = NULL;
477 }
478
479 pa_module_unload_request(u->module);
480 }
481 }
482
483 static void on_connection(PA_GCC_UNUSED pa_socket_client *c, pa_iochannel*io, void *userdata) {
484 struct userdata *u = userdata;
485
486 pa_socket_client_unref(u->client);
487 u->client = NULL;
488
489 if (!io) {
490 pa_log("Connection failed: %s", pa_cstrerror(errno));
491 pa_module_unload_request(u->module);
492 return;
493 }
494
495 pa_assert(!u->io);
496 u->io = io;
497 pa_iochannel_set_callback(u->io, io_callback, u);
498
499 pa_log_debug("Connection established, authenticating ...");
500 }
501
502 int pa__init(pa_module*m) {
503 struct userdata *u = NULL;
504 const char *p;
505 pa_sample_spec ss;
506 pa_modargs *ma = NULL;
507 char *t;
508 const char *espeaker;
509
510 pa_assert(m);
511
512 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
513 pa_log("failed to parse module arguments");
514 goto fail;
515 }
516
517 ss = m->core->default_sample_spec;
518 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
519 pa_log("invalid sample format specification");
520 goto fail;
521 }
522
523 if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
524 (ss.channels > 2)) {
525 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
526 goto fail;
527 }
528
529 u = pa_xnew0(struct userdata, 1);
530 u->core = m->core;
531 u->module = m;
532 m->userdata = u;
533 u->fd = -1;
534 u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE);
535 pa_memchunk_reset(&u->memchunk);
536 u->offset = 0;
537
538 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
539 u->rtpoll = pa_rtpoll_new();
540 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
541 u->rtpoll_item = NULL;
542
543 u->format =
544 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
545 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);
546 u->rate = ss.rate;
547 u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
548
549 u->read_data = u->write_data = NULL;
550 u->read_index = u->write_index = u->read_length = u->write_length = 0;
551
552 u->state = STATE_AUTH;
553 u->latency = 0;
554
555 if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) {
556 pa_log("Failed to create sink.");
557 goto fail;
558 }
559
560 u->sink->parent.process_msg = sink_process_msg;
561 u->sink->userdata = u;
562 u->sink->flags = PA_SINK_LATENCY|PA_SINK_NETWORK;
563
564 pa_sink_set_module(u->sink, m);
565 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
566 pa_sink_set_rtpoll(u->sink, u->rtpoll);
567
568 if (!(espeaker = getenv("ESPEAKER")))
569 espeaker = ESD_UNIX_SOCKET_NAME;
570
571 if (!(u->client = pa_socket_client_new_string(u->core->mainloop, p = pa_modargs_get_value(ma, "server", espeaker), ESD_DEFAULT_PORT))) {
572 pa_log("Failed to connect to server.");
573 goto fail;
574 }
575
576 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Esound sink '%s'", p));
577 pa_xfree(t);
578
579 pa_socket_client_set_callback(u->client, on_connection, u);
580
581 /* Prepare the initial request */
582 u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
583 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) {
584 pa_log("Failed to load cookie");
585 goto fail;
586 }
587 *(int32_t*) ((uint8_t*) u->write_data + ESD_KEY_LEN) = ESD_ENDIAN_KEY;
588
589 /* Reserve space for the response */
590 u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
591
592 if (!(u->thread = pa_thread_new(thread_func, u))) {
593 pa_log("Failed to create thread.");
594 goto fail;
595 }
596
597 pa_sink_put(u->sink);
598
599 pa_modargs_free(ma);
600
601 return 0;
602
603 fail:
604 if (ma)
605 pa_modargs_free(ma);
606
607 pa__done(m);
608
609 return -1;
610 }
611
612 void pa__done(pa_module*m) {
613 struct userdata *u;
614 pa_assert(m);
615
616 if (!(u = m->userdata))
617 return;
618
619 if (u->sink)
620 pa_sink_unlink(u->sink);
621
622 if (u->thread) {
623 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
624 pa_thread_free(u->thread);
625 }
626
627 pa_thread_mq_done(&u->thread_mq);
628
629 if (u->sink)
630 pa_sink_unref(u->sink);
631
632 if (u->io)
633 pa_iochannel_free(u->io);
634
635 if (u->rtpoll_item)
636 pa_rtpoll_item_free(u->rtpoll_item);
637
638 if (u->rtpoll)
639 pa_rtpoll_free(u->rtpoll);
640
641 if (u->memchunk.memblock)
642 pa_memblock_unref(u->memchunk.memblock);
643
644 if (u->client)
645 pa_socket_client_unref(u->client);
646
647 pa_xfree(u->read_data);
648 pa_xfree(u->write_data);
649
650 if (u->smoother)
651 pa_smoother_free(u->smoother);
652
653 if (u->fd >= 0)
654 pa_close(u->fd);
655
656 pa_xfree(u);
657 }