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