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