X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/4942ee7d61bf22ba38bf026c7d05028cb7db0d54..cca034e58b00be2f53f9145957346bcd3e451c45:/clients/playrtp.c diff --git a/clients/playrtp.c b/clients/playrtp.c index 7eed9eb..89e458e 100644 --- a/clients/playrtp.c +++ b/clients/playrtp.c @@ -1,6 +1,6 @@ /* * This file is part of DisOrder. - * Copyright (C) 2007-2009 Richard Kettlewell + * Copyright (C) 2007-2009, 2011, 2013 Richard Kettlewell * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -65,6 +65,9 @@ #include #include #include +#include +#include +#include #include "log.h" #include "mem.h" @@ -207,6 +210,7 @@ static const struct option options[] = { #if HAVE_COREAUDIO_AUDIOHARDWARE_H { "core-audio", no_argument, 0, 'c' }, #endif + { "api", required_argument, 0, 'A' }, { "dump", required_argument, 0, 'r' }, { "command", required_argument, 0, 'e' }, { "pause-mode", required_argument, 0, 'P' }, @@ -247,10 +251,10 @@ static void *control_thread(void attribute((unused)) *arg) { strcpy(sa.sun_path, control_socket); sfd = xsocket(PF_UNIX, SOCK_STREAM, 0); if(bind(sfd, (const struct sockaddr *)&sa, sizeof sa) < 0) - fatal(errno, "error binding to %s", control_socket); + disorder_fatal(errno, "error binding to %s", control_socket); if(listen(sfd, 128) < 0) - fatal(errno, "error calling listen on %s", control_socket); - info("listening on %s", control_socket); + disorder_fatal(errno, "error calling listen on %s", control_socket); + disorder_info("listening on %s", control_socket); for(;;) { salen = sizeof sa; cfd = accept(sfd, (struct sockaddr *)&sa, &salen); @@ -260,17 +264,17 @@ static void *control_thread(void attribute((unused)) *arg) { case EAGAIN: break; default: - fatal(errno, "error calling accept on %s", control_socket); + disorder_fatal(errno, "error calling accept on %s", control_socket); } } if(!(fp = fdopen(cfd, "r+"))) { - error(errno, "error calling fdopen for %s connection", control_socket); + disorder_error(errno, "error calling fdopen for %s connection", control_socket); close(cfd); continue; } if(!inputline(control_socket, fp, &line, '\n')) { if(!strcmp(line, "stop")) { - info("stopped via %s", control_socket); + disorder_info("stopped via %s", control_socket); exit(0); /* terminate immediately */ } if(!strcmp(line, "query")) @@ -278,7 +282,7 @@ static void *control_thread(void attribute((unused)) *arg) { xfree(line); } if(fclose(fp) < 0) - error(errno, "error closing %s connection", control_socket); + disorder_error(errno, "error closing %s connection", control_socket); } } @@ -371,19 +375,19 @@ static void *listen_thread(void attribute((unused)) *arg) { case EINTR: continue; default: - fatal(errno, "error reading from socket"); + disorder_fatal(errno, "error reading from socket"); } } /* Ignore too-short packets */ if((size_t)n <= sizeof (struct rtp_header)) { - info("ignored a short packet"); + disorder_info("ignored a short packet"); continue; } timestamp = htonl(header.timestamp); seq = htons(header.seq); /* Ignore packets in the past */ if(active && lt(timestamp, next_timestamp)) { - info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32, + disorder_info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32, timestamp, next_timestamp); continue; } @@ -402,8 +406,7 @@ static void *listen_thread(void attribute((unused)) *arg) { break; /* TODO support other RFC3551 media types (when the speaker does) */ default: - fatal(0, "unsupported RTP payload type %d", - header.mpt & 0x7F); + disorder_fatal(0, "unsupported RTP payload type %d", header.mpt & 0x7F); } /* See if packet is silent */ const uint16_t *s = p->samples_raw; @@ -449,7 +452,7 @@ void playrtp_fill_buffer(void) { //fprintf(stderr, "%8u/%u (%u) DROPPING\n", nsamples, maxbuffer, minbuffer); drop_first_packet(); } - info("Buffering..."); + disorder_info("Buffering..."); /* Wait until there's at least minbuffer samples available */ while(nsamples < minbuffer) { //fprintf(stderr, "%8u/%u (%u) FILLING\n", nsamples, maxbuffer, minbuffer); @@ -493,15 +496,19 @@ static void help(void) { " --max, -x FRAMES Buffer maximum size\n" " --rcvbuf, -R BYTES Socket receive buffer size\n" " --config, -C PATH Set configuration file\n" -#if HAVE_ALSA_ASOUNDLIB_H - " --alsa, -a Use ALSA to play audio\n" -#endif -#if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST - " --oss, -o Use OSS to play audio\n" -#endif -#if HAVE_COREAUDIO_AUDIOHARDWARE_H - " --core-audio, -c Use Core Audio to play audio\n" -#endif + " --api, -A API Select audio API. Possibilities:\n" + " "); + int first = 1; + for(int n = 0; uaudio_apis[n]; ++n) { + if(uaudio_apis[n]->flags & UAUDIO_API_CLIENT) { + if(first) + first = 0; + else + xprintf(", "); + xprintf("%s", uaudio_apis[n]->name); + } + } + xprintf("\n" " --command, -e COMMAND Pipe audio to command.\n" " --pause-mode, -P silence For -e: pauses send silence (default)\n" " --pause-mode, -P suspend For -e: pauses suspend writes\n" @@ -604,8 +611,8 @@ static size_t playrtp_callback(void *buffer, * basis. */ if(nsamples > minbuffer && silent) { - info("dropping %zu samples (%"PRIu32" > %"PRIu32")", - samples, nsamples, minbuffer); + disorder_info("dropping %zu samples (%"PRIu32" > %"PRIu32")", + samples, nsamples, minbuffer); samples = 0; } /* Junk obsolete packets */ @@ -614,6 +621,51 @@ static size_t playrtp_callback(void *buffer, return samples; } +static int compare_family(const struct ifaddrs *a, + const struct ifaddrs *b, + int family) { + int afamily = a->ifa_addr->sa_family; + int bfamily = b->ifa_addr->sa_family; + if(afamily != bfamily) { + /* Preferred family wins */ + if(afamily == family) return 1; + if(bfamily == family) return -1; + /* Either there's no preference or it doesn't help. Prefer IPv4 */ + if(afamily == AF_INET) return 1; + if(bfamily == AF_INET) return -1; + /* Failing that prefer IPv6 */ + if(afamily == AF_INET6) return 1; + if(bfamily == AF_INET6) return -1; + } + return 0; +} + +static int compare_flags(const struct ifaddrs *a, + const struct ifaddrs *b) { + unsigned aflags = a->ifa_flags, bflags = b->ifa_flags; + /* Up interfaces are better than down ones */ + unsigned aup = aflags & IFF_UP, bup = bflags & IFF_UP; + if(aup != bup) + return aup > bup ? 1 : -1; + /* Static addresses are better than dynamic */ + unsigned adynamic = aflags & IFF_DYNAMIC, bdynamic = bflags & IFF_DYNAMIC; + if(adynamic != bdynamic) + return adynamic < bdynamic ? 1 : -1; + unsigned aloopback = aflags & IFF_LOOPBACK, bloopback = bflags & IFF_LOOPBACK; + /* Static addresses are better than dynamic */ + if(aloopback != bloopback) + return aloopback < bloopback ? 1 : -1; + return 0; +} + +static int compare_interfaces(const struct ifaddrs *a, + const struct ifaddrs *b, + int family) { + int c; + if((c = compare_family(a, b, family))) return c; + return compare_flags(a, b); +} + int main(int argc, char **argv) { int n, err; struct addrinfo *res; @@ -623,7 +675,7 @@ int main(int argc, char **argv) { socklen_t len; struct ip_mreq mreq; struct ipv6_mreq mreq6; - disorder_client *c; + disorder_client *c = NULL; char *address, *port; int is_multicast; union any_sockaddr { @@ -648,9 +700,8 @@ int main(int argc, char **argv) { * timestamps in the logs */ logdate = 1; mem_init(); - if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); - backend = uaudio_apis[0]; - while((n = getopt_long(argc, argv, "hVdD:m:x:L:R:aocC:re:P:M", options, 0)) >= 0) { + if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale"); + while((n = getopt_long(argc, argv, "hVdD:m:x:L:R:aocC:re:P:MA:", options, 0)) >= 0) { switch(n) { case 'h': help(); case 'V': version("disorder-playrtp"); @@ -661,24 +712,45 @@ int main(int argc, char **argv) { case 'L': logfp = fopen(optarg, "w"); break; case 'R': target_rcvbuf = atoi(optarg); break; #if HAVE_ALSA_ASOUNDLIB_H - case 'a': backend = &uaudio_alsa; break; + case 'a': + disorder_error(0, "deprecated option; use --api alsa instead"); + backend = &uaudio_alsa; break; #endif #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST - case 'o': backend = &uaudio_oss; break; + case 'o': + disorder_error(0, "deprecated option; use --api oss instead"); + backend = &uaudio_oss; + break; #endif #if HAVE_COREAUDIO_AUDIOHARDWARE_H - case 'c': backend = &uaudio_coreaudio; break; + case 'c': + disorder_error(0, "deprecated option; use --api coreaudio instead"); + backend = &uaudio_coreaudio; + break; #endif + case 'A': backend = uaudio_find(optarg); break; case 'C': configfile = optarg; break; case 's': control_socket = optarg; break; case 'r': dumpfile = optarg; break; case 'e': backend = &uaudio_command; uaudio_set("command", optarg); break; case 'P': uaudio_set("pause-mode", optarg); break; case 'M': monitor = 1; break; - default: fatal(0, "invalid option"); + default: disorder_fatal(0, "invalid option"); } } - if(config_read(0, NULL)) fatal(0, "cannot read configuration"); + if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration"); + if(!backend) { + backend = uaudio_default(uaudio_apis, UAUDIO_API_CLIENT); + if(!backend) + disorder_fatal(0, "no default uaudio API found"); + disorder_info("default audio API %s", backend->name); + } + if(backend == &uaudio_rtp) { + /* This means that you have NO local sound output. This can happen if you + * use a non-Apple GCC on a Mac (because it doesn't know how to compile + * CoreAudio/AudioHardware.h). */ + disorder_fatal(0, "cannot play RTP through RTP"); + } if(!maxbuffer) maxbuffer = 2 * minbuffer; argc -= optind; @@ -701,105 +773,157 @@ int main(int argc, char **argv) { sl.s = argv; break; default: - fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]"); + disorder_fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]"); } - /* Look up address and port */ - if(!(res = get_address(&sl, &prefs, &sockname))) - exit(1); - /* Create the socket */ - if((rtpfd = socket(res->ai_family, - res->ai_socktype, - res->ai_protocol)) < 0) - fatal(errno, "error creating socket"); - /* Allow multiple listeners */ - xsetsockopt(rtpfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); - is_multicast = multicast(res->ai_addr); - /* The multicast and unicast/broadcast cases are different enough that they - * are totally split. Trying to find commonality between them causes more - * trouble that it's worth. */ - if(is_multicast) { - /* Stash the multicast group address */ - memcpy(&mgroup, res->ai_addr, res->ai_addrlen); - switch(res->ai_addr->sa_family) { - case AF_INET: - mgroup.in.sin_port = 0; - break; - case AF_INET6: - mgroup.in6.sin6_port = 0; - break; - default: - fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family); - } - /* Bind to to the multicast group address */ - if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) - fatal(errno, "error binding socket to %s", format_sockaddr(res->ai_addr)); - /* Add multicast group membership */ - switch(mgroup.sa.sa_family) { - case PF_INET: - mreq.imr_multiaddr = mgroup.in.sin_addr; - mreq.imr_interface.s_addr = 0; /* use primary interface */ - if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, - &mreq, sizeof mreq) < 0) - fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP"); - break; - case PF_INET6: - mreq6.ipv6mr_multiaddr = mgroup.in6.sin6_addr; - memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface); - if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, - &mreq6, sizeof mreq6) < 0) - fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP"); - break; - default: - fatal(0, "unsupported address family %d", res->ai_family); + disorder_info("version "VERSION" process ID %lu", + (unsigned long)getpid()); + struct sockaddr *addr; + socklen_t addr_len; + if(!strcmp(sl.s[0], "-")) { + /* Pick address family to match known-working connectivity to the server */ + int family = disorder_client_af(c); + /* Get a list of interfaces */ + struct ifaddrs *ifa, *bestifa = NULL; + if(getifaddrs(&ifa) < 0) + disorder_fatal(errno, "error calling getifaddrs"); + /* Try to pick a good one */ + for(; ifa; ifa = ifa->ifa_next) { + if(bestifa == NULL + || compare_interfaces(ifa, bestifa, family) > 0) + bestifa = ifa; } + if(!bestifa) + disorder_fatal(0, "failed to select a network interface"); + family = bestifa->ifa_addr->sa_family; + if((rtpfd = socket(family, + SOCK_DGRAM, + IPPROTO_UDP)) < 0) + disorder_fatal(errno, "error creating socket (family %d)", family); + /* Bind the address */ + if(bind(rtpfd, bestifa->ifa_addr, + family == AF_INET + ? sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6)) < 0) + disorder_fatal(errno, "error binding socket"); + static struct sockaddr_storage bound_address; + addr = (struct sockaddr *)&bound_address; + addr_len = sizeof bound_address; + if(getsockname(rtpfd, addr, &addr_len) < 0) + disorder_fatal(errno, "error getting socket address"); + /* Convert to string */ + char addrname[128], portname[32]; + if(getnameinfo(addr, addr_len, + addrname, sizeof addrname, + portname, sizeof portname, + NI_NUMERICHOST|NI_NUMERICSERV) < 0) + disorder_fatal(errno, "getnameinfo"); + /* Ask for audio data */ + if(disorder_rtp_request(c, addrname, portname)) exit(EXIT_FAILURE); /* Report what we did */ - info("listening on %s multicast group %s", - format_sockaddr(res->ai_addr), format_sockaddr(&mgroup.sa)); + disorder_info("listening on %s", format_sockaddr(addr)); } else { - /* Bind to 0/port */ - switch(res->ai_addr->sa_family) { - case AF_INET: { - struct sockaddr_in *in = (struct sockaddr_in *)res->ai_addr; + /* Look up address and port */ + if(!(res = get_address(&sl, &prefs, &sockname))) + exit(1); + addr = res->ai_addr; + addr_len = res->ai_addrlen; + /* Create the socket */ + if((rtpfd = socket(res->ai_family, + res->ai_socktype, + res->ai_protocol)) < 0) + disorder_fatal(errno, "error creating socket"); + /* Allow multiple listeners */ + xsetsockopt(rtpfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); + is_multicast = multicast(addr); + /* The multicast and unicast/broadcast cases are different enough that they + * are totally split. Trying to find commonality between them causes more + * trouble that it's worth. */ + if(is_multicast) { + /* Stash the multicast group address */ + memcpy(&mgroup, addr, addr_len); + switch(res->ai_addr->sa_family) { + case AF_INET: + mgroup.in.sin_port = 0; + break; + case AF_INET6: + mgroup.in6.sin6_port = 0; + break; + default: + disorder_fatal(0, "unsupported address family %d", + (int)addr->sa_family); + } + /* Bind to to the multicast group address */ + if(bind(rtpfd, addr, addr_len) < 0) + disorder_fatal(errno, "error binding socket to %s", + format_sockaddr(addr)); + /* Add multicast group membership */ + switch(mgroup.sa.sa_family) { + case PF_INET: + mreq.imr_multiaddr = mgroup.in.sin_addr; + mreq.imr_interface.s_addr = 0; /* use primary interface */ + if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, + &mreq, sizeof mreq) < 0) + disorder_fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP"); + break; + case PF_INET6: + mreq6.ipv6mr_multiaddr = mgroup.in6.sin6_addr; + memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface); + if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, + &mreq6, sizeof mreq6) < 0) + disorder_fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP"); + break; + default: + disorder_fatal(0, "unsupported address family %d", res->ai_family); + } + /* Report what we did */ + disorder_info("listening on %s multicast group %s", + format_sockaddr(addr), format_sockaddr(&mgroup.sa)); + } else { + /* Bind to 0/port */ + switch(addr->sa_family) { + case AF_INET: { + struct sockaddr_in *in = (struct sockaddr_in *)addr; - memset(&in->sin_addr, 0, sizeof (struct in_addr)); - break; - } - case AF_INET6: { - struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)res->ai_addr; + memset(&in->sin_addr, 0, sizeof (struct in_addr)); + break; + } + case AF_INET6: { + struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; - memset(&in6->sin6_addr, 0, sizeof (struct in6_addr)); - break; - } - default: - fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family); + memset(&in6->sin6_addr, 0, sizeof (struct in6_addr)); + break; + } + default: + disorder_fatal(0, "unsupported family %d", (int)addr->sa_family); + } + if(bind(rtpfd, addr, addr_len) < 0) + disorder_fatal(errno, "error binding socket to %s", + format_sockaddr(addr)); + /* Report what we did */ + disorder_info("listening on %s", format_sockaddr(addr)); } - if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) - fatal(errno, "error binding socket to %s", format_sockaddr(res->ai_addr)); - /* Report what we did */ - info("listening on %s", format_sockaddr(res->ai_addr)); } len = sizeof rcvbuf; if(getsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len) < 0) - fatal(errno, "error calling getsockopt SO_RCVBUF"); + disorder_fatal(errno, "error calling getsockopt SO_RCVBUF"); if(target_rcvbuf > rcvbuf) { if(setsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &target_rcvbuf, sizeof target_rcvbuf) < 0) - error(errno, "error calling setsockopt SO_RCVBUF %d", - target_rcvbuf); + disorder_error(errno, "error calling setsockopt SO_RCVBUF %d", + target_rcvbuf); /* We try to carry on anyway */ else - info("changed socket receive buffer from %d to %d", - rcvbuf, target_rcvbuf); + disorder_info("changed socket receive buffer from %d to %d", + rcvbuf, target_rcvbuf); } else - info("default socket receive buffer %d", rcvbuf); + disorder_info("default socket receive buffer %d", rcvbuf); //info("minbuffer %u maxbuffer %u", minbuffer, maxbuffer); if(logfp) - info("WARNING: -L option can impact performance"); + disorder_info("WARNING: -L option can impact performance"); if(control_socket) { pthread_t tid; if((err = pthread_create(&tid, 0, control_thread, 0))) - fatal(err, "pthread_create control_thread"); + disorder_fatal(err, "pthread_create control_thread"); } if(dumpfile) { int fd; @@ -807,39 +931,40 @@ int main(int argc, char **argv) { size_t written; if((fd = open(dumpfile, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) - fatal(errno, "opening %s", dumpfile); + disorder_fatal(errno, "opening %s", dumpfile); /* Fill with 0s to a suitable size */ memset(buffer, 0, sizeof buffer); for(written = 0; written < dump_size * sizeof(int16_t); written += sizeof buffer) { if(write(fd, buffer, sizeof buffer) < 0) - fatal(errno, "clearing %s", dumpfile); + disorder_fatal(errno, "clearing %s", dumpfile); } /* Map the buffer into memory for convenience */ dump_buffer = mmap(0, dump_size * sizeof(int16_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if(dump_buffer == (void *)-1) - fatal(errno, "mapping %s", dumpfile); - info("dumping to %s", dumpfile); + disorder_fatal(errno, "mapping %s", dumpfile); + disorder_info("dumping to %s", dumpfile); } /* Set up output. Currently we only support L16 so there's no harm setting * the format before we know what it is! */ uaudio_set_format(44100/*Hz*/, 2/*channels*/, 16/*bits/channel*/, 1/*signed*/); + uaudio_set("application", "disorder-playrtp"); backend->start(playrtp_callback, NULL); /* We receive and convert audio data in a background thread */ if((err = pthread_create(<id, 0, listen_thread, 0))) - fatal(err, "pthread_create listen_thread"); + disorder_fatal(err, "pthread_create listen_thread"); /* We have a second thread to add received packets to the queue */ if((err = pthread_create(<id, 0, queue_thread, 0))) - fatal(err, "pthread_create queue_thread"); + disorder_fatal(err, "pthread_create queue_thread"); pthread_mutex_lock(&lock); time_t lastlog = 0; for(;;) { /* Wait for the buffer to fill up a bit */ playrtp_fill_buffer(); /* Start playing now */ - info("Playing..."); + disorder_info("Playing..."); next_timestamp = pheap_first(&packets)->timestamp; active = 1; pthread_mutex_unlock(&lock); @@ -863,11 +988,11 @@ int main(int argc, char **argv) { if(now >= lastlog + 60) { int offset = nsamples - minbuffer; double offtime = (double)offset / (uaudio_rate * uaudio_channels); - info("%+d samples off (%d.%02ds, %d bytes)", - offset, - (int)fabs(offtime) * (offtime < 0 ? -1 : 1), - (int)(fabs(offtime) * 100) % 100, - offset * uaudio_bits / CHAR_BIT); + disorder_info("%+d samples off (%d.%02ds, %d bytes)", + offset, + (int)fabs(offtime) * (offtime < 0 ? -1 : 1), + (int)(fabs(offtime) * 100) % 100, + offset * uaudio_bits / CHAR_BIT); lastlog = now; } }