1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33
34 #ifdef HAVE_NETINET_TCP_H
35 #include <netinet/tcp.h>
36 #endif
37
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41
42 #ifdef HAVE_LINUX_SOCKIOS_H
43 #include <linux/sockios.h>
44 #endif
45
46 #include <pulse/rtclock.h>
47 #include <pulse/timeval.h>
48 #include <pulse/xmalloc.h>
49
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>
67
68 PA_MODULE_AUTHOR("Lennart Poettering");
69 PA_MODULE_DESCRIPTION("ESOUND Sink");
70 PA_MODULE_VERSION(PACKAGE_VERSION);
71 PA_MODULE_LOAD_ONCE(false);
72 PA_MODULE_USAGE(
73 "sink_name=<name for the sink> "
74 "sink_properties=<properties for the sink> "
75 "server=<address> cookie=<filename> "
76 "format=<sample format> "
77 "rate=<sample rate> "
78 "channels=<number of channels>");
79
80 #define DEFAULT_SINK_NAME "esound_out"
81
82 struct userdata {
83 pa_core *core;
84 pa_module *module;
85 pa_sink *sink;
86
87 pa_thread_mq thread_mq;
88 pa_rtpoll *rtpoll;
89 pa_rtpoll_item *rtpoll_item;
90 pa_thread *thread;
91
92 pa_memchunk memchunk;
93
94 void *write_data;
95 size_t write_length, write_index;
96
97 void *read_data;
98 size_t read_length, read_index;
99
100 enum {
101 STATE_AUTH,
102 STATE_LATENCY,
103 STATE_PREPARE,
104 STATE_RUNNING,
105 STATE_DEAD
106 } state;
107
108 pa_usec_t latency;
109
110 esd_format_t format;
111 int32_t rate;
112
113 pa_smoother *smoother;
114 int fd;
115
116 int64_t offset;
117
118 pa_iochannel *io;
119 pa_socket_client *client;
120
121 size_t block_size;
122 };
123
124 static const char* const valid_modargs[] = {
125 "sink_name",
126 "sink_properties",
127 "server",
128 "cookie",
129 "format",
130 "rate",
131 "channels",
132 NULL
133 };
134
135 enum {
136 SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX
137 };
138
sink_process_msg(pa_msgobject * o,int code,void * data,int64_t offset,pa_memchunk * chunk)139 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
140 struct userdata *u = PA_SINK(o)->userdata;
141
142 switch (code) {
143
144 case PA_SINK_MESSAGE_GET_LATENCY: {
145 pa_usec_t w, r;
146
147 r = pa_smoother_get(u->smoother, pa_rtclock_now());
148 w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec);
149
150 *((int64_t*) data) = (int64_t)w - r;
151 return 0;
152 }
153
154 case SINK_MESSAGE_PASS_SOCKET: {
155 struct pollfd *pollfd;
156
157 pa_assert(!u->rtpoll_item);
158
159 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
160 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
161 pollfd->fd = u->fd;
162 pollfd->events = pollfd->revents = 0;
163
164 return 0;
165 }
166 }
167
168 return pa_sink_process_msg(o, code, data, offset, chunk);
169 }
170
171 /* Called from the IO thread. */
sink_set_state_in_io_thread_cb(pa_sink * s,pa_sink_state_t new_state,pa_suspend_cause_t new_suspend_cause)172 static int sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
173 struct userdata *u;
174
175 pa_assert(s);
176 pa_assert_se(u = s->userdata);
177
178 /* It may be that only the suspend cause is changing, in which case there's
179 * nothing to do. */
180 if (new_state == s->thread_info.state)
181 return 0;
182
183 switch (new_state) {
184
185 case PA_SINK_SUSPENDED:
186 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
187
188 pa_smoother_pause(u->smoother, pa_rtclock_now());
189 break;
190
191 case PA_SINK_IDLE:
192 case PA_SINK_RUNNING:
193
194 if (s->thread_info.state == PA_SINK_SUSPENDED)
195 pa_smoother_resume(u->smoother, pa_rtclock_now(), true);
196
197 break;
198
199 case PA_SINK_UNLINKED:
200 case PA_SINK_INIT:
201 case PA_SINK_INVALID_STATE:
202 ;
203 }
204
205 return 0;
206 }
207
thread_func(void * userdata)208 static void thread_func(void *userdata) {
209 struct userdata *u = userdata;
210 int write_type = 0;
211
212 pa_assert(u);
213
214 pa_log_debug("Thread starting up");
215
216 pa_thread_mq_install(&u->thread_mq);
217
218 pa_smoother_set_time_offset(u->smoother, pa_rtclock_now());
219
220 for (;;) {
221 int ret;
222
223 if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
224 pa_sink_process_rewind(u->sink, 0);
225
226 if (u->rtpoll_item) {
227 struct pollfd *pollfd;
228 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
229
230 /* Render some data and write it to the fifo */
231 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && pollfd->revents) {
232 pa_usec_t usec;
233 int64_t n;
234
235 for (;;) {
236 ssize_t l;
237 void *p;
238
239 if (u->memchunk.length <= 0)
240 pa_sink_render(u->sink, u->block_size, &u->memchunk);
241
242 pa_assert(u->memchunk.length > 0);
243
244 p = pa_memblock_acquire(u->memchunk.memblock);
245 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
246 pa_memblock_release(u->memchunk.memblock);
247
248 pa_assert(l != 0);
249
250 if (l < 0) {
251
252 if (errno == EINTR)
253 continue;
254 else if (errno == EAGAIN) {
255
256 /* OK, we filled all socket buffers up
257 * now. */
258 goto filled_up;
259
260 } else {
261 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
262 goto fail;
263 }
264
265 } else {
266 u->offset += l;
267
268 u->memchunk.index += (size_t) l;
269 u->memchunk.length -= (size_t) l;
270
271 if (u->memchunk.length <= 0) {
272 pa_memblock_unref(u->memchunk.memblock);
273 pa_memchunk_reset(&u->memchunk);
274 }
275
276 pollfd->revents = 0;
277
278 if (u->memchunk.length > 0)
279
280 /* OK, we wrote less that we asked for,
281 * hence we can assume that the socket
282 * buffers are full now */
283 goto filled_up;
284 }
285 }
286
287 filled_up:
288
289 /* At this spot we know that the socket buffers are
290 * fully filled up. This is the best time to estimate
291 * the playback position of the server */
292
293 n = u->offset;
294
295 #ifdef SIOCOUTQ
296 {
297 int l;
298 if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
299 n -= l;
300 }
301 #endif
302
303 usec = pa_bytes_to_usec((uint64_t) n, &u->sink->sample_spec);
304
305 if (usec > u->latency)
306 usec -= u->latency;
307 else
308 usec = 0;
309
310 pa_smoother_put(u->smoother, pa_rtclock_now(), usec);
311 }
312
313 /* Hmm, nothing to do. Let's sleep */
314 pollfd->events = (short) (PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0);
315 }
316
317 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
318 goto fail;
319
320 if (ret == 0)
321 goto finish;
322
323 if (u->rtpoll_item) {
324 struct pollfd* pollfd;
325
326 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
327
328 if (pollfd->revents & ~POLLOUT) {
329 pa_log("FIFO shutdown.");
330 goto fail;
331 }
332 }
333 }
334
335 fail:
336 /* If this was no regular exit from the loop we have to continue
337 * processing messages until we received PA_MESSAGE_SHUTDOWN */
338 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
339 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
340
341 finish:
342 pa_log_debug("Thread shutting down");
343 }
344
do_write(struct userdata * u)345 static int do_write(struct userdata *u) {
346 ssize_t r;
347 pa_assert(u);
348
349 if (!pa_iochannel_is_writable(u->io))
350 return 0;
351
352 if (u->write_data) {
353 pa_assert(u->write_index < u->write_length);
354
355 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->write_data + u->write_index, u->write_length - u->write_index)) < 0) {
356 pa_log("write() failed: %s", pa_cstrerror(errno));
357 return -1;
358 }
359
360 u->write_index += (size_t) r;
361 pa_assert(u->write_index <= u->write_length);
362
363 if (u->write_index == u->write_length) {
364 pa_xfree(u->write_data);
365 u->write_data = NULL;
366 u->write_index = u->write_length = 0;
367 }
368 }
369
370 if (!u->write_data && u->state == STATE_PREPARE) {
371 int so_sndbuf = 0;
372 socklen_t sl = sizeof(int);
373
374 /* OK, we're done with sending all control data we need to, so
375 * let's hand the socket over to the IO thread now */
376
377 pa_assert(u->fd < 0);
378 u->fd = pa_iochannel_get_send_fd(u->io);
379
380 pa_iochannel_set_noclose(u->io, true);
381 pa_iochannel_free(u->io);
382 u->io = NULL;
383
384 pa_make_tcp_socket_low_delay(u->fd);
385
386 if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, (void *) &so_sndbuf, &sl) < 0)
387 pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno));
388 else {
389 pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf);
390 pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size));
391 }
392
393 pa_log_debug("Connection authenticated, handing fd to IO thread...");
394
395 pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
396 u->state = STATE_RUNNING;
397 }
398
399 return 0;
400 }
401
handle_response(struct userdata * u)402 static int handle_response(struct userdata *u) {
403 pa_assert(u);
404
405 switch (u->state) {
406
407 case STATE_AUTH:
408 pa_assert(u->read_length == sizeof(int32_t));
409
410 /* Process auth data */
411 if (!*(int32_t*) u->read_data) {
412 pa_log("Authentication failed: %s", pa_cstrerror(errno));
413 return -1;
414 }
415
416 /* Request latency data */
417 pa_assert(!u->write_data);
418 *(int32_t*) (u->write_data = pa_xmalloc(u->write_length = sizeof(int32_t))) = ESD_PROTO_LATENCY;
419
420 u->write_index = 0;
421 u->state = STATE_LATENCY;
422
423 /* Space for next response */
424 pa_assert(u->read_length >= sizeof(int32_t));
425 u->read_index = 0;
426 u->read_length = sizeof(int32_t);
427
428 break;
429
430 case STATE_LATENCY: {
431 int32_t *p;
432 pa_assert(u->read_length == sizeof(int32_t));
433
434 /* Process latency info */
435 u->latency = (pa_usec_t) ((double) (*(int32_t*) u->read_data) * 1000000 / 44100);
436 if (u->latency > 10000000) {
437 pa_log_warn("Invalid latency information received from server");
438 u->latency = 0;
439 }
440
441 /* Create stream */
442 pa_assert(!u->write_data);
443 p = u->write_data = pa_xmalloc0(u->write_length = sizeof(int32_t)*3+ESD_NAME_MAX);
444 *(p++) = ESD_PROTO_STREAM_PLAY;
445 *(p++) = u->format;
446 *(p++) = u->rate;
447 pa_strlcpy((char*) p, "PulseAudio Tunnel", ESD_NAME_MAX);
448
449 u->write_index = 0;
450 u->state = STATE_PREPARE;
451
452 /* Don't read any further */
453 pa_xfree(u->read_data);
454 u->read_data = NULL;
455 u->read_index = u->read_length = 0;
456
457 break;
458 }
459
460 default:
461 pa_assert_not_reached();
462 }
463
464 return 0;
465 }
466
do_read(struct userdata * u)467 static int do_read(struct userdata *u) {
468 pa_assert(u);
469
470 if (!pa_iochannel_is_readable(u->io))
471 return 0;
472
473 if (u->state == STATE_AUTH || u->state == STATE_LATENCY) {
474 ssize_t r;
475
476 if (!u->read_data)
477 return 0;
478
479 pa_assert(u->read_index < u->read_length);
480
481 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->read_data + u->read_index, u->read_length - u->read_index)) <= 0) {
482 pa_log("read() failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
483 return -1;
484 }
485
486 u->read_index += (size_t) r;
487 pa_assert(u->read_index <= u->read_length);
488
489 if (u->read_index == u->read_length)
490 return handle_response(u);
491 }
492
493 return 0;
494 }
495
io_callback(pa_iochannel * io,void * userdata)496 static void io_callback(pa_iochannel *io, void*userdata) {
497 struct userdata *u = userdata;
498 pa_assert(u);
499
500 if (do_read(u) < 0 || do_write(u) < 0) {
501
502 if (u->io) {
503 pa_iochannel_free(u->io);
504 u->io = NULL;
505 }
506
507 pa_module_unload_request(u->module, true);
508 }
509 }
510
on_connection(pa_socket_client * c,pa_iochannel * io,void * userdata)511 static void on_connection(pa_socket_client *c, pa_iochannel*io, void *userdata) {
512 struct userdata *u = userdata;
513
514 pa_socket_client_unref(u->client);
515 u->client = NULL;
516
517 if (!io) {
518 pa_log("Connection failed: %s", pa_cstrerror(errno));
519 pa_module_unload_request(u->module, true);
520 return;
521 }
522
523 pa_assert(!u->io);
524 u->io = io;
525 pa_iochannel_set_callback(u->io, io_callback, u);
526
527 pa_log_debug("Connection established, authenticating ...");
528 }
529
pa__init(pa_module * m)530 int pa__init(pa_module*m) {
531 struct userdata *u = NULL;
532 pa_sample_spec ss;
533 pa_modargs *ma = NULL;
534 const char *espeaker;
535 uint32_t key;
536 pa_sink_new_data data;
537 char *cookie_path;
538 int r;
539
540 pa_assert(m);
541
542 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
543 pa_log("failed to parse module arguments");
544 goto fail;
545 }
546
547 ss = m->core->default_sample_spec;
548 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
549 pa_log("invalid sample format specification");
550 goto fail;
551 }
552
553 if ((ss.format != PA_SAMPLE_U8 && ss.format != PA_SAMPLE_S16NE) ||
554 (ss.channels > 2)) {
555 pa_log("esound sample type support is limited to mono/stereo and U8 or S16NE sample data");
556 goto fail;
557 }
558
559 u = pa_xnew0(struct userdata, 1);
560 u->core = m->core;
561 u->module = m;
562 m->userdata = u;
563 u->fd = -1;
564 u->smoother = pa_smoother_new(
565 PA_USEC_PER_SEC,
566 PA_USEC_PER_SEC*2,
567 true,
568 true,
569 10,
570 0,
571 false);
572 pa_memchunk_reset(&u->memchunk);
573 u->offset = 0;
574
575 u->rtpoll = pa_rtpoll_new();
576
577 if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
578 pa_log("pa_thread_mq_init() failed.");
579 goto fail;
580 }
581
582 u->rtpoll_item = NULL;
583
584 u->format =
585 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
586 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);
587 u->rate = (int32_t) ss.rate;
588 u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
589
590 u->read_data = u->write_data = NULL;
591 u->read_index = u->write_index = u->read_length = u->write_length = 0;
592
593 u->state = STATE_AUTH;
594 u->latency = 0;
595
596 if (!(espeaker = getenv("ESPEAKER")))
597 espeaker = ESD_UNIX_SOCKET_NAME;
598
599 espeaker = pa_modargs_get_value(ma, "server", espeaker);
600
601 pa_sink_new_data_init(&data);
602 data.driver = __FILE__;
603 data.module = m;
604 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
605 pa_sink_new_data_set_sample_spec(&data, &ss);
606 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, espeaker);
607 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "esd");
608 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "EsounD Output on %s", espeaker);
609
610 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
611 pa_log("Invalid properties");
612 pa_sink_new_data_done(&data);
613 goto fail;
614 }
615
616 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
617 pa_sink_new_data_done(&data);
618
619 if (!u->sink) {
620 pa_log("Failed to create sink.");
621 goto fail;
622 }
623
624 u->sink->parent.process_msg = sink_process_msg;
625 u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
626 u->sink->userdata = u;
627
628 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
629 pa_sink_set_rtpoll(u->sink, u->rtpoll);
630
631 if (!(u->client = pa_socket_client_new_string(u->core->mainloop, true, espeaker, ESD_DEFAULT_PORT))) {
632 pa_log("Failed to connect to server.");
633 goto fail;
634 }
635
636 pa_socket_client_set_callback(u->client, on_connection, u);
637
638 cookie_path = pa_xstrdup(pa_modargs_get_value(ma, "cookie", NULL));
639 if (!cookie_path) {
640 if (pa_append_to_home_dir(".esd_auth", &cookie_path) < 0)
641 goto fail;
642 }
643
644 /* Prepare the initial request */
645 u->write_data = pa_xmalloc(u->write_length = ESD_KEY_LEN + sizeof(int32_t));
646
647 r = pa_authkey_load(cookie_path, true, u->write_data, ESD_KEY_LEN);
648 pa_xfree(cookie_path);
649 if (r < 0) {
650 pa_log("Failed to load cookie");
651 goto fail;
652 }
653
654 key = ESD_ENDIAN_KEY;
655 memcpy((uint8_t*) u->write_data + ESD_KEY_LEN, &key, sizeof(key));
656
657 /* Reserve space for the response */
658 u->read_data = pa_xmalloc(u->read_length = sizeof(int32_t));
659
660 if (!(u->thread = pa_thread_new("esound-sink", thread_func, u))) {
661 pa_log("Failed to create thread.");
662 goto fail;
663 }
664
665 pa_sink_put(u->sink);
666
667 pa_modargs_free(ma);
668
669 return 0;
670
671 fail:
672 if (ma)
673 pa_modargs_free(ma);
674
675 pa__done(m);
676
677 return -1;
678 }
679
pa__get_n_used(pa_module * m)680 int pa__get_n_used(pa_module *m) {
681 struct userdata *u;
682
683 pa_assert(m);
684 pa_assert_se(u = m->userdata);
685
686 return pa_sink_linked_by(u->sink);
687 }
688
pa__done(pa_module * m)689 void pa__done(pa_module*m) {
690 struct userdata *u;
691 pa_assert(m);
692
693 if (!(u = m->userdata))
694 return;
695
696 if (u->sink)
697 pa_sink_unlink(u->sink);
698
699 if (u->thread) {
700 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
701 pa_thread_free(u->thread);
702 }
703
704 pa_thread_mq_done(&u->thread_mq);
705
706 if (u->sink)
707 pa_sink_unref(u->sink);
708
709 if (u->io)
710 pa_iochannel_free(u->io);
711
712 if (u->rtpoll_item)
713 pa_rtpoll_item_free(u->rtpoll_item);
714
715 if (u->rtpoll)
716 pa_rtpoll_free(u->rtpoll);
717
718 if (u->memchunk.memblock)
719 pa_memblock_unref(u->memchunk.memblock);
720
721 if (u->client)
722 pa_socket_client_unref(u->client);
723
724 pa_xfree(u->read_data);
725 pa_xfree(u->write_data);
726
727 if (u->smoother)
728 pa_smoother_free(u->smoother);
729
730 if (u->fd >= 0)
731 pa_close(u->fd);
732
733 pa_xfree(u);
734 }
735