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