default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / modules / module-esound-sink.c
blobd79054f818772deb8e2da1f844e0a429a4141429
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
11 PulseAudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <unistd.h>
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
34 #endif
36 #ifdef HAVE_NETINET_TCP_H
37 #include <netinet/tcp.h>
38 #endif
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
42 #endif
44 #ifdef HAVE_LINUX_SOCKIOS_H
45 #include <linux/sockios.h>
46 #endif
48 #include <pulse/rtclock.h>
49 #include <pulse/timeval.h>
50 #include <pulse/xmalloc.h>
52 #include <pulsecore/socket.h>
53 #include <pulsecore/core-error.h>
54 #include <pulsecore/iochannel.h>
55 #include <pulsecore/sink.h>
56 #include <pulsecore/module.h>
57 #include <pulsecore/core-util.h>
58 #include <pulsecore/modargs.h>
59 #include <pulsecore/log.h>
60 #include <pulsecore/socket-client.h>
61 #include <pulsecore/esound.h>
62 #include <pulsecore/authkey.h>
63 #include <pulsecore/thread-mq.h>
64 #include <pulsecore/thread.h>
65 #include <pulsecore/time-smoother.h>
66 #include <pulsecore/socket-util.h>
67 #include <pulsecore/rtpoll.h>
68 #include <pulsecore/poll.h>
70 #include "module-esound-sink-symdef.h"
72 PA_MODULE_AUTHOR("Lennart Poettering");
73 PA_MODULE_DESCRIPTION("ESOUND Sink");
74 PA_MODULE_VERSION(PACKAGE_VERSION);
75 PA_MODULE_LOAD_ONCE(FALSE);
76 PA_MODULE_USAGE(
77 "sink_name=<name for the sink> "
78 "sink_properties=<properties for the sink> "
79 "server=<address> cookie=<filename> "
80 "format=<sample format> "
81 "rate=<sample rate> "
82 "channels=<number of channels>");
84 #define DEFAULT_SINK_NAME "esound_out"
86 struct userdata {
87 pa_core *core;
88 pa_module *module;
89 pa_sink *sink;
91 pa_thread_mq thread_mq;
92 pa_rtpoll *rtpoll;
93 pa_rtpoll_item *rtpoll_item;
94 pa_thread *thread;
96 pa_memchunk memchunk;
98 void *write_data;
99 size_t write_length, write_index;
101 void *read_data;
102 size_t read_length, read_index;
104 enum {
105 STATE_AUTH,
106 STATE_LATENCY,
107 STATE_PREPARE,
108 STATE_RUNNING,
109 STATE_DEAD
110 } state;
112 pa_usec_t latency;
114 esd_format_t format;
115 int32_t rate;
117 pa_smoother *smoother;
118 int fd;
120 int64_t offset;
122 pa_iochannel *io;
123 pa_socket_client *client;
125 size_t block_size;
128 static const char* const valid_modargs[] = {
129 "sink_name",
130 "sink_properties",
131 "server",
132 "cookie",
133 "format",
134 "rate",
135 "channels",
136 NULL
139 enum {
140 SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX
143 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
144 struct userdata *u = PA_SINK(o)->userdata;
146 switch (code) {
148 case PA_SINK_MESSAGE_SET_STATE:
150 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
152 case PA_SINK_SUSPENDED:
153 pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
155 pa_smoother_pause(u->smoother, pa_rtclock_now());
156 break;
158 case PA_SINK_IDLE:
159 case PA_SINK_RUNNING:
161 if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
162 pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE);
164 break;
166 case PA_SINK_UNLINKED:
167 case PA_SINK_INIT:
168 case PA_SINK_INVALID_STATE:
172 break;
174 case PA_SINK_MESSAGE_GET_LATENCY: {
175 pa_usec_t w, r;
177 r = pa_smoother_get(u->smoother, pa_rtclock_now());
178 w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec);
180 *((pa_usec_t*) data) = w > r ? w - r : 0;
181 return 0;
184 case SINK_MESSAGE_PASS_SOCKET: {
185 struct pollfd *pollfd;
187 pa_assert(!u->rtpoll_item);
189 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
190 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
191 pollfd->fd = u->fd;
192 pollfd->events = pollfd->revents = 0;
194 return 0;
198 return pa_sink_process_msg(o, code, data, offset, chunk);
201 static void thread_func(void *userdata) {
202 struct userdata *u = userdata;
203 int write_type = 0;
205 pa_assert(u);
207 pa_log_debug("Thread starting up");
209 pa_thread_mq_install(&u->thread_mq);
211 pa_smoother_set_time_offset(u->smoother, pa_rtclock_now());
213 for (;;) {
214 int ret;
216 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
217 if (u->sink->thread_info.rewind_requested)
218 pa_sink_process_rewind(u->sink, 0);
220 if (u->rtpoll_item) {
221 struct pollfd *pollfd;
222 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
224 /* Render some data and write it to the fifo */
225 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && pollfd->revents) {
226 pa_usec_t usec;
227 int64_t n;
229 for (;;) {
230 ssize_t l;
231 void *p;
233 if (u->memchunk.length <= 0)
234 pa_sink_render(u->sink, u->block_size, &u->memchunk);
236 pa_assert(u->memchunk.length > 0);
238 p = pa_memblock_acquire(u->memchunk.memblock);
239 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
240 pa_memblock_release(u->memchunk.memblock);
242 pa_assert(l != 0);
244 if (l < 0) {
246 if (errno == EINTR)
247 continue;
248 else if (errno == EAGAIN) {
250 /* OK, we filled all socket buffers up
251 * now. */
252 goto filled_up;
254 } else {
255 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
256 goto fail;
259 } else {
260 u->offset += l;
262 u->memchunk.index += (size_t) l;
263 u->memchunk.length -= (size_t) l;
265 if (u->memchunk.length <= 0) {
266 pa_memblock_unref(u->memchunk.memblock);
267 pa_memchunk_reset(&u->memchunk);
270 pollfd->revents = 0;
272 if (u->memchunk.length > 0)
274 /* OK, we wrote less that we asked for,
275 * hence we can assume that the socket
276 * buffers are full now */
277 goto filled_up;
281 filled_up:
283 /* At this spot we know that the socket buffers are
284 * fully filled up. This is the best time to estimate
285 * the playback position of the server */
287 n = u->offset;
289 #ifdef SIOCOUTQ
291 int l;
292 if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
293 n -= l;
295 #endif
297 usec = pa_bytes_to_usec((uint64_t) n, &u->sink->sample_spec);
299 if (usec > u->latency)
300 usec -= u->latency;
301 else
302 usec = 0;
304 pa_smoother_put(u->smoother, pa_rtclock_now(), usec);
307 /* Hmm, nothing to do. Let's sleep */
308 pollfd->events = (short) (PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0);
311 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
312 goto fail;
314 if (ret == 0)
315 goto finish;
317 if (u->rtpoll_item) {
318 struct pollfd* pollfd;
320 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
322 if (pollfd->revents & ~POLLOUT) {
323 pa_log("FIFO shutdown.");
324 goto fail;
329 fail:
330 /* If this was no regular exit from the loop we have to continue
331 * processing messages until we received PA_MESSAGE_SHUTDOWN */
332 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
333 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
335 finish:
336 pa_log_debug("Thread shutting down");
339 static int do_write(struct userdata *u) {
340 ssize_t r;
341 pa_assert(u);
343 if (!pa_iochannel_is_writable(u->io))
344 return 0;
346 if (u->write_data) {
347 pa_assert(u->write_index < u->write_length);
349 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) {
350 pa_log("write() failed: %s", pa_cstrerror(errno));
351 return -1;
354 u->write_index += (size_t) r;
355 pa_assert(u->write_index <= u->write_length);
357 if (u->write_index == u->write_length) {
358 pa_xfree(u->write_data);
359 u->write_data = NULL;
360 u->write_index = u->write_length = 0;
364 if (!u->write_data && u->state == STATE_PREPARE) {
365 int so_sndbuf = 0;
366 socklen_t sl = sizeof(int);
368 /* OK, we're done with sending all control data we need to, so
369 * let's hand the socket over to the IO thread now */
371 pa_assert(u->fd < 0);
372 u->fd = pa_iochannel_get_send_fd(u->io);
374 pa_iochannel_set_noclose(u->io, TRUE);
375 pa_iochannel_free(u->io);
376 u->io = NULL;
378 pa_make_tcp_socket_low_delay(u->fd);
380 if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, &so_sndbuf, &sl) < 0)
381 pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno));
382 else {
383 pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf);
384 pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size));
387 pa_log_debug("Connection authenticated, handing fd to IO thread...");
389 pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
390 u->state = STATE_RUNNING;
393 return 0;
396 static int handle_response(struct userdata *u) {
397 pa_assert(u);
399 switch (u->state) {
401 case STATE_AUTH:
402 pa_assert(u->read_length == sizeof(int32_t));
404 /* Process auth data */
405 if (!*(int32_t*) u->read_data) {
406 pa_log("Authentication failed: %s", pa_cstrerror(errno));
407 return -1;
410 /* Request latency data */
411 pa_assert(!u->write_data);
412 *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
414 u->write_index = 0;
415 u->state = STATE_LATENCY;
417 /* Space for next response */
418 pa_assert(u->read_length >= sizeof(int32_t));
419 u->read_index = 0;
420 u->read_length = sizeof(int32_t);
422 break;
424 case STATE_LATENCY: {
425 int32_t *p;
426 pa_assert(u->read_length == sizeof(int32_t));
428 /* Process latency info */
429 u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
430 if (u->latency > 10000000) {
431 pa_log_warn("Invalid latency information received from server");
432 u->latency = 0;
435 /* Create stream */
436 pa_assert(!u->write_data);
437 p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
438 *(p++) = ESD_PROTO_STREAM_PLAY;
439 *(p++) = u->format;
440 *(p++) = u->rate;
441 pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
443 u->write_index = 0;
444 u->state = STATE_PREPARE;
446 /* Don't read any further */
447 pa_xfree(u->read_data);
448 u->read_data = NULL;
449 u->read_index = u->read_length = 0;
451 break;
454 default:
455 pa_assert_not_reached();
458 return 0;
461 static int do_read(struct userdata *u) {
462 pa_assert(u);
464 if (!pa_iochannel_is_readable(u->io))
465 return 0;
467 if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
468 ssize_t r;
470 if (!u->read_data)
471 return 0;
473 pa_assert(u->read_index < u->read_length);
475 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
476 pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
477 return -1;
480 u->read_index += (size_t) r;
481 pa_assert(u->read_index <= u->read_length);
483 if (u->read_index == u->read_length)
484 return handle_response(u);
487 return 0;
490 static void io_callback(pa_iochannel *io, void*userdata) {
491 struct userdata *u = userdata;
492 pa_assert(u);
494 if (do_read(u) < 0 || do_write(u) < 0) {
496 if (u->io) {
497 pa_iochannel_free(u->io);
498 u->io = NULL;
501 pa_module_unload_request(u->module, TRUE);
505 static void on_connection(pa_socket_client *c, pa_iochannel*io, void *userdata) {
506 struct userdata *u = userdata;
508 pa_socket_client_unref(u->client);
509 u->client = NULL;
511 if (!io) {
512 pa_log("Connection failed: %s", pa_cstrerror(errno));
513 pa_module_unload_request(u->module, TRUE);
514 return;
517 pa_assert(!u->io);
518 u->io = io;
519 pa_iochannel_set_callback(u->io, io_callback, u);
521 pa_log_debug("Connection established, authenticating ...");
524 int pa__init(pa_module*m) {
525 struct userdata *u = NULL;
526 pa_sample_spec ss;
527 pa_modargs *ma = NULL;
528 const char *espeaker;
529 uint32_t key;
530 pa_sink_new_data data;
532 pa_assert(m);
534 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
535 pa_log("failed to parse module arguments");
536 goto fail;
539 ss = m->core->default_sample_spec;
540 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
541 pa_log("invalid sample format specification");
542 goto fail;
545 if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
546 (ss.channels > 2)) {
547 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
548 goto fail;
551 u = pa_xnew0(struct userdata, 1);
552 u->core = m->core;
553 u->module = m;
554 m->userdata = u;
555 u->fd = -1;
556 u->smoother = pa_smoother_new(
557 PA_USEC_PER_SEC,
558 PA_USEC_PER_SEC*2,
559 TRUE,
560 TRUE,
563 FALSE);
564 pa_memchunk_reset(&u->memchunk);
565 u->offset = 0;
567 u->rtpoll = pa_rtpoll_new();
568 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
569 u->rtpoll_item = NULL;
571 u->format =
572 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
573 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);
574 u->rate = (int32_t) ss.rate;
575 u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
577 u->read_data = u->write_data = NULL;
578 u->read_index = u->write_index = u->read_length = u->write_length = 0;
580 u->state = STATE_AUTH;
581 u->latency = 0;
583 if (!(espeaker = getenv("ESPEAKER")))
584 espeaker = ESD_UNIX_SOCKET_NAME;
586 espeaker = pa_modargs_get_value(ma, "server", espeaker);
588 pa_sink_new_data_init(&data);
589 data.driver = __FILE__;
590 data.module = m;
591 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
592 pa_sink_new_data_set_sample_spec(&data, &ss);
593 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, espeaker);
594 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "esd");
595 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "EsounD Output on %s", espeaker);
597 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
598 pa_log("Invalid properties");
599 pa_sink_new_data_done(&data);
600 goto fail;
603 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
604 pa_sink_new_data_done(&data);
606 if (!u->sink) {
607 pa_log("Failed to create sink.");
608 goto fail;
611 u->sink->parent.process_msg = sink_process_msg;
612 u->sink->userdata = u;
614 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
615 pa_sink_set_rtpoll(u->sink, u->rtpoll);
617 if (!(u->client = pa_socket_client_new_string(u->core->mainloop, TRUE, espeaker, ESD_DEFAULT_PORT))) {
618 pa_log("Failed to connect to server.");
619 goto fail;
622 pa_socket_client_set_callback(u->client, on_connection, u);
624 /* Prepare the initial request */
625 u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
626 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) {
627 pa_log("Failed to load cookie");
628 goto fail;
631 key = ESD_ENDIAN_KEY;
632 memcpy((uint8_t*) u->write_data + ESD_KEY_LEN, &key, sizeof(key));
634 /* Reserve space for the response */
635 u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
637 if (!(u->thread = pa_thread_new("esound-sink", thread_func, u))) {
638 pa_log("Failed to create thread.");
639 goto fail;
642 pa_sink_put(u->sink);
644 pa_modargs_free(ma);
646 return 0;
648 fail:
649 if (ma)
650 pa_modargs_free(ma);
652 pa__done(m);
654 return -1;
657 int pa__get_n_used(pa_module *m) {
658 struct userdata *u;
660 pa_assert(m);
661 pa_assert_se(u = m->userdata);
663 return pa_sink_linked_by(u->sink);
666 void pa__done(pa_module*m) {
667 struct userdata *u;
668 pa_assert(m);
670 if (!(u = m->userdata))
671 return;
673 if (u->sink)
674 pa_sink_unlink(u->sink);
676 if (u->thread) {
677 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
678 pa_thread_free(u->thread);
681 pa_thread_mq_done(&u->thread_mq);
683 if (u->sink)
684 pa_sink_unref(u->sink);
686 if (u->io)
687 pa_iochannel_free(u->io);
689 if (u->rtpoll_item)
690 pa_rtpoll_item_free(u->rtpoll_item);
692 if (u->rtpoll)
693 pa_rtpoll_free(u->rtpoll);
695 if (u->memchunk.memblock)
696 pa_memblock_unref(u->memchunk.memblock);
698 if (u->client)
699 pa_socket_client_unref(u->client);
701 pa_xfree(u->read_data);
702 pa_xfree(u->write_data);
704 if (u->smoother)
705 pa_smoother_free(u->smoother);
707 if (u->fd >= 0)
708 pa_close(u->fd);
710 pa_xfree(u);