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