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 | * | |
189e9830 RK |
27 | * The program runs (at least) three threads. listen_thread() is responsible |
28 | * for reading RTP packets off the wire and adding them to the linked list @ref | |
29 | * received_packets, assuming they are basically sound. queue_thread() takes | |
30 | * packets off this linked list and adds them to @ref packets (an operation | |
31 | * which might be much slower due to contention for @ref lock). | |
b0fdc63d | 32 | * |
33 | * The main thread is responsible for actually playing audio. In ALSA this | |
34 | * means it waits until ALSA says it's ready for more audio which it then | |
c593cf7c | 35 | * plays. See @ref clients/playrtp-alsa.c. |
b0fdc63d | 36 | * |
8e3fe3d8 | 37 | * In Core Audio the main thread is only responsible for starting and stopping |
b0fdc63d | 38 | * play: the system does the actual playback in its own private thread, and |
c593cf7c | 39 | * calls adioproc() to fetch the audio data. See @ref |
40 | * clients/playrtp-coreaudio.c. | |
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' }, |
a99c4e9a | 220 | { "socket", required_argument, 0, 's' }, |
a7e9570a | 221 | { "config", required_argument, 0, 'C' }, |
e83d0967 RK |
222 | { 0, 0, 0, 0 } |
223 | }; | |
224 | ||
a99c4e9a RK |
225 | /** @brief Control thread |
226 | * | |
227 | * This thread is responsible for accepting control commands from Disobedience | |
228 | * (or other controllers) over an AF_UNIX stream socket with a path specified | |
229 | * by the @c --socket option. The protocol uses simple string commands and | |
230 | * replies: | |
231 | * | |
232 | * - @c stop will shut the player down | |
233 | * - @c query will send back the reply @c running | |
234 | * - anything else is ignored | |
235 | * | |
236 | * Commands and response strings terminated by shutting down the connection or | |
237 | * by a newline. No attempt is made to multiplex multiple clients so it is | |
238 | * important that the command be sent as soon as the connection is made - it is | |
239 | * assumed that both parties to the protocol are entirely cooperating with one | |
240 | * another. | |
241 | */ | |
242 | static void *control_thread(void attribute((unused)) *arg) { | |
243 | struct sockaddr_un sa; | |
244 | int sfd, cfd; | |
245 | char *line; | |
246 | socklen_t salen; | |
247 | FILE *fp; | |
248 | ||
249 | assert(control_socket); | |
250 | unlink(control_socket); | |
251 | memset(&sa, 0, sizeof sa); | |
252 | sa.sun_family = AF_UNIX; | |
253 | strcpy(sa.sun_path, control_socket); | |
254 | sfd = xsocket(PF_UNIX, SOCK_STREAM, 0); | |
255 | if(bind(sfd, (const struct sockaddr *)&sa, sizeof sa) < 0) | |
256 | fatal(errno, "error binding to %s", control_socket); | |
257 | if(listen(sfd, 128) < 0) | |
258 | fatal(errno, "error calling listen on %s", control_socket); | |
259 | info("listening on %s", control_socket); | |
260 | for(;;) { | |
261 | salen = sizeof sa; | |
262 | cfd = accept(sfd, (struct sockaddr *)&sa, &salen); | |
263 | if(cfd < 0) { | |
264 | switch(errno) { | |
265 | case EINTR: | |
266 | case EAGAIN: | |
267 | break; | |
268 | default: | |
269 | fatal(errno, "error calling accept on %s", control_socket); | |
270 | } | |
271 | } | |
272 | if(!(fp = fdopen(cfd, "r+"))) { | |
273 | error(errno, "error calling fdopen for %s connection", control_socket); | |
274 | close(cfd); | |
275 | continue; | |
276 | } | |
277 | if(!inputline(control_socket, fp, &line, '\n')) { | |
278 | if(!strcmp(line, "stop")) { | |
279 | info("stopped via %s", control_socket); | |
280 | exit(0); /* terminate immediately */ | |
281 | } | |
282 | if(!strcmp(line, "query")) | |
283 | fprintf(fp, "running"); | |
284 | xfree(line); | |
285 | } | |
286 | if(fclose(fp) < 0) | |
287 | error(errno, "error closing %s connection", control_socket); | |
288 | } | |
289 | } | |
290 | ||
28bacdc0 RK |
291 | /** @brief Drop the first packet |
292 | * | |
293 | * Assumes that @ref lock is held. | |
294 | */ | |
295 | static void drop_first_packet(void) { | |
296 | if(pheap_count(&packets)) { | |
297 | struct packet *const p = pheap_remove(&packets); | |
298 | nsamples -= p->nsamples; | |
c593cf7c | 299 | playrtp_free_packet(p); |
2c7c9eae | 300 | pthread_cond_broadcast(&cond); |
2c7c9eae | 301 | } |
9086a105 RK |
302 | } |
303 | ||
189e9830 RK |
304 | /** @brief Background thread adding packets to heap |
305 | * | |
306 | * This just transfers packets from @ref received_packets to @ref packets. It | |
307 | * is important that it holds @ref receive_lock for as little time as possible, | |
308 | * in order to minimize the interval between calls to read() in | |
309 | * listen_thread(). | |
310 | */ | |
311 | static void *queue_thread(void attribute((unused)) *arg) { | |
312 | struct packet *p; | |
313 | ||
314 | for(;;) { | |
315 | /* Get the next packet */ | |
316 | pthread_mutex_lock(&receive_lock); | |
4dadf1a2 | 317 | while(!received_packets) { |
189e9830 | 318 | pthread_cond_wait(&receive_cond, &receive_lock); |
4dadf1a2 | 319 | } |
189e9830 RK |
320 | p = received_packets; |
321 | received_packets = p->next; | |
322 | if(!received_packets) | |
323 | received_tail = &received_packets; | |
324 | --nreceived; | |
325 | pthread_mutex_unlock(&receive_lock); | |
326 | /* Add it to the heap */ | |
327 | pthread_mutex_lock(&lock); | |
328 | pheap_insert(&packets, p); | |
329 | nsamples += p->nsamples; | |
330 | pthread_cond_broadcast(&cond); | |
331 | pthread_mutex_unlock(&lock); | |
332 | } | |
333 | } | |
334 | ||
09ee2f0d | 335 | /** @brief Background thread collecting samples |
0b75463f | 336 | * |
337 | * This function collects samples, perhaps converts them to the target format, | |
b0fdc63d | 338 | * and adds them to the packet list. |
339 | * | |
340 | * It is crucial that the gap between successive calls to read() is as small as | |
341 | * possible: otherwise packets will be dropped. | |
342 | * | |
343 | * We use a binary heap to ensure that the unavoidable effort is at worst | |
344 | * logarithmic in the total number of packets - in fact if packets are mostly | |
345 | * received in order then we will largely do constant work per packet since the | |
346 | * newest packet will always be last. | |
347 | * | |
348 | * Of more concern is that we must acquire the lock on the heap to add a packet | |
349 | * to it. If this proves a problem in practice then the answer would be | |
350 | * (probably doubly) linked list with new packets added the end and a second | |
351 | * thread which reads packets off the list and adds them to the heap. | |
352 | * | |
353 | * We keep memory allocation (mostly) very fast by keeping pre-allocated | |
c593cf7c | 354 | * packets around; see @ref playrtp_new_packet(). |
b0fdc63d | 355 | */ |
0b75463f | 356 | static void *listen_thread(void attribute((unused)) *arg) { |
2c7c9eae | 357 | struct packet *p = 0; |
0b75463f | 358 | int n; |
2c7c9eae RK |
359 | struct rtp_header header; |
360 | uint16_t seq; | |
361 | uint32_t timestamp; | |
362 | struct iovec iov[2]; | |
e83d0967 RK |
363 | |
364 | for(;;) { | |
189e9830 | 365 | if(!p) |
c593cf7c | 366 | p = playrtp_new_packet(); |
2c7c9eae RK |
367 | iov[0].iov_base = &header; |
368 | iov[0].iov_len = sizeof header; | |
369 | iov[1].iov_base = p->samples_raw; | |
b64efe7e | 370 | iov[1].iov_len = sizeof p->samples_raw / sizeof *p->samples_raw; |
2c7c9eae | 371 | n = readv(rtpfd, iov, 2); |
e83d0967 RK |
372 | if(n < 0) { |
373 | switch(errno) { | |
374 | case EINTR: | |
375 | continue; | |
376 | default: | |
377 | fatal(errno, "error reading from socket"); | |
378 | } | |
379 | } | |
0b75463f | 380 | /* Ignore too-short packets */ |
345ebe66 RK |
381 | if((size_t)n <= sizeof (struct rtp_header)) { |
382 | info("ignored a short packet"); | |
0b75463f | 383 | continue; |
345ebe66 | 384 | } |
2c7c9eae RK |
385 | timestamp = htonl(header.timestamp); |
386 | seq = htons(header.seq); | |
09ee2f0d | 387 | /* Ignore packets in the past */ |
2c7c9eae | 388 | if(active && lt(timestamp, next_timestamp)) { |
c0e41690 | 389 | info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32, |
2c7c9eae | 390 | timestamp, next_timestamp); |
09ee2f0d | 391 | continue; |
c0e41690 | 392 | } |
28f1495a RK |
393 | /* Ignore packets with the extension bit set. */ |
394 | if(header.vpxcc & 0x10) | |
395 | continue; | |
189e9830 | 396 | p->next = 0; |
58b5a68f | 397 | p->flags = 0; |
2c7c9eae | 398 | p->timestamp = timestamp; |
e83d0967 | 399 | /* Convert to target format */ |
58b5a68f RK |
400 | if(header.mpt & 0x80) |
401 | p->flags |= IDLE; | |
2c7c9eae | 402 | switch(header.mpt & 0x7F) { |
4fd38868 | 403 | case 10: /* L16 */ |
2c7c9eae | 404 | p->nsamples = (n - sizeof header) / sizeof(uint16_t); |
e83d0967 RK |
405 | break; |
406 | /* TODO support other RFC3551 media types (when the speaker does) */ | |
407 | default: | |
0b75463f | 408 | fatal(0, "unsupported RTP payload type %d", |
2c7c9eae | 409 | header.mpt & 0x7F); |
e83d0967 | 410 | } |
345ebe66 RK |
411 | if(logfp) |
412 | fprintf(logfp, "sequence %u timestamp %"PRIx32" length %"PRIx32" end %"PRIx32"\n", | |
2c7c9eae | 413 | seq, timestamp, p->nsamples, timestamp + p->nsamples); |
0b75463f | 414 | /* Stop reading if we've reached the maximum. |
415 | * | |
416 | * This is rather unsatisfactory: it means that if packets get heavily | |
417 | * out of order then we guarantee dropouts. But for now... */ | |
345ebe66 | 418 | if(nsamples >= maxbuffer) { |
189e9830 | 419 | pthread_mutex_lock(&lock); |
4dadf1a2 | 420 | while(nsamples >= maxbuffer) { |
345ebe66 | 421 | pthread_cond_wait(&cond, &lock); |
4dadf1a2 | 422 | } |
189e9830 | 423 | pthread_mutex_unlock(&lock); |
345ebe66 | 424 | } |
189e9830 RK |
425 | /* Add the packet to the receive queue */ |
426 | pthread_mutex_lock(&receive_lock); | |
427 | *received_tail = p; | |
428 | received_tail = &p->next; | |
429 | ++nreceived; | |
430 | pthread_cond_signal(&receive_cond); | |
431 | pthread_mutex_unlock(&receive_lock); | |
58b5a68f RK |
432 | /* We'll need a new packet */ |
433 | p = 0; | |
e83d0967 RK |
434 | } |
435 | } | |
436 | ||
5626f6d2 RK |
437 | /** @brief Wait until the buffer is adequately full |
438 | * | |
439 | * Must be called with @ref lock held. | |
440 | */ | |
c593cf7c | 441 | void playrtp_fill_buffer(void) { |
bfd27c14 RK |
442 | while(nsamples) |
443 | drop_first_packet(); | |
5626f6d2 | 444 | info("Buffering..."); |
4dadf1a2 | 445 | while(nsamples < readahead) { |
5626f6d2 | 446 | pthread_cond_wait(&cond, &lock); |
4dadf1a2 | 447 | } |
5626f6d2 RK |
448 | next_timestamp = pheap_first(&packets)->timestamp; |
449 | active = 1; | |
450 | } | |
451 | ||
452 | /** @brief Find next packet | |
453 | * @return Packet to play or NULL if none found | |
454 | * | |
455 | * The return packet is merely guaranteed not to be in the past: it might be | |
456 | * the first packet in the future rather than one that is actually suitable to | |
457 | * play. | |
458 | * | |
459 | * Must be called with @ref lock held. | |
460 | */ | |
c593cf7c | 461 | struct packet *playrtp_next_packet(void) { |
5626f6d2 RK |
462 | while(pheap_count(&packets)) { |
463 | struct packet *const p = pheap_first(&packets); | |
464 | if(le(p->timestamp + p->nsamples, next_timestamp)) { | |
465 | /* This packet is in the past. Drop it and try another one. */ | |
466 | drop_first_packet(); | |
467 | } else | |
468 | /* This packet is NOT in the past. (It might be in the future | |
469 | * however.) */ | |
470 | return p; | |
471 | } | |
472 | return 0; | |
473 | } | |
474 | ||
e83d0967 RK |
475 | /* display usage message and terminate */ |
476 | static void help(void) { | |
477 | xprintf("Usage:\n" | |
c897bb65 | 478 | " disorder-playrtp [OPTIONS] [[ADDRESS] PORT]\n" |
e83d0967 | 479 | "Options:\n" |
1153fd23 | 480 | " --device, -D DEVICE Output device\n" |
481 | " --min, -m FRAMES Buffer low water mark\n" | |
9086a105 RK |
482 | " --buffer, -b FRAMES Buffer high water mark\n" |
483 | " --max, -x FRAMES Buffer maximum size\n" | |
1f10f780 | 484 | " --rcvbuf, -R BYTES Socket receive buffer size\n" |
a7e9570a | 485 | " --config, -C PATH Set configuration file\n" |
146e86fb | 486 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 487 | " --alsa, -a Use ALSA to play audio\n" |
488 | #endif | |
a9f0ad12 | 489 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 490 | " --oss, -o Use OSS to play audio\n" |
491 | #endif | |
492 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
493 | " --core-audio, -c Use Core Audio to play audio\n" | |
494 | #endif | |
e979b844 | 495 | " --command, -e COMMAND Pipe audio to command\n" |
9086a105 RK |
496 | " --help, -h Display usage message\n" |
497 | " --version, -V Display version number\n" | |
498 | ); | |
e83d0967 RK |
499 | xfclose(stdout); |
500 | exit(0); | |
501 | } | |
502 | ||
4fd38868 | 503 | static size_t playrtp_callback(void *buffer, |
7a2c7068 RK |
504 | size_t max_samples, |
505 | void attribute((unused)) *userdata) { | |
506 | size_t samples; | |
507 | ||
508 | pthread_mutex_lock(&lock); | |
509 | /* Get the next packet, junking any that are now in the past */ | |
510 | const struct packet *p = playrtp_next_packet(); | |
511 | if(p && contains(p, next_timestamp)) { | |
512 | /* This packet is ready to play; the desired next timestamp points | |
513 | * somewhere into it. */ | |
514 | ||
515 | /* Timestamp of end of packet */ | |
516 | const uint32_t packet_end = p->timestamp + p->nsamples; | |
517 | ||
518 | /* Offset of desired next timestamp into current packet */ | |
519 | const uint32_t offset = next_timestamp - p->timestamp; | |
520 | ||
521 | /* Pointer to audio data */ | |
522 | const uint16_t *ptr = (void *)(p->samples_raw + offset); | |
523 | ||
524 | /* Compute number of samples left in packet, limited to output buffer | |
525 | * size */ | |
526 | samples = packet_end - next_timestamp; | |
527 | if(samples > max_samples) | |
528 | samples = max_samples; | |
529 | ||
530 | /* Copy into buffer, converting to native endianness */ | |
531 | size_t i = samples; | |
532 | int16_t *bufptr = buffer; | |
533 | while(i > 0) { | |
534 | *bufptr++ = (int16_t)ntohs(*ptr++); | |
535 | --i; | |
536 | } | |
537 | /* We don't junk the packet here; a subsequent call to | |
538 | * playrtp_next_packet() will dispose of it (if it's actually done with). */ | |
539 | } else { | |
540 | /* There is no suitable packet. We introduce 0s up to the next packet, or | |
541 | * to fill the buffer if there's no next packet or that's too many. The | |
542 | * comparison with max_samples deals with the otherwise troubling overflow | |
543 | * case. */ | |
544 | samples = p ? p->timestamp - next_timestamp : max_samples; | |
545 | if(samples > max_samples) | |
546 | samples = max_samples; | |
547 | //info("infill by %zu", samples); | |
4fd38868 | 548 | memset(buffer, 0, samples * uaudio_sample_size); |
7a2c7068 RK |
549 | } |
550 | /* Debug dump */ | |
551 | if(dump_buffer) { | |
552 | for(size_t i = 0; i < samples; ++i) { | |
4fd38868 | 553 | dump_buffer[dump_index++] = ((int16_t *)buffer)[i]; |
7a2c7068 RK |
554 | dump_index %= dump_size; |
555 | } | |
556 | } | |
557 | /* Advance timestamp */ | |
558 | next_timestamp += samples; | |
559 | pthread_mutex_unlock(&lock); | |
560 | return samples; | |
561 | } | |
562 | ||
e83d0967 | 563 | int main(int argc, char **argv) { |
a99c4e9a | 564 | int n, err; |
e83d0967 RK |
565 | struct addrinfo *res; |
566 | struct stringlist sl; | |
0b75463f | 567 | char *sockname; |
1f10f780 RK |
568 | int rcvbuf, target_rcvbuf = 131072; |
569 | socklen_t len; | |
23205f9c RK |
570 | struct ip_mreq mreq; |
571 | struct ipv6_mreq mreq6; | |
a7e9570a RK |
572 | disorder_client *c; |
573 | char *address, *port; | |
6fba990c RK |
574 | int is_multicast; |
575 | union any_sockaddr { | |
576 | struct sockaddr sa; | |
577 | struct sockaddr_in in; | |
578 | struct sockaddr_in6 in6; | |
579 | }; | |
580 | union any_sockaddr mgroup; | |
e9b635a3 | 581 | const char *dumpfile = 0; |
7a2c7068 | 582 | pthread_t ltid; |
e83d0967 | 583 | |
0b75463f | 584 | static const struct addrinfo prefs = { |
66613034 RK |
585 | .ai_flags = AI_PASSIVE, |
586 | .ai_family = PF_INET, | |
587 | .ai_socktype = SOCK_DGRAM, | |
588 | .ai_protocol = IPPROTO_UDP | |
e83d0967 RK |
589 | }; |
590 | ||
591 | mem_init(); | |
592 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); | |
902ccab0 | 593 | backend = uaudio_apis[0]; |
e979b844 | 594 | while((n = getopt_long(argc, argv, "hVdD:m:b:x:L:R:M:aocC:re:", options, 0)) >= 0) { |
e83d0967 RK |
595 | switch(n) { |
596 | case 'h': help(); | |
3fbdc96d | 597 | case 'V': version("disorder-playrtp"); |
e83d0967 | 598 | case 'd': debugging = 1; break; |
e979b844 | 599 | case 'D': uaudio_set("device", optarg); break; |
1153fd23 | 600 | case 'm': minbuffer = 2 * atol(optarg); break; |
601 | case 'b': readahead = 2 * atol(optarg); break; | |
9086a105 | 602 | case 'x': maxbuffer = 2 * atol(optarg); break; |
345ebe66 | 603 | case 'L': logfp = fopen(optarg, "w"); break; |
1f10f780 | 604 | case 'R': target_rcvbuf = atoi(optarg); break; |
146e86fb | 605 | #if HAVE_ALSA_ASOUNDLIB_H |
7a2c7068 | 606 | case 'a': backend = &uaudio_alsa; break; |
c593cf7c | 607 | #endif |
a9f0ad12 | 608 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
7a2c7068 | 609 | case 'o': backend = &uaudio_oss; break; |
c593cf7c | 610 | #endif |
611 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
7a2c7068 | 612 | case 'c': backend = &uaudio_coreaudio; break; |
c593cf7c | 613 | #endif |
a7e9570a | 614 | case 'C': configfile = optarg; break; |
a99c4e9a | 615 | case 's': control_socket = optarg; break; |
e9b635a3 | 616 | case 'r': dumpfile = optarg; break; |
e979b844 | 617 | case 'e': backend = &uaudio_command; uaudio_set("command", optarg); break; |
e83d0967 RK |
618 | default: fatal(0, "invalid option"); |
619 | } | |
620 | } | |
a7e9570a | 621 | if(config_read(0)) fatal(0, "cannot read configuration"); |
9086a105 RK |
622 | if(!maxbuffer) |
623 | maxbuffer = 4 * readahead; | |
e83d0967 RK |
624 | argc -= optind; |
625 | argv += optind; | |
a7e9570a RK |
626 | switch(argc) { |
627 | case 0: | |
6fba990c | 628 | /* Get configuration from server */ |
a7e9570a RK |
629 | if(!(c = disorder_new(1))) exit(EXIT_FAILURE); |
630 | if(disorder_connect(c)) exit(EXIT_FAILURE); | |
631 | if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE); | |
6fba990c RK |
632 | sl.n = 2; |
633 | sl.s = xcalloc(2, sizeof *sl.s); | |
634 | sl.s[0] = address; | |
635 | sl.s[1] = port; | |
a7e9570a | 636 | break; |
6fba990c | 637 | case 1: |
a7e9570a | 638 | case 2: |
6fba990c | 639 | /* Use command-line ADDRESS+PORT or just PORT */ |
a7e9570a RK |
640 | sl.n = argc; |
641 | sl.s = argv; | |
642 | break; | |
643 | default: | |
6fba990c | 644 | fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]"); |
a7e9570a | 645 | } |
6fba990c | 646 | /* Look up address and port */ |
0b75463f | 647 | if(!(res = get_address(&sl, &prefs, &sockname))) |
e83d0967 | 648 | exit(1); |
6fba990c | 649 | /* Create the socket */ |
e83d0967 RK |
650 | if((rtpfd = socket(res->ai_family, |
651 | res->ai_socktype, | |
652 | res->ai_protocol)) < 0) | |
653 | fatal(errno, "error creating socket"); | |
6fba990c RK |
654 | /* Stash the multicast group address */ |
655 | if((is_multicast = multicast(res->ai_addr))) { | |
656 | memcpy(&mgroup, res->ai_addr, res->ai_addrlen); | |
657 | switch(res->ai_addr->sa_family) { | |
658 | case AF_INET: | |
659 | mgroup.in.sin_port = 0; | |
660 | break; | |
661 | case AF_INET6: | |
662 | mgroup.in6.sin6_port = 0; | |
663 | break; | |
664 | } | |
665 | } | |
666 | /* Bind to 0/port */ | |
667 | switch(res->ai_addr->sa_family) { | |
668 | case AF_INET: | |
669 | memset(&((struct sockaddr_in *)res->ai_addr)->sin_addr, 0, | |
670 | sizeof (struct in_addr)); | |
671 | break; | |
672 | case AF_INET6: | |
673 | memset(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 0, | |
674 | sizeof (struct in6_addr)); | |
675 | break; | |
676 | default: | |
677 | fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family); | |
678 | } | |
e83d0967 RK |
679 | if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) |
680 | fatal(errno, "error binding socket to %s", sockname); | |
6fba990c RK |
681 | if(is_multicast) { |
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 | } | |
6fba990c RK |
700 | info("listening on %s multicast group %s", |
701 | format_sockaddr(res->ai_addr), format_sockaddr(&mgroup.sa)); | |
702 | } else | |
703 | info("listening on %s", format_sockaddr(res->ai_addr)); | |
1f10f780 RK |
704 | len = sizeof rcvbuf; |
705 | if(getsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len) < 0) | |
706 | fatal(errno, "error calling getsockopt SO_RCVBUF"); | |
f0bae611 | 707 | if(target_rcvbuf > rcvbuf) { |
1f10f780 RK |
708 | if(setsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, |
709 | &target_rcvbuf, sizeof target_rcvbuf) < 0) | |
710 | error(errno, "error calling setsockopt SO_RCVBUF %d", | |
711 | target_rcvbuf); | |
712 | /* We try to carry on anyway */ | |
713 | else | |
714 | info("changed socket receive buffer from %d to %d", | |
715 | rcvbuf, target_rcvbuf); | |
716 | } else | |
717 | info("default socket receive buffer %d", rcvbuf); | |
718 | if(logfp) | |
719 | info("WARNING: -L option can impact performance"); | |
a99c4e9a RK |
720 | if(control_socket) { |
721 | pthread_t tid; | |
722 | ||
723 | if((err = pthread_create(&tid, 0, control_thread, 0))) | |
724 | fatal(err, "pthread_create control_thread"); | |
725 | } | |
e9b635a3 RK |
726 | if(dumpfile) { |
727 | int fd; | |
728 | unsigned char buffer[65536]; | |
729 | size_t written; | |
730 | ||
731 | if((fd = open(dumpfile, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) | |
732 | fatal(errno, "opening %s", dumpfile); | |
733 | /* Fill with 0s to a suitable size */ | |
734 | memset(buffer, 0, sizeof buffer); | |
735 | for(written = 0; written < dump_size * sizeof(int16_t); | |
736 | written += sizeof buffer) { | |
737 | if(write(fd, buffer, sizeof buffer) < 0) | |
738 | fatal(errno, "clearing %s", dumpfile); | |
739 | } | |
740 | /* Map the buffer into memory for convenience */ | |
741 | dump_buffer = mmap(0, dump_size * sizeof(int16_t), PROT_READ|PROT_WRITE, | |
742 | MAP_SHARED, fd, 0); | |
743 | if(dump_buffer == (void *)-1) | |
744 | fatal(errno, "mapping %s", dumpfile); | |
745 | info("dumping to %s", dumpfile); | |
746 | } | |
4fd38868 RK |
747 | /* Set up output. Currently we only support L16 so there's no harm setting |
748 | * the format before we know what it is! */ | |
749 | uaudio_set_format(44100/*Hz*/, 2/*channels*/, | |
750 | 16/*bits/channel*/, 1/*signed*/); | |
7a2c7068 RK |
751 | backend->start(playrtp_callback, NULL); |
752 | /* We receive and convert audio data in a background thread */ | |
753 | if((err = pthread_create(<id, 0, listen_thread, 0))) | |
754 | fatal(err, "pthread_create listen_thread"); | |
755 | /* We have a second thread to add received packets to the queue */ | |
756 | if((err = pthread_create(<id, 0, queue_thread, 0))) | |
757 | fatal(err, "pthread_create queue_thread"); | |
758 | pthread_mutex_lock(&lock); | |
759 | for(;;) { | |
760 | /* Wait for the buffer to fill up a bit */ | |
761 | playrtp_fill_buffer(); | |
762 | /* Start playing now */ | |
763 | info("Playing..."); | |
764 | next_timestamp = pheap_first(&packets)->timestamp; | |
765 | active = 1; | |
766 | backend->activate(); | |
767 | /* Wait until the buffer empties out */ | |
768 | while(nsamples >= minbuffer | |
769 | || (nsamples > 0 | |
4fd38868 | 770 | && contains(pheap_first(&packets), next_timestamp))) { |
7a2c7068 | 771 | pthread_cond_wait(&cond, &lock); |
4fd38868 | 772 | } |
7a2c7068 RK |
773 | /* Stop playing for a bit until the buffer re-fills */ |
774 | backend->deactivate(); | |
775 | active = 0; | |
776 | /* Go back round */ | |
777 | } | |
e83d0967 RK |
778 | return 0; |
779 | } | |
780 | ||
781 | /* | |
782 | Local Variables: | |
783 | c-basic-offset:2 | |
784 | comment-column:40 | |
785 | fill-column:79 | |
786 | indent-tabs-mode:nil | |
787 | End: | |
788 | */ |