4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <sys/ioctl.h>
42 #ifdef HAVE_LINUX_SOCKIOS_H
43 #include <linux/sockios.h>
46 #include <pulse/xmalloc.h>
47 #include <pulse/timeval.h>
49 #include <pulsecore/core-error.h>
50 #include <pulsecore/iochannel.h>
51 #include <pulsecore/sink.h>
52 #include <pulsecore/module.h>
53 #include <pulsecore/core-util.h>
54 #include <pulsecore/modargs.h>
55 #include <pulsecore/log.h>
56 #include <pulsecore/socket-client.h>
57 #include <pulsecore/esound.h>
58 #include <pulsecore/authkey.h>
59 #include <pulsecore/thread-mq.h>
60 #include <pulsecore/thread.h>
61 #include <pulsecore/time-smoother.h>
62 #include <pulsecore/rtclock.h>
63 #include <pulsecore/socket-util.h>
65 #include "module-esound-sink-symdef.h"
67 PA_MODULE_AUTHOR("Lennart Poettering");
68 PA_MODULE_DESCRIPTION("ESOUND Sink");
69 PA_MODULE_VERSION(PACKAGE_VERSION
);
70 PA_MODULE_LOAD_ONCE(FALSE
);
72 "sink_name=<name for the sink> "
73 "server=<address> cookie=<filename> "
74 "format=<sample format> "
75 "channels=<number of channels> "
76 "rate=<sample rate>");
78 #define DEFAULT_SINK_NAME "esound_out"
85 pa_thread_mq thread_mq
;
87 pa_rtpoll_item
*rtpoll_item
;
93 size_t write_length
, write_index
;
96 size_t read_length
, read_index
;
111 pa_smoother
*smoother
;
117 pa_socket_client
*client
;
122 static const char* const valid_modargs
[] = {
133 SINK_MESSAGE_PASS_SOCKET
= PA_SINK_MESSAGE_MAX
136 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
137 struct userdata
*u
= PA_SINK(o
)->userdata
;
141 case PA_SINK_MESSAGE_SET_STATE
:
143 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
145 case PA_SINK_SUSPENDED
:
146 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
148 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
152 case PA_SINK_RUNNING
:
154 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
)
155 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
159 case PA_SINK_UNLINKED
:
166 case PA_SINK_MESSAGE_GET_LATENCY
: {
169 r
= pa_smoother_get(u
->smoother
, pa_rtclock_usec());
170 w
= pa_bytes_to_usec(u
->offset
+ u
->memchunk
.length
, &u
->sink
->sample_spec
);
172 *((pa_usec_t
*) data
) = w
> r
? w
- r
: 0;
176 case SINK_MESSAGE_PASS_SOCKET
: {
177 struct pollfd
*pollfd
;
179 pa_assert(!u
->rtpoll_item
);
181 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
182 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
184 pollfd
->events
= pollfd
->revents
= 0;
190 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
193 static void thread_func(void *userdata
) {
194 struct userdata
*u
= userdata
;
199 pa_log_debug("Thread starting up");
201 pa_thread_mq_install(&u
->thread_mq
);
202 pa_rtpoll_install(u
->rtpoll
);
204 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
209 if (u
->rtpoll_item
) {
210 struct pollfd
*pollfd
;
211 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
213 /* Render some data and write it to the fifo */
214 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
) && pollfd
->revents
) {
222 if (u
->memchunk
.length
<= 0)
223 pa_sink_render(u
->sink
, u
->block_size
, &u
->memchunk
);
225 pa_assert(u
->memchunk
.length
> 0);
227 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
228 l
= pa_write(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &write_type
);
229 pa_memblock_release(u
->memchunk
.memblock
);
237 else if (errno
== EAGAIN
) {
239 /* OK, we filled all socket buffers up
244 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
251 u
->memchunk
.index
+= l
;
252 u
->memchunk
.length
-= l
;
254 if (u
->memchunk
.length
<= 0) {
255 pa_memblock_unref(u
->memchunk
.memblock
);
256 pa_memchunk_reset(&u
->memchunk
);
261 if (u
->memchunk
.length
> 0)
263 /* OK, we wrote less that we asked for,
264 * hence we can assume that the socket
265 * buffers are full now */
272 /* At this spot we know that the socket buffers are
273 * fully filled up. This is the best time to estimate
274 * the playback position of the server */
281 if (ioctl(u
->fd
, SIOCOUTQ
, &l
) >= 0 && l
> 0)
286 usec
= pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
288 if (usec
> u
->latency
)
293 pa_smoother_put(u
->smoother
, pa_rtclock_usec(), usec
);
296 /* Hmm, nothing to do. Let's sleep */
297 pollfd
->events
= PA_SINK_OPENED(u
->sink
->thread_info
.state
) ? POLLOUT
: 0;
300 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
306 if (u
->rtpoll_item
) {
307 struct pollfd
* pollfd
;
309 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
311 if (pollfd
->revents
& ~POLLOUT
) {
312 pa_log("FIFO shutdown.");
319 /* If this was no regular exit from the loop we have to continue
320 * processing messages until we received PA_MESSAGE_SHUTDOWN */
321 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
322 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
325 pa_log_debug("Thread shutting down");
328 static int do_write(struct userdata
*u
) {
332 if (!pa_iochannel_is_writable(u
->io
))
336 pa_assert(u
->write_index
< u
->write_length
);
338 if ((r
= pa_iochannel_write(u
->io
, (uint8_t*) u
->write_data
+ u
->write_index
, u
->write_length
- u
->write_index
)) <= 0) {
339 pa_log("write() failed: %s", pa_cstrerror(errno
));
344 pa_assert(u
->write_index
<= u
->write_length
);
346 if (u
->write_index
== u
->write_length
) {
347 pa_xfree(u
->write_data
);
348 u
->write_data
= NULL
;
349 u
->write_index
= u
->write_length
= 0;
353 if (!u
->write_data
&& u
->state
== STATE_PREPARE
) {
354 /* OK, we're done with sending all control data we need to, so
355 * let's hand the socket over to the IO thread now */
357 pa_assert(u
->fd
< 0);
358 u
->fd
= pa_iochannel_get_send_fd(u
->io
);
360 pa_iochannel_set_noclose(u
->io
, TRUE
);
361 pa_iochannel_free(u
->io
);
364 pa_make_tcp_socket_low_delay(u
->fd
);
366 pa_log_debug("Connection authenticated, handing fd to IO thread...");
368 pa_asyncmsgq_post(u
->thread_mq
.inq
, PA_MSGOBJECT(u
->sink
), SINK_MESSAGE_PASS_SOCKET
, NULL
, 0, NULL
, NULL
);
369 u
->state
= STATE_RUNNING
;
375 static int handle_response(struct userdata
*u
) {
381 pa_assert(u
->read_length
== sizeof(int32_t));
383 /* Process auth data */
384 if (!*(int32_t*) u
->read_data
) {
385 pa_log("Authentication failed: %s", pa_cstrerror(errno
));
389 /* Request latency data */
390 pa_assert(!u
->write_data
);
391 *(int32_t*) (u
->write_data
= pa_xmalloc(u
->write_length
= sizeof(int32_t))) = ESD_PROTO_LATENCY
;
394 u
->state
= STATE_LATENCY
;
396 /* Space for next response */
397 pa_assert(u
->read_length
>= sizeof(int32_t));
399 u
->read_length
= sizeof(int32_t);
403 case STATE_LATENCY
: {
405 pa_assert(u
->read_length
== sizeof(int32_t));
407 /* Process latency info */
408 u
->latency
= (pa_usec_t
) ((double) (*(int32_t*) u
->read_data
) * 1000000 / 44100);
409 if (u
->latency
> 10000000) {
410 pa_log_warn("Invalid latency information received from server");
415 pa_assert(!u
->write_data
);
416 p
= u
->write_data
= pa_xmalloc0(u
->write_length
= sizeof(int32_t)*3+ESD_NAME_MAX
);
417 *(p
++) = ESD_PROTO_STREAM_PLAY
;
420 pa_strlcpy((char*) p
, "PulseAudio Tunnel", ESD_NAME_MAX
);
423 u
->state
= STATE_PREPARE
;
425 /* Don't read any further */
426 pa_xfree(u
->read_data
);
428 u
->read_index
= u
->read_length
= 0;
434 pa_assert_not_reached();
440 static int do_read(struct userdata
*u
) {
443 if (!pa_iochannel_is_readable(u
->io
))
446 if (u
->state
== STATE_AUTH
|| u
->state
== STATE_LATENCY
) {
452 pa_assert(u
->read_index
< u
->read_length
);
454 if ((r
= pa_iochannel_read(u
->io
, (uint8_t*) u
->read_data
+ u
->read_index
, u
->read_length
- u
->read_index
)) <= 0) {
455 pa_log("read() failed: %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
460 pa_assert(u
->read_index
<= u
->read_length
);
462 if (u
->read_index
== u
->read_length
)
463 return handle_response(u
);
469 static void io_callback(PA_GCC_UNUSED pa_iochannel
*io
, void*userdata
) {
470 struct userdata
*u
= userdata
;
473 if (do_read(u
) < 0 || do_write(u
) < 0) {
476 pa_iochannel_free(u
->io
);
480 pa_module_unload_request(u
->module
);
484 static void on_connection(PA_GCC_UNUSED pa_socket_client
*c
, pa_iochannel
*io
, void *userdata
) {
485 struct userdata
*u
= userdata
;
487 pa_socket_client_unref(u
->client
);
491 pa_log("Connection failed: %s", pa_cstrerror(errno
));
492 pa_module_unload_request(u
->module
);
498 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
500 pa_log_debug("Connection established, authenticating ...");
503 int pa__init(pa_module
*m
) {
504 struct userdata
*u
= NULL
;
507 pa_modargs
*ma
= NULL
;
509 const char *espeaker
;
514 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
515 pa_log("failed to parse module arguments");
519 ss
= m
->core
->default_sample_spec
;
520 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
521 pa_log("invalid sample format specification");
525 if ((ss
.format
!= PA_SAMPLE_U8
&& ss
.format
!= PA_SAMPLE_S16NE
) ||
527 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
531 u
= pa_xnew0(struct userdata
, 1);
536 u
->smoother
= pa_smoother_new(PA_USEC_PER_SEC
, PA_USEC_PER_SEC
*2, TRUE
);
537 pa_memchunk_reset(&u
->memchunk
);
540 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
541 u
->rtpoll
= pa_rtpoll_new();
542 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
543 u
->rtpoll_item
= NULL
;
546 (ss
.format
== PA_SAMPLE_U8
? ESD_BITS8
: ESD_BITS16
) |
547 (ss
.channels
== 2 ? ESD_STEREO
: ESD_MONO
);
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
;
557 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, NULL
))) {
558 pa_log("Failed to create sink.");
562 u
->sink
->parent
.process_msg
= sink_process_msg
;
563 u
->sink
->userdata
= u
;
564 u
->sink
->flags
= PA_SINK_LATENCY
|PA_SINK_NETWORK
;
566 pa_sink_set_module(u
->sink
, m
);
567 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
568 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
570 if (!(espeaker
= getenv("ESPEAKER")))
571 espeaker
= ESD_UNIX_SOCKET_NAME
;
573 if (!(u
->client
= pa_socket_client_new_string(u
->core
->mainloop
, p
= pa_modargs_get_value(ma
, "server", espeaker
), ESD_DEFAULT_PORT
))) {
574 pa_log("Failed to connect to server.");
578 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("Esound sink '%s'", p
));
581 pa_socket_client_set_callback(u
->client
, on_connection
, u
);
583 /* Prepare the initial request */
584 u
->write_data
= pa_xmalloc(u
->write_length
= ESD_KEY_LEN
+ sizeof(int32_t));
585 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", ".esd_auth"), u
->write_data
, ESD_KEY_LEN
) < 0) {
586 pa_log("Failed to load cookie");
590 key
= ESD_ENDIAN_KEY
;
591 memcpy((uint8_t*) u
->write_data
+ ESD_KEY_LEN
, &key
, sizeof(key
));
593 /* Reserve space for the response */
594 u
->read_data
= pa_xmalloc(u
->read_length
= sizeof(int32_t));
596 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
597 pa_log("Failed to create thread.");
601 pa_sink_put(u
->sink
);
616 void pa__done(pa_module
*m
) {
620 if (!(u
= m
->userdata
))
624 pa_sink_unlink(u
->sink
);
627 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
628 pa_thread_free(u
->thread
);
631 pa_thread_mq_done(&u
->thread_mq
);
634 pa_sink_unref(u
->sink
);
637 pa_iochannel_free(u
->io
);
640 pa_rtpoll_item_free(u
->rtpoll_item
);
643 pa_rtpoll_free(u
->rtpoll
);
645 if (u
->memchunk
.memblock
)
646 pa_memblock_unref(u
->memchunk
.memblock
);
649 pa_socket_client_unref(u
->client
);
651 pa_xfree(u
->read_data
);
652 pa_xfree(u
->write_data
);
655 pa_smoother_free(u
->smoother
);