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