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