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