Commit | Line | Data |
---|---|---|
1c3f1e73 | 1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2005, 2006, 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 server/speaker-network.c | |
21 | * @brief Support for @ref BACKEND_NETWORK */ | |
22 | ||
23 | #include <config.h> | |
24 | #include "types.h" | |
25 | ||
26 | #include <unistd.h> | |
27 | #include <poll.h> | |
28 | #include <netdb.h> | |
29 | #include <gcrypt.h> | |
30 | #include <sys/socket.h> | |
31 | #include <sys/uio.h> | |
32 | #include <assert.h> | |
81b1bf12 | 33 | #include <net/if.h> |
db2c19dc | 34 | #include <ifaddrs.h> |
6d2d327c | 35 | #include <errno.h> |
edbd470f | 36 | #include <netinet/in.h> |
1c3f1e73 | 37 | |
38 | #include "configuration.h" | |
39 | #include "syscalls.h" | |
40 | #include "log.h" | |
41 | #include "addr.h" | |
42 | #include "timeval.h" | |
43 | #include "rtp.h" | |
81b1bf12 | 44 | #include "ifreq.h" |
1c3f1e73 | 45 | #include "speaker-protocol.h" |
46 | #include "speaker.h" | |
47 | ||
48 | /** @brief Network socket | |
49 | * | |
50 | * This is the file descriptor to write to for @ref BACKEND_NETWORK. | |
51 | */ | |
52 | static int bfd = -1; | |
53 | ||
54 | /** @brief RTP timestamp | |
55 | * | |
56 | * This counts the number of samples played (NB not the number of frames | |
57 | * played). | |
58 | * | |
59 | * The timestamp in the packet header is only 32 bits wide. With 44100Hz | |
60 | * stereo, that only gives about half a day before wrapping, which is not | |
61 | * particularly convenient for certain debugging purposes. Therefore the | |
62 | * timestamp is maintained as a 64-bit integer, giving around six million years | |
63 | * before wrapping, and truncated to 32 bits when transmitting. | |
64 | */ | |
65 | static uint64_t rtp_time; | |
66 | ||
67 | /** @brief RTP base timestamp | |
68 | * | |
69 | * This is the real time correspoding to an @ref rtp_time of 0. It is used | |
70 | * to recalculate the timestamp after idle periods. | |
71 | */ | |
72 | static struct timeval rtp_time_0; | |
73 | ||
74 | /** @brief RTP packet sequence number */ | |
75 | static uint16_t rtp_seq; | |
76 | ||
77 | /** @brief RTP SSRC */ | |
78 | static uint32_t rtp_id; | |
79 | ||
80 | /** @brief Error counter */ | |
81 | static int audio_errors; | |
82 | ||
83 | /** @brief Network backend initialization */ | |
84 | static void network_init(void) { | |
85 | struct addrinfo *res, *sres; | |
86 | static const struct addrinfo pref = { | |
87 | 0, | |
88 | PF_INET, | |
89 | SOCK_DGRAM, | |
90 | IPPROTO_UDP, | |
91 | 0, | |
92 | 0, | |
93 | 0, | |
94 | 0 | |
95 | }; | |
96 | static const struct addrinfo prefbind = { | |
97 | AI_PASSIVE, | |
98 | PF_INET, | |
99 | SOCK_DGRAM, | |
100 | IPPROTO_UDP, | |
101 | 0, | |
102 | 0, | |
103 | 0, | |
104 | 0 | |
105 | }; | |
106 | static const int one = 1; | |
db2c19dc | 107 | int sndbuf, target_sndbuf = 131072; |
1c3f1e73 | 108 | socklen_t len; |
109 | char *sockname, *ssockname; | |
110 | ||
111 | res = get_address(&config->broadcast, &pref, &sockname); | |
112 | if(!res) exit(-1); | |
113 | if(config->broadcast_from.n) { | |
114 | sres = get_address(&config->broadcast_from, &prefbind, &ssockname); | |
115 | if(!sres) exit(-1); | |
116 | } else | |
117 | sres = 0; | |
118 | if((bfd = socket(res->ai_family, | |
119 | res->ai_socktype, | |
120 | res->ai_protocol)) < 0) | |
121 | fatal(errno, "error creating broadcast socket"); | |
6fba990c | 122 | if(multicast(res->ai_addr)) { |
23205f9c RK |
123 | /* Multicasting */ |
124 | switch(res->ai_family) { | |
125 | case PF_INET: { | |
126 | const int mttl = config->multicast_ttl; | |
127 | if(setsockopt(bfd, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, sizeof mttl) < 0) | |
128 | fatal(errno, "error setting IP_MULTICAST_TTL on multicast socket"); | |
61941295 RK |
129 | if(setsockopt(bfd, IPPROTO_IP, IP_MULTICAST_LOOP, |
130 | &config->multicast_loop, sizeof one) < 0) | |
131 | fatal(errno, "error setting IP_MULTICAST_LOOP on multicast socket"); | |
23205f9c RK |
132 | break; |
133 | } | |
134 | case PF_INET6: { | |
135 | const int mttl = config->multicast_ttl; | |
136 | if(setsockopt(bfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, | |
137 | &mttl, sizeof mttl) < 0) | |
138 | fatal(errno, "error setting IPV6_MULTICAST_HOPS on multicast socket"); | |
61941295 RK |
139 | if(setsockopt(bfd, IPPROTO_IP, IPV6_MULTICAST_LOOP, |
140 | &config->multicast_loop, sizeof (int)) < 0) | |
141 | fatal(errno, "error setting IPV6_MULTICAST_LOOP on multicast socket"); | |
23205f9c RK |
142 | break; |
143 | } | |
144 | default: | |
145 | fatal(0, "unsupported address family %d", res->ai_family); | |
146 | } | |
81b1bf12 | 147 | info("multicasting on %s", sockname); |
23205f9c | 148 | } else { |
db2c19dc | 149 | struct ifaddrs *ifs; |
81b1bf12 | 150 | |
db2c19dc RK |
151 | if(getifaddrs(&ifs) < 0) |
152 | fatal(errno, "error calling getifaddrs"); | |
153 | while(ifs) { | |
3aa6f359 RK |
154 | /* (At least on Darwin) IFF_BROADCAST might be set but ifa_broadaddr |
155 | * still a null pointer. It turns out that there's a subsequent entry | |
156 | * for he same interface which _does_ have ifa_broadaddr though... */ | |
db2c19dc | 157 | if((ifs->ifa_flags & IFF_BROADCAST) |
3aa6f359 | 158 | && ifs->ifa_broadaddr |
db2c19dc | 159 | && sockaddr_equal(ifs->ifa_broadaddr, res->ai_addr)) |
81b1bf12 | 160 | break; |
db2c19dc | 161 | ifs = ifs->ifa_next; |
81b1bf12 | 162 | } |
db2c19dc | 163 | if(ifs) { |
81b1bf12 RK |
164 | if(setsockopt(bfd, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) < 0) |
165 | fatal(errno, "error setting SO_BROADCAST on broadcast socket"); | |
db2c19dc | 166 | info("broadcasting on %s (%s)", sockname, ifs->ifa_name); |
81b1bf12 RK |
167 | } else |
168 | info("unicasting on %s", sockname); | |
23205f9c | 169 | } |
1c3f1e73 | 170 | len = sizeof sndbuf; |
171 | if(getsockopt(bfd, SOL_SOCKET, SO_SNDBUF, | |
172 | &sndbuf, &len) < 0) | |
173 | fatal(errno, "error getting SO_SNDBUF"); | |
174 | if(target_sndbuf > sndbuf) { | |
175 | if(setsockopt(bfd, SOL_SOCKET, SO_SNDBUF, | |
176 | &target_sndbuf, sizeof target_sndbuf) < 0) | |
177 | error(errno, "error setting SO_SNDBUF to %d", target_sndbuf); | |
178 | else | |
179 | info("changed socket send buffer size from %d to %d", | |
180 | sndbuf, target_sndbuf); | |
181 | } else | |
182 | info("default socket send buffer is %d", | |
183 | sndbuf); | |
184 | /* We might well want to set additional broadcast- or multicast-related | |
185 | * options here */ | |
186 | if(sres && bind(bfd, sres->ai_addr, sres->ai_addrlen) < 0) | |
187 | fatal(errno, "error binding broadcast socket to %s", ssockname); | |
188 | if(connect(bfd, res->ai_addr, res->ai_addrlen) < 0) | |
189 | fatal(errno, "error connecting broadcast socket to %s", sockname); | |
190 | /* Select an SSRC */ | |
191 | gcry_randomize(&rtp_id, sizeof rtp_id, GCRY_STRONG_RANDOM); | |
1c3f1e73 | 192 | } |
193 | ||
194 | /** @brief Play over the network */ | |
195 | static size_t network_play(size_t frames) { | |
196 | struct rtp_header header; | |
197 | struct iovec vec[2]; | |
6d2d327c | 198 | size_t bytes = frames * bpf, written_frames; |
1c3f1e73 | 199 | int written_bytes; |
200 | /* We transmit using RTP (RFC3550) and attempt to conform to the internet | |
201 | * AVT profile (RFC3551). */ | |
202 | ||
23741390 RK |
203 | /* If we're starting then initialize the base time */ |
204 | if(!rtp_time) | |
205 | xgettimeofday(&rtp_time_0, 0); | |
1c3f1e73 | 206 | if(idled) { |
207 | /* There may have been a gap. Fix up the RTP time accordingly. */ | |
208 | struct timeval now; | |
209 | uint64_t delta; | |
210 | uint64_t target_rtp_time; | |
211 | ||
212 | /* Find the current time */ | |
213 | xgettimeofday(&now, 0); | |
214 | /* Find the number of microseconds elapsed since rtp_time=0 */ | |
215 | delta = tvsub_us(now, rtp_time_0); | |
23741390 RK |
216 | if(delta > UINT64_MAX / 88200) |
217 | fatal(0, "rtp_time=%llu now=%ld.%06ld rtp_time_0=%ld.%06ld delta=%llu (%lld)", | |
218 | rtp_time, | |
219 | (long)now.tv_sec, (long)now.tv_usec, | |
220 | (long)rtp_time_0.tv_sec, (long)rtp_time_0.tv_usec, | |
221 | delta, delta); | |
6d2d327c RK |
222 | target_rtp_time = (delta * config->sample_format.rate |
223 | * config->sample_format.channels) / 1000000; | |
1c3f1e73 | 224 | /* Overflows at ~6 years uptime with 44100Hz stereo */ |
225 | ||
226 | /* rtp_time is the number of samples we've played. NB that we play | |
227 | * RTP_AHEAD_MS ahead of ourselves, so it may legitimately be ahead of | |
228 | * the value we deduce from time comparison. | |
229 | * | |
230 | * Suppose we have 1s track started at t=0, and another track begins to | |
24d6fd25 RK |
231 | * play at t=2s. Suppose 44100Hz stereo. We send 1s of audio over the |
232 | * next (about) one second, giving rtp_time=88200. rtp_time stops at this | |
233 | * point. | |
1c3f1e73 | 234 | * |
235 | * At t=2s we'll have calculated target_rtp_time=176400. In this case we | |
236 | * set rtp_time=176400 and the player can correctly conclude that it | |
237 | * should leave 1s between the tracks. | |
238 | * | |
24d6fd25 RK |
239 | * It's never right to reduce rtp_time, for that would imply packets with |
240 | * overlapping timestamp ranges, which does not make sense. | |
1c3f1e73 | 241 | */ |
242 | target_rtp_time &= ~(uint64_t)1; /* stereo! */ | |
243 | if(target_rtp_time > rtp_time) { | |
244 | /* More time has elapsed than we've transmitted samples. That implies | |
245 | * we've been 'sending' silence. */ | |
246 | info("advancing rtp_time by %"PRIu64" samples", | |
247 | target_rtp_time - rtp_time); | |
248 | rtp_time = target_rtp_time; | |
249 | } else if(target_rtp_time < rtp_time) { | |
24d6fd25 RK |
250 | info("would reverse rtp_time by %"PRIu64" samples", |
251 | rtp_time - target_rtp_time); | |
1c3f1e73 | 252 | } |
253 | } | |
254 | header.vpxcc = 2 << 6; /* V=2, P=0, X=0, CC=0 */ | |
255 | header.seq = htons(rtp_seq++); | |
256 | header.timestamp = htonl((uint32_t)rtp_time); | |
257 | header.ssrc = rtp_id; | |
258 | header.mpt = (idled ? 0x80 : 0x00) | 10; | |
259 | /* 10 = L16 = 16-bit x 2 x 44100KHz. We ought to deduce this value from | |
260 | * the sample rate (in a library somewhere so that configuration.c can rule | |
261 | * out invalid rates). | |
262 | */ | |
263 | idled = 0; | |
264 | if(bytes > NETWORK_BYTES - sizeof header) { | |
265 | bytes = NETWORK_BYTES - sizeof header; | |
266 | /* Always send a whole number of frames */ | |
6d2d327c | 267 | bytes -= bytes % bpf; |
1c3f1e73 | 268 | } |
269 | /* "The RTP clock rate used for generating the RTP timestamp is independent | |
270 | * of the number of channels and the encoding; it equals the number of | |
271 | * sampling periods per second. For N-channel encodings, each sampling | |
272 | * period (say, 1/8000 of a second) generates N samples. (This terminology | |
273 | * is standard, but somewhat confusing, as the total number of samples | |
274 | * generated per second is then the sampling rate times the channel | |
275 | * count.)" | |
276 | */ | |
277 | vec[0].iov_base = (void *)&header; | |
278 | vec[0].iov_len = sizeof header; | |
279 | vec[1].iov_base = playing->buffer + playing->start; | |
280 | vec[1].iov_len = bytes; | |
281 | do { | |
282 | written_bytes = writev(bfd, vec, 2); | |
283 | } while(written_bytes < 0 && errno == EINTR); | |
284 | if(written_bytes < 0) { | |
285 | error(errno, "error transmitting audio data"); | |
286 | ++audio_errors; | |
287 | if(audio_errors == 10) | |
288 | fatal(0, "too many audio errors"); | |
289 | return 0; | |
290 | } else | |
291 | audio_errors /= 2; | |
292 | written_bytes -= sizeof (struct rtp_header); | |
6d2d327c | 293 | written_frames = written_bytes / bpf; |
1c3f1e73 | 294 | /* Advance RTP's notion of the time */ |
6d2d327c | 295 | rtp_time += written_frames * config->sample_format.channels; |
1c3f1e73 | 296 | return written_frames; |
297 | } | |
298 | ||
299 | static int bfd_slot; | |
300 | ||
301 | /** @brief Set up poll array for network play */ | |
e84fb5f0 | 302 | static void network_beforepoll(int *timeoutp) { |
1c3f1e73 | 303 | struct timeval now; |
304 | uint64_t target_us; | |
305 | uint64_t target_rtp_time; | |
e84fb5f0 RK |
306 | const int64_t samples_per_second = config->sample_format.rate |
307 | * config->sample_format.channels; | |
e84fb5f0 | 308 | int64_t lead, ahead_ms; |
1c3f1e73 | 309 | |
310 | /* If we're starting then initialize the base time */ | |
311 | if(!rtp_time) | |
312 | xgettimeofday(&rtp_time_0, 0); | |
24d6fd25 | 313 | /* We send audio data whenever we would otherwise get behind */ |
1c3f1e73 | 314 | xgettimeofday(&now, 0); |
315 | target_us = tvsub_us(now, rtp_time_0); | |
23741390 RK |
316 | if(target_us > UINT64_MAX / 88200) |
317 | fatal(0, "rtp_time=%llu rtp_time_0=%ld.%06ld now=%ld.%06ld target_us=%llu (%lld)\n", | |
318 | rtp_time, | |
319 | (long)rtp_time_0.tv_sec, (long)rtp_time_0.tv_usec, | |
320 | (long)now.tv_sec, (long)now.tv_usec, | |
321 | target_us, target_us); | |
1c3f1e73 | 322 | target_rtp_time = (target_us * config->sample_format.rate |
323 | * config->sample_format.channels) | |
324 | / 1000000; | |
24d6fd25 | 325 | /* Lead is how far ahead we are */ |
e84fb5f0 | 326 | lead = rtp_time - target_rtp_time; |
24d6fd25 RK |
327 | if(lead <= 0) |
328 | /* We're behind or even, so we'll need to write as soon as we can */ | |
1c3f1e73 | 329 | bfd_slot = addfd(bfd, POLLOUT); |
e84fb5f0 | 330 | else { |
24d6fd25 RK |
331 | /* We've ahead, we can afford to wait a bit even if the IP stack thinks it |
332 | * can accept more. */ | |
333 | ahead_ms = 1000 * lead / samples_per_second; | |
e84fb5f0 RK |
334 | if(ahead_ms < *timeoutp) |
335 | *timeoutp = ahead_ms; | |
336 | } | |
1c3f1e73 | 337 | } |
338 | ||
339 | /** @brief Process poll() results for network play */ | |
340 | static int network_ready(void) { | |
341 | if(fds[bfd_slot].revents & (POLLOUT | POLLERR)) | |
342 | return 1; | |
343 | else | |
344 | return 0; | |
345 | } | |
346 | ||
347 | const struct speaker_backend network_backend = { | |
348 | BACKEND_NETWORK, | |
6d2d327c | 349 | 0, |
1c3f1e73 | 350 | network_init, |
351 | 0, /* activate */ | |
352 | network_play, | |
353 | 0, /* deactivate */ | |
354 | network_beforepoll, | |
355 | network_ready | |
356 | }; | |
357 | ||
358 | /* | |
359 | Local Variables: | |
360 | c-basic-offset:2 | |
361 | comment-column:40 | |
362 | fill-column:79 | |
363 | indent-tabs-mode:nil | |
364 | End: | |
365 | */ |