2
This file is part of PulseAudio.
4
Copyright 2004-2006 Lennart Poettering
6
PulseAudio is free software; you can redistribute it and/or modify
7
it under the terms of the GNU Lesser General Public License as published
8
by the Free Software Foundation; either version 2.1 of the License,
9
or (at your option) any later version.
11
PulseAudio is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public License
17
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
30
#ifdef HAVE_NETINET_IN_H
31
#include <netinet/in.h>
34
#ifdef HAVE_NETINET_TCP_H
35
#include <netinet/tcp.h>
38
#ifdef HAVE_SYS_IOCTL_H
39
#include <sys/ioctl.h>
42
#ifdef HAVE_LINUX_SOCKIOS_H
43
#include <linux/sockios.h>
46
#include <pulse/rtclock.h>
47
#include <pulse/timeval.h>
48
#include <pulse/xmalloc.h>
50
#include <pulsecore/socket.h>
51
#include <pulsecore/core-error.h>
52
#include <pulsecore/iochannel.h>
53
#include <pulsecore/sink.h>
54
#include <pulsecore/module.h>
55
#include <pulsecore/core-util.h>
56
#include <pulsecore/modargs.h>
57
#include <pulsecore/log.h>
58
#include <pulsecore/socket-client.h>
59
#include <pulsecore/esound.h>
60
#include <pulsecore/authkey.h>
61
#include <pulsecore/thread-mq.h>
62
#include <pulsecore/thread.h>
63
#include <pulsecore/time-smoother.h>
64
#include <pulsecore/socket-util.h>
65
#include <pulsecore/rtpoll.h>
66
#include <pulsecore/poll.h>
68
#include "module-esound-sink-symdef.h"
70
PA_MODULE_AUTHOR("Lennart Poettering");
71
PA_MODULE_DESCRIPTION("ESOUND Sink");
72
PA_MODULE_VERSION(PACKAGE_VERSION);
73
PA_MODULE_LOAD_ONCE(false);
75
"sink_name=<name for the sink> "
76
"sink_properties=<properties for the sink> "
77
"server=<address> cookie=<filename> "
78
"format=<sample format> "
80
"channels=<number of channels>");
82
#define DEFAULT_SINK_NAME "esound_out"
89
pa_thread_mq thread_mq;
91
pa_rtpoll_item *rtpoll_item;
97
size_t write_length, write_index;
100
size_t read_length, read_index;
115
pa_smoother *smoother;
121
pa_socket_client *client;
126
static const char* const valid_modargs[] = {
138
SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX
141
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
142
struct userdata *u = PA_SINK(o)->userdata;
146
case PA_SINK_MESSAGE_SET_STATE:
148
switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
150
case PA_SINK_SUSPENDED:
151
pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
153
pa_smoother_pause(u->smoother, pa_rtclock_now());
157
case PA_SINK_RUNNING:
159
if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
160
pa_smoother_resume(u->smoother, pa_rtclock_now(), true);
164
case PA_SINK_UNLINKED:
166
case PA_SINK_INVALID_STATE:
172
case PA_SINK_MESSAGE_GET_LATENCY: {
175
r = pa_smoother_get(u->smoother, pa_rtclock_now());
176
w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec);
178
*((pa_usec_t*) data) = w > r ? w - r : 0;
182
case SINK_MESSAGE_PASS_SOCKET: {
183
struct pollfd *pollfd;
185
pa_assert(!u->rtpoll_item);
187
u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
188
pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
190
pollfd->events = pollfd->revents = 0;
196
return pa_sink_process_msg(o, code, data, offset, chunk);
199
static void thread_func(void *userdata) {
200
struct userdata *u = userdata;
205
pa_log_debug("Thread starting up");
207
pa_thread_mq_install(&u->thread_mq);
209
pa_smoother_set_time_offset(u->smoother, pa_rtclock_now());
214
if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
215
pa_sink_process_rewind(u->sink, 0);
217
if (u->rtpoll_item) {
218
struct pollfd *pollfd;
219
pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
221
/* Render some data and write it to the fifo */
222
if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && pollfd->revents) {
230
if (u->memchunk.length <= 0)
231
pa_sink_render(u->sink, u->block_size, &u->memchunk);
233
pa_assert(u->memchunk.length > 0);
235
p = pa_memblock_acquire(u->memchunk.memblock);
236
l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
237
pa_memblock_release(u->memchunk.memblock);
245
else if (errno == EAGAIN) {
247
/* OK, we filled all socket buffers up
252
pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
259
u->memchunk.index += (size_t) l;
260
u->memchunk.length -= (size_t) l;
262
if (u->memchunk.length <= 0) {
263
pa_memblock_unref(u->memchunk.memblock);
264
pa_memchunk_reset(&u->memchunk);
269
if (u->memchunk.length > 0)
271
/* OK, we wrote less that we asked for,
272
* hence we can assume that the socket
273
* buffers are full now */
280
/* At this spot we know that the socket buffers are
281
* fully filled up. This is the best time to estimate
282
* the playback position of the server */
289
if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
294
usec = pa_bytes_to_usec((uint64_t) n, &u->sink->sample_spec);
296
if (usec > u->latency)
301
pa_smoother_put(u->smoother, pa_rtclock_now(), usec);
304
/* Hmm, nothing to do. Let's sleep */
305
pollfd->events = (short) (PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0);
308
if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
314
if (u->rtpoll_item) {
315
struct pollfd* pollfd;
317
pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
319
if (pollfd->revents & ~POLLOUT) {
320
pa_log("FIFO shutdown.");
327
/* If this was no regular exit from the loop we have to continue
328
* processing messages until we received PA_MESSAGE_SHUTDOWN */
329
pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
330
pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
333
pa_log_debug("Thread shutting down");
336
static int do_write(struct userdata *u) {
340
if (!pa_iochannel_is_writable(u->io))
344
pa_assert(u->write_index < u->write_length);
346
if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) < 0) {
347
pa_log("write() failed: %s", pa_cstrerror(errno));
351
u->write_index += (size_t) r;
352
pa_assert(u->write_index <= u->write_length);
354
if (u->write_index == u->write_length) {
355
pa_xfree(u->write_data);
356
u->write_data = NULL;
357
u->write_index = u->write_length = 0;
361
if (!u->write_data && u->state == STATE_PREPARE) {
363
socklen_t sl = sizeof(int);
365
/* OK, we're done with sending all control data we need to, so
366
* let's hand the socket over to the IO thread now */
368
pa_assert(u->fd < 0);
369
u->fd = pa_iochannel_get_send_fd(u->io);
371
pa_iochannel_set_noclose(u->io, true);
372
pa_iochannel_free(u->io);
375
pa_make_tcp_socket_low_delay(u->fd);
377
if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, (void *) &so_sndbuf, &sl) < 0)
378
pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno));
380
pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf);
381
pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size));
384
pa_log_debug("Connection authenticated, handing fd to IO thread...");
386
pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
387
u->state = STATE_RUNNING;
393
static int handle_response(struct userdata *u) {
399
pa_assert(u->read_length == sizeof(int32_t));
401
/* Process auth data */
402
if (!*(int32_t*) u->read_data) {
403
pa_log("Authentication failed: %s", pa_cstrerror(errno));
407
/* Request latency data */
408
pa_assert(!u->write_data);
409
*(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
412
u->state = STATE_LATENCY;
414
/* Space for next response */
415
pa_assert(u->read_length >= sizeof(int32_t));
417
u->read_length = sizeof(int32_t);
421
case STATE_LATENCY: {
423
pa_assert(u->read_length == sizeof(int32_t));
425
/* Process latency info */
426
u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
427
if (u->latency > 10000000) {
428
pa_log_warn("Invalid latency information received from server");
433
pa_assert(!u->write_data);
434
p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
435
*(p++) = ESD_PROTO_STREAM_PLAY;
438
pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
441
u->state = STATE_PREPARE;
443
/* Don't read any further */
444
pa_xfree(u->read_data);
446
u->read_index = u->read_length = 0;
452
pa_assert_not_reached();
458
static int do_read(struct userdata *u) {
461
if (!pa_iochannel_is_readable(u->io))
464
if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
470
pa_assert(u->read_index < u->read_length);
472
if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
473
pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
477
u->read_index += (size_t) r;
478
pa_assert(u->read_index <= u->read_length);
480
if (u->read_index == u->read_length)
481
return handle_response(u);
487
static void io_callback(pa_iochannel *io, void*userdata) {
488
struct userdata *u = userdata;
491
if (do_read(u) < 0 || do_write(u) < 0) {
494
pa_iochannel_free(u->io);
498
pa_module_unload_request(u->module, true);
502
static void on_connection(pa_socket_client *c, pa_iochannel*io, void *userdata) {
503
struct userdata *u = userdata;
505
pa_socket_client_unref(u->client);
509
pa_log("Connection failed: %s", pa_cstrerror(errno));
510
pa_module_unload_request(u->module, true);
516
pa_iochannel_set_callback(u->io, io_callback, u);
518
pa_log_debug("Connection established, authenticating ...");
521
int pa__init(pa_module*m) {
522
struct userdata *u = NULL;
524
pa_modargs *ma = NULL;
525
const char *espeaker;
527
pa_sink_new_data data;
533
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
534
pa_log("failed to parse module arguments");
538
ss = m->core->default_sample_spec;
539
if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
540
pa_log("invalid sample format specification");
544
if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
546
pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
550
u = pa_xnew0(struct userdata, 1);
555
u->smoother = pa_smoother_new(
563
pa_memchunk_reset(&u->memchunk);
566
u->rtpoll = pa_rtpoll_new();
567
pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
568
u->rtpoll_item = NULL;
571
(ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
572
(ss.channels == 2 ? ESD_STEREO : ESD_MONO);
573
u->rate = (int32_t) ss.rate;
574
u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
576
u->read_data = u->write_data = NULL;
577
u->read_index = u->write_index = u->read_length = u->write_length = 0;
579
u->state = STATE_AUTH;
582
if (!(espeaker = getenv("ESPEAKER")))
583
espeaker = ESD_UNIX_SOCKET_NAME;
585
espeaker = pa_modargs_get_value(ma, "server", espeaker);
587
pa_sink_new_data_init(&data);
588
data.driver = __FILE__;
590
pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
591
pa_sink_new_data_set_sample_spec(&data, &ss);
592
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, espeaker);
593
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "esd");
594
pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "EsounD Output on %s", espeaker);
596
if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
597
pa_log("Invalid properties");
598
pa_sink_new_data_done(&data);
602
u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
603
pa_sink_new_data_done(&data);
606
pa_log("Failed to create sink.");
610
u->sink->parent.process_msg = sink_process_msg;
611
u->sink->userdata = u;
613
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
614
pa_sink_set_rtpoll(u->sink, u->rtpoll);
616
if (!(u->client = pa_socket_client_new_string(u->core->mainloop, true, espeaker, ESD_DEFAULT_PORT))) {
617
pa_log("Failed to connect to server.");
621
pa_socket_client_set_callback(u->client, on_connection, u);
623
cookie_path = pa_xstrdup(pa_modargs_get_value(ma, "cookie", NULL));
625
if (pa_append_to_home_dir(".esd_auth", &cookie_path) < 0)
629
/* Prepare the initial request */
630
u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
632
r = pa_authkey_load(cookie_path, true, u->write_data, ESD_KEY_LEN);
633
pa_xfree(cookie_path);
635
pa_log("Failed to load cookie");
639
key = ESD_ENDIAN_KEY;
640
memcpy((uint8_t*) u->write_data + ESD_KEY_LEN, &key, sizeof(key));
642
/* Reserve space for the response */
643
u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
645
if (!(u->thread = pa_thread_new("esound-sink", thread_func, u))) {
646
pa_log("Failed to create thread.");
650
pa_sink_put(u->sink);
665
int pa__get_n_used(pa_module *m) {
669
pa_assert_se(u = m->userdata);
671
return pa_sink_linked_by(u->sink);
674
void pa__done(pa_module*m) {
678
if (!(u = m->userdata))
682
pa_sink_unlink(u->sink);
685
pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
686
pa_thread_free(u->thread);
689
pa_thread_mq_done(&u->thread_mq);
692
pa_sink_unref(u->sink);
695
pa_iochannel_free(u->io);
698
pa_rtpoll_item_free(u->rtpoll_item);
701
pa_rtpoll_free(u->rtpoll);
703
if (u->memchunk.memblock)
704
pa_memblock_unref(u->memchunk.memblock);
707
pa_socket_client_unref(u->client);
709
pa_xfree(u->read_data);
710
pa_xfree(u->write_data);
713
pa_smoother_free(u->smoother);