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
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>
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
);
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"
83 pa_thread_mq thread_mq
;
85 pa_rtpoll_item
*rtpoll_item
;
91 size_t write_length
, write_index
;
94 size_t read_length
, read_index
;
109 pa_smoother
*smoother
;
115 pa_socket_client
*client
;
120 static const char* const valid_modargs
[] = {
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
;
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());
150 case PA_SINK_RUNNING
:
152 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
)
153 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
157 case PA_SINK_UNLINKED
:
159 case PA_SINK_INVALID_STATE
:
165 case PA_SINK_MESSAGE_GET_LATENCY
: {
168 r
= pa_smoother_get(u
->smoother
, pa_rtclock_usec());
169 w
= pa_bytes_to_usec((uint64_t) u
->offset
+ u
->memchunk
.length
, &u
->sink
->sample_spec
);
171 *((pa_usec_t
*) data
) = w
> r
? w
- r
: 0;
175 case SINK_MESSAGE_PASS_SOCKET
: {
176 struct pollfd
*pollfd
;
178 pa_assert(!u
->rtpoll_item
);
180 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
181 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
183 pollfd
->events
= pollfd
->revents
= 0;
189 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
192 static void thread_func(void *userdata
) {
193 struct userdata
*u
= userdata
;
198 pa_log_debug("Thread starting up");
200 pa_thread_mq_install(&u
->thread_mq
);
201 pa_rtpoll_install(u
->rtpoll
);
203 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
208 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
209 if (u
->sink
->thread_info
.rewind_requested
)
210 pa_sink_process_rewind(u
->sink
, 0);
212 if (u
->rtpoll_item
) {
213 struct pollfd
*pollfd
;
214 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
216 /* Render some data and write it to the fifo */
217 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) && pollfd
->revents
) {
225 if (u
->memchunk
.length
<= 0)
226 pa_sink_render(u
->sink
, u
->block_size
, &u
->memchunk
);
228 pa_assert(u
->memchunk
.length
> 0);
230 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
231 l
= pa_write(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &write_type
);
232 pa_memblock_release(u
->memchunk
.memblock
);
240 else if (errno
== EAGAIN
) {
242 /* OK, we filled all socket buffers up
247 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
254 u
->memchunk
.index
+= (size_t) l
;
255 u
->memchunk
.length
-= (size_t) l
;
257 if (u
->memchunk
.length
<= 0) {
258 pa_memblock_unref(u
->memchunk
.memblock
);
259 pa_memchunk_reset(&u
->memchunk
);
264 if (u
->memchunk
.length
> 0)
266 /* OK, we wrote less that we asked for,
267 * hence we can assume that the socket
268 * buffers are full now */
275 /* At this spot we know that the socket buffers are
276 * fully filled up. This is the best time to estimate
277 * the playback position of the server */
284 if (ioctl(u
->fd
, SIOCOUTQ
, &l
) >= 0 && l
> 0)
289 usec
= pa_bytes_to_usec((uint64_t) n
, &u
->sink
->sample_spec
);
291 if (usec
> u
->latency
)
296 pa_smoother_put(u
->smoother
, pa_rtclock_usec(), usec
);
299 /* Hmm, nothing to do. Let's sleep */
300 pollfd
->events
= (short) (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
) ? POLLOUT
: 0);
303 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
309 if (u
->rtpoll_item
) {
310 struct pollfd
* pollfd
;
312 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
314 if (pollfd
->revents
& ~POLLOUT
) {
315 pa_log("FIFO shutdown.");
322 /* If this was no regular exit from the loop we have to continue
323 * processing messages until we received PA_MESSAGE_SHUTDOWN */
324 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
325 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
328 pa_log_debug("Thread shutting down");
331 static int do_write(struct userdata
*u
) {
335 if (!pa_iochannel_is_writable(u
->io
))
339 pa_assert(u
->write_index
< u
->write_length
);
341 if ((r
= pa_iochannel_write(u
->io
, (uint8_t*) u
->write_data
+ u
->write_index
, u
->write_length
- u
->write_index
)) <= 0) {
342 pa_log("write() failed: %s", pa_cstrerror(errno
));
346 u
->write_index
+= (size_t) r
;
347 pa_assert(u
->write_index
<= u
->write_length
);
349 if (u
->write_index
== u
->write_length
) {
350 pa_xfree(u
->write_data
);
351 u
->write_data
= NULL
;
352 u
->write_index
= u
->write_length
= 0;
356 if (!u
->write_data
&& u
->state
== STATE_PREPARE
) {
357 /* OK, we're done with sending all control data we need to, so
358 * let's hand the socket over to the IO thread now */
360 pa_assert(u
->fd
< 0);
361 u
->fd
= pa_iochannel_get_send_fd(u
->io
);
363 pa_iochannel_set_noclose(u
->io
, TRUE
);
364 pa_iochannel_free(u
->io
);
367 pa_make_tcp_socket_low_delay(u
->fd
);
369 pa_log_debug("Connection authenticated, handing fd to IO thread...");
371 pa_asyncmsgq_post(u
->thread_mq
.inq
, PA_MSGOBJECT(u
->sink
), SINK_MESSAGE_PASS_SOCKET
, NULL
, 0, NULL
, NULL
);
372 u
->state
= STATE_RUNNING
;
378 static int handle_response(struct userdata
*u
) {
384 pa_assert(u
->read_length
== sizeof(int32_t));
386 /* Process auth data */
387 if (!*(int32_t*) u
->read_data
) {
388 pa_log("Authentication failed: %s", pa_cstrerror(errno
));
392 /* Request latency data */
393 pa_assert(!u
->write_data
);
394 *(int32_t*) (u
->write_data
= pa_xmalloc(u
->write_length
= sizeof(int32_t))) = ESD_PROTO_LATENCY
;
397 u
->state
= STATE_LATENCY
;
399 /* Space for next response */
400 pa_assert(u
->read_length
>= sizeof(int32_t));
402 u
->read_length
= sizeof(int32_t);
406 case STATE_LATENCY
: {
408 pa_assert(u
->read_length
== sizeof(int32_t));
410 /* Process latency info */
411 u
->latency
= (pa_usec_t
) ((double) (*(int32_t*) u
->read_data
) * 1000000 / 44100);
412 if (u
->latency
> 10000000) {
413 pa_log_warn("Invalid latency information received from server");
418 pa_assert(!u
->write_data
);
419 p
= u
->write_data
= pa_xmalloc0(u
->write_length
= sizeof(int32_t)*3+ESD_NAME_MAX
);
420 *(p
++) = ESD_PROTO_STREAM_PLAY
;
423 pa_strlcpy((char*) p
, "PulseAudio Tunnel", ESD_NAME_MAX
);
426 u
->state
= STATE_PREPARE
;
428 /* Don't read any further */
429 pa_xfree(u
->read_data
);
431 u
->read_index
= u
->read_length
= 0;
437 pa_assert_not_reached();
443 static int do_read(struct userdata
*u
) {
446 if (!pa_iochannel_is_readable(u
->io
))
449 if (u
->state
== STATE_AUTH
|| u
->state
== STATE_LATENCY
) {
455 pa_assert(u
->read_index
< u
->read_length
);
457 if ((r
= pa_iochannel_read(u
->io
, (uint8_t*) u
->read_data
+ u
->read_index
, u
->read_length
- u
->read_index
)) <= 0) {
458 pa_log("read() failed: %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
462 u
->read_index
+= (size_t) r
;
463 pa_assert(u
->read_index
<= u
->read_length
);
465 if (u
->read_index
== u
->read_length
)
466 return handle_response(u
);
472 static void io_callback(pa_iochannel
*io
, void*userdata
) {
473 struct userdata
*u
= userdata
;
476 if (do_read(u
) < 0 || do_write(u
) < 0) {
479 pa_iochannel_free(u
->io
);
483 pa_module_unload_request(u
->module
, TRUE
);
487 static void on_connection(pa_socket_client
*c
, pa_iochannel
*io
, void *userdata
) {
488 struct userdata
*u
= userdata
;
490 pa_socket_client_unref(u
->client
);
494 pa_log("Connection failed: %s", pa_cstrerror(errno
));
495 pa_module_unload_request(u
->module
, TRUE
);
501 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
503 pa_log_debug("Connection established, authenticating ...");
506 int pa__init(pa_module
*m
) {
507 struct userdata
*u
= NULL
;
509 pa_modargs
*ma
= NULL
;
510 const char *espeaker
;
512 pa_sink_new_data data
;
516 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
517 pa_log("failed to parse module arguments");
521 ss
= m
->core
->default_sample_spec
;
522 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
523 pa_log("invalid sample format specification");
527 if ((ss
.format
!= PA_SAMPLE_U8
&& ss
.format
!= PA_SAMPLE_S16NE
) ||
529 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
533 u
= pa_xnew0(struct userdata
, 1);
538 u
->smoother
= pa_smoother_new(PA_USEC_PER_SEC
, PA_USEC_PER_SEC
*2, TRUE
, 10);
539 pa_memchunk_reset(&u
->memchunk
);
542 u
->rtpoll
= pa_rtpoll_new();
543 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
544 u
->rtpoll_item
= NULL
;
547 (ss
.format
== PA_SAMPLE_U8
? ESD_BITS8
: ESD_BITS16
) |
548 (ss
.channels
== 2 ? ESD_STEREO
: ESD_MONO
);
549 u
->rate
= (int32_t) ss
.rate
;
550 u
->block_size
= pa_usec_to_bytes(PA_USEC_PER_SEC
/20, &ss
);
552 u
->read_data
= u
->write_data
= NULL
;
553 u
->read_index
= u
->write_index
= u
->read_length
= u
->write_length
= 0;
555 u
->state
= STATE_AUTH
;
558 if (!(espeaker
= getenv("ESPEAKER")))
559 espeaker
= ESD_UNIX_SOCKET_NAME
;
561 espeaker
= pa_modargs_get_value(ma
, "server", espeaker
);
563 pa_sink_new_data_init(&data
);
564 data
.driver
= __FILE__
;
566 pa_sink_new_data_set_name(&data
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
));
567 pa_sink_new_data_set_sample_spec(&data
, &ss
);
568 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, espeaker
);
569 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Esound sink '%s'", espeaker
);
571 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_LATENCY
|PA_SINK_NETWORK
);
572 pa_sink_new_data_done(&data
);
575 pa_log("Failed to create sink.");
579 u
->sink
->parent
.process_msg
= sink_process_msg
;
580 u
->sink
->userdata
= u
;
582 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
583 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
585 if (!(u
->client
= pa_socket_client_new_string(u
->core
->mainloop
, espeaker
, ESD_DEFAULT_PORT
))) {
586 pa_log("Failed to connect to server.");
590 pa_socket_client_set_callback(u
->client
, on_connection
, u
);
592 /* Prepare the initial request */
593 u
->write_data
= pa_xmalloc(u
->write_length
= ESD_KEY_LEN
+ sizeof(int32_t));
594 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", ".esd_auth"), u
->write_data
, ESD_KEY_LEN
) < 0) {
595 pa_log("Failed to load cookie");
599 key
= ESD_ENDIAN_KEY
;
600 memcpy((uint8_t*) u
->write_data
+ ESD_KEY_LEN
, &key
, sizeof(key
));
602 /* Reserve space for the response */
603 u
->read_data
= pa_xmalloc(u
->read_length
= sizeof(int32_t));
605 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
606 pa_log("Failed to create thread.");
610 pa_sink_put(u
->sink
);
625 int pa__get_n_used(pa_module
*m
) {
629 pa_assert_se(u
= m
->userdata
);
631 return pa_sink_linked_by(u
->sink
);
634 void pa__done(pa_module
*m
) {
638 if (!(u
= m
->userdata
))
642 pa_sink_unlink(u
->sink
);
645 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
646 pa_thread_free(u
->thread
);
649 pa_thread_mq_done(&u
->thread_mq
);
652 pa_sink_unref(u
->sink
);
655 pa_iochannel_free(u
->io
);
658 pa_rtpoll_item_free(u
->rtpoll_item
);
661 pa_rtpoll_free(u
->rtpoll
);
663 if (u
->memchunk
.memblock
)
664 pa_memblock_unref(u
->memchunk
.memblock
);
667 pa_socket_client_unref(u
->client
);
669 pa_xfree(u
->read_data
);
670 pa_xfree(u
->write_data
);
673 pa_smoother_free(u
->smoother
);