| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2009 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 lib/uaudio-rtp.c |
| 19 | * @brief Support for RTP network play backend */ |
| 20 | #include "common.h" |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <ifaddrs.h> |
| 25 | #include <net/if.h> |
| 26 | #include <arpa/inet.h> |
| 27 | #include <netinet/in.h> |
| 28 | #include <gcrypt.h> |
| 29 | #include <unistd.h> |
| 30 | #include <time.h> |
| 31 | #include <sys/uio.h> |
| 32 | |
| 33 | #include "uaudio.h" |
| 34 | #include "mem.h" |
| 35 | #include "log.h" |
| 36 | #include "syscalls.h" |
| 37 | #include "rtp.h" |
| 38 | #include "addr.h" |
| 39 | #include "ifreq.h" |
| 40 | #include "timeval.h" |
| 41 | #include "configuration.h" |
| 42 | |
| 43 | /** @brief Bytes to send per network packet |
| 44 | * |
| 45 | * This is the maximum number of bytes we pass to write(2); to determine actual |
| 46 | * packet sizes, add a UDP header and an IP header (and a link layer header if |
| 47 | * it's the link layer size you care about). |
| 48 | * |
| 49 | * Don't make this too big or arithmetic will start to overflow. |
| 50 | */ |
| 51 | #define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/) |
| 52 | |
| 53 | /** @brief RTP payload type */ |
| 54 | static int rtp_payload; |
| 55 | |
| 56 | /** @brief RTP output socket */ |
| 57 | static int rtp_fd; |
| 58 | |
| 59 | /** @brief RTP SSRC */ |
| 60 | static uint32_t rtp_id; |
| 61 | |
| 62 | /** @brief RTP sequence number */ |
| 63 | static uint16_t rtp_sequence; |
| 64 | |
| 65 | /** @brief Network error count |
| 66 | * |
| 67 | * If too many errors occur in too short a time, we give up. |
| 68 | */ |
| 69 | static int rtp_errors; |
| 70 | |
| 71 | /** @brief Delay threshold in microseconds |
| 72 | * |
| 73 | * rtp_play() never attempts to introduce a delay shorter than this. |
| 74 | */ |
| 75 | static int64_t rtp_delay_threshold; |
| 76 | |
| 77 | static const char *const rtp_options[] = { |
| 78 | "rtp-destination", |
| 79 | "rtp-destination-port", |
| 80 | "rtp-source", |
| 81 | "rtp-source-port", |
| 82 | "multicast-ttl", |
| 83 | "multicast-loop", |
| 84 | "delay-threshold", |
| 85 | NULL |
| 86 | }; |
| 87 | |
| 88 | static size_t rtp_play(void *buffer, size_t nsamples) { |
| 89 | struct rtp_header header; |
| 90 | struct iovec vec[2]; |
| 91 | |
| 92 | /* We do as much work as possible before checking what time it is */ |
| 93 | /* Fill out header */ |
| 94 | header.vpxcc = 2 << 6; /* V=2, P=0, X=0, CC=0 */ |
| 95 | header.seq = htons(rtp_sequence++); |
| 96 | header.ssrc = rtp_id; |
| 97 | header.mpt = (uaudio_schedule_reactivated ? 0x80 : 0x00) | rtp_payload; |
| 98 | #if !WORDS_BIGENDIAN |
| 99 | /* Convert samples to network byte order */ |
| 100 | uint16_t *u = buffer, *const limit = u + nsamples; |
| 101 | while(u < limit) { |
| 102 | *u = htons(*u); |
| 103 | ++u; |
| 104 | } |
| 105 | #endif |
| 106 | vec[0].iov_base = (void *)&header; |
| 107 | vec[0].iov_len = sizeof header; |
| 108 | vec[1].iov_base = buffer; |
| 109 | vec[1].iov_len = nsamples * uaudio_sample_size; |
| 110 | uaudio_schedule_synchronize(); |
| 111 | header.timestamp = htonl((uint32_t)uaudio_schedule_timestamp); |
| 112 | int written_bytes; |
| 113 | do { |
| 114 | written_bytes = writev(rtp_fd, vec, 2); |
| 115 | } while(written_bytes < 0 && errno == EINTR); |
| 116 | if(written_bytes < 0) { |
| 117 | error(errno, "error transmitting audio data"); |
| 118 | ++rtp_errors; |
| 119 | if(rtp_errors == 10) |
| 120 | fatal(0, "too many audio tranmission errors"); |
| 121 | return 0; |
| 122 | } else |
| 123 | rtp_errors /= 2; /* gradual decay */ |
| 124 | written_bytes -= sizeof (struct rtp_header); |
| 125 | const size_t written_samples = written_bytes / uaudio_sample_size; |
| 126 | uaudio_schedule_update(written_samples); |
| 127 | return written_samples; |
| 128 | } |
| 129 | |
| 130 | static void rtp_open(void) { |
| 131 | struct addrinfo *res, *sres; |
| 132 | static const struct addrinfo pref = { |
| 133 | .ai_flags = 0, |
| 134 | .ai_family = PF_INET, |
| 135 | .ai_socktype = SOCK_DGRAM, |
| 136 | .ai_protocol = IPPROTO_UDP, |
| 137 | }; |
| 138 | static const struct addrinfo prefbind = { |
| 139 | .ai_flags = AI_PASSIVE, |
| 140 | .ai_family = PF_INET, |
| 141 | .ai_socktype = SOCK_DGRAM, |
| 142 | .ai_protocol = IPPROTO_UDP, |
| 143 | }; |
| 144 | static const int one = 1; |
| 145 | int sndbuf, target_sndbuf = 131072; |
| 146 | socklen_t len; |
| 147 | char *sockname, *ssockname; |
| 148 | struct stringlist dst, src; |
| 149 | |
| 150 | /* Get configuration */ |
| 151 | dst.n = 2; |
| 152 | dst.s = xcalloc(2, sizeof *dst.s); |
| 153 | dst.s[0] = uaudio_get("rtp-destination", NULL); |
| 154 | dst.s[1] = uaudio_get("rtp-destination-port", NULL); |
| 155 | src.n = 2; |
| 156 | src.s = xcalloc(2, sizeof *dst.s); |
| 157 | src.s[0] = uaudio_get("rtp-source", NULL); |
| 158 | src.s[1] = uaudio_get("rtp-source-port", NULL); |
| 159 | if(!dst.s[0]) |
| 160 | fatal(0, "'rtp-destination' not set"); |
| 161 | if(!dst.s[1]) |
| 162 | fatal(0, "'rtp-destination-port' not set"); |
| 163 | if(src.s[0]) { |
| 164 | if(!src.s[1]) |
| 165 | fatal(0, "'rtp-source-port' not set"); |
| 166 | src.n = 2; |
| 167 | } else |
| 168 | src.n = 0; |
| 169 | rtp_delay_threshold = atoi(uaudio_get("rtp-delay-threshold", "1000")); |
| 170 | /* ...microseconds */ |
| 171 | |
| 172 | /* Resolve addresses */ |
| 173 | res = get_address(&dst, &pref, &sockname); |
| 174 | if(!res) exit(-1); |
| 175 | if(src.n) { |
| 176 | sres = get_address(&src, &prefbind, &ssockname); |
| 177 | if(!sres) exit(-1); |
| 178 | } else |
| 179 | sres = 0; |
| 180 | /* Create the socket */ |
| 181 | if((rtp_fd = socket(res->ai_family, |
| 182 | res->ai_socktype, |
| 183 | res->ai_protocol)) < 0) |
| 184 | fatal(errno, "error creating broadcast socket"); |
| 185 | if(multicast(res->ai_addr)) { |
| 186 | /* Enable multicast options */ |
| 187 | const int ttl = atoi(uaudio_get("multicast-ttl", "1")); |
| 188 | const int loop = !strcmp(uaudio_get("multicast-loop", "yes"), "yes"); |
| 189 | switch(res->ai_family) { |
| 190 | case PF_INET: { |
| 191 | if(setsockopt(rtp_fd, IPPROTO_IP, IP_MULTICAST_TTL, |
| 192 | &ttl, sizeof ttl) < 0) |
| 193 | fatal(errno, "error setting IP_MULTICAST_TTL on multicast socket"); |
| 194 | if(setsockopt(rtp_fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
| 195 | &loop, sizeof loop) < 0) |
| 196 | fatal(errno, "error setting IP_MULTICAST_LOOP on multicast socket"); |
| 197 | break; |
| 198 | } |
| 199 | case PF_INET6: { |
| 200 | if(setsockopt(rtp_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, |
| 201 | &ttl, sizeof ttl) < 0) |
| 202 | fatal(errno, "error setting IPV6_MULTICAST_HOPS on multicast socket"); |
| 203 | if(setsockopt(rtp_fd, IPPROTO_IP, IPV6_MULTICAST_LOOP, |
| 204 | &loop, sizeof loop) < 0) |
| 205 | fatal(errno, "error setting IPV6_MULTICAST_LOOP on multicast socket"); |
| 206 | break; |
| 207 | } |
| 208 | default: |
| 209 | fatal(0, "unsupported address family %d", res->ai_family); |
| 210 | } |
| 211 | info("multicasting on %s TTL=%d loop=%s", |
| 212 | sockname, ttl, loop ? "yes" : "no"); |
| 213 | } else { |
| 214 | struct ifaddrs *ifs; |
| 215 | |
| 216 | if(getifaddrs(&ifs) < 0) |
| 217 | fatal(errno, "error calling getifaddrs"); |
| 218 | while(ifs) { |
| 219 | /* (At least on Darwin) IFF_BROADCAST might be set but ifa_broadaddr |
| 220 | * still a null pointer. It turns out that there's a subsequent entry |
| 221 | * for he same interface which _does_ have ifa_broadaddr though... */ |
| 222 | if((ifs->ifa_flags & IFF_BROADCAST) |
| 223 | && ifs->ifa_broadaddr |
| 224 | && sockaddr_equal(ifs->ifa_broadaddr, res->ai_addr)) |
| 225 | break; |
| 226 | ifs = ifs->ifa_next; |
| 227 | } |
| 228 | if(ifs) { |
| 229 | if(setsockopt(rtp_fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) < 0) |
| 230 | fatal(errno, "error setting SO_BROADCAST on broadcast socket"); |
| 231 | info("broadcasting on %s (%s)", sockname, ifs->ifa_name); |
| 232 | } else |
| 233 | info("unicasting on %s", sockname); |
| 234 | } |
| 235 | /* Enlarge the socket buffer */ |
| 236 | len = sizeof sndbuf; |
| 237 | if(getsockopt(rtp_fd, SOL_SOCKET, SO_SNDBUF, |
| 238 | &sndbuf, &len) < 0) |
| 239 | fatal(errno, "error getting SO_SNDBUF"); |
| 240 | if(target_sndbuf > sndbuf) { |
| 241 | if(setsockopt(rtp_fd, SOL_SOCKET, SO_SNDBUF, |
| 242 | &target_sndbuf, sizeof target_sndbuf) < 0) |
| 243 | error(errno, "error setting SO_SNDBUF to %d", target_sndbuf); |
| 244 | else |
| 245 | info("changed socket send buffer size from %d to %d", |
| 246 | sndbuf, target_sndbuf); |
| 247 | } else |
| 248 | info("default socket send buffer is %d", |
| 249 | sndbuf); |
| 250 | /* We might well want to set additional broadcast- or multicast-related |
| 251 | * options here */ |
| 252 | if(sres && bind(rtp_fd, sres->ai_addr, sres->ai_addrlen) < 0) |
| 253 | fatal(errno, "error binding broadcast socket to %s", ssockname); |
| 254 | if(connect(rtp_fd, res->ai_addr, res->ai_addrlen) < 0) |
| 255 | fatal(errno, "error connecting broadcast socket to %s", sockname); |
| 256 | } |
| 257 | |
| 258 | static void rtp_start(uaudio_callback *callback, |
| 259 | void *userdata) { |
| 260 | /* We only support L16 (but we do stereo and mono and will convert sign) */ |
| 261 | if(uaudio_channels == 2 |
| 262 | && uaudio_bits == 16 |
| 263 | && uaudio_rate == 44100) |
| 264 | rtp_payload = 10; |
| 265 | else if(uaudio_channels == 1 |
| 266 | && uaudio_bits == 16 |
| 267 | && uaudio_rate == 44100) |
| 268 | rtp_payload = 11; |
| 269 | else |
| 270 | fatal(0, "asked for %d/%d/%d 16/44100/1 and 16/44100/2", |
| 271 | uaudio_bits, uaudio_rate, uaudio_channels); |
| 272 | /* Various fields are required to have random initial values by RFC3550. The |
| 273 | * packet contents are highly public so there's no point asking for very |
| 274 | * strong randomness. */ |
| 275 | gcry_create_nonce(&rtp_id, sizeof rtp_id); |
| 276 | gcry_create_nonce(&rtp_sequence, sizeof rtp_sequence); |
| 277 | rtp_open(); |
| 278 | uaudio_schedule_init(); |
| 279 | uaudio_thread_start(callback, |
| 280 | userdata, |
| 281 | rtp_play, |
| 282 | 256 / uaudio_sample_size, |
| 283 | (NETWORK_BYTES - sizeof(struct rtp_header)) |
| 284 | / uaudio_sample_size); |
| 285 | } |
| 286 | |
| 287 | static void rtp_stop(void) { |
| 288 | uaudio_thread_stop(); |
| 289 | close(rtp_fd); |
| 290 | rtp_fd = -1; |
| 291 | } |
| 292 | |
| 293 | static void rtp_activate(void) { |
| 294 | uaudio_schedule_reactivated = 1; |
| 295 | uaudio_thread_activate(); |
| 296 | } |
| 297 | |
| 298 | static void rtp_deactivate(void) { |
| 299 | uaudio_thread_deactivate(); |
| 300 | } |
| 301 | |
| 302 | static void rtp_configure(void) { |
| 303 | char buffer[64]; |
| 304 | |
| 305 | uaudio_set("rtp-destination", config->broadcast.s[0]); |
| 306 | uaudio_set("rtp-destination-port", config->broadcast.s[1]); |
| 307 | if(config->broadcast_from.n) { |
| 308 | uaudio_set("rtp-source", config->broadcast_from.s[0]); |
| 309 | uaudio_set("rtp-source-port", config->broadcast_from.s[0]); |
| 310 | } else { |
| 311 | uaudio_set("rtp-source", NULL); |
| 312 | uaudio_set("rtp-source-port", NULL); |
| 313 | } |
| 314 | snprintf(buffer, sizeof buffer, "%ld", config->multicast_ttl); |
| 315 | uaudio_set("multicast-ttl", buffer); |
| 316 | uaudio_set("multicast-loop", config->multicast_loop ? "yes" : "no"); |
| 317 | snprintf(buffer, sizeof buffer, "%ld", config->rtp_delay_threshold); |
| 318 | uaudio_set("delay-threshold", buffer); |
| 319 | } |
| 320 | |
| 321 | const struct uaudio uaudio_rtp = { |
| 322 | .name = "rtp", |
| 323 | .options = rtp_options, |
| 324 | .start = rtp_start, |
| 325 | .stop = rtp_stop, |
| 326 | .activate = rtp_activate, |
| 327 | .deactivate = rtp_deactivate, |
| 328 | .configure = rtp_configure, |
| 329 | }; |
| 330 | |
| 331 | /* |
| 332 | Local Variables: |
| 333 | c-basic-offset:2 |
| 334 | comment-column:40 |
| 335 | fill-column:79 |
| 336 | indent-tabs-mode:nil |
| 337 | End: |
| 338 | */ |