Remove some unused vars and labels
[pulseaudio-mirror.git] / src / modules / module-esound-sink.c
blob14f1810a4a63f401f021a1df667471777fdb700c
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 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/xmalloc.h>
45 #include <pulse/timeval.h>
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/iochannel.h>
49 #include <pulsecore/sink.h>
50 #include <pulsecore/module.h>
51 #include <pulsecore/core-util.h>
52 #include <pulsecore/modargs.h>
53 #include <pulsecore/log.h>
54 #include <pulsecore/socket-client.h>
55 #include <pulsecore/esound.h>
56 #include <pulsecore/authkey.h>
57 #include <pulsecore/thread-mq.h>
58 #include <pulsecore/thread.h>
59 #include <pulsecore/time-smoother.h>
60 #include <pulsecore/rtclock.h>
61 #include <pulsecore/socket-util.h>
63 #include "module-esound-sink-symdef.h"
65 PA_MODULE_AUTHOR("Lennart Poettering");
66 PA_MODULE_DESCRIPTION("ESOUND Sink");
67 PA_MODULE_VERSION(PACKAGE_VERSION);
68 PA_MODULE_LOAD_ONCE(FALSE);
69 PA_MODULE_USAGE(
70 "sink_name=<name for the sink> "
71 "server=<address> cookie=<filename> "
72 "format=<sample format> "
73 "channels=<number of channels> "
74 "rate=<sample rate>");
76 #define DEFAULT_SINK_NAME "esound_out"
78 struct userdata {
79 pa_core *core;
80 pa_module *module;
81 pa_sink *sink;
83 pa_thread_mq thread_mq;
84 pa_rtpoll *rtpoll;
85 pa_rtpoll_item *rtpoll_item;
86 pa_thread *thread;
88 pa_memchunk memchunk;
90 void *write_data;
91 size_t write_length, write_index;
93 void *read_data;
94 size_t read_length, read_index;
96 enum {
97 STATE_AUTH,
98 STATE_LATENCY,
99 STATE_PREPARE,
100 STATE_RUNNING,
101 STATE_DEAD
102 } state;
104 pa_usec_t latency;
106 esd_format_t format;
107 int32_t rate;
109 pa_smoother *smoother;
110 int fd;
112 int64_t offset;
114 pa_iochannel *io;
115 pa_socket_client *client;
117 size_t block_size;
120 static const char* const valid_modargs[] = {
121 "server",
122 "cookie",
123 "rate",
124 "format",
125 "channels",
126 "sink_name",
127 NULL
130 enum {
131 SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX
134 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
135 struct userdata *u = PA_SINK(o)->userdata;
137 switch (code) {
139 case PA_SINK_MESSAGE_SET_STATE:
141 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
143 case PA_SINK_SUSPENDED:
144 pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
146 pa_smoother_pause(u->smoother, pa_rtclock_usec());
147 break;
149 case PA_SINK_IDLE:
150 case PA_SINK_RUNNING:
152 if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
153 pa_smoother_resume(u->smoother, pa_rtclock_usec());
155 break;
157 case PA_SINK_UNLINKED:
158 case PA_SINK_INIT:
162 break;
164 case PA_SINK_MESSAGE_GET_LATENCY: {
165 pa_usec_t w, r;
167 r = pa_smoother_get(u->smoother, pa_rtclock_usec());
168 w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec);
170 *((pa_usec_t*) data) = w > r ? w - r : 0;
171 break;
174 case SINK_MESSAGE_PASS_SOCKET: {
175 struct pollfd *pollfd;
177 pa_assert(!u->rtpoll_item);
179 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
180 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
181 pollfd->fd = u->fd;
182 pollfd->events = pollfd->revents = 0;
184 return 0;
188 return pa_sink_process_msg(o, code, data, offset, chunk);
191 static void thread_func(void *userdata) {
192 struct userdata *u = userdata;
193 int write_type = 0;
195 pa_assert(u);
197 pa_log_debug("Thread starting up");
199 pa_thread_mq_install(&u->thread_mq);
200 pa_rtpoll_install(u->rtpoll);
202 pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
204 for (;;) {
205 int ret;
207 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
208 if (u->sink->thread_info.rewind_requested)
209 pa_sink_process_rewind(u->sink, 0);
211 if (u->rtpoll_item) {
212 struct pollfd *pollfd;
213 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
215 /* Render some data and write it to the fifo */
216 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && pollfd->revents) {
217 pa_usec_t usec;
218 int64_t n;
220 for (;;) {
221 ssize_t l;
222 void *p;
224 if (u->memchunk.length <= 0)
225 pa_sink_render(u->sink, u->block_size, &u->memchunk);
227 pa_assert(u->memchunk.length > 0);
229 p = pa_memblock_acquire(u->memchunk.memblock);
230 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
231 pa_memblock_release(u->memchunk.memblock);
233 pa_assert(l != 0);
235 if (l < 0) {
237 if (errno == EINTR)
238 continue;
239 else if (errno == EAGAIN) {
241 /* OK, we filled all socket buffers up
242 * now. */
243 goto filled_up;
245 } else {
246 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
247 goto fail;
250 } else {
251 u->offset += l;
253 u->memchunk.index += (size_t) l;
254 u->memchunk.length -= (size_t) l;
256 if (u->memchunk.length <= 0) {
257 pa_memblock_unref(u->memchunk.memblock);
258 pa_memchunk_reset(&u->memchunk);
261 pollfd->revents = 0;
263 if (u->memchunk.length > 0)
265 /* OK, we wrote less that we asked for,
266 * hence we can assume that the socket
267 * buffers are full now */
268 goto filled_up;
272 filled_up:
274 /* At this spot we know that the socket buffers are
275 * fully filled up. This is the best time to estimate
276 * the playback position of the server */
278 n = u->offset;
280 #ifdef SIOCOUTQ
282 int l;
283 if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
284 n -= l;
286 #endif
288 usec = pa_bytes_to_usec((uint64_t) n, &u->sink->sample_spec);
290 if (usec > u->latency)
291 usec -= u->latency;
292 else
293 usec = 0;
295 pa_smoother_put(u->smoother, pa_rtclock_usec(), usec);
298 /* Hmm, nothing to do. Let's sleep */
299 pollfd->events = (short) (PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0);
302 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
303 goto fail;
305 if (ret == 0)
306 goto finish;
308 if (u->rtpoll_item) {
309 struct pollfd* pollfd;
311 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
313 if (pollfd->revents & ~POLLOUT) {
314 pa_log("FIFO shutdown.");
315 goto fail;
320 fail:
321 /* If this was no regular exit from the loop we have to continue
322 * processing messages until we received PA_MESSAGE_SHUTDOWN */
323 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
324 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
326 finish:
327 pa_log_debug("Thread shutting down");
330 static int do_write(struct userdata *u) {
331 ssize_t r;
332 pa_assert(u);
334 if (!pa_iochannel_is_writable(u->io))
335 return 0;
337 if (u->write_data) {
338 pa_assert(u->write_index < u->write_length);
340 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) {
341 pa_log("write() failed: %s", pa_cstrerror(errno));
342 return -1;
345 u->write_index += (size_t) r;
346 pa_assert(u->write_index <= u->write_length);
348 if (u->write_index == u->write_length) {
349 pa_xfree(u->write_data);
350 u->write_data = NULL;
351 u->write_index = u->write_length = 0;
355 if (!u->write_data && u->state == STATE_PREPARE) {
356 /* OK, we're done with sending all control data we need to, so
357 * let's hand the socket over to the IO thread now */
359 pa_assert(u->fd < 0);
360 u->fd = pa_iochannel_get_send_fd(u->io);
362 pa_iochannel_set_noclose(u->io, TRUE);
363 pa_iochannel_free(u->io);
364 u->io = NULL;
366 pa_make_tcp_socket_low_delay(u->fd);
368 pa_log_debug("Connection authenticated, handing fd to IO thread...");
370 pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
371 u->state = STATE_RUNNING;
374 return 0;
377 static int handle_response(struct userdata *u) {
378 pa_assert(u);
380 switch (u->state) {
382 case STATE_AUTH:
383 pa_assert(u->read_length == sizeof(int32_t));
385 /* Process auth data */
386 if (!*(int32_t*) u->read_data) {
387 pa_log("Authentication failed: %s", pa_cstrerror(errno));
388 return -1;
391 /* Request latency data */
392 pa_assert(!u->write_data);
393 *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
395 u->write_index = 0;
396 u->state = STATE_LATENCY;
398 /* Space for next response */
399 pa_assert(u->read_length >= sizeof(int32_t));
400 u->read_index = 0;
401 u->read_length = sizeof(int32_t);
403 break;
405 case STATE_LATENCY: {
406 int32_t *p;
407 pa_assert(u->read_length == sizeof(int32_t));
409 /* Process latency info */
410 u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
411 if (u->latency > 10000000) {
412 pa_log_warn("Invalid latency information received from server");
413 u->latency = 0;
416 /* Create stream */
417 pa_assert(!u->write_data);
418 p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
419 *(p++) = ESD_PROTO_STREAM_PLAY;
420 *(p++) = u->format;
421 *(p++) = u->rate;
422 pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
424 u->write_index = 0;
425 u->state = STATE_PREPARE;
427 /* Don't read any further */
428 pa_xfree(u->read_data);
429 u->read_data = NULL;
430 u->read_index = u->read_length = 0;
432 break;
435 default:
436 pa_assert_not_reached();
439 return 0;
442 static int do_read(struct userdata *u) {
443 pa_assert(u);
445 if (!pa_iochannel_is_readable(u->io))
446 return 0;
448 if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
449 ssize_t r;
451 if (!u->read_data)
452 return 0;
454 pa_assert(u->read_index < u->read_length);
456 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
457 pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
458 return -1;
461 u->read_index += (size_t) r;
462 pa_assert(u->read_index <= u->read_length);
464 if (u->read_index == u->read_length)
465 return handle_response(u);
468 return 0;
471 static void io_callback(pa_iochannel *io, void*userdata) {
472 struct userdata *u = userdata;
473 pa_assert(u);
475 if (do_read(u) < 0 || do_write(u) < 0) {
477 if (u->io) {
478 pa_iochannel_free(u->io);
479 u->io = NULL;
482 pa_module_unload_request(u->module, TRUE);
486 static void on_connection(pa_socket_client *c, pa_iochannel*io, void *userdata) {
487 struct userdata *u = userdata;
489 pa_socket_client_unref(u->client);
490 u->client = NULL;
492 if (!io) {
493 pa_log("Connection failed: %s", pa_cstrerror(errno));
494 pa_module_unload_request(u->module, TRUE);
495 return;
498 pa_assert(!u->io);
499 u->io = io;
500 pa_iochannel_set_callback(u->io, io_callback, u);
502 pa_log_debug("Connection established, authenticating ...");
505 int pa__init(pa_module*m) {
506 struct userdata *u = NULL;
507 pa_sample_spec ss;
508 pa_modargs *ma = NULL;
509 const char *espeaker;
510 uint32_t key;
511 pa_sink_new_data data;
513 pa_assert(m);
515 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
516 pa_log("failed to parse module arguments");
517 goto fail;
520 ss = m->core->default_sample_spec;
521 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
522 pa_log("invalid sample format specification");
523 goto fail;
526 if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
527 (ss.channels > 2)) {
528 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
529 goto fail;
532 u = pa_xnew0(struct userdata, 1);
533 u->core = m->core;
534 u->module = m;
535 m->userdata = u;
536 u->fd = -1;
537 u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10);
538 pa_memchunk_reset(&u->memchunk);
539 u->offset = 0;
541 u->rtpoll = pa_rtpoll_new();
542 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
543 u->rtpoll_item = NULL;
545 u->format =
546 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
547 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);
548 u->rate = (int32_t) ss.rate;
549 u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
551 u->read_data = u->write_data = NULL;
552 u->read_index = u->write_index = u->read_length = u->write_length = 0;
554 u->state = STATE_AUTH;
555 u->latency = 0;
557 if (!(espeaker = getenv("ESPEAKER")))
558 espeaker = ESD_UNIX_SOCKET_NAME;
560 espeaker = pa_modargs_get_value(ma, "server", espeaker);
562 pa_sink_new_data_init(&data);
563 data.driver = __FILE__;
564 data.module = m;
565 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
566 pa_sink_new_data_set_sample_spec(&data, &ss);
567 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, espeaker);
568 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Esound sink '%s'", espeaker);
570 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
571 pa_sink_new_data_done(&data);
573 if (!u->sink) {
574 pa_log("Failed to create sink.");
575 goto fail;
578 u->sink->parent.process_msg = sink_process_msg;
579 u->sink->userdata = u;
581 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
582 pa_sink_set_rtpoll(u->sink, u->rtpoll);
584 if (!(u->client = pa_socket_client_new_string(u->core->mainloop, espeaker, ESD_DEFAULT_PORT))) {
585 pa_log("Failed to connect to server.");
586 goto fail;
589 pa_socket_client_set_callback(u->client, on_connection, u);
591 /* Prepare the initial request */
592 u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
593 if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) {
594 pa_log("Failed to load cookie");
595 goto fail;
598 key = ESD_ENDIAN_KEY;
599 memcpy((uint8_t*) u->write_data + ESD_KEY_LEN, &key, sizeof(key));
601 /* Reserve space for the response */
602 u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
604 if (!(u->thread = pa_thread_new(thread_func, u))) {
605 pa_log("Failed to create thread.");
606 goto fail;
609 pa_sink_put(u->sink);
611 pa_modargs_free(ma);
613 return 0;
615 fail:
616 if (ma)
617 pa_modargs_free(ma);
619 pa__done(m);
621 return -1;
624 void pa__done(pa_module*m) {
625 struct userdata *u;
626 pa_assert(m);
628 if (!(u = m->userdata))
629 return;
631 if (u->sink)
632 pa_sink_unlink(u->sink);
634 if (u->thread) {
635 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
636 pa_thread_free(u->thread);
639 pa_thread_mq_done(&u->thread_mq);
641 if (u->sink)
642 pa_sink_unref(u->sink);
644 if (u->io)
645 pa_iochannel_free(u->io);
647 if (u->rtpoll_item)
648 pa_rtpoll_item_free(u->rtpoll_item);
650 if (u->rtpoll)
651 pa_rtpoll_free(u->rtpoll);
653 if (u->memchunk.memblock)
654 pa_memblock_unref(u->memchunk.memblock);
656 if (u->client)
657 pa_socket_client_unref(u->client);
659 pa_xfree(u->read_data);
660 pa_xfree(u->write_data);
662 if (u->smoother)
663 pa_smoother_free(u->smoother);
665 if (u->fd >= 0)
666 pa_close(u->fd);
668 pa_xfree(u);