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>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
42 /** @brief Bytes to send per network packet
44 * This is the maximum number of bytes we pass to write(2); to determine actual
45 * packet sizes, add a UDP header and an IP header (and a link layer header if
46 * it's the link layer size you care about).
48 * Don't make this too big or arithmetic will start to overflow.
50 #define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/)
52 /** @brief RTP payload type */
53 static int rtp_payload;
55 /** @brief RTP output socket */
58 /** @brief RTP SSRC */
59 static uint32_t rtp_id;
61 /** @brief RTP sequence number */
62 static uint16_t rtp_sequence;
64 /** @brief Network error count
66 * If too many errors occur in too short a time, we give up.
68 static int rtp_errors;
70 /** @brief Delay threshold in microseconds
72 * rtp_play() never attempts to introduce a delay shorter than this.
74 static int64_t rtp_delay_threshold;
76 static const char *const rtp_options[] = {
78 "rtp-destination-port",
87 static size_t rtp_play(void *buffer, size_t nsamples) {
88 struct rtp_header header;
91 /* We do as much work as possible before checking what time it is */
93 header.vpxcc = 2 << 6; /* V=2, P=0, X=0, CC=0 */
94 header.seq = htons(rtp_sequence++);
96 header.mpt = (uaudio_schedule_reactivated ? 0x80 : 0x00) | rtp_payload;
98 /* Convert samples to network byte order */
99 uint16_t *u = buffer, *const limit = u + nsamples;
105 vec[0].iov_base = (void *)&header;
106 vec[0].iov_len = sizeof header;
107 vec[1].iov_base = buffer;
108 vec[1].iov_len = nsamples * uaudio_sample_size;
109 uaudio_schedule_synchronize();
110 header.timestamp = htonl((uint32_t)uaudio_schedule_timestamp);
113 written_bytes = writev(rtp_fd, vec, 2);
114 } while(written_bytes < 0 && errno == EINTR);
115 if(written_bytes < 0) {
116 error(errno, "error transmitting audio data");
119 fatal(0, "too many audio tranmission errors");
122 rtp_errors /= 2; /* gradual decay */
123 written_bytes -= sizeof (struct rtp_header);
124 const size_t written_samples = written_bytes / uaudio_sample_size;
125 uaudio_schedule_update(written_samples);
126 return written_samples;
129 static void rtp_open(void) {
130 struct addrinfo *res, *sres;
131 static const struct addrinfo pref = {
133 .ai_family = PF_INET,
134 .ai_socktype = SOCK_DGRAM,
135 .ai_protocol = IPPROTO_UDP,
137 static const struct addrinfo prefbind = {
138 .ai_flags = AI_PASSIVE,
139 .ai_family = PF_INET,
140 .ai_socktype = SOCK_DGRAM,
141 .ai_protocol = IPPROTO_UDP,
143 static const int one = 1;
144 int sndbuf, target_sndbuf = 131072;
146 char *sockname, *ssockname;
147 struct stringlist dst, src;
149 /* Get configuration */
151 dst.s = xcalloc(2, sizeof *dst.s);
152 dst.s[0] = uaudio_get("rtp-destination", NULL);
153 dst.s[1] = uaudio_get("rtp-destination-port", NULL);
155 src.s = xcalloc(2, sizeof *dst.s);
156 src.s[0] = uaudio_get("rtp-source", NULL);
157 src.s[1] = uaudio_get("rtp-source-port", NULL);
159 fatal(0, "'rtp-destination' not set");
161 fatal(0, "'rtp-destination-port' not set");
164 fatal(0, "'rtp-source-port' not set");
168 rtp_delay_threshold = atoi(uaudio_get("rtp-delay-threshold", "1000"));
169 /* ...microseconds */
171 /* Resolve addresses */
172 res = get_address(&dst, &pref, &sockname);
175 sres = get_address(&src, &prefbind, &ssockname);
179 /* Create the socket */
180 if((rtp_fd = socket(res->ai_family,
182 res->ai_protocol)) < 0)
183 fatal(errno, "error creating broadcast socket");
184 if(multicast(res->ai_addr)) {
185 /* Enable multicast options */
186 const int ttl = atoi(uaudio_get("multicast-ttl", "1"));
187 const int loop = !strcmp(uaudio_get("multicast-loop", "yes"), "yes");
188 switch(res->ai_family) {
190 if(setsockopt(rtp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
191 &ttl, sizeof ttl) < 0)
192 fatal(errno, "error setting IP_MULTICAST_TTL on multicast socket");
193 if(setsockopt(rtp_fd, IPPROTO_IP, IP_MULTICAST_LOOP,
194 &loop, sizeof loop) < 0)
195 fatal(errno, "error setting IP_MULTICAST_LOOP on multicast socket");
199 if(setsockopt(rtp_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
200 &ttl, sizeof ttl) < 0)
201 fatal(errno, "error setting IPV6_MULTICAST_HOPS on multicast socket");
202 if(setsockopt(rtp_fd, IPPROTO_IP, IPV6_MULTICAST_LOOP,
203 &loop, sizeof loop) < 0)
204 fatal(errno, "error setting IPV6_MULTICAST_LOOP on multicast socket");
208 fatal(0, "unsupported address family %d", res->ai_family);
210 info("multicasting on %s TTL=%d loop=%s",
211 sockname, ttl, loop ? "yes" : "no");
215 if(getifaddrs(&ifs) < 0)
216 fatal(errno, "error calling getifaddrs");
218 /* (At least on Darwin) IFF_BROADCAST might be set but ifa_broadaddr
219 * still a null pointer. It turns out that there's a subsequent entry
220 * for he same interface which _does_ have ifa_broadaddr though... */
221 if((ifs->ifa_flags & IFF_BROADCAST)
222 && ifs->ifa_broadaddr
223 && sockaddr_equal(ifs->ifa_broadaddr, res->ai_addr))
228 if(setsockopt(rtp_fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) < 0)
229 fatal(errno, "error setting SO_BROADCAST on broadcast socket");
230 info("broadcasting on %s (%s)", sockname, ifs->ifa_name);
232 info("unicasting on %s", sockname);
234 /* Enlarge the socket buffer */
236 if(getsockopt(rtp_fd, SOL_SOCKET, SO_SNDBUF,
238 fatal(errno, "error getting SO_SNDBUF");
239 if(target_sndbuf > sndbuf) {
240 if(setsockopt(rtp_fd, SOL_SOCKET, SO_SNDBUF,
241 &target_sndbuf, sizeof target_sndbuf) < 0)
242 error(errno, "error setting SO_SNDBUF to %d", target_sndbuf);
244 info("changed socket send buffer size from %d to %d",
245 sndbuf, target_sndbuf);
247 info("default socket send buffer is %d",
249 /* We might well want to set additional broadcast- or multicast-related
251 if(sres && bind(rtp_fd, sres->ai_addr, sres->ai_addrlen) < 0)
252 fatal(errno, "error binding broadcast socket to %s", ssockname);
253 if(connect(rtp_fd, res->ai_addr, res->ai_addrlen) < 0)
254 fatal(errno, "error connecting broadcast socket to %s", sockname);
257 static void rtp_start(uaudio_callback *callback,
259 /* We only support L16 (but we do stereo and mono and will convert sign) */
260 if(uaudio_channels == 2
262 && uaudio_rate == 44100)
264 else if(uaudio_channels == 1
266 && uaudio_rate == 44100)
269 fatal(0, "asked for %d/%d/%d 16/44100/1 and 16/44100/2",
270 uaudio_bits, uaudio_rate, uaudio_channels);
271 /* Various fields are required to have random initial values by RFC3550. The
272 * packet contents are highly public so there's no point asking for very
273 * strong randomness. */
274 gcry_create_nonce(&rtp_id, sizeof rtp_id);
275 gcry_create_nonce(&rtp_sequence, sizeof rtp_sequence);
277 uaudio_schedule_init();
278 uaudio_thread_start(callback,
281 256 / uaudio_sample_size,
282 (NETWORK_BYTES - sizeof(struct rtp_header))
283 / uaudio_sample_size);
286 static void rtp_stop(void) {
287 uaudio_thread_stop();
292 static void rtp_activate(void) {
293 uaudio_schedule_reactivated = 1;
294 uaudio_thread_activate();
297 static void rtp_deactivate(void) {
298 uaudio_thread_deactivate();
301 const struct uaudio uaudio_rtp = {
303 .options = rtp_options,
306 .activate = rtp_activate,
307 .deactivate = rtp_deactivate