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" |
e83d0967 | 82 | |
1153fd23 | 83 | #define readahead linux_headers_are_borked |
84 | ||
e3426f7b RK |
85 | /** @brief Obsolete synonym */ |
86 | #ifndef IPV6_JOIN_GROUP | |
87 | # define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP | |
88 | #endif | |
89 | ||
0b75463f | 90 | /** @brief RTP socket */ |
e83d0967 RK |
91 | static int rtpfd; |
92 | ||
345ebe66 RK |
93 | /** @brief Log output */ |
94 | static FILE *logfp; | |
95 | ||
0b75463f | 96 | /** @brief Output device */ |
8e3fe3d8 | 97 | const char *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. */ | |
8d0c14d7 | 107 | static unsigned readahead = 2 * 2 * 44100; |
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 | |
3c499fe7 RK |
171 | #if DEFAULT_BACKEND == BACKEND_ALSA |
172 | # define DEFAULT_PLAYRTP_BACKEND playrtp_alsa | |
173 | #elif DEFAULT_BACKEND == BACKEND_OSS | |
174 | # define DEFAULT_PLAYRTP_BACKEND playrtp_oss | |
175 | #elif DEFAULT_BACKEND == BACKEND_COREAUDIO | |
176 | # define DEFAULT_PLAYRTP_BACKEND playrtp_coreaudio | |
c593cf7c | 177 | #endif |
178 | ||
179 | /** @brief Backend to play with */ | |
3c499fe7 | 180 | static void (*backend)(void) = DEFAULT_PLAYRTP_BACKEND; |
c593cf7c | 181 | |
8e3fe3d8 | 182 | HEAP_DEFINE(pheap, struct packet *, lt_packet); |
e83d0967 | 183 | |
a99c4e9a RK |
184 | /** @brief Control socket or NULL */ |
185 | const char *control_socket; | |
186 | ||
b28bddbb RK |
187 | /** @brief Buffer for debugging dump |
188 | * | |
189 | * The debug dump is enabled by the @c --dump option. It records the last 20s | |
190 | * of audio to the specified file (which will be about 3.5Mbytes). The file is | |
191 | * written as as ring buffer, so the start point will progress through it. | |
192 | * | |
193 | * Use clients/dump2wav to convert this to a WAV file, which can then be loaded | |
194 | * into (e.g.) Audacity for further inspection. | |
195 | * | |
196 | * All three backends (ALSA, OSS, Core Audio) now support this option. | |
197 | * | |
198 | * The idea is to allow the user a few seconds to react to an audible artefact. | |
199 | */ | |
e9b635a3 | 200 | int16_t *dump_buffer; |
b28bddbb RK |
201 | |
202 | /** @brief Current index within debugging dump */ | |
e9b635a3 | 203 | size_t dump_index; |
b28bddbb RK |
204 | |
205 | /** @brief Size of debugging dump in samples */ | |
206 | size_t dump_size = 44100/*Hz*/ * 2/*channels*/ * 20/*seconds*/; | |
e9b635a3 | 207 | |
e83d0967 RK |
208 | static const struct option options[] = { |
209 | { "help", no_argument, 0, 'h' }, | |
210 | { "version", no_argument, 0, 'V' }, | |
211 | { "debug", no_argument, 0, 'd' }, | |
0b75463f | 212 | { "device", required_argument, 0, 'D' }, |
1153fd23 | 213 | { "min", required_argument, 0, 'm' }, |
9086a105 | 214 | { "max", required_argument, 0, 'x' }, |
1153fd23 | 215 | { "buffer", required_argument, 0, 'b' }, |
1f10f780 | 216 | { "rcvbuf", required_argument, 0, 'R' }, |
a9f0ad12 | 217 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 218 | { "oss", no_argument, 0, 'o' }, |
219 | #endif | |
146e86fb | 220 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 221 | { "alsa", no_argument, 0, 'a' }, |
222 | #endif | |
223 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
224 | { "core-audio", no_argument, 0, 'c' }, | |
225 | #endif | |
e9b635a3 | 226 | { "dump", required_argument, 0, 'r' }, |
a99c4e9a | 227 | { "socket", required_argument, 0, 's' }, |
a7e9570a | 228 | { "config", required_argument, 0, 'C' }, |
e83d0967 RK |
229 | { 0, 0, 0, 0 } |
230 | }; | |
231 | ||
a99c4e9a RK |
232 | /** @brief Control thread |
233 | * | |
234 | * This thread is responsible for accepting control commands from Disobedience | |
235 | * (or other controllers) over an AF_UNIX stream socket with a path specified | |
236 | * by the @c --socket option. The protocol uses simple string commands and | |
237 | * replies: | |
238 | * | |
239 | * - @c stop will shut the player down | |
240 | * - @c query will send back the reply @c running | |
241 | * - anything else is ignored | |
242 | * | |
243 | * Commands and response strings terminated by shutting down the connection or | |
244 | * by a newline. No attempt is made to multiplex multiple clients so it is | |
245 | * important that the command be sent as soon as the connection is made - it is | |
246 | * assumed that both parties to the protocol are entirely cooperating with one | |
247 | * another. | |
248 | */ | |
249 | static void *control_thread(void attribute((unused)) *arg) { | |
250 | struct sockaddr_un sa; | |
251 | int sfd, cfd; | |
252 | char *line; | |
253 | socklen_t salen; | |
254 | FILE *fp; | |
255 | ||
256 | assert(control_socket); | |
257 | unlink(control_socket); | |
258 | memset(&sa, 0, sizeof sa); | |
259 | sa.sun_family = AF_UNIX; | |
260 | strcpy(sa.sun_path, control_socket); | |
261 | sfd = xsocket(PF_UNIX, SOCK_STREAM, 0); | |
262 | if(bind(sfd, (const struct sockaddr *)&sa, sizeof sa) < 0) | |
263 | fatal(errno, "error binding to %s", control_socket); | |
264 | if(listen(sfd, 128) < 0) | |
265 | fatal(errno, "error calling listen on %s", control_socket); | |
266 | info("listening on %s", control_socket); | |
267 | for(;;) { | |
268 | salen = sizeof sa; | |
269 | cfd = accept(sfd, (struct sockaddr *)&sa, &salen); | |
270 | if(cfd < 0) { | |
271 | switch(errno) { | |
272 | case EINTR: | |
273 | case EAGAIN: | |
274 | break; | |
275 | default: | |
276 | fatal(errno, "error calling accept on %s", control_socket); | |
277 | } | |
278 | } | |
279 | if(!(fp = fdopen(cfd, "r+"))) { | |
280 | error(errno, "error calling fdopen for %s connection", control_socket); | |
281 | close(cfd); | |
282 | continue; | |
283 | } | |
284 | if(!inputline(control_socket, fp, &line, '\n')) { | |
285 | if(!strcmp(line, "stop")) { | |
286 | info("stopped via %s", control_socket); | |
287 | exit(0); /* terminate immediately */ | |
288 | } | |
289 | if(!strcmp(line, "query")) | |
290 | fprintf(fp, "running"); | |
291 | xfree(line); | |
292 | } | |
293 | if(fclose(fp) < 0) | |
294 | error(errno, "error closing %s connection", control_socket); | |
295 | } | |
296 | } | |
297 | ||
28bacdc0 RK |
298 | /** @brief Drop the first packet |
299 | * | |
300 | * Assumes that @ref lock is held. | |
301 | */ | |
302 | static void drop_first_packet(void) { | |
303 | if(pheap_count(&packets)) { | |
304 | struct packet *const p = pheap_remove(&packets); | |
305 | nsamples -= p->nsamples; | |
c593cf7c | 306 | playrtp_free_packet(p); |
2c7c9eae | 307 | pthread_cond_broadcast(&cond); |
2c7c9eae | 308 | } |
9086a105 RK |
309 | } |
310 | ||
189e9830 RK |
311 | /** @brief Background thread adding packets to heap |
312 | * | |
313 | * This just transfers packets from @ref received_packets to @ref packets. It | |
314 | * is important that it holds @ref receive_lock for as little time as possible, | |
315 | * in order to minimize the interval between calls to read() in | |
316 | * listen_thread(). | |
317 | */ | |
318 | static void *queue_thread(void attribute((unused)) *arg) { | |
319 | struct packet *p; | |
320 | ||
321 | for(;;) { | |
322 | /* Get the next packet */ | |
323 | pthread_mutex_lock(&receive_lock); | |
4dadf1a2 | 324 | while(!received_packets) { |
189e9830 | 325 | pthread_cond_wait(&receive_cond, &receive_lock); |
4dadf1a2 | 326 | } |
189e9830 RK |
327 | p = received_packets; |
328 | received_packets = p->next; | |
329 | if(!received_packets) | |
330 | received_tail = &received_packets; | |
331 | --nreceived; | |
332 | pthread_mutex_unlock(&receive_lock); | |
333 | /* Add it to the heap */ | |
334 | pthread_mutex_lock(&lock); | |
335 | pheap_insert(&packets, p); | |
336 | nsamples += p->nsamples; | |
337 | pthread_cond_broadcast(&cond); | |
338 | pthread_mutex_unlock(&lock); | |
339 | } | |
340 | } | |
341 | ||
09ee2f0d | 342 | /** @brief Background thread collecting samples |
0b75463f | 343 | * |
344 | * This function collects samples, perhaps converts them to the target format, | |
b0fdc63d | 345 | * and adds them to the packet list. |
346 | * | |
347 | * It is crucial that the gap between successive calls to read() is as small as | |
348 | * possible: otherwise packets will be dropped. | |
349 | * | |
350 | * We use a binary heap to ensure that the unavoidable effort is at worst | |
351 | * logarithmic in the total number of packets - in fact if packets are mostly | |
352 | * received in order then we will largely do constant work per packet since the | |
353 | * newest packet will always be last. | |
354 | * | |
355 | * Of more concern is that we must acquire the lock on the heap to add a packet | |
356 | * to it. If this proves a problem in practice then the answer would be | |
357 | * (probably doubly) linked list with new packets added the end and a second | |
358 | * thread which reads packets off the list and adds them to the heap. | |
359 | * | |
360 | * We keep memory allocation (mostly) very fast by keeping pre-allocated | |
c593cf7c | 361 | * packets around; see @ref playrtp_new_packet(). |
b0fdc63d | 362 | */ |
0b75463f | 363 | static void *listen_thread(void attribute((unused)) *arg) { |
2c7c9eae | 364 | struct packet *p = 0; |
0b75463f | 365 | int n; |
2c7c9eae RK |
366 | struct rtp_header header; |
367 | uint16_t seq; | |
368 | uint32_t timestamp; | |
369 | struct iovec iov[2]; | |
e83d0967 RK |
370 | |
371 | for(;;) { | |
189e9830 | 372 | if(!p) |
c593cf7c | 373 | p = playrtp_new_packet(); |
2c7c9eae RK |
374 | iov[0].iov_base = &header; |
375 | iov[0].iov_len = sizeof header; | |
376 | iov[1].iov_base = p->samples_raw; | |
b64efe7e | 377 | iov[1].iov_len = sizeof p->samples_raw / sizeof *p->samples_raw; |
2c7c9eae | 378 | n = readv(rtpfd, iov, 2); |
e83d0967 RK |
379 | if(n < 0) { |
380 | switch(errno) { | |
381 | case EINTR: | |
382 | continue; | |
383 | default: | |
384 | fatal(errno, "error reading from socket"); | |
385 | } | |
386 | } | |
0b75463f | 387 | /* Ignore too-short packets */ |
345ebe66 RK |
388 | if((size_t)n <= sizeof (struct rtp_header)) { |
389 | info("ignored a short packet"); | |
0b75463f | 390 | continue; |
345ebe66 | 391 | } |
2c7c9eae RK |
392 | timestamp = htonl(header.timestamp); |
393 | seq = htons(header.seq); | |
09ee2f0d | 394 | /* Ignore packets in the past */ |
2c7c9eae | 395 | if(active && lt(timestamp, next_timestamp)) { |
c0e41690 | 396 | info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32, |
2c7c9eae | 397 | timestamp, next_timestamp); |
09ee2f0d | 398 | continue; |
c0e41690 | 399 | } |
189e9830 | 400 | p->next = 0; |
58b5a68f | 401 | p->flags = 0; |
2c7c9eae | 402 | p->timestamp = timestamp; |
e83d0967 | 403 | /* Convert to target format */ |
58b5a68f RK |
404 | if(header.mpt & 0x80) |
405 | p->flags |= IDLE; | |
2c7c9eae | 406 | switch(header.mpt & 0x7F) { |
e83d0967 | 407 | case 10: |
2c7c9eae | 408 | p->nsamples = (n - sizeof header) / sizeof(uint16_t); |
e83d0967 RK |
409 | break; |
410 | /* TODO support other RFC3551 media types (when the speaker does) */ | |
411 | default: | |
0b75463f | 412 | fatal(0, "unsupported RTP payload type %d", |
2c7c9eae | 413 | header.mpt & 0x7F); |
e83d0967 | 414 | } |
345ebe66 RK |
415 | if(logfp) |
416 | fprintf(logfp, "sequence %u timestamp %"PRIx32" length %"PRIx32" end %"PRIx32"\n", | |
2c7c9eae | 417 | seq, timestamp, p->nsamples, timestamp + p->nsamples); |
0b75463f | 418 | /* Stop reading if we've reached the maximum. |
419 | * | |
420 | * This is rather unsatisfactory: it means that if packets get heavily | |
421 | * out of order then we guarantee dropouts. But for now... */ | |
345ebe66 | 422 | if(nsamples >= maxbuffer) { |
189e9830 | 423 | pthread_mutex_lock(&lock); |
4dadf1a2 | 424 | while(nsamples >= maxbuffer) { |
345ebe66 | 425 | pthread_cond_wait(&cond, &lock); |
4dadf1a2 | 426 | } |
189e9830 | 427 | pthread_mutex_unlock(&lock); |
345ebe66 | 428 | } |
189e9830 RK |
429 | /* Add the packet to the receive queue */ |
430 | pthread_mutex_lock(&receive_lock); | |
431 | *received_tail = p; | |
432 | received_tail = &p->next; | |
433 | ++nreceived; | |
434 | pthread_cond_signal(&receive_cond); | |
435 | pthread_mutex_unlock(&receive_lock); | |
58b5a68f RK |
436 | /* We'll need a new packet */ |
437 | p = 0; | |
e83d0967 RK |
438 | } |
439 | } | |
440 | ||
5626f6d2 RK |
441 | /** @brief Wait until the buffer is adequately full |
442 | * | |
443 | * Must be called with @ref lock held. | |
444 | */ | |
c593cf7c | 445 | void playrtp_fill_buffer(void) { |
bfd27c14 RK |
446 | while(nsamples) |
447 | drop_first_packet(); | |
5626f6d2 | 448 | info("Buffering..."); |
4dadf1a2 | 449 | while(nsamples < readahead) { |
5626f6d2 | 450 | pthread_cond_wait(&cond, &lock); |
4dadf1a2 | 451 | } |
5626f6d2 RK |
452 | next_timestamp = pheap_first(&packets)->timestamp; |
453 | active = 1; | |
454 | } | |
455 | ||
456 | /** @brief Find next packet | |
457 | * @return Packet to play or NULL if none found | |
458 | * | |
459 | * The return packet is merely guaranteed not to be in the past: it might be | |
460 | * the first packet in the future rather than one that is actually suitable to | |
461 | * play. | |
462 | * | |
463 | * Must be called with @ref lock held. | |
464 | */ | |
c593cf7c | 465 | struct packet *playrtp_next_packet(void) { |
5626f6d2 RK |
466 | while(pheap_count(&packets)) { |
467 | struct packet *const p = pheap_first(&packets); | |
468 | if(le(p->timestamp + p->nsamples, next_timestamp)) { | |
469 | /* This packet is in the past. Drop it and try another one. */ | |
470 | drop_first_packet(); | |
471 | } else | |
472 | /* This packet is NOT in the past. (It might be in the future | |
473 | * however.) */ | |
474 | return p; | |
475 | } | |
476 | return 0; | |
477 | } | |
478 | ||
09ee2f0d | 479 | /** @brief Play an RTP stream |
480 | * | |
481 | * This is the guts of the program. It is responsible for: | |
482 | * - starting the listening thread | |
483 | * - opening the audio device | |
484 | * - reading ahead to build up a buffer | |
485 | * - arranging for audio to be played | |
486 | * - detecting when the buffer has got too small and re-buffering | |
487 | */ | |
0b75463f | 488 | static void play_rtp(void) { |
489 | pthread_t ltid; | |
a99c4e9a | 490 | int err; |
e83d0967 RK |
491 | |
492 | /* We receive and convert audio data in a background thread */ | |
a99c4e9a RK |
493 | if((err = pthread_create(<id, 0, listen_thread, 0))) |
494 | fatal(err, "pthread_create listen_thread"); | |
189e9830 | 495 | /* We have a second thread to add received packets to the queue */ |
a99c4e9a RK |
496 | if((err = pthread_create(<id, 0, queue_thread, 0))) |
497 | fatal(err, "pthread_create queue_thread"); | |
c593cf7c | 498 | /* The rest of the work is backend-specific */ |
499 | backend(); | |
e83d0967 RK |
500 | } |
501 | ||
502 | /* display usage message and terminate */ | |
503 | static void help(void) { | |
504 | xprintf("Usage:\n" | |
505 | " disorder-playrtp [OPTIONS] ADDRESS [PORT]\n" | |
506 | "Options:\n" | |
1153fd23 | 507 | " --device, -D DEVICE Output device\n" |
508 | " --min, -m FRAMES Buffer low water mark\n" | |
9086a105 RK |
509 | " --buffer, -b FRAMES Buffer high water mark\n" |
510 | " --max, -x FRAMES Buffer maximum size\n" | |
1f10f780 | 511 | " --rcvbuf, -R BYTES Socket receive buffer size\n" |
a7e9570a | 512 | " --config, -C PATH Set configuration file\n" |
146e86fb | 513 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 514 | " --alsa, -a Use ALSA to play audio\n" |
515 | #endif | |
a9f0ad12 | 516 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 517 | " --oss, -o Use OSS to play audio\n" |
518 | #endif | |
519 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
520 | " --core-audio, -c Use Core Audio to play audio\n" | |
521 | #endif | |
9086a105 RK |
522 | " --help, -h Display usage message\n" |
523 | " --version, -V Display version number\n" | |
524 | ); | |
e83d0967 RK |
525 | xfclose(stdout); |
526 | exit(0); | |
527 | } | |
528 | ||
e83d0967 | 529 | int main(int argc, char **argv) { |
a99c4e9a | 530 | int n, err; |
e83d0967 RK |
531 | struct addrinfo *res; |
532 | struct stringlist sl; | |
0b75463f | 533 | char *sockname; |
1f10f780 RK |
534 | int rcvbuf, target_rcvbuf = 131072; |
535 | socklen_t len; | |
23205f9c RK |
536 | struct ip_mreq mreq; |
537 | struct ipv6_mreq mreq6; | |
a7e9570a RK |
538 | disorder_client *c; |
539 | char *address, *port; | |
6fba990c RK |
540 | int is_multicast; |
541 | union any_sockaddr { | |
542 | struct sockaddr sa; | |
543 | struct sockaddr_in in; | |
544 | struct sockaddr_in6 in6; | |
545 | }; | |
546 | union any_sockaddr mgroup; | |
e9b635a3 | 547 | const char *dumpfile = 0; |
e83d0967 | 548 | |
0b75463f | 549 | static const struct addrinfo prefs = { |
66613034 RK |
550 | .ai_flags = AI_PASSIVE, |
551 | .ai_family = PF_INET, | |
552 | .ai_socktype = SOCK_DGRAM, | |
553 | .ai_protocol = IPPROTO_UDP | |
e83d0967 RK |
554 | }; |
555 | ||
556 | mem_init(); | |
557 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); | |
e9b635a3 | 558 | while((n = getopt_long(argc, argv, "hVdD:m:b:x:L:R:M:aocC:r", options, 0)) >= 0) { |
e83d0967 RK |
559 | switch(n) { |
560 | case 'h': help(); | |
3fbdc96d | 561 | case 'V': version("disorder-playrtp"); |
e83d0967 | 562 | case 'd': debugging = 1; break; |
0b75463f | 563 | case 'D': device = optarg; break; |
1153fd23 | 564 | case 'm': minbuffer = 2 * atol(optarg); break; |
565 | case 'b': readahead = 2 * atol(optarg); break; | |
9086a105 | 566 | case 'x': maxbuffer = 2 * atol(optarg); break; |
345ebe66 | 567 | case 'L': logfp = fopen(optarg, "w"); break; |
1f10f780 | 568 | case 'R': target_rcvbuf = atoi(optarg); break; |
146e86fb | 569 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 570 | case 'a': backend = playrtp_alsa; break; |
571 | #endif | |
a9f0ad12 | 572 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 573 | case 'o': backend = playrtp_oss; break; |
574 | #endif | |
575 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
576 | case 'c': backend = playrtp_coreaudio; break; | |
577 | #endif | |
a7e9570a | 578 | case 'C': configfile = optarg; break; |
a99c4e9a | 579 | case 's': control_socket = optarg; break; |
e9b635a3 | 580 | case 'r': dumpfile = optarg; break; |
e83d0967 RK |
581 | default: fatal(0, "invalid option"); |
582 | } | |
583 | } | |
a7e9570a | 584 | if(config_read(0)) fatal(0, "cannot read configuration"); |
9086a105 RK |
585 | if(!maxbuffer) |
586 | maxbuffer = 4 * readahead; | |
e83d0967 RK |
587 | argc -= optind; |
588 | argv += optind; | |
a7e9570a RK |
589 | switch(argc) { |
590 | case 0: | |
6fba990c | 591 | /* Get configuration from server */ |
a7e9570a RK |
592 | if(!(c = disorder_new(1))) exit(EXIT_FAILURE); |
593 | if(disorder_connect(c)) exit(EXIT_FAILURE); | |
594 | if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE); | |
6fba990c RK |
595 | sl.n = 2; |
596 | sl.s = xcalloc(2, sizeof *sl.s); | |
597 | sl.s[0] = address; | |
598 | sl.s[1] = port; | |
a7e9570a | 599 | break; |
6fba990c | 600 | case 1: |
a7e9570a | 601 | case 2: |
6fba990c | 602 | /* Use command-line ADDRESS+PORT or just PORT */ |
a7e9570a RK |
603 | sl.n = argc; |
604 | sl.s = argv; | |
605 | break; | |
606 | default: | |
6fba990c | 607 | fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]"); |
a7e9570a | 608 | } |
6fba990c | 609 | /* Look up address and port */ |
0b75463f | 610 | if(!(res = get_address(&sl, &prefs, &sockname))) |
e83d0967 | 611 | exit(1); |
6fba990c | 612 | /* Create the socket */ |
e83d0967 RK |
613 | if((rtpfd = socket(res->ai_family, |
614 | res->ai_socktype, | |
615 | res->ai_protocol)) < 0) | |
616 | fatal(errno, "error creating socket"); | |
6fba990c RK |
617 | /* Stash the multicast group address */ |
618 | if((is_multicast = multicast(res->ai_addr))) { | |
619 | memcpy(&mgroup, res->ai_addr, res->ai_addrlen); | |
620 | switch(res->ai_addr->sa_family) { | |
621 | case AF_INET: | |
622 | mgroup.in.sin_port = 0; | |
623 | break; | |
624 | case AF_INET6: | |
625 | mgroup.in6.sin6_port = 0; | |
626 | break; | |
627 | } | |
628 | } | |
629 | /* Bind to 0/port */ | |
630 | switch(res->ai_addr->sa_family) { | |
631 | case AF_INET: | |
632 | memset(&((struct sockaddr_in *)res->ai_addr)->sin_addr, 0, | |
633 | sizeof (struct in_addr)); | |
634 | break; | |
635 | case AF_INET6: | |
636 | memset(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 0, | |
637 | sizeof (struct in6_addr)); | |
638 | break; | |
639 | default: | |
640 | fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family); | |
641 | } | |
e83d0967 RK |
642 | if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) |
643 | fatal(errno, "error binding socket to %s", sockname); | |
6fba990c RK |
644 | if(is_multicast) { |
645 | switch(mgroup.sa.sa_family) { | |
23205f9c | 646 | case PF_INET: |
6fba990c | 647 | mreq.imr_multiaddr = mgroup.in.sin_addr; |
23205f9c RK |
648 | mreq.imr_interface.s_addr = 0; /* use primary interface */ |
649 | if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
650 | &mreq, sizeof mreq) < 0) | |
651 | fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP"); | |
652 | break; | |
653 | case PF_INET6: | |
6fba990c | 654 | mreq6.ipv6mr_multiaddr = mgroup.in6.sin6_addr; |
23205f9c RK |
655 | memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface); |
656 | if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, | |
657 | &mreq6, sizeof mreq6) < 0) | |
658 | fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP"); | |
659 | break; | |
660 | default: | |
661 | fatal(0, "unsupported address family %d", res->ai_family); | |
662 | } | |
6fba990c RK |
663 | info("listening on %s multicast group %s", |
664 | format_sockaddr(res->ai_addr), format_sockaddr(&mgroup.sa)); | |
665 | } else | |
666 | info("listening on %s", format_sockaddr(res->ai_addr)); | |
1f10f780 RK |
667 | len = sizeof rcvbuf; |
668 | if(getsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len) < 0) | |
669 | fatal(errno, "error calling getsockopt SO_RCVBUF"); | |
f0bae611 | 670 | if(target_rcvbuf > rcvbuf) { |
1f10f780 RK |
671 | if(setsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, |
672 | &target_rcvbuf, sizeof target_rcvbuf) < 0) | |
673 | error(errno, "error calling setsockopt SO_RCVBUF %d", | |
674 | target_rcvbuf); | |
675 | /* We try to carry on anyway */ | |
676 | else | |
677 | info("changed socket receive buffer from %d to %d", | |
678 | rcvbuf, target_rcvbuf); | |
679 | } else | |
680 | info("default socket receive buffer %d", rcvbuf); | |
681 | if(logfp) | |
682 | info("WARNING: -L option can impact performance"); | |
a99c4e9a RK |
683 | if(control_socket) { |
684 | pthread_t tid; | |
685 | ||
686 | if((err = pthread_create(&tid, 0, control_thread, 0))) | |
687 | fatal(err, "pthread_create control_thread"); | |
688 | } | |
e9b635a3 RK |
689 | if(dumpfile) { |
690 | int fd; | |
691 | unsigned char buffer[65536]; | |
692 | size_t written; | |
693 | ||
694 | if((fd = open(dumpfile, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) | |
695 | fatal(errno, "opening %s", dumpfile); | |
696 | /* Fill with 0s to a suitable size */ | |
697 | memset(buffer, 0, sizeof buffer); | |
698 | for(written = 0; written < dump_size * sizeof(int16_t); | |
699 | written += sizeof buffer) { | |
700 | if(write(fd, buffer, sizeof buffer) < 0) | |
701 | fatal(errno, "clearing %s", dumpfile); | |
702 | } | |
703 | /* Map the buffer into memory for convenience */ | |
704 | dump_buffer = mmap(0, dump_size * sizeof(int16_t), PROT_READ|PROT_WRITE, | |
705 | MAP_SHARED, fd, 0); | |
706 | if(dump_buffer == (void *)-1) | |
707 | fatal(errno, "mapping %s", dumpfile); | |
708 | info("dumping to %s", dumpfile); | |
709 | } | |
e83d0967 RK |
710 | play_rtp(); |
711 | return 0; | |
712 | } | |
713 | ||
714 | /* | |
715 | Local Variables: | |
716 | c-basic-offset:2 | |
717 | comment-column:40 | |
718 | fill-column:79 | |
719 | indent-tabs-mode:nil | |
720 | End: | |
721 | */ |