udev: when a device appears that we cannot access right-away try again later on inotify
[pulseaudio-mirror.git] / src / modules / module-esound-sink.c
blobd7c678ca9d5a3cda1961e3d67ac6cd335ee33aed
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 <sys/stat.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <poll.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/tcp.h>
38 #include <sys/ioctl.h>
40 #ifdef HAVE_LINUX_SOCKIOS_H
41 #include <linux/sockios.h>
42 #endif
44 #include <pulse/rtclock.h>
45 #include <pulse/timeval.h>
46 #include <pulse/xmalloc.h>
48 #include <pulsecore/core-error.h>
49 #include <pulsecore/iochannel.h>
50 #include <pulsecore/sink.h>
51 #include <pulsecore/module.h>
52 #include <pulsecore/core-rtclock.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/socket-util.h>
64 #include "module-esound-sink-symdef.h"
66 PA_MODULE_AUTHOR("Lennart Poettering");
67 PA_MODULE_DESCRIPTION("ESOUND Sink");
68 PA_MODULE_VERSION(PACKAGE_VERSION);
69 PA_MODULE_LOAD_ONCE(FALSE);
70 PA_MODULE_USAGE(
71 "sink_name=<name for the sink> "
72 "sink_properties=<properties for the sink> "
73 "server=<address> cookie=<filename> "
74 "format=<sample format> "
75 "rate=<sample rate> "
76 "channels=<number of channels>");
78 #define DEFAULT_SINK_NAME "esound_out"
80 struct userdata {
81 pa_core *core;
82 pa_module *module;
83 pa_sink *sink;
85 pa_thread_mq thread_mq;
86 pa_rtpoll *rtpoll;
87 pa_rtpoll_item *rtpoll_item;
88 pa_thread *thread;
90 pa_memchunk memchunk;
92 void *write_data;
93 size_t write_length, write_index;
95 void *read_data;
96 size_t read_length, read_index;
98 enum {
99 STATE_AUTH,
100 STATE_LATENCY,
101 STATE_PREPARE,
102 STATE_RUNNING,
103 STATE_DEAD
104 } state;
106 pa_usec_t latency;
108 esd_format_t format;
109 int32_t rate;
111 pa_smoother *smoother;
112 int fd;
114 int64_t offset;
116 pa_iochannel *io;
117 pa_socket_client *client;
119 size_t block_size;
122 static const char* const valid_modargs[] = {
123 "sink_name",
124 "sink_properties",
125 "server",
126 "cookie",
127 "format",
128 "rate",
129 "channels",
130 NULL
133 enum {
134 SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX
137 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
138 struct userdata *u = PA_SINK(o)->userdata;
140 switch (code) {
142 case PA_SINK_MESSAGE_SET_STATE:
144 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
146 case PA_SINK_SUSPENDED:
147 pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
149 pa_smoother_pause(u->smoother, pa_rtclock_now());
150 break;
152 case PA_SINK_IDLE:
153 case PA_SINK_RUNNING:
155 if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
156 pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE);
158 break;
160 case PA_SINK_UNLINKED:
161 case PA_SINK_INIT:
162 case PA_SINK_INVALID_STATE:
166 break;
168 case PA_SINK_MESSAGE_GET_LATENCY: {
169 pa_usec_t w, r;
171 r = pa_smoother_get(u->smoother, pa_rtclock_now());
172 w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec);
174 *((pa_usec_t*) data) = w > r ? w - r : 0;
175 return 0;
178 case SINK_MESSAGE_PASS_SOCKET: {
179 struct pollfd *pollfd;
181 pa_assert(!u->rtpoll_item);
183 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
184 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
185 pollfd->fd = u->fd;
186 pollfd->events = pollfd->revents = 0;
188 return 0;
192 return pa_sink_process_msg(o, code, data, offset, chunk);
195 static void thread_func(void *userdata) {
196 struct userdata *u = userdata;
197 int write_type = 0;
199 pa_assert(u);
201 pa_log_debug("Thread starting up");
203 pa_thread_mq_install(&u->thread_mq);
205 pa_smoother_set_time_offset(u->smoother, pa_rtclock_now());
207 for (;;) {
208 int ret;
210 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
211 if (u->sink->thread_info.rewind_requested)
212 pa_sink_process_rewind(u->sink, 0);
214 if (u->rtpoll_item) {
215 struct pollfd *pollfd;
216 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
218 /* Render some data and write it to the fifo */
219 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && pollfd->revents) {
220 pa_usec_t usec;
221 int64_t n;
223 for (;;) {
224 ssize_t l;
225 void *p;
227 if (u->memchunk.length <= 0)
228 pa_sink_render(u->sink, u->block_size, &u->memchunk);
230 pa_assert(u->memchunk.length > 0);
232 p = pa_memblock_acquire(u->memchunk.memblock);
233 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
234 pa_memblock_release(u->memchunk.memblock);
236 pa_assert(l != 0);
238 if (l < 0) {
240 if (errno == EINTR)
241 continue;
242 else if (errno == EAGAIN) {
244 /* OK, we filled all socket buffers up
245 * now. */
246 goto filled_up;
248 } else {
249 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
250 goto fail;
253 } else {
254 u->offset += l;
256 u->memchunk.index += (size_t) l;
257 u->memchunk.length -= (size_t) l;
259 if (u->memchunk.length <= 0) {
260 pa_memblock_unref(u->memchunk.memblock);
261 pa_memchunk_reset(&u->memchunk);
264 pollfd->revents = 0;
266 if (u->memchunk.length > 0)
268 /* OK, we wrote less that we asked for,
269 * hence we can assume that the socket
270 * buffers are full now */
271 goto filled_up;
275 filled_up:
277 /* At this spot we know that the socket buffers are
278 * fully filled up. This is the best time to estimate
279 * the playback position of the server */
281 n = u->offset;
283 #ifdef SIOCOUTQ
285 int l;
286 if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
287 n -= l;
289 #endif
291 usec = pa_bytes_to_usec((uint64_t) n, &u->sink->sample_spec);
293 if (usec > u->latency)
294 usec -= u->latency;
295 else
296 usec = 0;
298 pa_smoother_put(u->smoother, pa_rtclock_now(), usec);
301 /* Hmm, nothing to do. Let's sleep */
302 pollfd->events = (short) (PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0);
305 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
306 goto fail;
308 if (ret == 0)
309 goto finish;
311 if (u->rtpoll_item) {
312 struct pollfd* pollfd;
314 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
316 if (pollfd->revents & ~POLLOUT) {
317 pa_log("FIFO shutdown.");
318 goto fail;
323 fail:
324 /* If this was no regular exit from the loop we have to continue
325 * processing messages until we received PA_MESSAGE_SHUTDOWN */
326 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
327 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
329 finish:
330 pa_log_debug("Thread shutting down");
333 static int do_write(struct userdata *u) {
334 ssize_t r;
335 pa_assert(u);
337 if (!pa_iochannel_is_writable(u->io))
338 return 0;
340 if (u->write_data) {
341 pa_assert(u->write_index < u->write_length);
343 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) {
344 pa_log("write() failed: %s", pa_cstrerror(errno));
345 return -1;
348 u->write_index += (size_t) r;
349 pa_assert(u->write_index <= u->write_length);
351 if (u->write_index == u->write_length) {
352 pa_xfree(u->write_data);
353 u->write_data = NULL;
354 u->write_index = u->write_length = 0;
358 if (!u->write_data && u->state == STATE_PREPARE) {
359 int so_sndbuf = 0;
360 socklen_t sl = sizeof(int);
362 /* OK, we're done with sending all control data we need to, so
363 * let's hand the socket over to the IO thread now */
365 pa_assert(u->fd < 0);
366 u->fd = pa_iochannel_get_send_fd(u->io);
368 pa_iochannel_set_noclose(u->io, TRUE);
369 pa_iochannel_free(u->io);
370 u->io = NULL;
372 pa_make_tcp_socket_low_delay(u->fd);
374 if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, &so_sndbuf, &sl) < 0)
375 pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno));
376 else {
377 pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf);
378 pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size));
381 pa_log_debug("Connection authenticated, handing fd to IO thread...");
383 pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
384 u->state = STATE_RUNNING;
387 return 0;
390 static int handle_response(struct userdata *u) {
391 pa_assert(u);
393 switch (u->state) {
395 case STATE_AUTH:
396 pa_assert(u->read_length == sizeof(int32_t));
398 /* Process auth data */
399 if (!*(int32_t*) u->read_data) {
400 pa_log("Authentication failed: %s", pa_cstrerror(errno));
401 return -1;
404 /* Request latency data */
405 pa_assert(!u->write_data);
406 *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
408 u->write_index = 0;
409 u->state = STATE_LATENCY;
411 /* Space for next response */
412 pa_assert(u->read_length >= sizeof(int32_t));
413 u->read_index = 0;
414 u->read_length = sizeof(int32_t);
416 break;
418 case STATE_LATENCY: {
419 int32_t *p;
420 pa_assert(u->read_length == sizeof(int32_t));
422 /* Process latency info */
423 u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
424 if (u->latency > 10000000) {
425 pa_log_warn("Invalid latency information received from server");
426 u->latency = 0;
429 /* Create stream */
430 pa_assert(!u->write_data);
431 p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
432 *(p++) = ESD_PROTO_STREAM_PLAY;
433 *(p++) = u->format;
434 *(p++) = u->rate;
435 pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
437 u->write_index = 0;
438 u->state = STATE_PREPARE;
440 /* Don't read any further */
441 pa_xfree(u->read_data);
442 u->read_data = NULL;
443 u->read_index = u->read_length = 0;
445 break;
448 default:
449 pa_assert_not_reached();
452 return 0;
455 static int do_read(struct userdata *u) {
456 pa_assert(u);
458 if (!pa_iochannel_is_readable(u->io))
459 return 0;
461 if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
462 ssize_t r;
464 if (!u->read_data)
465 return 0;
467 pa_assert(u->read_index < u->read_length);
469 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
470 pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
471 return -1;
474 u->read_index += (size_t) r;
475 pa_assert(u->read_index <= u->read_length);
477 if (u->read_index == u->read_length)
478 return handle_response(u);
481 return 0;
484 static void io_callback(pa_iochannel *io, void*userdata) {
485 struct userdata *u = userdata;
486 pa_assert(u);
488 if (do_read(u) < 0 || do_write(u) < 0) {
490 if (u->io) {
491 pa_iochannel_free(u->io);
492 u->io = NULL;
495 pa_module_unload_request(u->module, TRUE);
499 static void on_connection(pa_socket_client *c, pa_iochannel*io, void *userdata) {
500 struct userdata *u = userdata;
502 pa_socket_client_unref(u->client);
503 u->client = NULL;
505 if (!io) {
506 pa_log("Connection failed: %s", pa_cstrerror(errno));
507 pa_module_unload_request(u->module, TRUE);
508 return;
511 pa_assert(!u->io);
512 u->io = io;
513 pa_iochannel_set_callback(u->io, io_callback, u);
515 pa_log_debug("Connection established, authenticating ...");
518 int pa__init(pa_module*m) {
519 struct userdata *u = NULL;
520 pa_sample_spec ss;
521 pa_modargs *ma = NULL;
522 const char *espeaker;
523 uint32_t key;
524 pa_sink_new_data data;
526 pa_assert(m);
528 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
529 pa_log("failed to parse module arguments");
530 goto fail;
533 ss = m->core->default_sample_spec;
534 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
535 pa_log("invalid sample format specification");
536 goto fail;
539 if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
540 (ss.channels > 2)) {
541 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
542 goto fail;
545 u = pa_xnew0(struct userdata, 1);
546 u->core = m->core;
547 u->module = m;
548 m->userdata = u;
549 u->fd = -1;
550 u->smoother = pa_smoother_new(
551 PA_USEC_PER_SEC,
552 PA_USEC_PER_SEC*2,
553 TRUE,
554 TRUE,
557 FALSE);
558 pa_memchunk_reset(&u->memchunk);
559 u->offset = 0;
561 u->rtpoll = pa_rtpoll_new();
562 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
563 u->rtpoll_item = NULL;
565 u->format =
566 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
567 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);
568 u->rate = (int32_t) ss.rate;
569 u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
571 u->read_data = u->write_data = NULL;
572 u->read_index = u->write_index = u->read_length = u->write_length = 0;
574 u->state = STATE_AUTH;
575 u->latency = 0;
577 if (!(espeaker = getenv("ESPEAKER")))
578 espeaker = ESD_UNIX_SOCKET_NAME;
580 espeaker = pa_modargs_get_value(ma, "server", espeaker);
582 pa_sink_new_data_init(&data);
583 data.driver = __FILE__;
584 data.module = m;
585 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
586 pa_sink_new_data_set_sample_spec(&data, &ss);
587 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, espeaker);
588 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "esd");
589 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "EsounD Output on %s", espeaker);
591 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
592 pa_log("Invalid properties");
593 pa_sink_new_data_done(&data);
594 goto fail;
597 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
598 pa_sink_new_data_done(&data);
600 if (!u->sink) {
601 pa_log("Failed to create sink.");
602 goto fail;
605 u->sink->parent.process_msg = sink_process_msg;
606 u->sink->userdata = u;
608 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
609 pa_sink_set_rtpoll(u->sink, u->rtpoll);
611 if (!(u->client = pa_socket_client_new_string(u->core->mainloop, TRUE, espeaker, ESD_DEFAULT_PORT))) {
612 pa_log("Failed to connect to server.");
613 goto fail;
616 pa_socket_client_set_callback(u->client, on_connection, u);
618 /* Prepare the initial request */
619 u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
620 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) {
621 pa_log("Failed to load cookie");
622 goto fail;
625 key = ESD_ENDIAN_KEY;
626 memcpy((uint8_t*) u->write_data + ESD_KEY_LEN, &key, sizeof(key));
628 /* Reserve space for the response */
629 u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
631 if (!(u->thread = pa_thread_new(thread_func, u))) {
632 pa_log("Failed to create thread.");
633 goto fail;
636 pa_sink_put(u->sink);
638 pa_modargs_free(ma);
640 return 0;
642 fail:
643 if (ma)
644 pa_modargs_free(ma);
646 pa__done(m);
648 return -1;
651 int pa__get_n_used(pa_module *m) {
652 struct userdata *u;
654 pa_assert(m);
655 pa_assert_se(u = m->userdata);
657 return pa_sink_linked_by(u->sink);
660 void pa__done(pa_module*m) {
661 struct userdata *u;
662 pa_assert(m);
664 if (!(u = m->userdata))
665 return;
667 if (u->sink)
668 pa_sink_unlink(u->sink);
670 if (u->thread) {
671 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
672 pa_thread_free(u->thread);
675 pa_thread_mq_done(&u->thread_mq);
677 if (u->sink)
678 pa_sink_unref(u->sink);
680 if (u->io)
681 pa_iochannel_free(u->io);
683 if (u->rtpoll_item)
684 pa_rtpoll_item_free(u->rtpoll_item);
686 if (u->rtpoll)
687 pa_rtpoll_free(u->rtpoll);
689 if (u->memchunk.memblock)
690 pa_memblock_unref(u->memchunk.memblock);
692 if (u->client)
693 pa_socket_client_unref(u->client);
695 pa_xfree(u->read_data);
696 pa_xfree(u->write_data);
698 if (u->smoother)
699 pa_smoother_free(u->smoother);
701 if (u->fd >= 0)
702 pa_close(u->fd);
704 pa_xfree(u);