chiark / gitweb /
Abolish uaudio_apis[]. Instead, define UAUDIO_DEFAULT to indicate
[disorder] / lib / uaudio-rtp.c
CommitLineData
7a2c7068
RK
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 */
e8c185c3 18/** @file lib/uaudio-rtp.c
7a2c7068
RK
19 * @brief Support for RTP network play backend */
20#include "common.h"
21
dfa51bb7 22#include <errno.h>
60e5bc86 23#include <sys/socket.h>
dfa51bb7
RK
24#include <ifaddrs.h>
25#include <net/if.h>
26#include <gcrypt.h>
27#include <unistd.h>
28#include <time.h>
60e5bc86 29#include <sys/uio.h>
7a2c7068
RK
30
31#include "uaudio.h"
32#include "mem.h"
33#include "log.h"
34#include "syscalls.h"
dfa51bb7
RK
35#include "rtp.h"
36#include "addr.h"
37#include "ifreq.h"
38#include "timeval.h"
39
40/** @brief Bytes to send per network packet
41 *
42 * This is the maximum number of bytes we pass to write(2); to determine actual
43 * packet sizes, add a UDP header and an IP header (and a link layer header if
44 * it's the link layer size you care about).
45 *
46 * Don't make this too big or arithmetic will start to overflow.
47 */
48#define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/)
49
50/** @brief RTP payload type */
51static int rtp_payload;
52
53/** @brief RTP output socket */
54static int rtp_fd;
55
56/** @brief RTP SSRC */
57static uint32_t rtp_id;
58
59/** @brief RTP sequence number */
60static uint16_t rtp_sequence;
61
62/** @brief RTP timestamp
63 *
64 * This is the timestamp that will be used on the next outbound packet.
65 *
66 * The timestamp in the packet header is only 32 bits wide. With 44100Hz
67 * stereo, that only gives about half a day before wrapping, which is not
68 * particularly convenient for certain debugging purposes. Therefore the
69 * timestamp is maintained as a 64-bit integer, giving around six million years
70 * before wrapping, and truncated to 32 bits when transmitting.
71 */
72static uint64_t rtp_timestamp;
73
74/** @brief Actual time corresponding to @ref rtp_timestamp
75 *
76 * This is the time, on this machine, at which the sample at @ref rtp_timestamp
77 * ought to be sent, interpreted as the time the last packet was sent plus the
78 * time length of the packet. */
79static struct timeval rtp_timeval;
80
81/** @brief Set when we (re-)activate, to provoke timestamp resync */
82static int rtp_reactivated;
83
84/** @brief Network error count
85 *
86 * If too many errors occur in too short a time, we give up.
87 */
88static int rtp_errors;
89
90/** @brief Delay threshold in microseconds
91 *
92 * rtp_play() never attempts to introduce a delay shorter than this.
93 */
94static int64_t rtp_delay_threshold;
7a2c7068
RK
95
96static const char *const rtp_options[] = {
dfa51bb7
RK
97 "rtp-destination",
98 "rtp-destination-port",
99 "rtp-source",
100 "rtp-source-port",
101 "multicast-ttl",
102 "multicast-loop",
103 "rtp-delay-threshold",
7a2c7068
RK
104 NULL
105};
106
dfa51bb7
RK
107static size_t rtp_play(void *buffer, size_t nsamples) {
108 struct rtp_header header;
109 struct iovec vec[2];
110 struct timeval now;
111
112 /* We do as much work as possible before checking what time it is */
113 /* Fill out header */
114 header.vpxcc = 2 << 6; /* V=2, P=0, X=0, CC=0 */
115 header.seq = htons(rtp_sequence++);
116 header.ssrc = rtp_id;
117 header.mpt = (rtp_reactivated ? 0x80 : 0x00) | rtp_payload;
118#if !WORDS_BIGENDIAN
119 /* Convert samples to network byte order */
120 uint16_t *u = buffer, *const limit = u + nsamples;
121 while(u < limit) {
122 *u = htons(*u);
123 ++u;
124 }
125#endif
126 vec[0].iov_base = (void *)&header;
127 vec[0].iov_len = sizeof header;
128 vec[1].iov_base = buffer;
129 vec[1].iov_len = nsamples * uaudio_sample_size;
130retry:
131 xgettimeofday(&now, NULL);
132 if(rtp_reactivated) {
133 /* We've been deactivated for some unknown interval. We need to advance
134 * rtp_timestamp to account for the dead air. */
135 /* On the first run through we'll set the start time. */
136 if(!rtp_timeval.tv_sec)
137 rtp_timeval = now;
138 /* See how much time we missed.
139 *
140 * This will be 0 on the first run through, in which case we'll not modify
141 * anything.
142 *
143 * It'll be negative in the (rare) situation where the deactivation
144 * interval is shorter than the last packet we sent. In this case we wait
145 * for that much time and then return having sent no samples, which will
146 * cause uaudio_play_thread_fn() to retry.
147 *
148 * In the normal case it will be positive.
149 */
150 const int64_t delay = tvsub_us(now, rtp_timeval); /* microseconds */
151 if(delay < 0) {
152 usleep(-delay);
153 goto retry;
154 }
155 /* Advance the RTP timestamp to the present. With 44.1KHz stereo this will
156 * overflow the intermediate value with a delay of a bit over 6 years.
157 * This seems acceptable. */
158 uint64_t update = (delay * uaudio_rate * uaudio_channels) / 1000000;
159 /* Don't throw off channel synchronization */
160 update -= update % uaudio_channels;
161 /* We log nontrivial changes */
162 if(update)
163 info("advancing rtp_time by %"PRIu64" samples", update);
164 rtp_timestamp += update;
165 rtp_timeval = now;
166 rtp_reactivated = 0;
167 } else {
168 /* Chances are we've been called right on the heels of the previous packet.
169 * If we just sent packets as fast as we got audio data we'd get way ahead
170 * of the player and some buffer somewhere would fill (or at least become
171 * unreasonably large).
172 *
173 * First find out how far ahead of the target time we are.
174 */
175 const int64_t ahead = tvsub_us(now, rtp_timeval); /* microseconds */
176 /* Only delay at all if we are nontrivially ahead. */
177 if(ahead > rtp_delay_threshold) {
178 /* Don't delay by the full amount */
179 usleep(ahead - rtp_delay_threshold / 2);
180 /* Refetch time (so we don't get out of step with reality) */
181 xgettimeofday(&now, NULL);
182 }
183 }
184 header.timestamp = htonl((uint32_t)rtp_timestamp);
185 int written_bytes;
186 do {
187 written_bytes = writev(rtp_fd, vec, 2);
188 } while(written_bytes < 0 && errno == EINTR);
189 if(written_bytes < 0) {
190 error(errno, "error transmitting audio data");
191 ++rtp_errors;
192 if(rtp_errors == 10)
193 fatal(0, "too many audio tranmission errors");
194 return 0;
195 } else
196 rtp_errors /= 2; /* gradual decay */
197 written_bytes -= sizeof (struct rtp_header);
198 size_t written_samples = written_bytes / uaudio_sample_size;
199 /* rtp_timestamp and rtp_timestamp are supposed to refer to the first sample
200 * of the next packet */
201 rtp_timestamp += written_samples;
202 const unsigned usec = (rtp_timeval.tv_usec
203 + 1000000 * written_samples / (uaudio_rate
204 * uaudio_channels));
205 /* ...will only overflow 32 bits if one packet is more than about half an
206 * hour long, which is not plausible. */
207 rtp_timeval.tv_sec += usec / 1000000;
208 rtp_timeval.tv_usec = usec % 1000000;
209 return written_samples;
210}
211
212static void rtp_open(void) {
213 struct addrinfo *res, *sres;
214 static const struct addrinfo pref = {
215 .ai_flags = 0,
216 .ai_family = PF_INET,
217 .ai_socktype = SOCK_DGRAM,
218 .ai_protocol = IPPROTO_UDP,
219 };
220 static const struct addrinfo prefbind = {
221 .ai_flags = AI_PASSIVE,
222 .ai_family = PF_INET,
223 .ai_socktype = SOCK_DGRAM,
224 .ai_protocol = IPPROTO_UDP,
225 };
226 static const int one = 1;
227 int sndbuf, target_sndbuf = 131072;
228 socklen_t len;
229 char *sockname, *ssockname;
230 struct stringlist dst, src;
231 const char *delay;
232
233 /* Get configuration */
234 dst.n = 2;
235 dst.s = xcalloc(2, sizeof *dst.s);
236 dst.s[0] = uaudio_get("rtp-destination");
237 dst.s[1] = uaudio_get("rtp-destination-port");
238 src.n = 2;
239 src.s = xcalloc(2, sizeof *dst.s);
240 src.s[0] = uaudio_get("rtp-source");
241 src.s[1] = uaudio_get("rtp-source-port");
242 if(!dst.s[0])
243 fatal(0, "'rtp-destination' not set");
244 if(!dst.s[1])
245 fatal(0, "'rtp-destination-port' not set");
246 if(src.s[0]) {
247 if(!src.s[1])
248 fatal(0, "'rtp-source-port' not set");
249 src.n = 2;
250 } else
251 src.n = 0;
252 if((delay = uaudio_get("rtp-delay-threshold")))
253 rtp_delay_threshold = atoi(delay);
254 else
255 rtp_delay_threshold = 1000; /* microseconds */
256
257 /* Resolve addresses */
258 res = get_address(&dst, &pref, &sockname);
259 if(!res) exit(-1);
260 if(src.n) {
261 sres = get_address(&src, &prefbind, &ssockname);
262 if(!sres) exit(-1);
263 } else
264 sres = 0;
265 /* Create the socket */
266 if((rtp_fd = socket(res->ai_family,
267 res->ai_socktype,
268 res->ai_protocol)) < 0)
269 fatal(errno, "error creating broadcast socket");
270 if(multicast(res->ai_addr)) {
271 /* Enable multicast options */
272 const char *ttls = uaudio_get("multicast-ttl");
273 const int ttl = ttls ? atoi(ttls) : 1;
274 const char *loops = uaudio_get("multicast-loop");
275 const int loop = loops ? !strcmp(loops, "yes") : 1;
276 switch(res->ai_family) {
277 case PF_INET: {
278 if(setsockopt(rtp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
279 &ttl, sizeof ttl) < 0)
280 fatal(errno, "error setting IP_MULTICAST_TTL on multicast socket");
281 if(setsockopt(rtp_fd, IPPROTO_IP, IP_MULTICAST_LOOP,
282 &loop, sizeof loop) < 0)
283 fatal(errno, "error setting IP_MULTICAST_LOOP on multicast socket");
284 break;
285 }
286 case PF_INET6: {
287 if(setsockopt(rtp_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
288 &ttl, sizeof ttl) < 0)
289 fatal(errno, "error setting IPV6_MULTICAST_HOPS on multicast socket");
290 if(setsockopt(rtp_fd, IPPROTO_IP, IPV6_MULTICAST_LOOP,
291 &loop, sizeof loop) < 0)
292 fatal(errno, "error setting IPV6_MULTICAST_LOOP on multicast socket");
293 break;
294 }
295 default:
296 fatal(0, "unsupported address family %d", res->ai_family);
297 }
298 info("multicasting on %s TTL=%d loop=%s",
299 sockname, ttl, loop ? "yes" : "no");
300 } else {
301 struct ifaddrs *ifs;
302
303 if(getifaddrs(&ifs) < 0)
304 fatal(errno, "error calling getifaddrs");
305 while(ifs) {
306 /* (At least on Darwin) IFF_BROADCAST might be set but ifa_broadaddr
307 * still a null pointer. It turns out that there's a subsequent entry
308 * for he same interface which _does_ have ifa_broadaddr though... */
309 if((ifs->ifa_flags & IFF_BROADCAST)
310 && ifs->ifa_broadaddr
311 && sockaddr_equal(ifs->ifa_broadaddr, res->ai_addr))
312 break;
313 ifs = ifs->ifa_next;
314 }
315 if(ifs) {
316 if(setsockopt(rtp_fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) < 0)
317 fatal(errno, "error setting SO_BROADCAST on broadcast socket");
318 info("broadcasting on %s (%s)", sockname, ifs->ifa_name);
319 } else
320 info("unicasting on %s", sockname);
321 }
322 /* Enlarge the socket buffer */
323 len = sizeof sndbuf;
324 if(getsockopt(rtp_fd, SOL_SOCKET, SO_SNDBUF,
325 &sndbuf, &len) < 0)
326 fatal(errno, "error getting SO_SNDBUF");
327 if(target_sndbuf > sndbuf) {
328 if(setsockopt(rtp_fd, SOL_SOCKET, SO_SNDBUF,
329 &target_sndbuf, sizeof target_sndbuf) < 0)
330 error(errno, "error setting SO_SNDBUF to %d", target_sndbuf);
331 else
332 info("changed socket send buffer size from %d to %d",
333 sndbuf, target_sndbuf);
334 } else
335 info("default socket send buffer is %d",
336 sndbuf);
337 /* We might well want to set additional broadcast- or multicast-related
338 * options here */
339 if(sres && bind(rtp_fd, sres->ai_addr, sres->ai_addrlen) < 0)
340 fatal(errno, "error binding broadcast socket to %s", ssockname);
341 if(connect(rtp_fd, res->ai_addr, res->ai_addrlen) < 0)
342 fatal(errno, "error connecting broadcast socket to %s", sockname);
343 /* Various fields are required to have random initial values by RFC3550. The
344 * packet contents are highly public so there's no point asking for very
345 * strong randomness. */
346 gcry_create_nonce(&rtp_id, sizeof rtp_id);
347 gcry_create_nonce(&rtp_sequence, sizeof rtp_sequence);
348 gcry_create_nonce(&rtp_timestamp, sizeof rtp_timestamp);
349 /* rtp_play() will spot this and choose an initial value */
350 rtp_timeval.tv_sec = 0;
351}
352
7a2c7068
RK
353static void rtp_start(uaudio_callback *callback,
354 void *userdata) {
dfa51bb7
RK
355 /* We only support L16 (but we do stereo and mono and will convert sign) */
356 if(uaudio_channels == 2
357 && uaudio_bits == 16
358 && uaudio_rate == 44100)
359 rtp_payload = 10;
360 else if(uaudio_channels == 1
361 && uaudio_bits == 16
362 && uaudio_rate == 44100)
363 rtp_payload = 11;
364 else
365 fatal(0, "asked for %d/%d/%d 16/44100/1 and 16/44100/2",
366 uaudio_bits, uaudio_rate, uaudio_channels);
367 rtp_open();
368 uaudio_thread_start(callback,
369 userdata,
370 rtp_play,
371 256 / uaudio_sample_size,
372 (NETWORK_BYTES - sizeof(struct rtp_header))
373 / uaudio_sample_size);
7a2c7068
RK
374}
375
376static void rtp_stop(void) {
dfa51bb7
RK
377 uaudio_thread_stop();
378 close(rtp_fd);
379 rtp_fd = -1;
7a2c7068
RK
380}
381
382static void rtp_activate(void) {
dfa51bb7
RK
383 rtp_reactivated = 1;
384 uaudio_thread_activate();
7a2c7068
RK
385}
386
387static void rtp_deactivate(void) {
dfa51bb7 388 uaudio_thread_deactivate();
7a2c7068
RK
389}
390
391const struct uaudio uaudio_rtp = {
392 .name = "rtp",
393 .options = rtp_options,
394 .start = rtp_start,
395 .stop = rtp_stop,
396 .activate = rtp_activate,
397 .deactivate = rtp_deactivate
398};
399
400/*
401Local Variables:
402c-basic-offset:2
403comment-column:40
404fill-column:79
405indent-tabs-mode:nil
406End:
407*/