2 * This file is part of DisOrder.
3 * Copyright (C) 2009 Richard Kettlewell
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.
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.
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/>.
18 /** @file lib/uaudio-rtp.c
19 * @brief Support for RTP network play backend */
23 #include <sys/socket.h>
40 /** @brief Bytes to send per network packet
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).
46 * Don't make this too big or arithmetic will start to overflow.
48 #define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/)
50 /** @brief RTP payload type */
51 static int rtp_payload;
53 /** @brief RTP output socket */
56 /** @brief RTP SSRC */
57 static uint32_t rtp_id;
59 /** @brief RTP sequence number */
60 static uint16_t rtp_sequence;
62 /** @brief Network error count
64 * If too many errors occur in too short a time, we give up.
66 static int rtp_errors;
68 /** @brief Delay threshold in microseconds
70 * rtp_play() never attempts to introduce a delay shorter than this.
72 static int64_t rtp_delay_threshold;
74 static const char *const rtp_options[] = {
76 "rtp-destination-port",
85 static size_t rtp_play(void *buffer, size_t nsamples) {
86 struct rtp_header header;
89 /* We do as much work as possible before checking what time it is */
91 header.vpxcc = 2 << 6; /* V=2, P=0, X=0, CC=0 */
92 header.seq = htons(rtp_sequence++);
94 header.mpt = (uaudio_schedule_reactivated ? 0x80 : 0x00) | rtp_payload;
96 /* Convert samples to network byte order */
97 uint16_t *u = buffer, *const limit = u + nsamples;
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;
107 uaudio_schedule_synchronize();
108 header.timestamp = htonl((uint32_t)uaudio_schedule_timestamp);
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");
117 fatal(0, "too many audio tranmission errors");
120 rtp_errors /= 2; /* gradual decay */
121 written_bytes -= sizeof (struct rtp_header);
122 const size_t written_samples = written_bytes / uaudio_sample_size;
123 uaudio_schedule_update(written_samples);
124 return written_samples;
127 static void rtp_open(void) {
128 struct addrinfo *res, *sres;
129 static const struct addrinfo pref = {
131 .ai_family = PF_INET,
132 .ai_socktype = SOCK_DGRAM,
133 .ai_protocol = IPPROTO_UDP,
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,
141 static const int one = 1;
142 int sndbuf, target_sndbuf = 131072;
144 char *sockname, *ssockname;
145 struct stringlist dst, src;
148 /* Get configuration */
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");
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");
158 fatal(0, "'rtp-destination' not set");
160 fatal(0, "'rtp-destination-port' not set");
163 fatal(0, "'rtp-source-port' not set");
167 if((delay = uaudio_get("rtp-delay-threshold")))
168 rtp_delay_threshold = atoi(delay);
170 rtp_delay_threshold = 1000; /* microseconds */
172 /* Resolve addresses */
173 res = get_address(&dst, &pref, &sockname);
176 sres = get_address(&src, &prefbind, &ssockname);
180 /* Create the socket */
181 if((rtp_fd = socket(res->ai_family,
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) {
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");
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");
211 fatal(0, "unsupported address family %d", res->ai_family);
213 info("multicasting on %s TTL=%d loop=%s",
214 sockname, ttl, loop ? "yes" : "no");
218 if(getifaddrs(&ifs) < 0)
219 fatal(errno, "error calling getifaddrs");
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))
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);
235 info("unicasting on %s", sockname);
237 /* Enlarge the socket buffer */
239 if(getsockopt(rtp_fd, SOL_SOCKET, SO_SNDBUF,
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);
247 info("changed socket send buffer size from %d to %d",
248 sndbuf, target_sndbuf);
250 info("default socket send buffer is %d",
252 /* We might well want to set additional broadcast- or multicast-related
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);
260 static void rtp_start(uaudio_callback *callback,
262 /* We only support L16 (but we do stereo and mono and will convert sign) */
263 if(uaudio_channels == 2
265 && uaudio_rate == 44100)
267 else if(uaudio_channels == 1
269 && uaudio_rate == 44100)
272 fatal(0, "asked for %d/%d/%d 16/44100/1 and 16/44100/2",
273 uaudio_bits, uaudio_rate, uaudio_channels);
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);
280 uaudio_schedule_init();
281 uaudio_thread_start(callback,
284 256 / uaudio_sample_size,
285 (NETWORK_BYTES - sizeof(struct rtp_header))
286 / uaudio_sample_size);
289 static void rtp_stop(void) {
290 uaudio_thread_stop();
295 static void rtp_activate(void) {
296 uaudio_schedule_reactivated = 1;
297 uaudio_thread_activate();
300 static void rtp_deactivate(void) {
301 uaudio_thread_deactivate();
304 const struct uaudio uaudio_rtp = {
306 .options = rtp_options,
309 .activate = rtp_activate,
310 .deactivate = rtp_deactivate