]> code.delx.au - pulseaudio/blob - src/modules/module-raop-sink.c
move flat volume logic into the core. while doing so add n_volume_steps field to...
[pulseaudio] / src / modules / module-raop-sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2008 Colin Guthrie
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <limits.h>
35 #include <poll.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/tcp.h>
39 #include <sys/ioctl.h>
40
41 #ifdef HAVE_LINUX_SOCKIOS_H
42 #include <linux/sockios.h>
43 #endif
44
45 #include <pulse/xmalloc.h>
46 #include <pulse/timeval.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-util.h>
53 #include <pulsecore/modargs.h>
54 #include <pulsecore/log.h>
55 #include <pulsecore/socket-client.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-raop-sink-symdef.h"
64 #include "rtp.h"
65 #include "sdp.h"
66 #include "sap.h"
67 #include "raop_client.h"
68
69 PA_MODULE_AUTHOR("Colin Guthrie");
70 PA_MODULE_DESCRIPTION("RAOP Sink (Apple Airtunes)");
71 PA_MODULE_VERSION(PACKAGE_VERSION);
72 PA_MODULE_LOAD_ONCE(FALSE);
73 PA_MODULE_USAGE(
74 "sink_name=<name for the sink> "
75 "server=<address> "
76 "format=<sample format> "
77 "channels=<number of channels> "
78 "rate=<sample rate>");
79
80 #define DEFAULT_SINK_NAME "airtunes"
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 raw_memchunk;
93 pa_memchunk encoded_memchunk;
94
95 void *write_data;
96 size_t write_length, write_index;
97
98 void *read_data;
99 size_t read_length, read_index;
100
101 pa_usec_t latency;
102
103 pa_volume_t volume;
104 pa_bool_t muted;
105
106 /*esd_format_t format;*/
107 int32_t rate;
108
109 pa_smoother *smoother;
110 int fd;
111
112 int64_t offset;
113 int64_t encoding_overhead;
114 int32_t next_encoding_overhead;
115 double encoding_ratio;
116
117 pa_raop_client *raop;
118
119 size_t block_size;
120 };
121
122 static const char* const valid_modargs[] = {
123 "server",
124 "rate",
125 "format",
126 "channels",
127 "sink_name",
128 NULL
129 };
130
131 enum {
132 SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX,
133 SINK_MESSAGE_RIP_SOCKET
134 };
135
136 static void on_connection(PA_GCC_UNUSED int fd, void*userdata) {
137 struct userdata *u = userdata;
138 pa_assert(u);
139
140 pa_assert(u->fd < 0);
141 u->fd = fd;
142
143 /* Set the initial volume */
144 pa_raop_client_set_volume(u->raop, u->volume);
145
146 pa_log_debug("Connection authenticated, handing fd to IO thread...");
147
148 pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
149 }
150
151 static void on_close(void*userdata) {
152 struct userdata *u = userdata;
153 pa_assert(u);
154
155 pa_log_debug("Connection closed, informing IO thread...");
156
157 pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_RIP_SOCKET, NULL, 0, NULL, NULL);
158 }
159
160 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
161 struct userdata *u = PA_SINK(o)->userdata;
162
163 switch (code) {
164
165 case PA_SINK_MESSAGE_SET_STATE:
166
167 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
168
169 case PA_SINK_SUSPENDED:
170 pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
171
172 pa_smoother_pause(u->smoother, pa_rtclock_usec());
173
174 /* Issue a FLUSH if we are connected */
175 if (u->fd >= 0) {
176 pa_raop_flush(u->raop);
177 }
178 break;
179
180 case PA_SINK_IDLE:
181 case PA_SINK_RUNNING:
182
183 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
184 pa_smoother_resume(u->smoother, pa_rtclock_usec());
185
186 /* The connection can be closed when idle, so check to
187 see if we need to reestablish it */
188 if (u->fd < 0)
189 pa_raop_connect(u->raop);
190 else
191 pa_raop_flush(u->raop);
192 }
193
194 break;
195
196 case PA_SINK_UNLINKED:
197 case PA_SINK_INIT:
198 case PA_SINK_INVALID_STATE:
199 ;
200 }
201
202 break;
203
204 case PA_SINK_MESSAGE_GET_LATENCY: {
205 pa_usec_t w, r;
206
207 r = pa_smoother_get(u->smoother, pa_rtclock_usec());
208 w = pa_bytes_to_usec((u->offset - u->encoding_overhead + (u->encoded_memchunk.length / u->encoding_ratio)), &u->sink->sample_spec);
209
210 *((pa_usec_t*) data) = w > r ? w - r : 0;
211 break;
212 }
213
214 case SINK_MESSAGE_PASS_SOCKET: {
215 struct pollfd *pollfd;
216
217 pa_assert(!u->rtpoll_item);
218
219 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
220 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
221 pollfd->fd = u->fd;
222 pollfd->events = POLLOUT;
223 /*pollfd->events = */pollfd->revents = 0;
224
225 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
226 /* Our stream has been suspended so we just flush it.... */
227 pa_raop_flush(u->raop);
228 }
229 return 0;
230 }
231
232 case SINK_MESSAGE_RIP_SOCKET: {
233 pa_assert(u->fd >= 0);
234
235 pa_close(u->fd);
236 u->fd = -1;
237
238 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
239
240 pa_log_debug("RTSP control connection closed, but we're suspended so let's not worry about it... we'll open it again later");
241
242 if (u->rtpoll_item)
243 pa_rtpoll_item_free(u->rtpoll_item);
244 u->rtpoll_item = NULL;
245 } else {
246 /* Quesiton: is this valid here: or should we do some sort of:
247 return pa_sink_process_msg(PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL);
248 ?? */
249 pa_module_unload_request(u->module, TRUE);
250 }
251 return 0;
252 }
253 }
254
255 return pa_sink_process_msg(o, code, data, offset, chunk);
256 }
257
258 static void sink_get_volume_cb(pa_sink *s) {
259 struct userdata *u = s->userdata;
260 int i;
261
262 pa_assert(u);
263
264 for (i = 0; i < s->sample_spec.channels; i++)
265 s->virtual_volume.values[i] = u->volume;
266 }
267
268 static void sink_set_volume_cb(pa_sink *s) {
269 struct userdata *u = s->userdata;
270 int rv;
271
272 pa_assert(u);
273
274 /* If we're muted, we fake it */
275 if (u->muted)
276 return;
277
278 pa_assert(s->sample_spec.channels > 0);
279
280 /* Avoid pointless volume sets */
281 if (u->volume == s->virtual_volume.values[0])
282 return;
283
284 rv = pa_raop_client_set_volume(u->raop, s->virtual_volume.values[0]);
285 if (0 == rv)
286 u->volume = s->virtual_volume.values[0];
287 }
288
289 static void sink_get_mute_cb(pa_sink *s) {
290 struct userdata *u = s->userdata;
291
292 pa_assert(u);
293
294 s->muted = u->muted;
295 }
296
297 static void sink_set_mute_cb(pa_sink *s) {
298 struct userdata *u = s->userdata;
299
300 pa_assert(u);
301
302 pa_raop_client_set_volume(u->raop, (s->muted ? PA_VOLUME_MUTED : u->volume));
303 u->muted = s->muted;
304 }
305
306 static void thread_func(void *userdata) {
307 struct userdata *u = userdata;
308 int write_type = 0;
309 pa_memchunk silence;
310 uint32_t silence_overhead = 0;
311 double silence_ratio = 0;
312
313 pa_assert(u);
314
315 pa_log_debug("Thread starting up");
316
317 pa_thread_mq_install(&u->thread_mq);
318 pa_rtpoll_install(u->rtpoll);
319
320 pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
321
322 /* Create a chunk of memory that is our encoded silence sample. */
323 pa_memchunk_reset(&silence);
324
325 for (;;) {
326 int ret;
327
328 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
329 if (u->sink->thread_info.rewind_requested)
330 pa_sink_process_rewind(u->sink, 0);
331
332 if (u->rtpoll_item) {
333 struct pollfd *pollfd;
334 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
335
336 /* Render some data and write it to the fifo */
337 if (/*PA_SINK_IS_OPENED(u->sink->thread_info.state) && */pollfd->revents) {
338 pa_usec_t usec;
339 int64_t n;
340 void *p;
341
342 if (!silence.memblock) {
343 pa_memchunk silence_tmp;
344
345 pa_memchunk_reset(&silence_tmp);
346 silence_tmp.memblock = pa_memblock_new(u->core->mempool, 4096);
347 silence_tmp.length = 4096;
348 p = pa_memblock_acquire(silence_tmp.memblock);
349 memset(p, 0, 4096);
350 pa_memblock_release(silence_tmp.memblock);
351 pa_raop_client_encode_sample(u->raop, &silence_tmp, &silence);
352 pa_assert(0 == silence_tmp.length);
353 silence_overhead = silence_tmp.length - 4096;
354 silence_ratio = silence_tmp.length / 4096;
355 pa_memblock_unref(silence_tmp.memblock);
356 }
357
358 for (;;) {
359 ssize_t l;
360
361 if (u->encoded_memchunk.length <= 0) {
362 if (u->encoded_memchunk.memblock)
363 pa_memblock_unref(u->encoded_memchunk.memblock);
364 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
365 size_t rl;
366
367 /* We render real data */
368 if (u->raw_memchunk.length <= 0) {
369 if (u->raw_memchunk.memblock)
370 pa_memblock_unref(u->raw_memchunk.memblock);
371 pa_memchunk_reset(&u->raw_memchunk);
372
373 /* Grab unencoded data */
374 pa_sink_render(u->sink, u->block_size, &u->raw_memchunk);
375 }
376 pa_assert(u->raw_memchunk.length > 0);
377
378 /* Encode it */
379 rl = u->raw_memchunk.length;
380 u->encoding_overhead += u->next_encoding_overhead;
381 pa_raop_client_encode_sample(u->raop, &u->raw_memchunk, &u->encoded_memchunk);
382 u->next_encoding_overhead = (u->encoded_memchunk.length - (rl - u->raw_memchunk.length));
383 u->encoding_ratio = u->encoded_memchunk.length / (rl - u->raw_memchunk.length);
384 } else {
385 /* We render some silence into our memchunk */
386 memcpy(&u->encoded_memchunk, &silence, sizeof(pa_memchunk));
387 pa_memblock_ref(silence.memblock);
388
389 /* Calculate/store some values to be used with the smoother */
390 u->next_encoding_overhead = silence_overhead;
391 u->encoding_ratio = silence_ratio;
392 }
393 }
394 pa_assert(u->encoded_memchunk.length > 0);
395
396 p = pa_memblock_acquire(u->encoded_memchunk.memblock);
397 l = pa_write(u->fd, (uint8_t*) p + u->encoded_memchunk.index, u->encoded_memchunk.length, &write_type);
398 pa_memblock_release(u->encoded_memchunk.memblock);
399
400 pa_assert(l != 0);
401
402 if (l < 0) {
403
404 if (errno == EINTR)
405 continue;
406 else if (errno == EAGAIN) {
407
408 /* OK, we filled all socket buffers up
409 * now. */
410 goto filled_up;
411
412 } else {
413 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
414 goto fail;
415 }
416
417 } else {
418 u->offset += l;
419
420 u->encoded_memchunk.index += l;
421 u->encoded_memchunk.length -= l;
422
423 pollfd->revents = 0;
424
425 if (u->encoded_memchunk.length > 0) {
426 /* we've completely written the encoded data, so update our overhead */
427 u->encoding_overhead += u->next_encoding_overhead;
428
429 /* OK, we wrote less that we asked for,
430 * hence we can assume that the socket
431 * buffers are full now */
432 goto filled_up;
433 }
434 }
435 }
436
437 filled_up:
438
439 /* At this spot we know that the socket buffers are
440 * fully filled up. This is the best time to estimate
441 * the playback position of the server */
442
443 n = u->offset - u->encoding_overhead;
444
445 #ifdef SIOCOUTQ
446 {
447 int l;
448 if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
449 n -= (l / u->encoding_ratio);
450 }
451 #endif
452
453 usec = pa_bytes_to_usec(n, &u->sink->sample_spec);
454
455 if (usec > u->latency)
456 usec -= u->latency;
457 else
458 usec = 0;
459
460 pa_smoother_put(u->smoother, pa_rtclock_usec(), usec);
461 }
462
463 /* Hmm, nothing to do. Let's sleep */
464 pollfd->events = POLLOUT; /*PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0;*/
465 }
466
467 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
468 goto fail;
469
470 if (ret == 0)
471 goto finish;
472
473 if (u->rtpoll_item) {
474 struct pollfd* pollfd;
475
476 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
477
478 if (pollfd->revents & ~POLLOUT) {
479 if (u->sink->thread_info.state != PA_SINK_SUSPENDED) {
480 pa_log("FIFO shutdown.");
481 goto fail;
482 }
483
484 /* We expect this to happen on occasion if we are not sending data.
485 It's perfectly natural and normal and natural */
486 if (u->rtpoll_item)
487 pa_rtpoll_item_free(u->rtpoll_item);
488 u->rtpoll_item = NULL;
489 }
490 }
491 }
492
493 fail:
494 /* If this was no regular exit from the loop we have to continue
495 * processing messages until we received PA_MESSAGE_SHUTDOWN */
496 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
497 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
498
499 finish:
500 if (silence.memblock)
501 pa_memblock_unref(silence.memblock);
502 pa_log_debug("Thread shutting down");
503 }
504
505 int pa__init(pa_module*m) {
506 struct userdata *u = NULL;
507 pa_sample_spec ss;
508 pa_modargs *ma = NULL;
509 const char *server;
510 pa_sink_new_data data;
511
512 pa_assert(m);
513
514 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
515 pa_log("failed to parse module arguments");
516 goto fail;
517 }
518
519 ss = m->core->default_sample_spec;
520 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
521 pa_log("invalid sample format specification");
522 goto fail;
523 }
524
525 if ((/*ss.format != PA_SAMPLE_U8 &&*/ ss.format != PA_SAMPLE_S16NE) ||
526 (ss.channels > 2)) {
527 pa_log("sample type support is limited to mono/stereo and U8 or S16NE sample data");
528 goto fail;
529 }
530
531 u = pa_xnew0(struct userdata, 1);
532 u->core = m->core;
533 u->module = m;
534 m->userdata = u;
535 u->fd = -1;
536 u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10);
537 pa_memchunk_reset(&u->raw_memchunk);
538 pa_memchunk_reset(&u->encoded_memchunk);
539 u->offset = 0;
540 u->encoding_overhead = 0;
541 u->next_encoding_overhead = 0;
542 u->encoding_ratio = 1.0;
543
544 u->volume = roundf(0.7 * PA_VOLUME_NORM);
545 u->muted = FALSE;
546
547 u->rtpoll = pa_rtpoll_new();
548 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
549 u->rtpoll_item = NULL;
550
551 /*u->format =
552 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
553 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);*/
554 u->rate = ss.rate;
555 u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
556
557 u->read_data = u->write_data = NULL;
558 u->read_index = u->write_index = u->read_length = u->write_length = 0;
559
560 /*u->state = STATE_AUTH;*/
561 u->latency = 0;
562
563 if (!(server = pa_modargs_get_value(ma, "server", NULL))) {
564 pa_log("No server argument given.");
565 goto fail;
566 }
567
568 pa_sink_new_data_init(&data);
569 data.driver = __FILE__;
570 data.module = m;
571 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
572 pa_sink_new_data_set_sample_spec(&data, &ss);
573 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server);
574 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Airtunes sink '%s'", server);
575
576 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
577 pa_sink_new_data_done(&data);
578
579 if (!u->sink) {
580 pa_log("Failed to create sink.");
581 goto fail;
582 }
583
584 u->sink->parent.process_msg = sink_process_msg;
585 u->sink->userdata = u;
586 u->sink->get_volume = sink_get_volume_cb;
587 u->sink->set_volume = sink_set_volume_cb;
588 u->sink->get_mute = sink_get_mute_cb;
589 u->sink->set_mute = sink_set_mute_cb;
590 u->sink->flags = PA_SINK_LATENCY|PA_SINK_NETWORK|PA_SINK_HW_VOLUME_CTRL;
591
592 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
593 pa_sink_set_rtpoll(u->sink, u->rtpoll);
594
595 if (!(u->raop = pa_raop_client_new(u->core, server))) {
596 pa_log("Failed to connect to server.");
597 goto fail;
598 }
599
600 pa_raop_client_set_callback(u->raop, on_connection, u);
601 pa_raop_client_set_closed_callback(u->raop, on_close, u);
602
603 if (!(u->thread = pa_thread_new(thread_func, u))) {
604 pa_log("Failed to create thread.");
605 goto fail;
606 }
607
608 pa_sink_put(u->sink);
609
610 pa_modargs_free(ma);
611
612 return 0;
613
614 fail:
615 if (ma)
616 pa_modargs_free(ma);
617
618 pa__done(m);
619
620 return -1;
621 }
622
623 int pa__get_n_used(pa_module *m) {
624 struct userdata *u;
625
626 pa_assert(m);
627 pa_assert_se(u = m->userdata);
628
629 return pa_sink_linked_by(u->sink);
630 }
631
632 void pa__done(pa_module*m) {
633 struct userdata *u;
634 pa_assert(m);
635
636 if (!(u = m->userdata))
637 return;
638
639 if (u->sink)
640 pa_sink_unlink(u->sink);
641
642 if (u->thread) {
643 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
644 pa_thread_free(u->thread);
645 }
646
647 pa_thread_mq_done(&u->thread_mq);
648
649 if (u->sink)
650 pa_sink_unref(u->sink);
651
652 if (u->rtpoll_item)
653 pa_rtpoll_item_free(u->rtpoll_item);
654
655 if (u->rtpoll)
656 pa_rtpoll_free(u->rtpoll);
657
658 if (u->raw_memchunk.memblock)
659 pa_memblock_unref(u->raw_memchunk.memblock);
660
661 if (u->encoded_memchunk.memblock)
662 pa_memblock_unref(u->encoded_memchunk.memblock);
663
664 if (u->raop)
665 pa_raop_client_free(u->raop);
666
667 pa_xfree(u->read_data);
668 pa_xfree(u->write_data);
669
670 if (u->smoother)
671 pa_smoother_free(u->smoother);
672
673 if (u->fd >= 0)
674 pa_close(u->fd);
675
676 pa_xfree(u);
677 }