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