| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | /* $Id$ */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /***
 | 
					
						
							| 
									
										
										
										
											2006-06-19 21:53:48 +00:00
										 |  |  |   This file is part of PulseAudio. | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-13 15:35:19 +00:00
										 |  |  |   Copyright 2004-2006 Lennart Poettering | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-19 21:53:48 +00:00
										 |  |  |   PulseAudio is free software; you can redistribute it and/or modify | 
					
						
							| 
									
										
										
										
											2004-11-14 14:58:54 +00:00
										 |  |  |   it under the terms of the GNU Lesser General Public License as published | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |   by the Free Software Foundation; either version 2 of the License, | 
					
						
							|  |  |  |   or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-19 21:53:48 +00:00
										 |  |  |   PulseAudio is distributed in the hope that it will be useful, but | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |   WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 
					
						
							|  |  |  |   General Public License for more details. | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 14:58:54 +00:00
										 |  |  |   You should have received a copy of the GNU Lesser General Public License | 
					
						
							| 
									
										
										
										
											2006-06-19 21:53:48 +00:00
										 |  |  |   along with PulseAudio; if not, write to the Free Software | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | 
					
						
							|  |  |  |   USA. | 
					
						
							|  |  |  | ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_CONFIG_H
 | 
					
						
							|  |  |  | #include <config.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <limits.h>
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | #include <poll.h>
 | 
					
						
							|  |  |  | #include <sys/socket.h>
 | 
					
						
							|  |  |  | #include <netinet/in.h>
 | 
					
						
							|  |  |  | #include <netinet/tcp.h>
 | 
					
						
							|  |  |  | #include <sys/ioctl.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_LINUX_SOCKIOS_H
 | 
					
						
							|  |  |  | #include <linux/sockios.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-19 21:53:48 +00:00
										 |  |  | #include <pulse/xmalloc.h>
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | #include <pulse/timeval.h>
 | 
					
						
							| 
									
										
										
										
											2006-05-17 16:34:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-19 21:53:48 +00:00
										 |  |  | #include <pulsecore/core-error.h>
 | 
					
						
							|  |  |  | #include <pulsecore/iochannel.h>
 | 
					
						
							|  |  |  | #include <pulsecore/sink.h>
 | 
					
						
							|  |  |  | #include <pulsecore/module.h>
 | 
					
						
							|  |  |  | #include <pulsecore/core-util.h>
 | 
					
						
							|  |  |  | #include <pulsecore/modargs.h>
 | 
					
						
							|  |  |  | #include <pulsecore/log.h>
 | 
					
						
							|  |  |  | #include <pulsecore/socket-client.h>
 | 
					
						
							|  |  |  | #include <pulsecore/esound.h>
 | 
					
						
							|  |  |  | #include <pulsecore/authkey.h>
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | #include <pulsecore/thread-mq.h>
 | 
					
						
							|  |  |  | #include <pulsecore/thread.h>
 | 
					
						
							|  |  |  | #include <pulsecore/time-smoother.h>
 | 
					
						
							|  |  |  | #include <pulsecore/rtclock.h>
 | 
					
						
							|  |  |  | #include <pulsecore/socket-util.h>
 | 
					
						
							| 
									
										
										
										
											2006-02-16 19:19:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | #include "module-esound-sink-symdef.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-09 18:25:40 +00:00
										 |  |  | PA_MODULE_AUTHOR("Lennart Poettering"); | 
					
						
							|  |  |  | PA_MODULE_DESCRIPTION("ESOUND Sink"); | 
					
						
							|  |  |  | PA_MODULE_VERSION(PACKAGE_VERSION); | 
					
						
							|  |  |  | PA_MODULE_LOAD_ONCE(FALSE); | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | PA_MODULE_USAGE( | 
					
						
							|  |  |  |         "sink_name=<name for the sink> " | 
					
						
							|  |  |  |         "server=<address> cookie=<filename>  " | 
					
						
							|  |  |  |         "format=<sample format> " | 
					
						
							|  |  |  |         "channels=<number of channels> " | 
					
						
							| 
									
										
										
										
											2007-11-09 18:25:40 +00:00
										 |  |  |         "rate=<sample rate>"); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | #define DEFAULT_SINK_NAME "esound_out"
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct userdata { | 
					
						
							| 
									
										
										
										
											2006-01-11 01:17:39 +00:00
										 |  |  |     pa_core *core; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_module *module; | 
					
						
							| 
									
										
										
										
											2006-01-11 01:17:39 +00:00
										 |  |  |     pa_sink *sink; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_thread_mq thread_mq; | 
					
						
							|  |  |  |     pa_rtpoll *rtpoll; | 
					
						
							|  |  |  |     pa_rtpoll_item *rtpoll_item; | 
					
						
							|  |  |  |     pa_thread *thread; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-11 01:17:39 +00:00
										 |  |  |     pa_memchunk memchunk; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void *write_data; | 
					
						
							|  |  |  |     size_t write_length, write_index; | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     void *read_data; | 
					
						
							|  |  |  |     size_t read_length, read_index; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     enum { | 
					
						
							|  |  |  |         STATE_AUTH, | 
					
						
							|  |  |  |         STATE_LATENCY, | 
					
						
							|  |  |  |         STATE_PREPARE, | 
					
						
							|  |  |  |         STATE_RUNNING, | 
					
						
							|  |  |  |         STATE_DEAD | 
					
						
							|  |  |  |     } state; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pa_usec_t latency; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     esd_format_t format; | 
					
						
							|  |  |  |     int32_t rate; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pa_smoother *smoother; | 
					
						
							|  |  |  |     int fd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int64_t offset; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_iochannel *io; | 
					
						
							|  |  |  |     pa_socket_client *client; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t block_size; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const char* const valid_modargs[] = { | 
					
						
							|  |  |  |     "server", | 
					
						
							|  |  |  |     "cookie", | 
					
						
							|  |  |  |     "rate", | 
					
						
							|  |  |  |     "format", | 
					
						
							|  |  |  |     "channels", | 
					
						
							|  |  |  |     "sink_name", | 
					
						
							|  |  |  |     NULL | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | enum { | 
					
						
							|  |  |  |     SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { | 
					
						
							|  |  |  |     struct userdata *u = PA_SINK(o)->userdata; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     switch (code) { | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         case PA_SINK_MESSAGE_SET_STATE: | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 case PA_SINK_SUSPENDED: | 
					
						
							| 
									
										
										
										
											2008-05-15 23:34:41 +00:00
										 |  |  |                     pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state)); | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     pa_smoother_pause(u->smoother, pa_rtclock_usec()); | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 case PA_SINK_IDLE: | 
					
						
							|  |  |  |                 case PA_SINK_RUNNING: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (u->sink->thread_info.state == PA_SINK_SUSPENDED) | 
					
						
							|  |  |  |                         pa_smoother_resume(u->smoother, pa_rtclock_usec()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 case PA_SINK_UNLINKED: | 
					
						
							|  |  |  |                 case PA_SINK_INIT: | 
					
						
							|  |  |  |                     ; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case PA_SINK_MESSAGE_GET_LATENCY: { | 
					
						
							|  |  |  |             pa_usec_t w, r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             r = pa_smoother_get(u->smoother, pa_rtclock_usec()); | 
					
						
							|  |  |  |             w = pa_bytes_to_usec(u->offset + u->memchunk.length, &u->sink->sample_spec); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             *((pa_usec_t*) data) = w > r ? w - r : 0; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case SINK_MESSAGE_PASS_SOCKET: { | 
					
						
							|  |  |  |             struct pollfd *pollfd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             pa_assert(!u->rtpoll_item); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); | 
					
						
							|  |  |  |             pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); | 
					
						
							|  |  |  |             pollfd->fd = u->fd; | 
					
						
							|  |  |  |             pollfd->events = pollfd->revents = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     return pa_sink_process_msg(o, code, data, offset, chunk); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void thread_func(void *userdata) { | 
					
						
							|  |  |  |     struct userdata *u = userdata; | 
					
						
							|  |  |  |     int write_type = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_assert(u); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_log_debug("Thread starting up"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_thread_mq_install(&u->thread_mq); | 
					
						
							|  |  |  |     pa_rtpoll_install(u->rtpoll); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (u->rtpoll_item) { | 
					
						
							|  |  |  |             struct pollfd *pollfd; | 
					
						
							|  |  |  |             pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /* Render some data and write it to the fifo */ | 
					
						
							| 
									
										
										
										
											2008-05-15 23:34:41 +00:00
										 |  |  |             if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && pollfd->revents) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |                 pa_usec_t usec; | 
					
						
							|  |  |  |                 int64_t n; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 for (;;) { | 
					
						
							|  |  |  |                     ssize_t l; | 
					
						
							|  |  |  |                     void *p; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (u->memchunk.length <= 0) | 
					
						
							|  |  |  |                         pa_sink_render(u->sink, u->block_size, &u->memchunk); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     pa_assert(u->memchunk.length > 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     p = pa_memblock_acquire(u->memchunk.memblock); | 
					
						
							|  |  |  |                     l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type); | 
					
						
							|  |  |  |                     pa_memblock_release(u->memchunk.memblock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     pa_assert(l != 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (l < 0) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         if (errno == EINTR) | 
					
						
							|  |  |  |                             continue; | 
					
						
							|  |  |  |                         else if (errno == EAGAIN) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             /* OK, we filled all socket buffers up
 | 
					
						
							|  |  |  |                              * now. */ | 
					
						
							|  |  |  |                             goto filled_up; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         } else { | 
					
						
							|  |  |  |                             pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno)); | 
					
						
							|  |  |  |                             goto fail; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         u->offset += l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         u->memchunk.index += l; | 
					
						
							|  |  |  |                         u->memchunk.length -= l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         if (u->memchunk.length <= 0) { | 
					
						
							|  |  |  |                             pa_memblock_unref(u->memchunk.memblock); | 
					
						
							|  |  |  |                             pa_memchunk_reset(&u->memchunk); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         pollfd->revents = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         if (u->memchunk.length > 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             /* OK, we wrote less that we asked for,
 | 
					
						
							|  |  |  |                              * hence we can assume that the socket | 
					
						
							|  |  |  |                              * buffers are full now */ | 
					
						
							|  |  |  |                             goto filled_up; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             filled_up: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 /* At this spot we know that the socket buffers are
 | 
					
						
							|  |  |  |                  * fully filled up. This is the best time to estimate | 
					
						
							|  |  |  |                  * the playback position of the server */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 n = u->offset; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef SIOCOUTQ
 | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     int l; | 
					
						
							|  |  |  |                     if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0) | 
					
						
							|  |  |  |                         n -= l; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 usec = pa_bytes_to_usec(n, &u->sink->sample_spec); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (usec > u->latency) | 
					
						
							|  |  |  |                     usec -= u->latency; | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                     usec = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 pa_smoother_put(u->smoother, pa_rtclock_usec(), usec); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /* Hmm, nothing to do. Let's sleep */ | 
					
						
							| 
									
										
										
										
											2008-05-15 23:34:41 +00:00
										 |  |  |             pollfd->events = PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) | 
					
						
							|  |  |  |             goto fail; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ret == 0) | 
					
						
							|  |  |  |             goto finish; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (u->rtpoll_item) { | 
					
						
							|  |  |  |             struct pollfd* pollfd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (pollfd->revents & ~POLLOUT) { | 
					
						
							|  |  |  |                 pa_log("FIFO shutdown."); | 
					
						
							|  |  |  |                 goto fail; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | fail: | 
					
						
							|  |  |  |     /* If this was no regular exit from the loop we have to continue
 | 
					
						
							|  |  |  |      * processing messages until we received PA_MESSAGE_SHUTDOWN */ | 
					
						
							|  |  |  |     pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); | 
					
						
							|  |  |  |     pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | finish: | 
					
						
							|  |  |  |     pa_log_debug("Thread shutting down"); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int do_write(struct userdata *u) { | 
					
						
							|  |  |  |     ssize_t r; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_assert(u); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!pa_iochannel_is_writable(u->io)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (u->write_data) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_assert(u->write_index < u->write_length); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) <= 0) { | 
					
						
							| 
									
										
										
										
											2006-08-18 21:38:40 +00:00
										 |  |  |             pa_log("write() failed: %s", pa_cstrerror(errno)); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         u->write_index += r; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_assert(u->write_index <= u->write_length); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         if (u->write_index == u->write_length) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             pa_xfree(u->write_data); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |             u->write_data = NULL; | 
					
						
							|  |  |  |             u->write_index = u->write_length = 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     if (!u->write_data && u->state == STATE_PREPARE) { | 
					
						
							|  |  |  |         /* OK, we're done with sending all control data we need to, so
 | 
					
						
							|  |  |  |          * let's hand the socket over to the IO thread now */ | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_assert(u->fd < 0); | 
					
						
							|  |  |  |         u->fd = pa_iochannel_get_send_fd(u->io); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_iochannel_set_noclose(u->io, TRUE); | 
					
						
							|  |  |  |         pa_iochannel_free(u->io); | 
					
						
							|  |  |  |         u->io = NULL; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_make_tcp_socket_low_delay(u->fd); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-29 16:39:23 +00:00
										 |  |  |         pa_log_debug("Connection authenticated, handing fd to IO thread..."); | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL); | 
					
						
							|  |  |  |         u->state = STATE_RUNNING; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int handle_response(struct userdata *u) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_assert(u); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     switch (u->state) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         case STATE_AUTH: | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             pa_assert(u->read_length == sizeof(int32_t)); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             /* Process auth data */ | 
					
						
							|  |  |  |             if (!*(int32_t*) u->read_data) { | 
					
						
							| 
									
										
										
										
											2006-08-18 21:38:40 +00:00
										 |  |  |                 pa_log("Authentication failed: %s", pa_cstrerror(errno)); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /* Request latency data */ | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             pa_assert(!u->write_data); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |             *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             u->write_index = 0; | 
					
						
							|  |  |  |             u->state = STATE_LATENCY; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /* Space for next response */ | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             pa_assert(u->read_length >= sizeof(int32_t)); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |             u->read_index = 0; | 
					
						
							|  |  |  |             u->read_length = sizeof(int32_t); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case STATE_LATENCY: { | 
					
						
							|  |  |  |             int32_t *p; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             pa_assert(u->read_length == sizeof(int32_t)); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             /* Process latency info */ | 
					
						
							|  |  |  |             u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100); | 
					
						
							|  |  |  |             if (u->latency > 10000000) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |                 pa_log_warn("Invalid latency information received from server"); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |                 u->latency = 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /* Create stream */ | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             pa_assert(!u->write_data); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |             p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX); | 
					
						
							|  |  |  |             *(p++) = ESD_PROTO_STREAM_PLAY; | 
					
						
							|  |  |  |             *(p++) = u->format; | 
					
						
							|  |  |  |             *(p++) = u->rate; | 
					
						
							| 
									
										
										
										
											2006-06-19 22:11:49 +00:00
										 |  |  |             pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             u->write_index = 0; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             u->state = STATE_PREPARE; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             /* Don't read any further */ | 
					
						
							|  |  |  |             pa_xfree(u->read_data); | 
					
						
							|  |  |  |             u->read_data = NULL; | 
					
						
							|  |  |  |             u->read_index = u->read_length = 0; | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |             pa_assert_not_reached(); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int do_read(struct userdata *u) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_assert(u); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     if (!pa_iochannel_is_readable(u->io)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     if (u->state == STATE_AUTH || u->state == STATE_LATENCY) { | 
					
						
							|  |  |  |         ssize_t r; | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         if (!u->read_data) | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_assert(u->read_index < u->read_length); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) { | 
					
						
							| 
									
										
										
										
											2006-08-18 21:38:40 +00:00
										 |  |  |             pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF"); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         u->read_index += r; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_assert(u->read_index <= u->read_length); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (u->read_index == u->read_length) | 
					
						
							|  |  |  |             return handle_response(u); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) { | 
					
						
							|  |  |  |     struct userdata *u = userdata; | 
					
						
							|  |  |  |     pa_assert(u); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     if (do_read(u) < 0 || do_write(u) < 0) { | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         if (u->io) { | 
					
						
							|  |  |  |             pa_iochannel_free(u->io); | 
					
						
							|  |  |  |             u->io = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |        pa_module_unload_request(u->module); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-11 01:17:39 +00:00
										 |  |  | static void on_connection(PA_GCC_UNUSED pa_socket_client *c, pa_iochannel*io, void *userdata) { | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     struct userdata *u = userdata; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_socket_client_unref(u->client); | 
					
						
							|  |  |  |     u->client = NULL; | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     if (!io) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_log("Connection failed: %s", pa_cstrerror(errno)); | 
					
						
							|  |  |  |         pa_module_unload_request(u->module); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_assert(!u->io); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     u->io = io; | 
					
						
							|  |  |  |     pa_iochannel_set_callback(u->io, io_callback, u); | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-29 16:39:23 +00:00
										 |  |  |     pa_log_debug("Connection established, authenticating ..."); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | int pa__init(pa_module*m) { | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     struct userdata *u = NULL; | 
					
						
							| 
									
										
										
										
											2006-01-11 01:17:39 +00:00
										 |  |  |     pa_sample_spec ss; | 
					
						
							|  |  |  |     pa_modargs *ma = NULL; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     const char *espeaker; | 
					
						
							| 
									
										
										
										
											2007-11-04 16:51:26 +00:00
										 |  |  |     uint32_t key; | 
					
						
							| 
									
										
										
										
											2008-05-15 23:34:41 +00:00
										 |  |  |     pa_sink_new_data data; | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_assert(m); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { | 
					
						
							| 
									
										
										
										
											2006-08-18 21:38:40 +00:00
										 |  |  |         pa_log("failed to parse module arguments"); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     ss = m->core->default_sample_spec; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     if (pa_modargs_get_sample_spec(ma, &ss) < 0) { | 
					
						
							| 
									
										
										
										
											2006-08-18 21:38:40 +00:00
										 |  |  |         pa_log("invalid sample format specification"); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) || | 
					
						
							|  |  |  |         (ss.channels > 2)) { | 
					
						
							| 
									
										
										
										
											2006-08-18 21:38:40 +00:00
										 |  |  |         pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data"); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     u = pa_xnew0(struct userdata, 1); | 
					
						
							|  |  |  |     u->core = m->core; | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     u->module = m; | 
					
						
							|  |  |  |     m->userdata = u; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     u->fd = -1; | 
					
						
							| 
									
										
										
										
											2008-05-15 23:34:41 +00:00
										 |  |  |     u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10); | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_memchunk_reset(&u->memchunk); | 
					
						
							|  |  |  |     u->offset = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u->rtpoll = pa_rtpoll_new(); | 
					
						
							| 
									
										
										
										
											2008-05-15 23:34:41 +00:00
										 |  |  |     pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     u->rtpoll_item = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     u->format = | 
					
						
							|  |  |  |         (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) | | 
					
						
							|  |  |  |         (ss.channels == 2 ? ESD_STEREO : ESD_MONO); | 
					
						
							|  |  |  |     u->rate = ss.rate; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     u->read_data = u->write_data = NULL; | 
					
						
							|  |  |  |     u->read_index = u->write_index = u->read_length = u->write_length = 0; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     u->state = STATE_AUTH; | 
					
						
							|  |  |  |     u->latency = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-15 23:34:41 +00:00
										 |  |  |     if (!(espeaker = getenv("ESPEAKER"))) | 
					
						
							|  |  |  |         espeaker = ESD_UNIX_SOCKET_NAME; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     espeaker = pa_modargs_get_value(ma, "server", espeaker); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_sink_new_data_init(&data); | 
					
						
							|  |  |  |     data.driver = __FILE__; | 
					
						
							|  |  |  |     data.module = m; | 
					
						
							|  |  |  |     pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); | 
					
						
							|  |  |  |     pa_sink_new_data_set_sample_spec(&data, &ss); | 
					
						
							|  |  |  |     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, espeaker); | 
					
						
							|  |  |  |     pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Esound sink '%s'", espeaker); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK); | 
					
						
							|  |  |  |     pa_sink_new_data_done(&data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!u->sink) { | 
					
						
							| 
									
										
										
										
											2007-10-29 16:39:23 +00:00
										 |  |  |         pa_log("Failed to create sink."); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     u->sink->parent.process_msg = sink_process_msg; | 
					
						
							|  |  |  |     u->sink->userdata = u; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); | 
					
						
							|  |  |  |     pa_sink_set_rtpoll(u->sink, u->rtpoll); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-15 23:34:41 +00:00
										 |  |  |     if (!(u->client = pa_socket_client_new_string(u->core->mainloop, espeaker, ESD_DEFAULT_PORT))) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_log("Failed to connect to server."); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     pa_socket_client_set_callback(u->client, on_connection, u); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Prepare the initial request */ | 
					
						
							|  |  |  |     u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t)); | 
					
						
							|  |  |  |     if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", ".esd_auth"), u->write_data, ESD_KEY_LEN) < 0) { | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |         pa_log("Failed to load cookie"); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-11-04 16:51:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     key = ESD_ENDIAN_KEY; | 
					
						
							|  |  |  |     memcpy((uint8_t*) u->write_data + ESD_KEY_LEN, &key, sizeof(key)); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Reserve space for the response */ | 
					
						
							|  |  |  |     u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t)); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     if (!(u->thread = pa_thread_new(thread_func, u))) { | 
					
						
							|  |  |  |         pa_log("Failed to create thread."); | 
					
						
							|  |  |  |         goto fail; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_sink_put(u->sink); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     pa_modargs_free(ma); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fail: | 
					
						
							|  |  |  |     if (ma) | 
					
						
							|  |  |  |         pa_modargs_free(ma); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa__done(m); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  | void pa__done(pa_module*m) { | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     struct userdata *u; | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_assert(m); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!(u = m->userdata)) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     if (u->sink) | 
					
						
							|  |  |  |         pa_sink_unlink(u->sink); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (u->thread) { | 
					
						
							|  |  |  |         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); | 
					
						
							|  |  |  |         pa_thread_free(u->thread); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pa_thread_mq_done(&u->thread_mq); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (u->sink) | 
					
						
							|  |  |  |         pa_sink_unref(u->sink); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (u->io) | 
					
						
							|  |  |  |         pa_iochannel_free(u->io); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (u->rtpoll_item) | 
					
						
							|  |  |  |         pa_rtpoll_item_free(u->rtpoll_item); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (u->rtpoll) | 
					
						
							|  |  |  |         pa_rtpoll_free(u->rtpoll); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     if (u->memchunk.memblock) | 
					
						
							|  |  |  |         pa_memblock_unref(u->memchunk.memblock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (u->client) | 
					
						
							|  |  |  |         pa_socket_client_unref(u->client); | 
					
						
							| 
									
										
										
										
											2007-01-04 13:43:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  |     pa_xfree(u->read_data); | 
					
						
							|  |  |  |     pa_xfree(u->write_data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     if (u->smoother) | 
					
						
							|  |  |  |         pa_smoother_free(u->smoother); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     if (u->fd >= 0) | 
					
						
							|  |  |  |         pa_close(u->fd); | 
					
						
							| 
									
										
										
										
											2004-11-14 02:36:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 19:13:50 +00:00
										 |  |  |     pa_xfree(u); | 
					
						
							|  |  |  | } |