]> code.delx.au - pulseaudio/blob - src/modules/module-raop-sink.c
A few related changes:
[pulseaudio] / src / modules / module-raop-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2008 Colin Guthrie
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #include <poll.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <sys/ioctl.h>
41
42 #ifdef HAVE_LINUX_SOCKIOS_H
43 #include <linux/sockios.h>
44 #endif
45
46 #include <pulse/xmalloc.h>
47 #include <pulse/timeval.h>
48
49 #include <pulsecore/core-error.h>
50 #include <pulsecore/iochannel.h>
51 #include <pulsecore/sink.h>
52 #include <pulsecore/module.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/authkey.h>
58 #include <pulsecore/thread-mq.h>
59 #include <pulsecore/thread.h>
60 #include <pulsecore/time-smoother.h>
61 #include <pulsecore/rtclock.h>
62 #include <pulsecore/socket-util.h>
63
64 #include "module-raop-sink-symdef.h"
65 #include "rtp.h"
66 #include "sdp.h"
67 #include "sap.h"
68 #include "raop_client.h"
69
70 PA_MODULE_AUTHOR("Colin Guthrie");
71 PA_MODULE_DESCRIPTION("RAOP Sink (Apple Airtunes)");
72 PA_MODULE_VERSION(PACKAGE_VERSION);
73 PA_MODULE_LOAD_ONCE(FALSE);
74 PA_MODULE_USAGE(
75 "sink_name=<name for the sink> "
76 "server=<address> cookie=<filename> "
77 "format=<sample format> "
78 "channels=<number of channels> "
79 "rate=<sample rate>");
80
81 #define DEFAULT_SINK_NAME "airtunes"
82
83 struct userdata {
84 pa_core *core;
85 pa_module *module;
86 pa_sink *sink;
87
88 pa_thread_mq thread_mq;
89 pa_rtpoll *rtpoll;
90 pa_rtpoll_item *rtpoll_item;
91 pa_thread *thread;
92
93 pa_memchunk raw_memchunk;
94 pa_memchunk encoded_memchunk;
95
96 void *write_data;
97 size_t write_length, write_index;
98
99 void *read_data;
100 size_t read_length, read_index;
101
102 pa_usec_t latency;
103
104 /*esd_format_t format;*/
105 int32_t rate;
106
107 pa_smoother *smoother;
108 int fd;
109
110 int64_t offset;
111 int64_t encoding_overhead;
112 int32_t next_encoding_overhead;
113 double encoding_ratio;
114
115 pa_raop_client *raop;
116
117 size_t block_size;
118 };
119
120 static const char* const valid_modargs[] = {
121 "server",
122 "rate",
123 "format",
124 "channels",
125 "sink_name",
126 NULL
127 };
128
129 enum {
130 SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX
131 };
132
133 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
134 struct userdata *u = PA_SINK(o)->userdata;
135
136 switch (code) {
137
138 case PA_SINK_MESSAGE_SET_STATE:
139
140 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
141
142 case PA_SINK_SUSPENDED:
143 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
144
145 pa_smoother_pause(u->smoother, pa_rtclock_usec());
146 break;
147
148 case PA_SINK_IDLE:
149 case PA_SINK_RUNNING:
150
151 if (u->sink->thread_info.state == PA_SINK_SUSPENDED)
152 pa_smoother_resume(u->smoother, pa_rtclock_usec());
153
154 break;
155
156 case PA_SINK_UNLINKED:
157 case PA_SINK_INIT:
158 ;
159 }
160
161 break;
162
163 case PA_SINK_MESSAGE_GET_LATENCY: {
164 pa_usec_t w, r;
165
166 r = pa_smoother_get(u->smoother, pa_rtclock_usec());
167 w = pa_bytes_to_usec((u->offset - u->encoding_overhead + (u->encoded_memchunk.length / u->encoding_ratio)), &u->sink->sample_spec);
168
169 *((pa_usec_t*) data) = w > r ? w - r : 0;
170 break;
171 }
172
173 case SINK_MESSAGE_PASS_SOCKET: {
174 struct pollfd *pollfd;
175
176 pa_assert(!u->rtpoll_item);
177
178 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
179 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
180 pollfd->fd = u->fd;
181 pollfd->events = POLLOUT;
182 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 pa_memchunk silence;
195 uint32_t silence_overhead;
196 double silence_ratio;
197
198 pa_assert(u);
199
200 pa_log_debug("Thread starting up");
201
202 pa_thread_mq_install(&u->thread_mq);
203 pa_rtpoll_install(u->rtpoll);
204
205 pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
206
207 /* Create a chunk of memory that is our encoded silence sample. */
208 pa_memchunk_reset(&silence);
209
210 for (;;) {
211 int ret;
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 (pollfd->revents) {
219 pa_usec_t usec;
220 int64_t n;
221 void *p;
222
223 if (!silence.memblock) {
224 pa_memchunk silence_tmp;
225
226 pa_memchunk_reset(&silence_tmp);
227 silence_tmp.memblock = pa_memblock_new(u->core->mempool, 4096);
228 silence_tmp.length = 4096;
229 p = pa_memblock_acquire(silence_tmp.memblock);
230 memset(p, 0, 4096);
231 pa_memblock_release(silence_tmp.memblock);
232 pa_raop_client_encode_sample(u->raop, &silence_tmp, &silence);
233 pa_assert(0 == silence_tmp.length);
234 silence_overhead = silence_tmp.length - 4096;
235 silence_ratio = silence_tmp.length / 4096;
236 pa_memblock_unref(silence_tmp.memblock);
237 }
238
239 for (;;) {
240 ssize_t l;
241
242 if (u->encoded_memchunk.length <= 0) {
243 if (u->encoded_memchunk.memblock)
244 pa_memblock_unref(u->encoded_memchunk.memblock);
245 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
246 size_t rl;
247
248 /* We render real data */
249 if (u->raw_memchunk.length <= 0) {
250 if (u->raw_memchunk.memblock)
251 pa_memblock_unref(u->raw_memchunk.memblock);
252 pa_memchunk_reset(&u->raw_memchunk);
253
254 /* Grab unencoded data */
255 pa_sink_render(u->sink, u->block_size, &u->raw_memchunk);
256 }
257 pa_assert(u->raw_memchunk.length > 0);
258
259 /* Encode it */
260 rl = u->raw_memchunk.length;
261 u->encoding_overhead += u->next_encoding_overhead;
262 pa_raop_client_encode_sample(u->raop, &u->raw_memchunk, &u->encoded_memchunk);
263 u->next_encoding_overhead = (u->encoded_memchunk.length - (rl - u->raw_memchunk.length));
264 u->encoding_ratio = u->encoded_memchunk.length / (rl - u->raw_memchunk.length);
265 } else {
266 /* We render some silence into our memchunk */
267 u->encoding_overhead += u->next_encoding_overhead;
268 memcpy(&u->encoded_memchunk, &silence, sizeof(pa_memchunk));
269 pa_memblock_ref(silence.memblock);
270 u->next_encoding_overhead = silence_overhead;
271 u->encoding_ratio = silence_ratio;
272 }
273 }
274 pa_assert(u->encoded_memchunk.length > 0);
275
276 p = pa_memblock_acquire(u->encoded_memchunk.memblock);
277 l = pa_write(u->fd, (uint8_t*) p + u->encoded_memchunk.index, u->encoded_memchunk.length, &write_type);
278 pa_memblock_release(u->encoded_memchunk.memblock);
279
280 pa_assert(l != 0);
281
282 if (l < 0) {
283
284 if (errno == EINTR)
285 continue;
286 else if (errno == EAGAIN) {
287
288 /* OK, we filled all socket buffers up
289 * now. */
290 goto filled_up;
291
292 } else {
293 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
294 goto fail;
295 }
296
297 } else {
298 u->offset += l;
299
300 u->encoded_memchunk.index += l;
301 u->encoded_memchunk.length -= l;
302
303 pollfd->revents = 0;
304
305 if (u->encoded_memchunk.length > 0)
306
307 /* OK, we wrote less that we asked for,
308 * hence we can assume that the socket
309 * buffers are full now */
310 goto filled_up;
311 }
312 }
313
314 filled_up:
315
316 /* At this spot we know that the socket buffers are
317 * fully filled up. This is the best time to estimate
318 * the playback position of the server */
319
320 n = u->offset;
321
322 #ifdef SIOCOUTQ
323 {
324 int l;
325 if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0)
326 n -= l;
327 }
328 #endif
329
330 usec = pa_bytes_to_usec(n, &u->sink->sample_spec);
331
332 if (usec > u->latency)
333 usec -= u->latency;
334 else
335 usec = 0;
336
337 pa_smoother_put(u->smoother, pa_rtclock_usec(), usec);
338 }
339
340 /* Hmm, nothing to do. Let's sleep */
341 /* pollfd->events = PA_SINK_OPENED(u->sink->thread_info.state) ? POLLOUT : 0; */
342 }
343
344 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
345 goto fail;
346
347 if (ret == 0)
348 goto finish;
349
350 if (u->rtpoll_item) {
351 struct pollfd* pollfd;
352
353 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
354
355 if (pollfd->revents & ~POLLOUT) {
356 pa_log("FIFO shutdown.");
357 goto fail;
358 }
359 }
360 }
361
362 fail:
363 /* If this was no regular exit from the loop we have to continue
364 * processing messages until we received PA_MESSAGE_SHUTDOWN */
365 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
366 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
367
368 finish:
369 if (silence.memblock)
370 pa_memblock_unref(silence.memblock);
371 pa_log_debug("Thread shutting down");
372 }
373
374 static void on_connection(PA_GCC_UNUSED int fd, void*userdata) {
375 struct userdata *u = userdata;
376 pa_assert(u);
377
378 pa_assert(u->fd < 0);
379 u->fd = fd;
380
381 pa_log_debug("Connection authenticated, handing fd to IO thread...");
382
383 pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL);
384 }
385
386 static void on_close(void*userdata) {
387 struct userdata *u = userdata;
388 pa_assert(u);
389
390 pa_log_debug("Control connection closed.");
391 pa_module_unload_request(u->module);
392 }
393
394 int pa__init(pa_module*m) {
395 struct userdata *u = NULL;
396 const char *p;
397 pa_sample_spec ss;
398 pa_modargs *ma = NULL;
399 char *t;
400
401 pa_assert(m);
402
403 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
404 pa_log("failed to parse module arguments");
405 goto fail;
406 }
407
408 ss = m->core->default_sample_spec;
409 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
410 pa_log("invalid sample format specification");
411 goto fail;
412 }
413
414 if ((/*ss.format != PA_SAMPLE_U8 &&*/ ss.format != PA_SAMPLE_S16NE) ||
415 (ss.channels > 2)) {
416 pa_log("sample type support is limited to mono/stereo and U8 or S16NE sample data");
417 goto fail;
418 }
419
420 u = pa_xnew0(struct userdata, 1);
421 u->core = m->core;
422 u->module = m;
423 m->userdata = u;
424 u->fd = -1;
425 u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE);
426 pa_memchunk_reset(&u->raw_memchunk);
427 pa_memchunk_reset(&u->encoded_memchunk);
428 u->offset = 0;
429 u->encoding_overhead = 0;
430 u->next_encoding_overhead = 0;
431 u->encoding_ratio = 1.0;
432
433 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
434 u->rtpoll = pa_rtpoll_new();
435 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
436 u->rtpoll_item = NULL;
437
438 /*u->format =
439 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
440 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);*/
441 u->rate = ss.rate;
442 u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss);
443
444 u->read_data = u->write_data = NULL;
445 u->read_index = u->write_index = u->read_length = u->write_length = 0;
446
447 /*u->state = STATE_AUTH;*/
448 u->latency = 0;
449
450 if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) {
451 pa_log("Failed to create sink.");
452 goto fail;
453 }
454
455 u->sink->parent.process_msg = sink_process_msg;
456 u->sink->userdata = u;
457 u->sink->flags = PA_SINK_LATENCY|PA_SINK_NETWORK;
458
459 pa_sink_set_module(u->sink, m);
460 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
461 pa_sink_set_rtpoll(u->sink, u->rtpoll);
462
463 if (!(p = pa_modargs_get_value(ma, "server", NULL))) {
464 pa_log("No server argument given.");
465 goto fail;
466 }
467
468 if (!(u->raop = pa_raop_client_new(u->core, p))) {
469 pa_log("Failed to connect to server.");
470 goto fail;
471 }
472
473 pa_raop_client_set_callback(u->raop, on_connection, u);
474 pa_raop_client_set_closed_callback(u->raop, on_close, u);
475 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Airtunes sink '%s'", p));
476 pa_xfree(t);
477
478
479 if (!(u->thread = pa_thread_new(thread_func, u))) {
480 pa_log("Failed to create thread.");
481 goto fail;
482 }
483
484 pa_sink_put(u->sink);
485
486 pa_modargs_free(ma);
487
488 return 0;
489
490 fail:
491 if (ma)
492 pa_modargs_free(ma);
493
494 pa__done(m);
495
496 return -1;
497 }
498
499 void pa__done(pa_module*m) {
500 struct userdata *u;
501 pa_assert(m);
502
503 if (!(u = m->userdata))
504 return;
505
506 if (u->sink)
507 pa_sink_unlink(u->sink);
508
509 if (u->thread) {
510 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
511 pa_thread_free(u->thread);
512 }
513
514 pa_thread_mq_done(&u->thread_mq);
515
516 if (u->sink)
517 pa_sink_unref(u->sink);
518
519 if (u->rtpoll_item)
520 pa_rtpoll_item_free(u->rtpoll_item);
521
522 if (u->rtpoll)
523 pa_rtpoll_free(u->rtpoll);
524
525 if (u->raw_memchunk.memblock)
526 pa_memblock_unref(u->raw_memchunk.memblock);
527
528 if (u->encoded_memchunk.memblock)
529 pa_memblock_unref(u->encoded_memchunk.memblock);
530
531 if (u->raop)
532 pa_raop_client_free(u->raop);
533
534 pa_xfree(u->read_data);
535 pa_xfree(u->write_data);
536
537 if (u->smoother)
538 pa_smoother_free(u->smoother);
539
540 if (u->fd >= 0)
541 pa_close(u->fd);
542
543 pa_xfree(u);
544 }