Commit | Line | Data |
---|---|---|
e83d0967 RK |
1 | /* |
2 | * This file is part of DisOrder. | |
5aff007d | 3 | * Copyright (C) 2007, 2008 Richard Kettlewell |
e83d0967 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
e83d0967 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
e83d0967 RK |
8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
e83d0967 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
e83d0967 | 17 | */ |
28bacdc0 RK |
18 | /** @file clients/playrtp.c |
19 | * @brief RTP player | |
20 | * | |
b0fdc63d | 21 | * This player supports Linux (<a href="http://www.alsa-project.org/">ALSA</a>) |
22 | * and Apple Mac (<a | |
23 | * href="http://developer.apple.com/audio/coreaudio.html">Core Audio</a>) | |
24 | * systems. There is no support for Microsoft Windows yet, and that will in | |
25 | * fact probably an entirely separate program. | |
26 | * | |
8d251217 RK |
27 | * The program runs (at least) three threads: |
28 | * | |
29 | * listen_thread() is responsible for reading RTP packets off the wire and | |
30 | * adding them to the linked list @ref received_packets, assuming they are | |
31 | * basically sound. | |
32 | * | |
33 | * queue_thread() takes packets off this linked list and adds them to @ref | |
34 | * packets (an operation which might be much slower due to contention for @ref | |
35 | * lock). | |
36 | * | |
37 | * control_thread() accepts commands from Disobedience (or anything else). | |
38 | * | |
39 | * The main thread activates and deactivates audio playing via the @ref | |
40 | * lib/uaudio.h API (which probably implies at least one further thread). | |
b0fdc63d | 41 | * |
42 | * Sometimes it happens that there is no audio available to play. This may | |
43 | * because the server went away, or a packet was dropped, or the server | |
44 | * deliberately did not send any sound because it encountered a silence. | |
189e9830 RK |
45 | * |
46 | * Assumptions: | |
47 | * - it is safe to read uint32_t values without a lock protecting them | |
28bacdc0 | 48 | */ |
e83d0967 | 49 | |
05b75f8d | 50 | #include "common.h" |
e83d0967 RK |
51 | |
52 | #include <getopt.h> | |
e83d0967 RK |
53 | #include <sys/socket.h> |
54 | #include <sys/types.h> | |
55 | #include <sys/socket.h> | |
56 | #include <netdb.h> | |
57 | #include <pthread.h> | |
0b75463f | 58 | #include <locale.h> |
2c7c9eae | 59 | #include <sys/uio.h> |
c593cf7c | 60 | #include <errno.h> |
e3426f7b | 61 | #include <netinet/in.h> |
2d2effe2 | 62 | #include <sys/time.h> |
a99c4e9a | 63 | #include <sys/un.h> |
9fbe0996 | 64 | #include <unistd.h> |
e9b635a3 RK |
65 | #include <sys/mman.h> |
66 | #include <fcntl.h> | |
e83d0967 RK |
67 | |
68 | #include "log.h" | |
69 | #include "mem.h" | |
70 | #include "configuration.h" | |
71 | #include "addr.h" | |
72 | #include "syscalls.h" | |
73 | #include "rtp.h" | |
0b75463f | 74 | #include "defs.h" |
28bacdc0 RK |
75 | #include "vector.h" |
76 | #include "heap.h" | |
189e9830 | 77 | #include "timeval.h" |
a7e9570a | 78 | #include "client.h" |
8e3fe3d8 | 79 | #include "playrtp.h" |
a99c4e9a | 80 | #include "inputline.h" |
3fbdc96d | 81 | #include "version.h" |
7a2c7068 | 82 | #include "uaudio.h" |
e83d0967 | 83 | |
1153fd23 | 84 | #define readahead linux_headers_are_borked |
85 | ||
e3426f7b RK |
86 | /** @brief Obsolete synonym */ |
87 | #ifndef IPV6_JOIN_GROUP | |
88 | # define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP | |
89 | #endif | |
90 | ||
0b75463f | 91 | /** @brief RTP socket */ |
e83d0967 RK |
92 | static int rtpfd; |
93 | ||
345ebe66 RK |
94 | /** @brief Log output */ |
95 | static FILE *logfp; | |
96 | ||
0b75463f | 97 | /** @brief Output device */ |
0b75463f | 98 | |
9086a105 | 99 | /** @brief Minimum low watermark |
0b75463f | 100 | * |
101 | * We'll stop playing if there's only this many samples in the buffer. */ | |
c593cf7c | 102 | unsigned minbuffer = 2 * 44100 / 10; /* 0.2 seconds */ |
0b75463f | 103 | |
9086a105 | 104 | /** @brief Buffer high watermark |
1153fd23 | 105 | * |
106 | * We'll only start playing when this many samples are available. */ | |
c35b9279 | 107 | static unsigned readahead = 44100; /* 0.5 seconds */ |
0b75463f | 108 | |
9086a105 RK |
109 | /** @brief Maximum buffer size |
110 | * | |
111 | * We'll stop reading from the network if we have this many samples. */ | |
112 | static unsigned maxbuffer; | |
113 | ||
189e9830 RK |
114 | /** @brief Received packets |
115 | * Protected by @ref receive_lock | |
116 | * | |
117 | * Received packets are added to this list, and queue_thread() picks them off | |
118 | * it and adds them to @ref packets. Whenever a packet is added to it, @ref | |
119 | * receive_cond is signalled. | |
120 | */ | |
8e3fe3d8 | 121 | struct packet *received_packets; |
189e9830 RK |
122 | |
123 | /** @brief Tail of @ref received_packets | |
124 | * Protected by @ref receive_lock | |
125 | */ | |
8e3fe3d8 | 126 | struct packet **received_tail = &received_packets; |
189e9830 RK |
127 | |
128 | /** @brief Lock protecting @ref received_packets | |
129 | * | |
130 | * Only listen_thread() and queue_thread() ever hold this lock. It is vital | |
131 | * that queue_thread() not hold it any longer than it strictly has to. */ | |
8e3fe3d8 | 132 | pthread_mutex_t receive_lock = PTHREAD_MUTEX_INITIALIZER; |
189e9830 RK |
133 | |
134 | /** @brief Condition variable signalled when @ref received_packets is updated | |
135 | * | |
136 | * Used by listen_thread() to notify queue_thread() that it has added another | |
137 | * packet to @ref received_packets. */ | |
8e3fe3d8 | 138 | pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER; |
189e9830 RK |
139 | |
140 | /** @brief Length of @ref received_packets */ | |
8e3fe3d8 | 141 | uint32_t nreceived; |
28bacdc0 RK |
142 | |
143 | /** @brief Binary heap of received packets */ | |
8e3fe3d8 | 144 | struct pheap packets; |
28bacdc0 | 145 | |
189e9830 RK |
146 | /** @brief Total number of samples available |
147 | * | |
148 | * We make this volatile because we inspect it without a protecting lock, | |
149 | * so the usual pthread_* guarantees aren't available. | |
150 | */ | |
8e3fe3d8 | 151 | volatile uint32_t nsamples; |
0b75463f | 152 | |
153 | /** @brief Timestamp of next packet to play. | |
154 | * | |
155 | * This is set to the timestamp of the last packet, plus the number of | |
09ee2f0d | 156 | * samples it contained. Only valid if @ref active is nonzero. |
0b75463f | 157 | */ |
8e3fe3d8 | 158 | uint32_t next_timestamp; |
e83d0967 | 159 | |
09ee2f0d | 160 | /** @brief True if actively playing |
161 | * | |
162 | * This is true when playing and false when just buffering. */ | |
8e3fe3d8 | 163 | int active; |
09ee2f0d | 164 | |
189e9830 | 165 | /** @brief Lock protecting @ref packets */ |
8e3fe3d8 | 166 | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
189e9830 RK |
167 | |
168 | /** @brief Condition variable signalled whenever @ref packets is changed */ | |
8e3fe3d8 | 169 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
2c7c9eae | 170 | |
c593cf7c | 171 | /** @brief Backend to play with */ |
7a2c7068 | 172 | static const struct uaudio *backend; |
c593cf7c | 173 | |
8e3fe3d8 | 174 | HEAP_DEFINE(pheap, struct packet *, lt_packet); |
e83d0967 | 175 | |
a99c4e9a RK |
176 | /** @brief Control socket or NULL */ |
177 | const char *control_socket; | |
178 | ||
b28bddbb RK |
179 | /** @brief Buffer for debugging dump |
180 | * | |
181 | * The debug dump is enabled by the @c --dump option. It records the last 20s | |
182 | * of audio to the specified file (which will be about 3.5Mbytes). The file is | |
183 | * written as as ring buffer, so the start point will progress through it. | |
184 | * | |
185 | * Use clients/dump2wav to convert this to a WAV file, which can then be loaded | |
186 | * into (e.g.) Audacity for further inspection. | |
187 | * | |
188 | * All three backends (ALSA, OSS, Core Audio) now support this option. | |
189 | * | |
190 | * The idea is to allow the user a few seconds to react to an audible artefact. | |
191 | */ | |
e9b635a3 | 192 | int16_t *dump_buffer; |
b28bddbb RK |
193 | |
194 | /** @brief Current index within debugging dump */ | |
e9b635a3 | 195 | size_t dump_index; |
b28bddbb RK |
196 | |
197 | /** @brief Size of debugging dump in samples */ | |
198 | size_t dump_size = 44100/*Hz*/ * 2/*channels*/ * 20/*seconds*/; | |
e9b635a3 | 199 | |
e83d0967 RK |
200 | static const struct option options[] = { |
201 | { "help", no_argument, 0, 'h' }, | |
202 | { "version", no_argument, 0, 'V' }, | |
203 | { "debug", no_argument, 0, 'd' }, | |
0b75463f | 204 | { "device", required_argument, 0, 'D' }, |
1153fd23 | 205 | { "min", required_argument, 0, 'm' }, |
9086a105 | 206 | { "max", required_argument, 0, 'x' }, |
1153fd23 | 207 | { "buffer", required_argument, 0, 'b' }, |
1f10f780 | 208 | { "rcvbuf", required_argument, 0, 'R' }, |
a9f0ad12 | 209 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 210 | { "oss", no_argument, 0, 'o' }, |
211 | #endif | |
146e86fb | 212 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 213 | { "alsa", no_argument, 0, 'a' }, |
214 | #endif | |
215 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
216 | { "core-audio", no_argument, 0, 'c' }, | |
217 | #endif | |
e9b635a3 | 218 | { "dump", required_argument, 0, 'r' }, |
e979b844 | 219 | { "command", required_argument, 0, 'e' }, |
287ad384 | 220 | { "pause-mode", required_argument, 0, 'P' }, |
a99c4e9a | 221 | { "socket", required_argument, 0, 's' }, |
a7e9570a | 222 | { "config", required_argument, 0, 'C' }, |
e83d0967 RK |
223 | { 0, 0, 0, 0 } |
224 | }; | |
225 | ||
a99c4e9a RK |
226 | /** @brief Control thread |
227 | * | |
228 | * This thread is responsible for accepting control commands from Disobedience | |
229 | * (or other controllers) over an AF_UNIX stream socket with a path specified | |
230 | * by the @c --socket option. The protocol uses simple string commands and | |
231 | * replies: | |
232 | * | |
233 | * - @c stop will shut the player down | |
234 | * - @c query will send back the reply @c running | |
235 | * - anything else is ignored | |
236 | * | |
237 | * Commands and response strings terminated by shutting down the connection or | |
238 | * by a newline. No attempt is made to multiplex multiple clients so it is | |
239 | * important that the command be sent as soon as the connection is made - it is | |
240 | * assumed that both parties to the protocol are entirely cooperating with one | |
241 | * another. | |
242 | */ | |
243 | static void *control_thread(void attribute((unused)) *arg) { | |
244 | struct sockaddr_un sa; | |
245 | int sfd, cfd; | |
246 | char *line; | |
247 | socklen_t salen; | |
248 | FILE *fp; | |
249 | ||
250 | assert(control_socket); | |
251 | unlink(control_socket); | |
252 | memset(&sa, 0, sizeof sa); | |
253 | sa.sun_family = AF_UNIX; | |
254 | strcpy(sa.sun_path, control_socket); | |
255 | sfd = xsocket(PF_UNIX, SOCK_STREAM, 0); | |
256 | if(bind(sfd, (const struct sockaddr *)&sa, sizeof sa) < 0) | |
257 | fatal(errno, "error binding to %s", control_socket); | |
258 | if(listen(sfd, 128) < 0) | |
259 | fatal(errno, "error calling listen on %s", control_socket); | |
260 | info("listening on %s", control_socket); | |
261 | for(;;) { | |
262 | salen = sizeof sa; | |
263 | cfd = accept(sfd, (struct sockaddr *)&sa, &salen); | |
264 | if(cfd < 0) { | |
265 | switch(errno) { | |
266 | case EINTR: | |
267 | case EAGAIN: | |
268 | break; | |
269 | default: | |
270 | fatal(errno, "error calling accept on %s", control_socket); | |
271 | } | |
272 | } | |
273 | if(!(fp = fdopen(cfd, "r+"))) { | |
274 | error(errno, "error calling fdopen for %s connection", control_socket); | |
275 | close(cfd); | |
276 | continue; | |
277 | } | |
278 | if(!inputline(control_socket, fp, &line, '\n')) { | |
279 | if(!strcmp(line, "stop")) { | |
280 | info("stopped via %s", control_socket); | |
281 | exit(0); /* terminate immediately */ | |
282 | } | |
283 | if(!strcmp(line, "query")) | |
284 | fprintf(fp, "running"); | |
285 | xfree(line); | |
286 | } | |
287 | if(fclose(fp) < 0) | |
288 | error(errno, "error closing %s connection", control_socket); | |
289 | } | |
290 | } | |
291 | ||
28bacdc0 RK |
292 | /** @brief Drop the first packet |
293 | * | |
294 | * Assumes that @ref lock is held. | |
295 | */ | |
296 | static void drop_first_packet(void) { | |
297 | if(pheap_count(&packets)) { | |
298 | struct packet *const p = pheap_remove(&packets); | |
299 | nsamples -= p->nsamples; | |
c593cf7c | 300 | playrtp_free_packet(p); |
2c7c9eae | 301 | pthread_cond_broadcast(&cond); |
2c7c9eae | 302 | } |
9086a105 RK |
303 | } |
304 | ||
189e9830 RK |
305 | /** @brief Background thread adding packets to heap |
306 | * | |
307 | * This just transfers packets from @ref received_packets to @ref packets. It | |
308 | * is important that it holds @ref receive_lock for as little time as possible, | |
309 | * in order to minimize the interval between calls to read() in | |
310 | * listen_thread(). | |
311 | */ | |
312 | static void *queue_thread(void attribute((unused)) *arg) { | |
313 | struct packet *p; | |
314 | ||
315 | for(;;) { | |
316 | /* Get the next packet */ | |
317 | pthread_mutex_lock(&receive_lock); | |
4dadf1a2 | 318 | while(!received_packets) { |
189e9830 | 319 | pthread_cond_wait(&receive_cond, &receive_lock); |
4dadf1a2 | 320 | } |
189e9830 RK |
321 | p = received_packets; |
322 | received_packets = p->next; | |
323 | if(!received_packets) | |
324 | received_tail = &received_packets; | |
325 | --nreceived; | |
326 | pthread_mutex_unlock(&receive_lock); | |
327 | /* Add it to the heap */ | |
328 | pthread_mutex_lock(&lock); | |
329 | pheap_insert(&packets, p); | |
330 | nsamples += p->nsamples; | |
331 | pthread_cond_broadcast(&cond); | |
332 | pthread_mutex_unlock(&lock); | |
333 | } | |
334 | } | |
335 | ||
09ee2f0d | 336 | /** @brief Background thread collecting samples |
0b75463f | 337 | * |
338 | * This function collects samples, perhaps converts them to the target format, | |
b0fdc63d | 339 | * and adds them to the packet list. |
340 | * | |
341 | * It is crucial that the gap between successive calls to read() is as small as | |
342 | * possible: otherwise packets will be dropped. | |
343 | * | |
344 | * We use a binary heap to ensure that the unavoidable effort is at worst | |
345 | * logarithmic in the total number of packets - in fact if packets are mostly | |
346 | * received in order then we will largely do constant work per packet since the | |
347 | * newest packet will always be last. | |
348 | * | |
349 | * Of more concern is that we must acquire the lock on the heap to add a packet | |
350 | * to it. If this proves a problem in practice then the answer would be | |
351 | * (probably doubly) linked list with new packets added the end and a second | |
352 | * thread which reads packets off the list and adds them to the heap. | |
353 | * | |
354 | * We keep memory allocation (mostly) very fast by keeping pre-allocated | |
c593cf7c | 355 | * packets around; see @ref playrtp_new_packet(). |
b0fdc63d | 356 | */ |
0b75463f | 357 | static void *listen_thread(void attribute((unused)) *arg) { |
2c7c9eae | 358 | struct packet *p = 0; |
0b75463f | 359 | int n; |
2c7c9eae RK |
360 | struct rtp_header header; |
361 | uint16_t seq; | |
362 | uint32_t timestamp; | |
363 | struct iovec iov[2]; | |
e83d0967 RK |
364 | |
365 | for(;;) { | |
189e9830 | 366 | if(!p) |
c593cf7c | 367 | p = playrtp_new_packet(); |
2c7c9eae RK |
368 | iov[0].iov_base = &header; |
369 | iov[0].iov_len = sizeof header; | |
370 | iov[1].iov_base = p->samples_raw; | |
b64efe7e | 371 | iov[1].iov_len = sizeof p->samples_raw / sizeof *p->samples_raw; |
2c7c9eae | 372 | n = readv(rtpfd, iov, 2); |
e83d0967 RK |
373 | if(n < 0) { |
374 | switch(errno) { | |
375 | case EINTR: | |
376 | continue; | |
377 | default: | |
378 | fatal(errno, "error reading from socket"); | |
379 | } | |
380 | } | |
0b75463f | 381 | /* Ignore too-short packets */ |
345ebe66 RK |
382 | if((size_t)n <= sizeof (struct rtp_header)) { |
383 | info("ignored a short packet"); | |
0b75463f | 384 | continue; |
345ebe66 | 385 | } |
2c7c9eae RK |
386 | timestamp = htonl(header.timestamp); |
387 | seq = htons(header.seq); | |
09ee2f0d | 388 | /* Ignore packets in the past */ |
2c7c9eae | 389 | if(active && lt(timestamp, next_timestamp)) { |
c0e41690 | 390 | info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32, |
2c7c9eae | 391 | timestamp, next_timestamp); |
09ee2f0d | 392 | continue; |
c0e41690 | 393 | } |
28f1495a RK |
394 | /* Ignore packets with the extension bit set. */ |
395 | if(header.vpxcc & 0x10) | |
396 | continue; | |
189e9830 | 397 | p->next = 0; |
58b5a68f | 398 | p->flags = 0; |
2c7c9eae | 399 | p->timestamp = timestamp; |
e83d0967 | 400 | /* Convert to target format */ |
58b5a68f RK |
401 | if(header.mpt & 0x80) |
402 | p->flags |= IDLE; | |
2c7c9eae | 403 | switch(header.mpt & 0x7F) { |
4fd38868 | 404 | case 10: /* L16 */ |
2c7c9eae | 405 | p->nsamples = (n - sizeof header) / sizeof(uint16_t); |
e83d0967 RK |
406 | break; |
407 | /* TODO support other RFC3551 media types (when the speaker does) */ | |
408 | default: | |
0b75463f | 409 | fatal(0, "unsupported RTP payload type %d", |
2c7c9eae | 410 | header.mpt & 0x7F); |
e83d0967 | 411 | } |
345ebe66 RK |
412 | if(logfp) |
413 | fprintf(logfp, "sequence %u timestamp %"PRIx32" length %"PRIx32" end %"PRIx32"\n", | |
2c7c9eae | 414 | seq, timestamp, p->nsamples, timestamp + p->nsamples); |
0b75463f | 415 | /* Stop reading if we've reached the maximum. |
416 | * | |
417 | * This is rather unsatisfactory: it means that if packets get heavily | |
418 | * out of order then we guarantee dropouts. But for now... */ | |
345ebe66 | 419 | if(nsamples >= maxbuffer) { |
189e9830 | 420 | pthread_mutex_lock(&lock); |
4dadf1a2 | 421 | while(nsamples >= maxbuffer) { |
345ebe66 | 422 | pthread_cond_wait(&cond, &lock); |
4dadf1a2 | 423 | } |
189e9830 | 424 | pthread_mutex_unlock(&lock); |
345ebe66 | 425 | } |
189e9830 RK |
426 | /* Add the packet to the receive queue */ |
427 | pthread_mutex_lock(&receive_lock); | |
428 | *received_tail = p; | |
429 | received_tail = &p->next; | |
430 | ++nreceived; | |
431 | pthread_cond_signal(&receive_cond); | |
432 | pthread_mutex_unlock(&receive_lock); | |
58b5a68f RK |
433 | /* We'll need a new packet */ |
434 | p = 0; | |
e83d0967 RK |
435 | } |
436 | } | |
437 | ||
5626f6d2 RK |
438 | /** @brief Wait until the buffer is adequately full |
439 | * | |
440 | * Must be called with @ref lock held. | |
441 | */ | |
c593cf7c | 442 | void playrtp_fill_buffer(void) { |
bfd27c14 RK |
443 | while(nsamples) |
444 | drop_first_packet(); | |
5626f6d2 | 445 | info("Buffering..."); |
4dadf1a2 | 446 | while(nsamples < readahead) { |
5626f6d2 | 447 | pthread_cond_wait(&cond, &lock); |
4dadf1a2 | 448 | } |
5626f6d2 RK |
449 | next_timestamp = pheap_first(&packets)->timestamp; |
450 | active = 1; | |
451 | } | |
452 | ||
453 | /** @brief Find next packet | |
454 | * @return Packet to play or NULL if none found | |
455 | * | |
456 | * The return packet is merely guaranteed not to be in the past: it might be | |
457 | * the first packet in the future rather than one that is actually suitable to | |
458 | * play. | |
459 | * | |
460 | * Must be called with @ref lock held. | |
461 | */ | |
c593cf7c | 462 | struct packet *playrtp_next_packet(void) { |
5626f6d2 RK |
463 | while(pheap_count(&packets)) { |
464 | struct packet *const p = pheap_first(&packets); | |
465 | if(le(p->timestamp + p->nsamples, next_timestamp)) { | |
466 | /* This packet is in the past. Drop it and try another one. */ | |
467 | drop_first_packet(); | |
468 | } else | |
469 | /* This packet is NOT in the past. (It might be in the future | |
470 | * however.) */ | |
471 | return p; | |
472 | } | |
473 | return 0; | |
474 | } | |
475 | ||
e83d0967 RK |
476 | /* display usage message and terminate */ |
477 | static void help(void) { | |
478 | xprintf("Usage:\n" | |
c897bb65 | 479 | " disorder-playrtp [OPTIONS] [[ADDRESS] PORT]\n" |
e83d0967 | 480 | "Options:\n" |
1153fd23 | 481 | " --device, -D DEVICE Output device\n" |
482 | " --min, -m FRAMES Buffer low water mark\n" | |
9086a105 RK |
483 | " --buffer, -b FRAMES Buffer high water mark\n" |
484 | " --max, -x FRAMES Buffer maximum size\n" | |
1f10f780 | 485 | " --rcvbuf, -R BYTES Socket receive buffer size\n" |
a7e9570a | 486 | " --config, -C PATH Set configuration file\n" |
146e86fb | 487 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 488 | " --alsa, -a Use ALSA to play audio\n" |
489 | #endif | |
a9f0ad12 | 490 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 491 | " --oss, -o Use OSS to play audio\n" |
492 | #endif | |
493 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
494 | " --core-audio, -c Use Core Audio to play audio\n" | |
495 | #endif | |
287ad384 RK |
496 | " --command, -e COMMAND Pipe audio to command.\n" |
497 | " --pause-mode, -P silence For -e: pauses send silence (default)\n" | |
498 | " --pause-mode, -P suspend For -e: pauses suspend writes\n" | |
9086a105 RK |
499 | " --help, -h Display usage message\n" |
500 | " --version, -V Display version number\n" | |
501 | ); | |
e83d0967 RK |
502 | xfclose(stdout); |
503 | exit(0); | |
504 | } | |
505 | ||
4fd38868 | 506 | static size_t playrtp_callback(void *buffer, |
7a2c7068 RK |
507 | size_t max_samples, |
508 | void attribute((unused)) *userdata) { | |
509 | size_t samples; | |
510 | ||
511 | pthread_mutex_lock(&lock); | |
512 | /* Get the next packet, junking any that are now in the past */ | |
513 | const struct packet *p = playrtp_next_packet(); | |
514 | if(p && contains(p, next_timestamp)) { | |
515 | /* This packet is ready to play; the desired next timestamp points | |
516 | * somewhere into it. */ | |
517 | ||
518 | /* Timestamp of end of packet */ | |
519 | const uint32_t packet_end = p->timestamp + p->nsamples; | |
520 | ||
521 | /* Offset of desired next timestamp into current packet */ | |
522 | const uint32_t offset = next_timestamp - p->timestamp; | |
523 | ||
524 | /* Pointer to audio data */ | |
525 | const uint16_t *ptr = (void *)(p->samples_raw + offset); | |
526 | ||
527 | /* Compute number of samples left in packet, limited to output buffer | |
528 | * size */ | |
529 | samples = packet_end - next_timestamp; | |
530 | if(samples > max_samples) | |
531 | samples = max_samples; | |
532 | ||
533 | /* Copy into buffer, converting to native endianness */ | |
534 | size_t i = samples; | |
535 | int16_t *bufptr = buffer; | |
536 | while(i > 0) { | |
537 | *bufptr++ = (int16_t)ntohs(*ptr++); | |
538 | --i; | |
539 | } | |
540 | /* We don't junk the packet here; a subsequent call to | |
541 | * playrtp_next_packet() will dispose of it (if it's actually done with). */ | |
542 | } else { | |
543 | /* There is no suitable packet. We introduce 0s up to the next packet, or | |
544 | * to fill the buffer if there's no next packet or that's too many. The | |
545 | * comparison with max_samples deals with the otherwise troubling overflow | |
546 | * case. */ | |
547 | samples = p ? p->timestamp - next_timestamp : max_samples; | |
548 | if(samples > max_samples) | |
549 | samples = max_samples; | |
550 | //info("infill by %zu", samples); | |
4fd38868 | 551 | memset(buffer, 0, samples * uaudio_sample_size); |
7a2c7068 RK |
552 | } |
553 | /* Debug dump */ | |
554 | if(dump_buffer) { | |
555 | for(size_t i = 0; i < samples; ++i) { | |
4fd38868 | 556 | dump_buffer[dump_index++] = ((int16_t *)buffer)[i]; |
7a2c7068 RK |
557 | dump_index %= dump_size; |
558 | } | |
559 | } | |
560 | /* Advance timestamp */ | |
561 | next_timestamp += samples; | |
562 | pthread_mutex_unlock(&lock); | |
563 | return samples; | |
564 | } | |
565 | ||
e83d0967 | 566 | int main(int argc, char **argv) { |
a99c4e9a | 567 | int n, err; |
e83d0967 RK |
568 | struct addrinfo *res; |
569 | struct stringlist sl; | |
0b75463f | 570 | char *sockname; |
1f10f780 RK |
571 | int rcvbuf, target_rcvbuf = 131072; |
572 | socklen_t len; | |
23205f9c RK |
573 | struct ip_mreq mreq; |
574 | struct ipv6_mreq mreq6; | |
a7e9570a RK |
575 | disorder_client *c; |
576 | char *address, *port; | |
6fba990c RK |
577 | int is_multicast; |
578 | union any_sockaddr { | |
579 | struct sockaddr sa; | |
580 | struct sockaddr_in in; | |
581 | struct sockaddr_in6 in6; | |
582 | }; | |
583 | union any_sockaddr mgroup; | |
e9b635a3 | 584 | const char *dumpfile = 0; |
7a2c7068 | 585 | pthread_t ltid; |
983c3357 | 586 | static const int one = 1; |
e83d0967 | 587 | |
0b75463f | 588 | static const struct addrinfo prefs = { |
66613034 RK |
589 | .ai_flags = AI_PASSIVE, |
590 | .ai_family = PF_INET, | |
591 | .ai_socktype = SOCK_DGRAM, | |
592 | .ai_protocol = IPPROTO_UDP | |
e83d0967 RK |
593 | }; |
594 | ||
595 | mem_init(); | |
596 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); | |
902ccab0 | 597 | backend = uaudio_apis[0]; |
287ad384 | 598 | while((n = getopt_long(argc, argv, "hVdD:m:b:x:L:R:M:aocC:re:P:", options, 0)) >= 0) { |
e83d0967 RK |
599 | switch(n) { |
600 | case 'h': help(); | |
3fbdc96d | 601 | case 'V': version("disorder-playrtp"); |
e83d0967 | 602 | case 'd': debugging = 1; break; |
e979b844 | 603 | case 'D': uaudio_set("device", optarg); break; |
1153fd23 | 604 | case 'm': minbuffer = 2 * atol(optarg); break; |
605 | case 'b': readahead = 2 * atol(optarg); break; | |
9086a105 | 606 | case 'x': maxbuffer = 2 * atol(optarg); break; |
345ebe66 | 607 | case 'L': logfp = fopen(optarg, "w"); break; |
1f10f780 | 608 | case 'R': target_rcvbuf = atoi(optarg); break; |
146e86fb | 609 | #if HAVE_ALSA_ASOUNDLIB_H |
7a2c7068 | 610 | case 'a': backend = &uaudio_alsa; break; |
c593cf7c | 611 | #endif |
a9f0ad12 | 612 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
7a2c7068 | 613 | case 'o': backend = &uaudio_oss; break; |
c593cf7c | 614 | #endif |
615 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
7a2c7068 | 616 | case 'c': backend = &uaudio_coreaudio; break; |
c593cf7c | 617 | #endif |
a7e9570a | 618 | case 'C': configfile = optarg; break; |
a99c4e9a | 619 | case 's': control_socket = optarg; break; |
e9b635a3 | 620 | case 'r': dumpfile = optarg; break; |
e979b844 | 621 | case 'e': backend = &uaudio_command; uaudio_set("command", optarg); break; |
287ad384 | 622 | case 'P': uaudio_set("pause-mode", optarg); break; |
e83d0967 RK |
623 | default: fatal(0, "invalid option"); |
624 | } | |
625 | } | |
a7e9570a | 626 | if(config_read(0)) fatal(0, "cannot read configuration"); |
9086a105 RK |
627 | if(!maxbuffer) |
628 | maxbuffer = 4 * readahead; | |
e83d0967 RK |
629 | argc -= optind; |
630 | argv += optind; | |
a7e9570a RK |
631 | switch(argc) { |
632 | case 0: | |
6fba990c | 633 | /* Get configuration from server */ |
a7e9570a RK |
634 | if(!(c = disorder_new(1))) exit(EXIT_FAILURE); |
635 | if(disorder_connect(c)) exit(EXIT_FAILURE); | |
636 | if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE); | |
6fba990c RK |
637 | sl.n = 2; |
638 | sl.s = xcalloc(2, sizeof *sl.s); | |
639 | sl.s[0] = address; | |
640 | sl.s[1] = port; | |
a7e9570a | 641 | break; |
6fba990c | 642 | case 1: |
a7e9570a | 643 | case 2: |
6fba990c | 644 | /* Use command-line ADDRESS+PORT or just PORT */ |
a7e9570a RK |
645 | sl.n = argc; |
646 | sl.s = argv; | |
647 | break; | |
648 | default: | |
6fba990c | 649 | fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]"); |
a7e9570a | 650 | } |
6fba990c | 651 | /* Look up address and port */ |
0b75463f | 652 | if(!(res = get_address(&sl, &prefs, &sockname))) |
e83d0967 | 653 | exit(1); |
6fba990c | 654 | /* Create the socket */ |
e83d0967 RK |
655 | if((rtpfd = socket(res->ai_family, |
656 | res->ai_socktype, | |
657 | res->ai_protocol)) < 0) | |
658 | fatal(errno, "error creating socket"); | |
983c3357 RK |
659 | /* Allow multiple listeners */ |
660 | xsetsockopt(rtpfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); | |
661 | is_multicast = multicast(res->ai_addr); | |
662 | /* The multicast and unicast/broadcast cases are different enough that they | |
663 | * are totally split. Trying to find commonality between them causes more | |
664 | * trouble that it's worth. */ | |
665 | if(is_multicast) { | |
666 | /* Stash the multicast group address */ | |
6fba990c RK |
667 | memcpy(&mgroup, res->ai_addr, res->ai_addrlen); |
668 | switch(res->ai_addr->sa_family) { | |
669 | case AF_INET: | |
670 | mgroup.in.sin_port = 0; | |
671 | break; | |
672 | case AF_INET6: | |
673 | mgroup.in6.sin6_port = 0; | |
674 | break; | |
983c3357 RK |
675 | default: |
676 | fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family); | |
6fba990c | 677 | } |
983c3357 RK |
678 | /* Bind to to the multicast group address */ |
679 | if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) | |
680 | fatal(errno, "error binding socket to %s", format_sockaddr(res->ai_addr)); | |
681 | /* Add multicast group membership */ | |
6fba990c | 682 | switch(mgroup.sa.sa_family) { |
23205f9c | 683 | case PF_INET: |
6fba990c | 684 | mreq.imr_multiaddr = mgroup.in.sin_addr; |
23205f9c RK |
685 | mreq.imr_interface.s_addr = 0; /* use primary interface */ |
686 | if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
687 | &mreq, sizeof mreq) < 0) | |
688 | fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP"); | |
689 | break; | |
690 | case PF_INET6: | |
6fba990c | 691 | mreq6.ipv6mr_multiaddr = mgroup.in6.sin6_addr; |
23205f9c RK |
692 | memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface); |
693 | if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, | |
694 | &mreq6, sizeof mreq6) < 0) | |
695 | fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP"); | |
696 | break; | |
697 | default: | |
698 | fatal(0, "unsupported address family %d", res->ai_family); | |
699 | } | |
983c3357 | 700 | /* Report what we did */ |
6fba990c RK |
701 | info("listening on %s multicast group %s", |
702 | format_sockaddr(res->ai_addr), format_sockaddr(&mgroup.sa)); | |
983c3357 RK |
703 | } else { |
704 | /* Bind to 0/port */ | |
705 | switch(res->ai_addr->sa_family) { | |
706 | case AF_INET: { | |
707 | struct sockaddr_in *in = (struct sockaddr_in *)res->ai_addr; | |
708 | ||
709 | memset(&in->sin_addr, 0, sizeof (struct in_addr)); | |
710 | if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) | |
711 | fatal(errno, "error binding socket to 0.0.0.0 port %d", | |
712 | ntohs(in->sin_port)); | |
713 | break; | |
714 | } | |
715 | case AF_INET6: { | |
716 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)res->ai_addr; | |
717 | ||
718 | memset(&in6->sin6_addr, 0, sizeof (struct in6_addr)); | |
719 | break; | |
720 | } | |
721 | default: | |
722 | fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family); | |
723 | } | |
724 | if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) | |
725 | fatal(errno, "error binding socket to %s", format_sockaddr(res->ai_addr)); | |
726 | /* Report what we did */ | |
6fba990c | 727 | info("listening on %s", format_sockaddr(res->ai_addr)); |
983c3357 | 728 | } |
1f10f780 RK |
729 | len = sizeof rcvbuf; |
730 | if(getsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len) < 0) | |
731 | fatal(errno, "error calling getsockopt SO_RCVBUF"); | |
f0bae611 | 732 | if(target_rcvbuf > rcvbuf) { |
1f10f780 RK |
733 | if(setsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, |
734 | &target_rcvbuf, sizeof target_rcvbuf) < 0) | |
735 | error(errno, "error calling setsockopt SO_RCVBUF %d", | |
736 | target_rcvbuf); | |
737 | /* We try to carry on anyway */ | |
738 | else | |
739 | info("changed socket receive buffer from %d to %d", | |
740 | rcvbuf, target_rcvbuf); | |
741 | } else | |
742 | info("default socket receive buffer %d", rcvbuf); | |
743 | if(logfp) | |
744 | info("WARNING: -L option can impact performance"); | |
a99c4e9a RK |
745 | if(control_socket) { |
746 | pthread_t tid; | |
747 | ||
748 | if((err = pthread_create(&tid, 0, control_thread, 0))) | |
749 | fatal(err, "pthread_create control_thread"); | |
750 | } | |
e9b635a3 RK |
751 | if(dumpfile) { |
752 | int fd; | |
753 | unsigned char buffer[65536]; | |
754 | size_t written; | |
755 | ||
756 | if((fd = open(dumpfile, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) | |
757 | fatal(errno, "opening %s", dumpfile); | |
758 | /* Fill with 0s to a suitable size */ | |
759 | memset(buffer, 0, sizeof buffer); | |
760 | for(written = 0; written < dump_size * sizeof(int16_t); | |
761 | written += sizeof buffer) { | |
762 | if(write(fd, buffer, sizeof buffer) < 0) | |
763 | fatal(errno, "clearing %s", dumpfile); | |
764 | } | |
765 | /* Map the buffer into memory for convenience */ | |
766 | dump_buffer = mmap(0, dump_size * sizeof(int16_t), PROT_READ|PROT_WRITE, | |
767 | MAP_SHARED, fd, 0); | |
768 | if(dump_buffer == (void *)-1) | |
769 | fatal(errno, "mapping %s", dumpfile); | |
770 | info("dumping to %s", dumpfile); | |
771 | } | |
4fd38868 RK |
772 | /* Set up output. Currently we only support L16 so there's no harm setting |
773 | * the format before we know what it is! */ | |
774 | uaudio_set_format(44100/*Hz*/, 2/*channels*/, | |
775 | 16/*bits/channel*/, 1/*signed*/); | |
7a2c7068 RK |
776 | backend->start(playrtp_callback, NULL); |
777 | /* We receive and convert audio data in a background thread */ | |
778 | if((err = pthread_create(<id, 0, listen_thread, 0))) | |
779 | fatal(err, "pthread_create listen_thread"); | |
780 | /* We have a second thread to add received packets to the queue */ | |
781 | if((err = pthread_create(<id, 0, queue_thread, 0))) | |
782 | fatal(err, "pthread_create queue_thread"); | |
783 | pthread_mutex_lock(&lock); | |
784 | for(;;) { | |
785 | /* Wait for the buffer to fill up a bit */ | |
786 | playrtp_fill_buffer(); | |
787 | /* Start playing now */ | |
788 | info("Playing..."); | |
789 | next_timestamp = pheap_first(&packets)->timestamp; | |
790 | active = 1; | |
d4170ca7 | 791 | pthread_mutex_unlock(&lock); |
7a2c7068 | 792 | backend->activate(); |
d4170ca7 | 793 | pthread_mutex_lock(&lock); |
7a2c7068 RK |
794 | /* Wait until the buffer empties out */ |
795 | while(nsamples >= minbuffer | |
796 | || (nsamples > 0 | |
4fd38868 | 797 | && contains(pheap_first(&packets), next_timestamp))) { |
7a2c7068 | 798 | pthread_cond_wait(&cond, &lock); |
4fd38868 | 799 | } |
7a2c7068 | 800 | /* Stop playing for a bit until the buffer re-fills */ |
d4170ca7 | 801 | pthread_mutex_unlock(&lock); |
7a2c7068 | 802 | backend->deactivate(); |
d4170ca7 | 803 | pthread_mutex_lock(&lock); |
7a2c7068 RK |
804 | active = 0; |
805 | /* Go back round */ | |
806 | } | |
e83d0967 RK |
807 | return 0; |
808 | } | |
809 | ||
810 | /* | |
811 | Local Variables: | |
812 | c-basic-offset:2 | |
813 | comment-column:40 | |
814 | fill-column:79 | |
815 | indent-tabs-mode:nil | |
816 | End: | |
817 | */ |