Commit | Line | Data |
---|---|---|
e83d0967 RK |
1 | /* |
2 | * This file is part of DisOrder. | |
06385470 | 3 | * Copyright (C) 2007-2009, 2011, 2013 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> | |
b0619501 | 67 | #include <math.h> |
cca034e5 RK |
68 | #include <arpa/inet.h> |
69 | #include <ifaddrs.h> | |
70 | #include <net/if.h> | |
e83d0967 RK |
71 | |
72 | #include "log.h" | |
73 | #include "mem.h" | |
74 | #include "configuration.h" | |
75 | #include "addr.h" | |
76 | #include "syscalls.h" | |
77 | #include "rtp.h" | |
0b75463f | 78 | #include "defs.h" |
28bacdc0 RK |
79 | #include "vector.h" |
80 | #include "heap.h" | |
189e9830 | 81 | #include "timeval.h" |
a7e9570a | 82 | #include "client.h" |
8e3fe3d8 | 83 | #include "playrtp.h" |
a99c4e9a | 84 | #include "inputline.h" |
3fbdc96d | 85 | #include "version.h" |
7a2c7068 | 86 | #include "uaudio.h" |
e83d0967 | 87 | |
e3426f7b RK |
88 | /** @brief Obsolete synonym */ |
89 | #ifndef IPV6_JOIN_GROUP | |
90 | # define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP | |
91 | #endif | |
92 | ||
0b75463f | 93 | /** @brief RTP socket */ |
e83d0967 RK |
94 | static int rtpfd; |
95 | ||
345ebe66 RK |
96 | /** @brief Log output */ |
97 | static FILE *logfp; | |
98 | ||
0b75463f | 99 | /** @brief Output device */ |
0b75463f | 100 | |
ad535598 RK |
101 | /** @brief Buffer low watermark in samples */ |
102 | unsigned minbuffer = 4 * (2 * 44100) / 10; /* 0.4 seconds */ | |
0b75463f | 103 | |
ad535598 | 104 | /** @brief Maximum buffer size in samples |
9086a105 | 105 | * |
ad535598 RK |
106 | * We'll stop reading from the network if we have this many samples. |
107 | */ | |
9086a105 RK |
108 | static unsigned maxbuffer; |
109 | ||
189e9830 RK |
110 | /** @brief Received packets |
111 | * Protected by @ref receive_lock | |
112 | * | |
113 | * Received packets are added to this list, and queue_thread() picks them off | |
114 | * it and adds them to @ref packets. Whenever a packet is added to it, @ref | |
115 | * receive_cond is signalled. | |
116 | */ | |
8e3fe3d8 | 117 | struct packet *received_packets; |
189e9830 RK |
118 | |
119 | /** @brief Tail of @ref received_packets | |
120 | * Protected by @ref receive_lock | |
121 | */ | |
8e3fe3d8 | 122 | struct packet **received_tail = &received_packets; |
189e9830 RK |
123 | |
124 | /** @brief Lock protecting @ref received_packets | |
125 | * | |
126 | * Only listen_thread() and queue_thread() ever hold this lock. It is vital | |
127 | * that queue_thread() not hold it any longer than it strictly has to. */ | |
8e3fe3d8 | 128 | pthread_mutex_t receive_lock = PTHREAD_MUTEX_INITIALIZER; |
189e9830 RK |
129 | |
130 | /** @brief Condition variable signalled when @ref received_packets is updated | |
131 | * | |
132 | * Used by listen_thread() to notify queue_thread() that it has added another | |
133 | * packet to @ref received_packets. */ | |
8e3fe3d8 | 134 | pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER; |
189e9830 RK |
135 | |
136 | /** @brief Length of @ref received_packets */ | |
8e3fe3d8 | 137 | uint32_t nreceived; |
28bacdc0 RK |
138 | |
139 | /** @brief Binary heap of received packets */ | |
8e3fe3d8 | 140 | struct pheap packets; |
28bacdc0 | 141 | |
189e9830 RK |
142 | /** @brief Total number of samples available |
143 | * | |
144 | * We make this volatile because we inspect it without a protecting lock, | |
145 | * so the usual pthread_* guarantees aren't available. | |
146 | */ | |
8e3fe3d8 | 147 | volatile uint32_t nsamples; |
0b75463f | 148 | |
149 | /** @brief Timestamp of next packet to play. | |
150 | * | |
151 | * This is set to the timestamp of the last packet, plus the number of | |
09ee2f0d | 152 | * samples it contained. Only valid if @ref active is nonzero. |
0b75463f | 153 | */ |
8e3fe3d8 | 154 | uint32_t next_timestamp; |
e83d0967 | 155 | |
09ee2f0d | 156 | /** @brief True if actively playing |
157 | * | |
158 | * This is true when playing and false when just buffering. */ | |
8e3fe3d8 | 159 | int active; |
09ee2f0d | 160 | |
189e9830 | 161 | /** @brief Lock protecting @ref packets */ |
8e3fe3d8 | 162 | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
189e9830 RK |
163 | |
164 | /** @brief Condition variable signalled whenever @ref packets is changed */ | |
8e3fe3d8 | 165 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
2c7c9eae | 166 | |
c593cf7c | 167 | /** @brief Backend to play with */ |
7a2c7068 | 168 | static const struct uaudio *backend; |
c593cf7c | 169 | |
8e3fe3d8 | 170 | HEAP_DEFINE(pheap, struct packet *, lt_packet); |
e83d0967 | 171 | |
a99c4e9a RK |
172 | /** @brief Control socket or NULL */ |
173 | const char *control_socket; | |
174 | ||
b28bddbb RK |
175 | /** @brief Buffer for debugging dump |
176 | * | |
177 | * The debug dump is enabled by the @c --dump option. It records the last 20s | |
178 | * of audio to the specified file (which will be about 3.5Mbytes). The file is | |
179 | * written as as ring buffer, so the start point will progress through it. | |
180 | * | |
181 | * Use clients/dump2wav to convert this to a WAV file, which can then be loaded | |
182 | * into (e.g.) Audacity for further inspection. | |
183 | * | |
184 | * All three backends (ALSA, OSS, Core Audio) now support this option. | |
185 | * | |
186 | * The idea is to allow the user a few seconds to react to an audible artefact. | |
187 | */ | |
e9b635a3 | 188 | int16_t *dump_buffer; |
b28bddbb RK |
189 | |
190 | /** @brief Current index within debugging dump */ | |
e9b635a3 | 191 | size_t dump_index; |
b28bddbb RK |
192 | |
193 | /** @brief Size of debugging dump in samples */ | |
194 | size_t dump_size = 44100/*Hz*/ * 2/*channels*/ * 20/*seconds*/; | |
e9b635a3 | 195 | |
e83d0967 RK |
196 | static const struct option options[] = { |
197 | { "help", no_argument, 0, 'h' }, | |
198 | { "version", no_argument, 0, 'V' }, | |
199 | { "debug", no_argument, 0, 'd' }, | |
0b75463f | 200 | { "device", required_argument, 0, 'D' }, |
1153fd23 | 201 | { "min", required_argument, 0, 'm' }, |
9086a105 | 202 | { "max", required_argument, 0, 'x' }, |
1f10f780 | 203 | { "rcvbuf", required_argument, 0, 'R' }, |
a9f0ad12 | 204 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 205 | { "oss", no_argument, 0, 'o' }, |
206 | #endif | |
146e86fb | 207 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 208 | { "alsa", no_argument, 0, 'a' }, |
209 | #endif | |
210 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
211 | { "core-audio", no_argument, 0, 'c' }, | |
212 | #endif | |
ba32e50c | 213 | { "api", required_argument, 0, 'A' }, |
e9b635a3 | 214 | { "dump", required_argument, 0, 'r' }, |
e979b844 | 215 | { "command", required_argument, 0, 'e' }, |
287ad384 | 216 | { "pause-mode", required_argument, 0, 'P' }, |
a99c4e9a | 217 | { "socket", required_argument, 0, 's' }, |
a7e9570a | 218 | { "config", required_argument, 0, 'C' }, |
b0619501 | 219 | { "monitor", no_argument, 0, 'M' }, |
e83d0967 RK |
220 | { 0, 0, 0, 0 } |
221 | }; | |
222 | ||
a99c4e9a RK |
223 | /** @brief Control thread |
224 | * | |
225 | * This thread is responsible for accepting control commands from Disobedience | |
226 | * (or other controllers) over an AF_UNIX stream socket with a path specified | |
227 | * by the @c --socket option. The protocol uses simple string commands and | |
228 | * replies: | |
229 | * | |
230 | * - @c stop will shut the player down | |
231 | * - @c query will send back the reply @c running | |
232 | * - anything else is ignored | |
233 | * | |
234 | * Commands and response strings terminated by shutting down the connection or | |
235 | * by a newline. No attempt is made to multiplex multiple clients so it is | |
236 | * important that the command be sent as soon as the connection is made - it is | |
237 | * assumed that both parties to the protocol are entirely cooperating with one | |
238 | * another. | |
239 | */ | |
240 | static void *control_thread(void attribute((unused)) *arg) { | |
241 | struct sockaddr_un sa; | |
242 | int sfd, cfd; | |
243 | char *line; | |
244 | socklen_t salen; | |
245 | FILE *fp; | |
af21fb6b | 246 | int vl, vr; |
a99c4e9a RK |
247 | |
248 | assert(control_socket); | |
249 | unlink(control_socket); | |
250 | memset(&sa, 0, sizeof sa); | |
251 | sa.sun_family = AF_UNIX; | |
252 | strcpy(sa.sun_path, control_socket); | |
253 | sfd = xsocket(PF_UNIX, SOCK_STREAM, 0); | |
254 | if(bind(sfd, (const struct sockaddr *)&sa, sizeof sa) < 0) | |
2e9ba080 | 255 | disorder_fatal(errno, "error binding to %s", control_socket); |
a99c4e9a | 256 | if(listen(sfd, 128) < 0) |
2e9ba080 RK |
257 | disorder_fatal(errno, "error calling listen on %s", control_socket); |
258 | disorder_info("listening on %s", control_socket); | |
a99c4e9a RK |
259 | for(;;) { |
260 | salen = sizeof sa; | |
261 | cfd = accept(sfd, (struct sockaddr *)&sa, &salen); | |
262 | if(cfd < 0) { | |
263 | switch(errno) { | |
264 | case EINTR: | |
265 | case EAGAIN: | |
266 | break; | |
267 | default: | |
2e9ba080 | 268 | disorder_fatal(errno, "error calling accept on %s", control_socket); |
a99c4e9a RK |
269 | } |
270 | } | |
271 | if(!(fp = fdopen(cfd, "r+"))) { | |
2e9ba080 | 272 | disorder_error(errno, "error calling fdopen for %s connection", control_socket); |
a99c4e9a RK |
273 | close(cfd); |
274 | continue; | |
275 | } | |
276 | if(!inputline(control_socket, fp, &line, '\n')) { | |
277 | if(!strcmp(line, "stop")) { | |
2e9ba080 | 278 | disorder_info("stopped via %s", control_socket); |
a99c4e9a | 279 | exit(0); /* terminate immediately */ |
af21fb6b | 280 | } else if(!strcmp(line, "query")) |
a99c4e9a | 281 | fprintf(fp, "running"); |
af21fb6b MW |
282 | else if(!strcmp(line, "getvol")) { |
283 | if(backend->get_volume) backend->get_volume(&vl, &vr); | |
284 | else vl = vr = 0; | |
285 | fprintf(fp, "%d %d\n", vl, vr); | |
286 | } else if(!strncmp(line, "setvol ", 7)) { | |
287 | if(!backend->set_volume) | |
288 | vl = vr = 0; | |
289 | else if(sscanf(line + 7, "%d %d", &vl, &vr) == 2) | |
290 | backend->set_volume(&vl, &vr); | |
291 | else | |
292 | backend->get_volume(&vl, &vr); | |
293 | fprintf(fp, "%d %d\n", vl, vr); | |
294 | } | |
a99c4e9a RK |
295 | xfree(line); |
296 | } | |
297 | if(fclose(fp) < 0) | |
2e9ba080 | 298 | disorder_error(errno, "error closing %s connection", control_socket); |
a99c4e9a RK |
299 | } |
300 | } | |
301 | ||
28bacdc0 RK |
302 | /** @brief Drop the first packet |
303 | * | |
304 | * Assumes that @ref lock is held. | |
305 | */ | |
306 | static void drop_first_packet(void) { | |
307 | if(pheap_count(&packets)) { | |
308 | struct packet *const p = pheap_remove(&packets); | |
309 | nsamples -= p->nsamples; | |
c593cf7c | 310 | playrtp_free_packet(p); |
2c7c9eae | 311 | pthread_cond_broadcast(&cond); |
2c7c9eae | 312 | } |
9086a105 RK |
313 | } |
314 | ||
189e9830 RK |
315 | /** @brief Background thread adding packets to heap |
316 | * | |
317 | * This just transfers packets from @ref received_packets to @ref packets. It | |
318 | * is important that it holds @ref receive_lock for as little time as possible, | |
319 | * in order to minimize the interval between calls to read() in | |
320 | * listen_thread(). | |
321 | */ | |
322 | static void *queue_thread(void attribute((unused)) *arg) { | |
323 | struct packet *p; | |
324 | ||
325 | for(;;) { | |
326 | /* Get the next packet */ | |
327 | pthread_mutex_lock(&receive_lock); | |
4dadf1a2 | 328 | while(!received_packets) { |
189e9830 | 329 | pthread_cond_wait(&receive_cond, &receive_lock); |
4dadf1a2 | 330 | } |
189e9830 RK |
331 | p = received_packets; |
332 | received_packets = p->next; | |
333 | if(!received_packets) | |
334 | received_tail = &received_packets; | |
335 | --nreceived; | |
336 | pthread_mutex_unlock(&receive_lock); | |
337 | /* Add it to the heap */ | |
338 | pthread_mutex_lock(&lock); | |
339 | pheap_insert(&packets, p); | |
340 | nsamples += p->nsamples; | |
341 | pthread_cond_broadcast(&cond); | |
342 | pthread_mutex_unlock(&lock); | |
343 | } | |
c6a70f38 RK |
344 | #if HAVE_STUPID_GCC44 |
345 | return NULL; | |
346 | #endif | |
189e9830 RK |
347 | } |
348 | ||
09ee2f0d | 349 | /** @brief Background thread collecting samples |
0b75463f | 350 | * |
351 | * This function collects samples, perhaps converts them to the target format, | |
b0fdc63d | 352 | * and adds them to the packet list. |
353 | * | |
354 | * It is crucial that the gap between successive calls to read() is as small as | |
355 | * possible: otherwise packets will be dropped. | |
356 | * | |
357 | * We use a binary heap to ensure that the unavoidable effort is at worst | |
358 | * logarithmic in the total number of packets - in fact if packets are mostly | |
359 | * received in order then we will largely do constant work per packet since the | |
360 | * newest packet will always be last. | |
361 | * | |
362 | * Of more concern is that we must acquire the lock on the heap to add a packet | |
363 | * to it. If this proves a problem in practice then the answer would be | |
364 | * (probably doubly) linked list with new packets added the end and a second | |
365 | * thread which reads packets off the list and adds them to the heap. | |
366 | * | |
367 | * We keep memory allocation (mostly) very fast by keeping pre-allocated | |
c593cf7c | 368 | * packets around; see @ref playrtp_new_packet(). |
b0fdc63d | 369 | */ |
0b75463f | 370 | static void *listen_thread(void attribute((unused)) *arg) { |
2c7c9eae | 371 | struct packet *p = 0; |
0b75463f | 372 | int n; |
2c7c9eae RK |
373 | struct rtp_header header; |
374 | uint16_t seq; | |
375 | uint32_t timestamp; | |
376 | struct iovec iov[2]; | |
e83d0967 RK |
377 | |
378 | for(;;) { | |
189e9830 | 379 | if(!p) |
c593cf7c | 380 | p = playrtp_new_packet(); |
2c7c9eae RK |
381 | iov[0].iov_base = &header; |
382 | iov[0].iov_len = sizeof header; | |
383 | iov[1].iov_base = p->samples_raw; | |
b64efe7e | 384 | iov[1].iov_len = sizeof p->samples_raw / sizeof *p->samples_raw; |
2c7c9eae | 385 | n = readv(rtpfd, iov, 2); |
e83d0967 RK |
386 | if(n < 0) { |
387 | switch(errno) { | |
388 | case EINTR: | |
389 | continue; | |
390 | default: | |
2e9ba080 | 391 | disorder_fatal(errno, "error reading from socket"); |
e83d0967 RK |
392 | } |
393 | } | |
0b75463f | 394 | /* Ignore too-short packets */ |
345ebe66 | 395 | if((size_t)n <= sizeof (struct rtp_header)) { |
2e9ba080 | 396 | disorder_info("ignored a short packet"); |
0b75463f | 397 | continue; |
345ebe66 | 398 | } |
2c7c9eae RK |
399 | timestamp = htonl(header.timestamp); |
400 | seq = htons(header.seq); | |
09ee2f0d | 401 | /* Ignore packets in the past */ |
2c7c9eae | 402 | if(active && lt(timestamp, next_timestamp)) { |
2e9ba080 | 403 | disorder_info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32, |
2c7c9eae | 404 | timestamp, next_timestamp); |
09ee2f0d | 405 | continue; |
c0e41690 | 406 | } |
28f1495a RK |
407 | /* Ignore packets with the extension bit set. */ |
408 | if(header.vpxcc & 0x10) | |
409 | continue; | |
189e9830 | 410 | p->next = 0; |
58b5a68f | 411 | p->flags = 0; |
2c7c9eae | 412 | p->timestamp = timestamp; |
e83d0967 | 413 | /* Convert to target format */ |
58b5a68f RK |
414 | if(header.mpt & 0x80) |
415 | p->flags |= IDLE; | |
2c7c9eae | 416 | switch(header.mpt & 0x7F) { |
4fd38868 | 417 | case 10: /* L16 */ |
2c7c9eae | 418 | p->nsamples = (n - sizeof header) / sizeof(uint16_t); |
e83d0967 RK |
419 | break; |
420 | /* TODO support other RFC3551 media types (when the speaker does) */ | |
421 | default: | |
2e9ba080 | 422 | disorder_fatal(0, "unsupported RTP payload type %d", header.mpt & 0x7F); |
e83d0967 | 423 | } |
67d308e7 RK |
424 | /* See if packet is silent */ |
425 | const uint16_t *s = p->samples_raw; | |
7bdf42d0 | 426 | n = p->nsamples; |
67d308e7 RK |
427 | for(; n > 0; --n) |
428 | if(*s++) | |
429 | break; | |
430 | if(!n) | |
431 | p->flags |= SILENT; | |
345ebe66 RK |
432 | if(logfp) |
433 | fprintf(logfp, "sequence %u timestamp %"PRIx32" length %"PRIx32" end %"PRIx32"\n", | |
2c7c9eae | 434 | seq, timestamp, p->nsamples, timestamp + p->nsamples); |
0b75463f | 435 | /* Stop reading if we've reached the maximum. |
436 | * | |
437 | * This is rather unsatisfactory: it means that if packets get heavily | |
438 | * out of order then we guarantee dropouts. But for now... */ | |
345ebe66 | 439 | if(nsamples >= maxbuffer) { |
189e9830 | 440 | pthread_mutex_lock(&lock); |
4dadf1a2 | 441 | while(nsamples >= maxbuffer) { |
345ebe66 | 442 | pthread_cond_wait(&cond, &lock); |
4dadf1a2 | 443 | } |
189e9830 | 444 | pthread_mutex_unlock(&lock); |
345ebe66 | 445 | } |
189e9830 RK |
446 | /* Add the packet to the receive queue */ |
447 | pthread_mutex_lock(&receive_lock); | |
448 | *received_tail = p; | |
449 | received_tail = &p->next; | |
450 | ++nreceived; | |
451 | pthread_cond_signal(&receive_cond); | |
452 | pthread_mutex_unlock(&receive_lock); | |
58b5a68f RK |
453 | /* We'll need a new packet */ |
454 | p = 0; | |
e83d0967 RK |
455 | } |
456 | } | |
457 | ||
5626f6d2 RK |
458 | /** @brief Wait until the buffer is adequately full |
459 | * | |
460 | * Must be called with @ref lock held. | |
461 | */ | |
c593cf7c | 462 | void playrtp_fill_buffer(void) { |
0e72bf84 | 463 | /* Discard current buffer contents */ |
ad535598 RK |
464 | while(nsamples) { |
465 | //fprintf(stderr, "%8u/%u (%u) DROPPING\n", nsamples, maxbuffer, minbuffer); | |
bfd27c14 | 466 | drop_first_packet(); |
ad535598 | 467 | } |
2e9ba080 | 468 | disorder_info("Buffering..."); |
0e72bf84 RK |
469 | /* Wait until there's at least minbuffer samples available */ |
470 | while(nsamples < minbuffer) { | |
ad535598 | 471 | //fprintf(stderr, "%8u/%u (%u) FILLING\n", nsamples, maxbuffer, minbuffer); |
5626f6d2 | 472 | pthread_cond_wait(&cond, &lock); |
4dadf1a2 | 473 | } |
0e72bf84 | 474 | /* Start from whatever is earliest */ |
5626f6d2 RK |
475 | next_timestamp = pheap_first(&packets)->timestamp; |
476 | active = 1; | |
477 | } | |
478 | ||
479 | /** @brief Find next packet | |
480 | * @return Packet to play or NULL if none found | |
481 | * | |
482 | * The return packet is merely guaranteed not to be in the past: it might be | |
483 | * the first packet in the future rather than one that is actually suitable to | |
484 | * play. | |
485 | * | |
486 | * Must be called with @ref lock held. | |
487 | */ | |
c593cf7c | 488 | struct packet *playrtp_next_packet(void) { |
5626f6d2 RK |
489 | while(pheap_count(&packets)) { |
490 | struct packet *const p = pheap_first(&packets); | |
491 | if(le(p->timestamp + p->nsamples, next_timestamp)) { | |
492 | /* This packet is in the past. Drop it and try another one. */ | |
493 | drop_first_packet(); | |
494 | } else | |
495 | /* This packet is NOT in the past. (It might be in the future | |
496 | * however.) */ | |
497 | return p; | |
498 | } | |
499 | return 0; | |
500 | } | |
501 | ||
e83d0967 | 502 | /* display usage message and terminate */ |
16bf32dc | 503 | static void attribute((noreturn)) help(void) { |
e83d0967 | 504 | xprintf("Usage:\n" |
c897bb65 | 505 | " disorder-playrtp [OPTIONS] [[ADDRESS] PORT]\n" |
e83d0967 | 506 | "Options:\n" |
1153fd23 | 507 | " --device, -D DEVICE Output device\n" |
508 | " --min, -m FRAMES Buffer low water mark\n" | |
9086a105 | 509 | " --max, -x FRAMES Buffer maximum size\n" |
1f10f780 | 510 | " --rcvbuf, -R BYTES Socket receive buffer size\n" |
a7e9570a | 511 | " --config, -C PATH Set configuration file\n" |
ba32e50c RK |
512 | " --api, -A API Select audio API. Possibilities:\n" |
513 | " "); | |
514 | int first = 1; | |
515 | for(int n = 0; uaudio_apis[n]; ++n) { | |
516 | if(uaudio_apis[n]->flags & UAUDIO_API_CLIENT) { | |
517 | if(first) | |
518 | first = 0; | |
519 | else | |
520 | xprintf(", "); | |
521 | xprintf("%s", uaudio_apis[n]->name); | |
522 | } | |
523 | } | |
524 | xprintf("\n" | |
287ad384 RK |
525 | " --command, -e COMMAND Pipe audio to command.\n" |
526 | " --pause-mode, -P silence For -e: pauses send silence (default)\n" | |
527 | " --pause-mode, -P suspend For -e: pauses suspend writes\n" | |
9086a105 RK |
528 | " --help, -h Display usage message\n" |
529 | " --version, -V Display version number\n" | |
530 | ); | |
e83d0967 RK |
531 | xfclose(stdout); |
532 | exit(0); | |
533 | } | |
534 | ||
4fd38868 | 535 | static size_t playrtp_callback(void *buffer, |
7a2c7068 RK |
536 | size_t max_samples, |
537 | void attribute((unused)) *userdata) { | |
538 | size_t samples; | |
67d308e7 | 539 | int silent = 0; |
7a2c7068 RK |
540 | |
541 | pthread_mutex_lock(&lock); | |
542 | /* Get the next packet, junking any that are now in the past */ | |
543 | const struct packet *p = playrtp_next_packet(); | |
544 | if(p && contains(p, next_timestamp)) { | |
545 | /* This packet is ready to play; the desired next timestamp points | |
546 | * somewhere into it. */ | |
547 | ||
548 | /* Timestamp of end of packet */ | |
549 | const uint32_t packet_end = p->timestamp + p->nsamples; | |
550 | ||
551 | /* Offset of desired next timestamp into current packet */ | |
552 | const uint32_t offset = next_timestamp - p->timestamp; | |
553 | ||
554 | /* Pointer to audio data */ | |
555 | const uint16_t *ptr = (void *)(p->samples_raw + offset); | |
556 | ||
557 | /* Compute number of samples left in packet, limited to output buffer | |
558 | * size */ | |
559 | samples = packet_end - next_timestamp; | |
560 | if(samples > max_samples) | |
561 | samples = max_samples; | |
562 | ||
563 | /* Copy into buffer, converting to native endianness */ | |
564 | size_t i = samples; | |
565 | int16_t *bufptr = buffer; | |
566 | while(i > 0) { | |
567 | *bufptr++ = (int16_t)ntohs(*ptr++); | |
568 | --i; | |
569 | } | |
67d308e7 | 570 | silent = !!(p->flags & SILENT); |
7a2c7068 RK |
571 | } else { |
572 | /* There is no suitable packet. We introduce 0s up to the next packet, or | |
573 | * to fill the buffer if there's no next packet or that's too many. The | |
574 | * comparison with max_samples deals with the otherwise troubling overflow | |
575 | * case. */ | |
576 | samples = p ? p->timestamp - next_timestamp : max_samples; | |
577 | if(samples > max_samples) | |
578 | samples = max_samples; | |
579 | //info("infill by %zu", samples); | |
4fd38868 | 580 | memset(buffer, 0, samples * uaudio_sample_size); |
67d308e7 | 581 | silent = 1; |
7a2c7068 RK |
582 | } |
583 | /* Debug dump */ | |
584 | if(dump_buffer) { | |
585 | for(size_t i = 0; i < samples; ++i) { | |
4fd38868 | 586 | dump_buffer[dump_index++] = ((int16_t *)buffer)[i]; |
7a2c7068 RK |
587 | dump_index %= dump_size; |
588 | } | |
589 | } | |
590 | /* Advance timestamp */ | |
591 | next_timestamp += samples; | |
7edc7e42 RK |
592 | /* If we're getting behind then try to drop just silent packets |
593 | * | |
594 | * In theory this shouldn't be necessary. The server is supposed to send | |
595 | * packets at the right rate and compares the number of samples sent with the | |
596 | * time in order to ensure this. | |
597 | * | |
598 | * However, various things could throw this off: | |
599 | * | |
600 | * - the server's clock could advance at the wrong rate. This would cause it | |
601 | * to mis-estimate the right number of samples to have sent and | |
602 | * inappropriately throttle or speed up. | |
603 | * | |
604 | * - playback could happen at the wrong rate. If the playback host's sound | |
605 | * card has a slightly incorrect clock then eventually it will get out | |
606 | * of step. | |
607 | * | |
608 | * So if we play back slightly slower than the server sends for either of | |
609 | * these reasons then eventually our buffer, and the socket's buffer, will | |
610 | * fill, and the kernel will start dropping packets. The result is audible | |
611 | * and not very nice. | |
612 | * | |
613 | * Therefore if we're getting behind, we pre-emptively drop silent packets, | |
614 | * since a change in the duration of a silence is less noticeable than a | |
615 | * dropped packet from the middle of continuous music. | |
616 | * | |
617 | * (If things go wrong the other way then eventually we run out of packets to | |
618 | * play and are forced to play silence. This doesn't seem to happen in | |
619 | * practice but if it does then in the same way we can artificially extend | |
620 | * silent packets to compensate.) | |
621 | * | |
622 | * Dropped packets are always logged; use 'disorder-playrtp --monitor' to | |
623 | * track how close to target buffer occupancy we are on a once-a-minute | |
624 | * basis. | |
625 | */ | |
67d308e7 | 626 | if(nsamples > minbuffer && silent) { |
2e9ba080 RK |
627 | disorder_info("dropping %zu samples (%"PRIu32" > %"PRIu32")", |
628 | samples, nsamples, minbuffer); | |
67d308e7 RK |
629 | samples = 0; |
630 | } | |
ad535598 RK |
631 | /* Junk obsolete packets */ |
632 | playrtp_next_packet(); | |
7a2c7068 RK |
633 | pthread_mutex_unlock(&lock); |
634 | return samples; | |
635 | } | |
636 | ||
cca034e5 RK |
637 | static int compare_family(const struct ifaddrs *a, |
638 | const struct ifaddrs *b, | |
639 | int family) { | |
640 | int afamily = a->ifa_addr->sa_family; | |
641 | int bfamily = b->ifa_addr->sa_family; | |
642 | if(afamily != bfamily) { | |
643 | /* Preferred family wins */ | |
644 | if(afamily == family) return 1; | |
645 | if(bfamily == family) return -1; | |
646 | /* Either there's no preference or it doesn't help. Prefer IPv4 */ | |
647 | if(afamily == AF_INET) return 1; | |
648 | if(bfamily == AF_INET) return -1; | |
649 | /* Failing that prefer IPv6 */ | |
650 | if(afamily == AF_INET6) return 1; | |
651 | if(bfamily == AF_INET6) return -1; | |
652 | } | |
653 | return 0; | |
654 | } | |
655 | ||
656 | static int compare_flags(const struct ifaddrs *a, | |
657 | const struct ifaddrs *b) { | |
658 | unsigned aflags = a->ifa_flags, bflags = b->ifa_flags; | |
659 | /* Up interfaces are better than down ones */ | |
660 | unsigned aup = aflags & IFF_UP, bup = bflags & IFF_UP; | |
661 | if(aup != bup) | |
662 | return aup > bup ? 1 : -1; | |
ebd61a13 | 663 | #if IFF_DYNAMIC |
cca034e5 RK |
664 | /* Static addresses are better than dynamic */ |
665 | unsigned adynamic = aflags & IFF_DYNAMIC, bdynamic = bflags & IFF_DYNAMIC; | |
666 | if(adynamic != bdynamic) | |
667 | return adynamic < bdynamic ? 1 : -1; | |
ebd61a13 | 668 | #endif |
cca034e5 RK |
669 | unsigned aloopback = aflags & IFF_LOOPBACK, bloopback = bflags & IFF_LOOPBACK; |
670 | /* Static addresses are better than dynamic */ | |
671 | if(aloopback != bloopback) | |
672 | return aloopback < bloopback ? 1 : -1; | |
673 | return 0; | |
674 | } | |
675 | ||
676 | static int compare_interfaces(const struct ifaddrs *a, | |
677 | const struct ifaddrs *b, | |
678 | int family) { | |
679 | int c; | |
680 | if((c = compare_family(a, b, family))) return c; | |
681 | return compare_flags(a, b); | |
682 | } | |
683 | ||
e83d0967 | 684 | int main(int argc, char **argv) { |
a99c4e9a | 685 | int n, err; |
e83d0967 RK |
686 | struct addrinfo *res; |
687 | struct stringlist sl; | |
0b75463f | 688 | char *sockname; |
0e72bf84 | 689 | int rcvbuf, target_rcvbuf = 0; |
1f10f780 | 690 | socklen_t len; |
23205f9c RK |
691 | struct ip_mreq mreq; |
692 | struct ipv6_mreq mreq6; | |
cca034e5 | 693 | disorder_client *c = NULL; |
a7e9570a | 694 | char *address, *port; |
6fba990c RK |
695 | int is_multicast; |
696 | union any_sockaddr { | |
697 | struct sockaddr sa; | |
698 | struct sockaddr_in in; | |
699 | struct sockaddr_in6 in6; | |
700 | }; | |
701 | union any_sockaddr mgroup; | |
e9b635a3 | 702 | const char *dumpfile = 0; |
7a2c7068 | 703 | pthread_t ltid; |
b0619501 | 704 | int monitor = 0; |
983c3357 | 705 | static const int one = 1; |
e83d0967 | 706 | |
c5fbc6c7 | 707 | struct addrinfo prefs = { |
66613034 RK |
708 | .ai_flags = AI_PASSIVE, |
709 | .ai_family = PF_INET, | |
710 | .ai_socktype = SOCK_DGRAM, | |
711 | .ai_protocol = IPPROTO_UDP | |
e83d0967 RK |
712 | }; |
713 | ||
9d7a6129 RK |
714 | /* Timing information is often important to debugging playrtp, so we include |
715 | * timestamps in the logs */ | |
716 | logdate = 1; | |
e83d0967 | 717 | mem_init(); |
2e9ba080 | 718 | if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale"); |
ba32e50c | 719 | while((n = getopt_long(argc, argv, "hVdD:m:x:L:R:aocC:re:P:MA:", options, 0)) >= 0) { |
e83d0967 RK |
720 | switch(n) { |
721 | case 'h': help(); | |
3fbdc96d | 722 | case 'V': version("disorder-playrtp"); |
e83d0967 | 723 | case 'd': debugging = 1; break; |
e979b844 | 724 | case 'D': uaudio_set("device", optarg); break; |
1153fd23 | 725 | case 'm': minbuffer = 2 * atol(optarg); break; |
9086a105 | 726 | case 'x': maxbuffer = 2 * atol(optarg); break; |
345ebe66 | 727 | case 'L': logfp = fopen(optarg, "w"); break; |
1f10f780 | 728 | case 'R': target_rcvbuf = atoi(optarg); break; |
146e86fb | 729 | #if HAVE_ALSA_ASOUNDLIB_H |
ba32e50c RK |
730 | case 'a': |
731 | disorder_error(0, "deprecated option; use --api alsa instead"); | |
732 | backend = &uaudio_alsa; break; | |
c593cf7c | 733 | #endif |
a9f0ad12 | 734 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
ba32e50c RK |
735 | case 'o': |
736 | disorder_error(0, "deprecated option; use --api oss instead"); | |
737 | backend = &uaudio_oss; | |
738 | break; | |
c593cf7c | 739 | #endif |
740 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
ba32e50c RK |
741 | case 'c': |
742 | disorder_error(0, "deprecated option; use --api coreaudio instead"); | |
743 | backend = &uaudio_coreaudio; | |
744 | break; | |
c593cf7c | 745 | #endif |
ba32e50c | 746 | case 'A': backend = uaudio_find(optarg); break; |
a7e9570a | 747 | case 'C': configfile = optarg; break; |
a99c4e9a | 748 | case 's': control_socket = optarg; break; |
e9b635a3 | 749 | case 'r': dumpfile = optarg; break; |
e979b844 | 750 | case 'e': backend = &uaudio_command; uaudio_set("command", optarg); break; |
287ad384 | 751 | case 'P': uaudio_set("pause-mode", optarg); break; |
b0619501 | 752 | case 'M': monitor = 1; break; |
2e9ba080 | 753 | default: disorder_fatal(0, "invalid option"); |
e83d0967 RK |
754 | } |
755 | } | |
2e9ba080 | 756 | if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration"); |
163ed8a5 | 757 | /* Choose a sensible default audio backend */ |
06385470 RK |
758 | if(!backend) { |
759 | backend = uaudio_default(uaudio_apis, UAUDIO_API_CLIENT); | |
760 | if(!backend) | |
761 | disorder_fatal(0, "no default uaudio API found"); | |
762 | disorder_info("default audio API %s", backend->name); | |
763 | } | |
30a82196 RK |
764 | if(backend == &uaudio_rtp) { |
765 | /* This means that you have NO local sound output. This can happen if you | |
766 | * use a non-Apple GCC on a Mac (because it doesn't know how to compile | |
767 | * CoreAudio/AudioHardware.h). */ | |
768 | disorder_fatal(0, "cannot play RTP through RTP"); | |
769 | } | |
9086a105 | 770 | if(!maxbuffer) |
0e72bf84 | 771 | maxbuffer = 2 * minbuffer; |
e83d0967 RK |
772 | argc -= optind; |
773 | argv += optind; | |
a7e9570a RK |
774 | switch(argc) { |
775 | case 0: | |
6fba990c | 776 | /* Get configuration from server */ |
a7e9570a RK |
777 | if(!(c = disorder_new(1))) exit(EXIT_FAILURE); |
778 | if(disorder_connect(c)) exit(EXIT_FAILURE); | |
779 | if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE); | |
6fba990c RK |
780 | sl.n = 2; |
781 | sl.s = xcalloc(2, sizeof *sl.s); | |
782 | sl.s[0] = address; | |
783 | sl.s[1] = port; | |
a7e9570a | 784 | break; |
6fba990c | 785 | case 1: |
a7e9570a | 786 | case 2: |
6fba990c | 787 | /* Use command-line ADDRESS+PORT or just PORT */ |
a7e9570a RK |
788 | sl.n = argc; |
789 | sl.s = argv; | |
790 | break; | |
791 | default: | |
2e9ba080 | 792 | disorder_fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]"); |
a7e9570a | 793 | } |
7077d53f RK |
794 | disorder_info("version "VERSION" process ID %lu", |
795 | (unsigned long)getpid()); | |
cca034e5 RK |
796 | struct sockaddr *addr; |
797 | socklen_t addr_len; | |
798 | if(!strcmp(sl.s[0], "-")) { | |
d7255a30 MW |
799 | /* We'll need a connection to request the incoming stream, so open one if |
800 | * we don't have one already */ | |
801 | if(!c) { | |
802 | if(!(c = disorder_new(1))) exit(EXIT_FAILURE); | |
803 | if(disorder_connect(c)) exit(EXIT_FAILURE); | |
804 | } | |
cca034e5 RK |
805 | /* Pick address family to match known-working connectivity to the server */ |
806 | int family = disorder_client_af(c); | |
807 | /* Get a list of interfaces */ | |
808 | struct ifaddrs *ifa, *bestifa = NULL; | |
809 | if(getifaddrs(&ifa) < 0) | |
810 | disorder_fatal(errno, "error calling getifaddrs"); | |
811 | /* Try to pick a good one */ | |
812 | for(; ifa; ifa = ifa->ifa_next) { | |
684115ed | 813 | if(!ifa->ifa_addr) continue; |
cca034e5 RK |
814 | if(bestifa == NULL |
815 | || compare_interfaces(ifa, bestifa, family) > 0) | |
816 | bestifa = ifa; | |
23205f9c | 817 | } |
cca034e5 RK |
818 | if(!bestifa) |
819 | disorder_fatal(0, "failed to select a network interface"); | |
820 | family = bestifa->ifa_addr->sa_family; | |
821 | if((rtpfd = socket(family, | |
822 | SOCK_DGRAM, | |
823 | IPPROTO_UDP)) < 0) | |
824 | disorder_fatal(errno, "error creating socket (family %d)", family); | |
825 | /* Bind the address */ | |
826 | if(bind(rtpfd, bestifa->ifa_addr, | |
827 | family == AF_INET | |
828 | ? sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6)) < 0) | |
829 | disorder_fatal(errno, "error binding socket"); | |
830 | static struct sockaddr_storage bound_address; | |
831 | addr = (struct sockaddr *)&bound_address; | |
832 | addr_len = sizeof bound_address; | |
833 | if(getsockname(rtpfd, addr, &addr_len) < 0) | |
834 | disorder_fatal(errno, "error getting socket address"); | |
835 | /* Convert to string */ | |
836 | char addrname[128], portname[32]; | |
837 | if(getnameinfo(addr, addr_len, | |
838 | addrname, sizeof addrname, | |
839 | portname, sizeof portname, | |
840 | NI_NUMERICHOST|NI_NUMERICSERV) < 0) | |
841 | disorder_fatal(errno, "getnameinfo"); | |
842 | /* Ask for audio data */ | |
843 | if(disorder_rtp_request(c, addrname, portname)) exit(EXIT_FAILURE); | |
983c3357 | 844 | /* Report what we did */ |
cca034e5 | 845 | disorder_info("listening on %s", format_sockaddr(addr)); |
983c3357 | 846 | } else { |
cca034e5 RK |
847 | /* Look up address and port */ |
848 | if(!(res = get_address(&sl, &prefs, &sockname))) | |
849 | exit(1); | |
850 | addr = res->ai_addr; | |
851 | addr_len = res->ai_addrlen; | |
852 | /* Create the socket */ | |
853 | if((rtpfd = socket(res->ai_family, | |
854 | res->ai_socktype, | |
855 | res->ai_protocol)) < 0) | |
856 | disorder_fatal(errno, "error creating socket"); | |
857 | /* Allow multiple listeners */ | |
858 | xsetsockopt(rtpfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); | |
859 | is_multicast = multicast(addr); | |
860 | /* The multicast and unicast/broadcast cases are different enough that they | |
861 | * are totally split. Trying to find commonality between them causes more | |
862 | * trouble that it's worth. */ | |
863 | if(is_multicast) { | |
864 | /* Stash the multicast group address */ | |
865 | memcpy(&mgroup, addr, addr_len); | |
866 | switch(res->ai_addr->sa_family) { | |
867 | case AF_INET: | |
868 | mgroup.in.sin_port = 0; | |
869 | break; | |
870 | case AF_INET6: | |
871 | mgroup.in6.sin6_port = 0; | |
872 | break; | |
873 | default: | |
874 | disorder_fatal(0, "unsupported address family %d", | |
875 | (int)addr->sa_family); | |
876 | } | |
877 | /* Bind to to the multicast group address */ | |
878 | if(bind(rtpfd, addr, addr_len) < 0) | |
879 | disorder_fatal(errno, "error binding socket to %s", | |
880 | format_sockaddr(addr)); | |
881 | /* Add multicast group membership */ | |
882 | switch(mgroup.sa.sa_family) { | |
883 | case PF_INET: | |
884 | mreq.imr_multiaddr = mgroup.in.sin_addr; | |
885 | mreq.imr_interface.s_addr = 0; /* use primary interface */ | |
886 | if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
887 | &mreq, sizeof mreq) < 0) | |
888 | disorder_fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP"); | |
889 | break; | |
890 | case PF_INET6: | |
891 | mreq6.ipv6mr_multiaddr = mgroup.in6.sin6_addr; | |
892 | memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface); | |
893 | if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, | |
894 | &mreq6, sizeof mreq6) < 0) | |
895 | disorder_fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP"); | |
896 | break; | |
897 | default: | |
898 | disorder_fatal(0, "unsupported address family %d", res->ai_family); | |
899 | } | |
900 | /* Report what we did */ | |
901 | disorder_info("listening on %s multicast group %s", | |
902 | format_sockaddr(addr), format_sockaddr(&mgroup.sa)); | |
903 | } else { | |
904 | /* Bind to 0/port */ | |
905 | switch(addr->sa_family) { | |
906 | case AF_INET: { | |
907 | struct sockaddr_in *in = (struct sockaddr_in *)addr; | |
983c3357 | 908 | |
cca034e5 RK |
909 | memset(&in->sin_addr, 0, sizeof (struct in_addr)); |
910 | break; | |
911 | } | |
912 | case AF_INET6: { | |
913 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; | |
983c3357 | 914 | |
cca034e5 RK |
915 | memset(&in6->sin6_addr, 0, sizeof (struct in6_addr)); |
916 | break; | |
917 | } | |
918 | default: | |
919 | disorder_fatal(0, "unsupported family %d", (int)addr->sa_family); | |
920 | } | |
921 | if(bind(rtpfd, addr, addr_len) < 0) | |
922 | disorder_fatal(errno, "error binding socket to %s", | |
923 | format_sockaddr(addr)); | |
924 | /* Report what we did */ | |
925 | disorder_info("listening on %s", format_sockaddr(addr)); | |
983c3357 | 926 | } |
983c3357 | 927 | } |
1f10f780 RK |
928 | len = sizeof rcvbuf; |
929 | if(getsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len) < 0) | |
2e9ba080 | 930 | disorder_fatal(errno, "error calling getsockopt SO_RCVBUF"); |
f0bae611 | 931 | if(target_rcvbuf > rcvbuf) { |
1f10f780 RK |
932 | if(setsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, |
933 | &target_rcvbuf, sizeof target_rcvbuf) < 0) | |
2e9ba080 RK |
934 | disorder_error(errno, "error calling setsockopt SO_RCVBUF %d", |
935 | target_rcvbuf); | |
1f10f780 RK |
936 | /* We try to carry on anyway */ |
937 | else | |
2e9ba080 RK |
938 | disorder_info("changed socket receive buffer from %d to %d", |
939 | rcvbuf, target_rcvbuf); | |
1f10f780 | 940 | } else |
2e9ba080 | 941 | disorder_info("default socket receive buffer %d", rcvbuf); |
ad535598 | 942 | //info("minbuffer %u maxbuffer %u", minbuffer, maxbuffer); |
1f10f780 | 943 | if(logfp) |
2e9ba080 | 944 | disorder_info("WARNING: -L option can impact performance"); |
a99c4e9a RK |
945 | if(control_socket) { |
946 | pthread_t tid; | |
947 | ||
948 | if((err = pthread_create(&tid, 0, control_thread, 0))) | |
2e9ba080 | 949 | disorder_fatal(err, "pthread_create control_thread"); |
a99c4e9a | 950 | } |
e9b635a3 RK |
951 | if(dumpfile) { |
952 | int fd; | |
953 | unsigned char buffer[65536]; | |
954 | size_t written; | |
955 | ||
956 | if((fd = open(dumpfile, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) | |
2e9ba080 | 957 | disorder_fatal(errno, "opening %s", dumpfile); |
e9b635a3 RK |
958 | /* Fill with 0s to a suitable size */ |
959 | memset(buffer, 0, sizeof buffer); | |
960 | for(written = 0; written < dump_size * sizeof(int16_t); | |
961 | written += sizeof buffer) { | |
962 | if(write(fd, buffer, sizeof buffer) < 0) | |
2e9ba080 | 963 | disorder_fatal(errno, "clearing %s", dumpfile); |
e9b635a3 RK |
964 | } |
965 | /* Map the buffer into memory for convenience */ | |
966 | dump_buffer = mmap(0, dump_size * sizeof(int16_t), PROT_READ|PROT_WRITE, | |
967 | MAP_SHARED, fd, 0); | |
968 | if(dump_buffer == (void *)-1) | |
2e9ba080 RK |
969 | disorder_fatal(errno, "mapping %s", dumpfile); |
970 | disorder_info("dumping to %s", dumpfile); | |
e9b635a3 | 971 | } |
4fd38868 RK |
972 | /* Set up output. Currently we only support L16 so there's no harm setting |
973 | * the format before we know what it is! */ | |
974 | uaudio_set_format(44100/*Hz*/, 2/*channels*/, | |
975 | 16/*bits/channel*/, 1/*signed*/); | |
de0ef46e | 976 | uaudio_set("application", "disorder-playrtp"); |
3b9aa3e5 | 977 | backend->configure(); |
7a2c7068 | 978 | backend->start(playrtp_callback, NULL); |
af21fb6b | 979 | if(backend->open_mixer) backend->open_mixer(); |
7a2c7068 RK |
980 | /* We receive and convert audio data in a background thread */ |
981 | if((err = pthread_create(<id, 0, listen_thread, 0))) | |
2e9ba080 | 982 | disorder_fatal(err, "pthread_create listen_thread"); |
7a2c7068 RK |
983 | /* We have a second thread to add received packets to the queue */ |
984 | if((err = pthread_create(<id, 0, queue_thread, 0))) | |
2e9ba080 | 985 | disorder_fatal(err, "pthread_create queue_thread"); |
7a2c7068 | 986 | pthread_mutex_lock(&lock); |
b0619501 | 987 | time_t lastlog = 0; |
7a2c7068 RK |
988 | for(;;) { |
989 | /* Wait for the buffer to fill up a bit */ | |
990 | playrtp_fill_buffer(); | |
991 | /* Start playing now */ | |
2e9ba080 | 992 | disorder_info("Playing..."); |
7a2c7068 RK |
993 | next_timestamp = pheap_first(&packets)->timestamp; |
994 | active = 1; | |
d4170ca7 | 995 | pthread_mutex_unlock(&lock); |
7a2c7068 | 996 | backend->activate(); |
d4170ca7 | 997 | pthread_mutex_lock(&lock); |
0e72bf84 RK |
998 | /* Wait until the buffer empties out |
999 | * | |
1000 | * If there's a packet that we can play right now then we definitely | |
1001 | * continue. | |
1002 | * | |
1003 | * Also if there's at least minbuffer samples we carry on regardless and | |
1004 | * insert silence. The assumption is there's been a pause but more data | |
1005 | * is now available. | |
1006 | */ | |
7a2c7068 RK |
1007 | while(nsamples >= minbuffer |
1008 | || (nsamples > 0 | |
4fd38868 | 1009 | && contains(pheap_first(&packets), next_timestamp))) { |
b0619501 | 1010 | if(monitor) { |
4265e5d3 | 1011 | time_t now = xtime(0); |
b0619501 RK |
1012 | |
1013 | if(now >= lastlog + 60) { | |
1014 | int offset = nsamples - minbuffer; | |
1015 | double offtime = (double)offset / (uaudio_rate * uaudio_channels); | |
2e9ba080 RK |
1016 | disorder_info("%+d samples off (%d.%02ds, %d bytes)", |
1017 | offset, | |
1018 | (int)fabs(offtime) * (offtime < 0 ? -1 : 1), | |
1019 | (int)(fabs(offtime) * 100) % 100, | |
1020 | offset * uaudio_bits / CHAR_BIT); | |
b0619501 RK |
1021 | lastlog = now; |
1022 | } | |
1023 | } | |
ad535598 | 1024 | //fprintf(stderr, "%8u/%u (%u) PLAYING\n", nsamples, maxbuffer, minbuffer); |
7a2c7068 | 1025 | pthread_cond_wait(&cond, &lock); |
4fd38868 | 1026 | } |
ad535598 RK |
1027 | #if 0 |
1028 | if(nsamples) { | |
1029 | struct packet *p = pheap_first(&packets); | |
1030 | fprintf(stderr, "nsamples=%u (%u) next_timestamp=%"PRIx32", first packet is [%"PRIx32",%"PRIx32")\n", | |
1031 | nsamples, minbuffer, next_timestamp,p->timestamp,p->timestamp+p->nsamples); | |
1032 | } | |
1033 | #endif | |
7a2c7068 | 1034 | /* Stop playing for a bit until the buffer re-fills */ |
d4170ca7 | 1035 | pthread_mutex_unlock(&lock); |
7a2c7068 | 1036 | backend->deactivate(); |
d4170ca7 | 1037 | pthread_mutex_lock(&lock); |
7a2c7068 RK |
1038 | active = 0; |
1039 | /* Go back round */ | |
1040 | } | |
e83d0967 RK |
1041 | return 0; |
1042 | } | |
1043 | ||
1044 | /* | |
1045 | Local Variables: | |
1046 | c-basic-offset:2 | |
1047 | comment-column:40 | |
1048 | fill-column:79 | |
1049 | indent-tabs-mode:nil | |
1050 | End: | |
1051 | */ |