| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2007, 2008 Richard Kettlewell |
| 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 3 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, |
| 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 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file clients/playrtp.c |
| 19 | * @brief RTP player |
| 20 | * |
| 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 | * |
| 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). |
| 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 |
| 35 | * plays. See @ref clients/playrtp-alsa.c. |
| 36 | * |
| 37 | * In Core Audio the main thread is only responsible for starting and stopping |
| 38 | * play: the system does the actual playback in its own private thread, and |
| 39 | * calls adioproc() to fetch the audio data. See @ref |
| 40 | * clients/playrtp-coreaudio.c. |
| 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. |
| 45 | * |
| 46 | * Assumptions: |
| 47 | * - it is safe to read uint32_t values without a lock protecting them |
| 48 | */ |
| 49 | |
| 50 | #include "common.h" |
| 51 | |
| 52 | #include <getopt.h> |
| 53 | #include <sys/socket.h> |
| 54 | #include <sys/types.h> |
| 55 | #include <sys/socket.h> |
| 56 | #include <netdb.h> |
| 57 | #include <pthread.h> |
| 58 | #include <locale.h> |
| 59 | #include <sys/uio.h> |
| 60 | #include <errno.h> |
| 61 | #include <netinet/in.h> |
| 62 | #include <sys/time.h> |
| 63 | #include <sys/un.h> |
| 64 | #include <unistd.h> |
| 65 | #include <sys/mman.h> |
| 66 | #include <fcntl.h> |
| 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" |
| 74 | #include "defs.h" |
| 75 | #include "vector.h" |
| 76 | #include "heap.h" |
| 77 | #include "timeval.h" |
| 78 | #include "client.h" |
| 79 | #include "playrtp.h" |
| 80 | #include "inputline.h" |
| 81 | #include "version.h" |
| 82 | |
| 83 | #define readahead linux_headers_are_borked |
| 84 | |
| 85 | /** @brief Obsolete synonym */ |
| 86 | #ifndef IPV6_JOIN_GROUP |
| 87 | # define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP |
| 88 | #endif |
| 89 | |
| 90 | /** @brief RTP socket */ |
| 91 | static int rtpfd; |
| 92 | |
| 93 | /** @brief Log output */ |
| 94 | static FILE *logfp; |
| 95 | |
| 96 | /** @brief Output device */ |
| 97 | const char *device; |
| 98 | |
| 99 | /** @brief Minimum low watermark |
| 100 | * |
| 101 | * We'll stop playing if there's only this many samples in the buffer. */ |
| 102 | unsigned minbuffer = 2 * 44100 / 10; /* 0.2 seconds */ |
| 103 | |
| 104 | /** @brief Buffer high watermark |
| 105 | * |
| 106 | * We'll only start playing when this many samples are available. */ |
| 107 | static unsigned readahead = 2 * 2 * 44100; |
| 108 | |
| 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 | |
| 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 | */ |
| 121 | struct packet *received_packets; |
| 122 | |
| 123 | /** @brief Tail of @ref received_packets |
| 124 | * Protected by @ref receive_lock |
| 125 | */ |
| 126 | struct packet **received_tail = &received_packets; |
| 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. */ |
| 132 | pthread_mutex_t receive_lock = PTHREAD_MUTEX_INITIALIZER; |
| 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. */ |
| 138 | pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER; |
| 139 | |
| 140 | /** @brief Length of @ref received_packets */ |
| 141 | uint32_t nreceived; |
| 142 | |
| 143 | /** @brief Binary heap of received packets */ |
| 144 | struct pheap packets; |
| 145 | |
| 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 | */ |
| 151 | volatile uint32_t nsamples; |
| 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 |
| 156 | * samples it contained. Only valid if @ref active is nonzero. |
| 157 | */ |
| 158 | uint32_t next_timestamp; |
| 159 | |
| 160 | /** @brief True if actively playing |
| 161 | * |
| 162 | * This is true when playing and false when just buffering. */ |
| 163 | int active; |
| 164 | |
| 165 | /** @brief Lock protecting @ref packets */ |
| 166 | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| 167 | |
| 168 | /** @brief Condition variable signalled whenever @ref packets is changed */ |
| 169 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 170 | |
| 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 |
| 177 | #endif |
| 178 | |
| 179 | /** @brief Backend to play with */ |
| 180 | static void (*backend)(void) = DEFAULT_PLAYRTP_BACKEND; |
| 181 | |
| 182 | HEAP_DEFINE(pheap, struct packet *, lt_packet); |
| 183 | |
| 184 | /** @brief Control socket or NULL */ |
| 185 | const char *control_socket; |
| 186 | |
| 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 | */ |
| 200 | int16_t *dump_buffer; |
| 201 | |
| 202 | /** @brief Current index within debugging dump */ |
| 203 | size_t dump_index; |
| 204 | |
| 205 | /** @brief Size of debugging dump in samples */ |
| 206 | size_t dump_size = 44100/*Hz*/ * 2/*channels*/ * 20/*seconds*/; |
| 207 | |
| 208 | static const struct option options[] = { |
| 209 | { "help", no_argument, 0, 'h' }, |
| 210 | { "version", no_argument, 0, 'V' }, |
| 211 | { "debug", no_argument, 0, 'd' }, |
| 212 | { "device", required_argument, 0, 'D' }, |
| 213 | { "min", required_argument, 0, 'm' }, |
| 214 | { "max", required_argument, 0, 'x' }, |
| 215 | { "buffer", required_argument, 0, 'b' }, |
| 216 | { "rcvbuf", required_argument, 0, 'R' }, |
| 217 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
| 218 | { "oss", no_argument, 0, 'o' }, |
| 219 | #endif |
| 220 | #if HAVE_ALSA_ASOUNDLIB_H |
| 221 | { "alsa", no_argument, 0, 'a' }, |
| 222 | #endif |
| 223 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H |
| 224 | { "core-audio", no_argument, 0, 'c' }, |
| 225 | #endif |
| 226 | { "dump", required_argument, 0, 'r' }, |
| 227 | { "socket", required_argument, 0, 's' }, |
| 228 | { "config", required_argument, 0, 'C' }, |
| 229 | { 0, 0, 0, 0 } |
| 230 | }; |
| 231 | |
| 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 | |
| 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; |
| 306 | playrtp_free_packet(p); |
| 307 | pthread_cond_broadcast(&cond); |
| 308 | } |
| 309 | } |
| 310 | |
| 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); |
| 324 | while(!received_packets) { |
| 325 | pthread_cond_wait(&receive_cond, &receive_lock); |
| 326 | } |
| 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 | |
| 342 | /** @brief Background thread collecting samples |
| 343 | * |
| 344 | * This function collects samples, perhaps converts them to the target format, |
| 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 |
| 361 | * packets around; see @ref playrtp_new_packet(). |
| 362 | */ |
| 363 | static void *listen_thread(void attribute((unused)) *arg) { |
| 364 | struct packet *p = 0; |
| 365 | int n; |
| 366 | struct rtp_header header; |
| 367 | uint16_t seq; |
| 368 | uint32_t timestamp; |
| 369 | struct iovec iov[2]; |
| 370 | |
| 371 | for(;;) { |
| 372 | if(!p) |
| 373 | p = playrtp_new_packet(); |
| 374 | iov[0].iov_base = &header; |
| 375 | iov[0].iov_len = sizeof header; |
| 376 | iov[1].iov_base = p->samples_raw; |
| 377 | iov[1].iov_len = sizeof p->samples_raw / sizeof *p->samples_raw; |
| 378 | n = readv(rtpfd, iov, 2); |
| 379 | if(n < 0) { |
| 380 | switch(errno) { |
| 381 | case EINTR: |
| 382 | continue; |
| 383 | default: |
| 384 | fatal(errno, "error reading from socket"); |
| 385 | } |
| 386 | } |
| 387 | /* Ignore too-short packets */ |
| 388 | if((size_t)n <= sizeof (struct rtp_header)) { |
| 389 | info("ignored a short packet"); |
| 390 | continue; |
| 391 | } |
| 392 | timestamp = htonl(header.timestamp); |
| 393 | seq = htons(header.seq); |
| 394 | /* Ignore packets in the past */ |
| 395 | if(active && lt(timestamp, next_timestamp)) { |
| 396 | info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32, |
| 397 | timestamp, next_timestamp); |
| 398 | continue; |
| 399 | } |
| 400 | /* Ignore packets with the extension bit set. */ |
| 401 | if(header.vpxcc & 0x10) |
| 402 | continue; |
| 403 | p->next = 0; |
| 404 | p->flags = 0; |
| 405 | p->timestamp = timestamp; |
| 406 | /* Convert to target format */ |
| 407 | if(header.mpt & 0x80) |
| 408 | p->flags |= IDLE; |
| 409 | switch(header.mpt & 0x7F) { |
| 410 | case 10: |
| 411 | p->nsamples = (n - sizeof header) / sizeof(uint16_t); |
| 412 | break; |
| 413 | /* TODO support other RFC3551 media types (when the speaker does) */ |
| 414 | default: |
| 415 | fatal(0, "unsupported RTP payload type %d", |
| 416 | header.mpt & 0x7F); |
| 417 | } |
| 418 | if(logfp) |
| 419 | fprintf(logfp, "sequence %u timestamp %"PRIx32" length %"PRIx32" end %"PRIx32"\n", |
| 420 | seq, timestamp, p->nsamples, timestamp + p->nsamples); |
| 421 | /* Stop reading if we've reached the maximum. |
| 422 | * |
| 423 | * This is rather unsatisfactory: it means that if packets get heavily |
| 424 | * out of order then we guarantee dropouts. But for now... */ |
| 425 | if(nsamples >= maxbuffer) { |
| 426 | pthread_mutex_lock(&lock); |
| 427 | while(nsamples >= maxbuffer) { |
| 428 | pthread_cond_wait(&cond, &lock); |
| 429 | } |
| 430 | pthread_mutex_unlock(&lock); |
| 431 | } |
| 432 | /* Add the packet to the receive queue */ |
| 433 | pthread_mutex_lock(&receive_lock); |
| 434 | *received_tail = p; |
| 435 | received_tail = &p->next; |
| 436 | ++nreceived; |
| 437 | pthread_cond_signal(&receive_cond); |
| 438 | pthread_mutex_unlock(&receive_lock); |
| 439 | /* We'll need a new packet */ |
| 440 | p = 0; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** @brief Wait until the buffer is adequately full |
| 445 | * |
| 446 | * Must be called with @ref lock held. |
| 447 | */ |
| 448 | void playrtp_fill_buffer(void) { |
| 449 | while(nsamples) |
| 450 | drop_first_packet(); |
| 451 | info("Buffering..."); |
| 452 | while(nsamples < readahead) { |
| 453 | pthread_cond_wait(&cond, &lock); |
| 454 | } |
| 455 | next_timestamp = pheap_first(&packets)->timestamp; |
| 456 | active = 1; |
| 457 | } |
| 458 | |
| 459 | /** @brief Find next packet |
| 460 | * @return Packet to play or NULL if none found |
| 461 | * |
| 462 | * The return packet is merely guaranteed not to be in the past: it might be |
| 463 | * the first packet in the future rather than one that is actually suitable to |
| 464 | * play. |
| 465 | * |
| 466 | * Must be called with @ref lock held. |
| 467 | */ |
| 468 | struct packet *playrtp_next_packet(void) { |
| 469 | while(pheap_count(&packets)) { |
| 470 | struct packet *const p = pheap_first(&packets); |
| 471 | if(le(p->timestamp + p->nsamples, next_timestamp)) { |
| 472 | /* This packet is in the past. Drop it and try another one. */ |
| 473 | drop_first_packet(); |
| 474 | } else |
| 475 | /* This packet is NOT in the past. (It might be in the future |
| 476 | * however.) */ |
| 477 | return p; |
| 478 | } |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /** @brief Play an RTP stream |
| 483 | * |
| 484 | * This is the guts of the program. It is responsible for: |
| 485 | * - starting the listening thread |
| 486 | * - opening the audio device |
| 487 | * - reading ahead to build up a buffer |
| 488 | * - arranging for audio to be played |
| 489 | * - detecting when the buffer has got too small and re-buffering |
| 490 | */ |
| 491 | static void play_rtp(void) { |
| 492 | pthread_t ltid; |
| 493 | int err; |
| 494 | |
| 495 | /* We receive and convert audio data in a background thread */ |
| 496 | if((err = pthread_create(<id, 0, listen_thread, 0))) |
| 497 | fatal(err, "pthread_create listen_thread"); |
| 498 | /* We have a second thread to add received packets to the queue */ |
| 499 | if((err = pthread_create(<id, 0, queue_thread, 0))) |
| 500 | fatal(err, "pthread_create queue_thread"); |
| 501 | /* The rest of the work is backend-specific */ |
| 502 | backend(); |
| 503 | } |
| 504 | |
| 505 | /* display usage message and terminate */ |
| 506 | static void help(void) { |
| 507 | xprintf("Usage:\n" |
| 508 | " disorder-playrtp [OPTIONS] ADDRESS [PORT]\n" |
| 509 | "Options:\n" |
| 510 | " --device, -D DEVICE Output device\n" |
| 511 | " --min, -m FRAMES Buffer low water mark\n" |
| 512 | " --buffer, -b FRAMES Buffer high water mark\n" |
| 513 | " --max, -x FRAMES Buffer maximum size\n" |
| 514 | " --rcvbuf, -R BYTES Socket receive buffer size\n" |
| 515 | " --config, -C PATH Set configuration file\n" |
| 516 | #if HAVE_ALSA_ASOUNDLIB_H |
| 517 | " --alsa, -a Use ALSA to play audio\n" |
| 518 | #endif |
| 519 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
| 520 | " --oss, -o Use OSS to play audio\n" |
| 521 | #endif |
| 522 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H |
| 523 | " --core-audio, -c Use Core Audio to play audio\n" |
| 524 | #endif |
| 525 | " --help, -h Display usage message\n" |
| 526 | " --version, -V Display version number\n" |
| 527 | ); |
| 528 | xfclose(stdout); |
| 529 | exit(0); |
| 530 | } |
| 531 | |
| 532 | int main(int argc, char **argv) { |
| 533 | int n, err; |
| 534 | struct addrinfo *res; |
| 535 | struct stringlist sl; |
| 536 | char *sockname; |
| 537 | int rcvbuf, target_rcvbuf = 131072; |
| 538 | socklen_t len; |
| 539 | struct ip_mreq mreq; |
| 540 | struct ipv6_mreq mreq6; |
| 541 | disorder_client *c; |
| 542 | char *address, *port; |
| 543 | int is_multicast; |
| 544 | union any_sockaddr { |
| 545 | struct sockaddr sa; |
| 546 | struct sockaddr_in in; |
| 547 | struct sockaddr_in6 in6; |
| 548 | }; |
| 549 | union any_sockaddr mgroup; |
| 550 | const char *dumpfile = 0; |
| 551 | |
| 552 | static const struct addrinfo prefs = { |
| 553 | .ai_flags = AI_PASSIVE, |
| 554 | .ai_family = PF_INET, |
| 555 | .ai_socktype = SOCK_DGRAM, |
| 556 | .ai_protocol = IPPROTO_UDP |
| 557 | }; |
| 558 | |
| 559 | mem_init(); |
| 560 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
| 561 | while((n = getopt_long(argc, argv, "hVdD:m:b:x:L:R:M:aocC:r", options, 0)) >= 0) { |
| 562 | switch(n) { |
| 563 | case 'h': help(); |
| 564 | case 'V': version("disorder-playrtp"); |
| 565 | case 'd': debugging = 1; break; |
| 566 | case 'D': device = optarg; break; |
| 567 | case 'm': minbuffer = 2 * atol(optarg); break; |
| 568 | case 'b': readahead = 2 * atol(optarg); break; |
| 569 | case 'x': maxbuffer = 2 * atol(optarg); break; |
| 570 | case 'L': logfp = fopen(optarg, "w"); break; |
| 571 | case 'R': target_rcvbuf = atoi(optarg); break; |
| 572 | #if HAVE_ALSA_ASOUNDLIB_H |
| 573 | case 'a': backend = playrtp_alsa; break; |
| 574 | #endif |
| 575 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
| 576 | case 'o': backend = playrtp_oss; break; |
| 577 | #endif |
| 578 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H |
| 579 | case 'c': backend = playrtp_coreaudio; break; |
| 580 | #endif |
| 581 | case 'C': configfile = optarg; break; |
| 582 | case 's': control_socket = optarg; break; |
| 583 | case 'r': dumpfile = optarg; break; |
| 584 | default: fatal(0, "invalid option"); |
| 585 | } |
| 586 | } |
| 587 | if(config_read(0)) fatal(0, "cannot read configuration"); |
| 588 | if(!maxbuffer) |
| 589 | maxbuffer = 4 * readahead; |
| 590 | argc -= optind; |
| 591 | argv += optind; |
| 592 | switch(argc) { |
| 593 | case 0: |
| 594 | /* Get configuration from server */ |
| 595 | if(!(c = disorder_new(1))) exit(EXIT_FAILURE); |
| 596 | if(disorder_connect(c)) exit(EXIT_FAILURE); |
| 597 | if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE); |
| 598 | sl.n = 2; |
| 599 | sl.s = xcalloc(2, sizeof *sl.s); |
| 600 | sl.s[0] = address; |
| 601 | sl.s[1] = port; |
| 602 | break; |
| 603 | case 1: |
| 604 | case 2: |
| 605 | /* Use command-line ADDRESS+PORT or just PORT */ |
| 606 | sl.n = argc; |
| 607 | sl.s = argv; |
| 608 | break; |
| 609 | default: |
| 610 | fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]"); |
| 611 | } |
| 612 | /* Look up address and port */ |
| 613 | if(!(res = get_address(&sl, &prefs, &sockname))) |
| 614 | exit(1); |
| 615 | /* Create the socket */ |
| 616 | if((rtpfd = socket(res->ai_family, |
| 617 | res->ai_socktype, |
| 618 | res->ai_protocol)) < 0) |
| 619 | fatal(errno, "error creating socket"); |
| 620 | /* Stash the multicast group address */ |
| 621 | if((is_multicast = multicast(res->ai_addr))) { |
| 622 | memcpy(&mgroup, res->ai_addr, res->ai_addrlen); |
| 623 | switch(res->ai_addr->sa_family) { |
| 624 | case AF_INET: |
| 625 | mgroup.in.sin_port = 0; |
| 626 | break; |
| 627 | case AF_INET6: |
| 628 | mgroup.in6.sin6_port = 0; |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | /* Bind to 0/port */ |
| 633 | switch(res->ai_addr->sa_family) { |
| 634 | case AF_INET: |
| 635 | memset(&((struct sockaddr_in *)res->ai_addr)->sin_addr, 0, |
| 636 | sizeof (struct in_addr)); |
| 637 | break; |
| 638 | case AF_INET6: |
| 639 | memset(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 0, |
| 640 | sizeof (struct in6_addr)); |
| 641 | break; |
| 642 | default: |
| 643 | fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family); |
| 644 | } |
| 645 | if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) |
| 646 | fatal(errno, "error binding socket to %s", sockname); |
| 647 | if(is_multicast) { |
| 648 | switch(mgroup.sa.sa_family) { |
| 649 | case PF_INET: |
| 650 | mreq.imr_multiaddr = mgroup.in.sin_addr; |
| 651 | mreq.imr_interface.s_addr = 0; /* use primary interface */ |
| 652 | if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 653 | &mreq, sizeof mreq) < 0) |
| 654 | fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP"); |
| 655 | break; |
| 656 | case PF_INET6: |
| 657 | mreq6.ipv6mr_multiaddr = mgroup.in6.sin6_addr; |
| 658 | memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface); |
| 659 | if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, |
| 660 | &mreq6, sizeof mreq6) < 0) |
| 661 | fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP"); |
| 662 | break; |
| 663 | default: |
| 664 | fatal(0, "unsupported address family %d", res->ai_family); |
| 665 | } |
| 666 | info("listening on %s multicast group %s", |
| 667 | format_sockaddr(res->ai_addr), format_sockaddr(&mgroup.sa)); |
| 668 | } else |
| 669 | info("listening on %s", format_sockaddr(res->ai_addr)); |
| 670 | len = sizeof rcvbuf; |
| 671 | if(getsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len) < 0) |
| 672 | fatal(errno, "error calling getsockopt SO_RCVBUF"); |
| 673 | if(target_rcvbuf > rcvbuf) { |
| 674 | if(setsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, |
| 675 | &target_rcvbuf, sizeof target_rcvbuf) < 0) |
| 676 | error(errno, "error calling setsockopt SO_RCVBUF %d", |
| 677 | target_rcvbuf); |
| 678 | /* We try to carry on anyway */ |
| 679 | else |
| 680 | info("changed socket receive buffer from %d to %d", |
| 681 | rcvbuf, target_rcvbuf); |
| 682 | } else |
| 683 | info("default socket receive buffer %d", rcvbuf); |
| 684 | if(logfp) |
| 685 | info("WARNING: -L option can impact performance"); |
| 686 | if(control_socket) { |
| 687 | pthread_t tid; |
| 688 | |
| 689 | if((err = pthread_create(&tid, 0, control_thread, 0))) |
| 690 | fatal(err, "pthread_create control_thread"); |
| 691 | } |
| 692 | if(dumpfile) { |
| 693 | int fd; |
| 694 | unsigned char buffer[65536]; |
| 695 | size_t written; |
| 696 | |
| 697 | if((fd = open(dumpfile, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) |
| 698 | fatal(errno, "opening %s", dumpfile); |
| 699 | /* Fill with 0s to a suitable size */ |
| 700 | memset(buffer, 0, sizeof buffer); |
| 701 | for(written = 0; written < dump_size * sizeof(int16_t); |
| 702 | written += sizeof buffer) { |
| 703 | if(write(fd, buffer, sizeof buffer) < 0) |
| 704 | fatal(errno, "clearing %s", dumpfile); |
| 705 | } |
| 706 | /* Map the buffer into memory for convenience */ |
| 707 | dump_buffer = mmap(0, dump_size * sizeof(int16_t), PROT_READ|PROT_WRITE, |
| 708 | MAP_SHARED, fd, 0); |
| 709 | if(dump_buffer == (void *)-1) |
| 710 | fatal(errno, "mapping %s", dumpfile); |
| 711 | info("dumping to %s", dumpfile); |
| 712 | } |
| 713 | play_rtp(); |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | Local Variables: |
| 719 | c-basic-offset:2 |
| 720 | comment-column:40 |
| 721 | fill-column:79 |
| 722 | indent-tabs-mode:nil |
| 723 | End: |
| 724 | */ |