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