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>
41 #include "configuration.h"
43 /** @brief Bytes to send per network packet
45 * This is the maximum number of bytes we pass to write(2); to determine actual
46 * packet sizes, add a UDP header and an IP header (and a link layer header if
47 * it's the link layer size you care about).
49 * Don't make this too big or arithmetic will start to overflow.
51 #define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/)
53 /** @brief RTP payload type */
54 static int rtp_payload
;
56 /** @brief RTP output socket */
59 /** @brief RTP SSRC */
60 static uint32_t rtp_id
;
62 /** @brief RTP sequence number */
63 static uint16_t rtp_sequence
;
65 /** @brief Network error count
67 * If too many errors occur in too short a time, we give up.
69 static int rtp_errors
;
71 /** @brief Delay threshold in microseconds
73 * rtp_play() never attempts to introduce a delay shorter than this.
75 static int64_t rtp_delay_threshold
;
77 static const char *const rtp_options
[] = {
79 "rtp-destination-port",
88 static size_t rtp_play(void *buffer
, size_t nsamples
) {
89 struct rtp_header header
;
92 /* We do as much work as possible before checking what time it is */
94 header
.vpxcc
= 2 << 6; /* V=2, P=0, X=0, CC=0 */
95 header
.seq
= htons(rtp_sequence
++);
97 header
.mpt
= (uaudio_schedule_reactivated ?
0x80 : 0x00) | rtp_payload
;
99 /* Convert samples to network byte order */
100 uint16_t *u
= buffer
, *const limit
= u
+ nsamples
;
106 vec
[0].iov_base
= (void *)&header
;
107 vec
[0].iov_len
= sizeof header
;
108 vec
[1].iov_base
= buffer
;
109 vec
[1].iov_len
= nsamples
* uaudio_sample_size
;
110 uaudio_schedule_synchronize();
111 header
.timestamp
= htonl((uint32_t)uaudio_schedule_timestamp
);
114 written_bytes
= writev(rtp_fd
, vec
, 2);
115 } while(written_bytes
< 0 && errno
== EINTR
);
116 if(written_bytes
< 0) {
117 error(errno
, "error transmitting audio data");
120 fatal(0, "too many audio tranmission errors");
123 rtp_errors
/= 2; /* gradual decay */
124 written_bytes
-= sizeof (struct rtp_header
);
125 const size_t written_samples
= written_bytes
/ uaudio_sample_size
;
126 uaudio_schedule_update(written_samples
);
127 return written_samples
;
130 static void rtp_open(void) {
131 struct addrinfo
*res
, *sres
;
132 static const struct addrinfo pref
= {
134 .ai_family
= PF_INET
,
135 .ai_socktype
= SOCK_DGRAM
,
136 .ai_protocol
= IPPROTO_UDP
,
138 static const struct addrinfo prefbind
= {
139 .ai_flags
= AI_PASSIVE
,
140 .ai_family
= PF_INET
,
141 .ai_socktype
= SOCK_DGRAM
,
142 .ai_protocol
= IPPROTO_UDP
,
144 static const int one
= 1;
145 int sndbuf
, target_sndbuf
= 131072;
147 char *sockname
, *ssockname
;
148 struct stringlist dst
, src
;
150 /* Get configuration */
152 dst
.s
= xcalloc(2, sizeof *dst
.s
);
153 dst
.s
[0] = uaudio_get("rtp-destination", NULL
);
154 dst
.s
[1] = uaudio_get("rtp-destination-port", NULL
);
156 src
.s
= xcalloc(2, sizeof *dst
.s
);
157 src
.s
[0] = uaudio_get("rtp-source", NULL
);
158 src
.s
[1] = uaudio_get("rtp-source-port", NULL
);
160 fatal(0, "'rtp-destination' not set");
162 fatal(0, "'rtp-destination-port' not set");
165 fatal(0, "'rtp-source-port' not set");
169 rtp_delay_threshold
= atoi(uaudio_get("rtp-delay-threshold", "1000"));
170 /* ...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 int ttl
= atoi(uaudio_get("multicast-ttl", "1"));
188 const int loop
= !strcmp(uaudio_get("multicast-loop", "yes"), "yes");
189 switch(res
->ai_family
) {
191 if(setsockopt(rtp_fd
, IPPROTO_IP
, IP_MULTICAST_TTL
,
192 &ttl
, sizeof ttl
) < 0)
193 fatal(errno
, "error setting IP_MULTICAST_TTL on multicast socket");
194 if(setsockopt(rtp_fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
195 &loop
, sizeof loop
) < 0)
196 fatal(errno
, "error setting IP_MULTICAST_LOOP on multicast socket");
200 if(setsockopt(rtp_fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
201 &ttl
, sizeof ttl
) < 0)
202 fatal(errno
, "error setting IPV6_MULTICAST_HOPS on multicast socket");
203 if(setsockopt(rtp_fd
, IPPROTO_IP
, IPV6_MULTICAST_LOOP
,
204 &loop
, sizeof loop
) < 0)
205 fatal(errno
, "error setting IPV6_MULTICAST_LOOP on multicast socket");
209 fatal(0, "unsupported address family %d", res
->ai_family
);
211 info("multicasting on %s TTL=%d loop=%s",
212 sockname
, ttl
, loop ?
"yes" : "no");
216 if(getifaddrs(&ifs
) < 0)
217 fatal(errno
, "error calling getifaddrs");
219 /* (At least on Darwin) IFF_BROADCAST might be set but ifa_broadaddr
220 * still a null pointer. It turns out that there's a subsequent entry
221 * for he same interface which _does_ have ifa_broadaddr though... */
222 if((ifs
->ifa_flags
& IFF_BROADCAST
)
223 && ifs
->ifa_broadaddr
224 && sockaddr_equal(ifs
->ifa_broadaddr
, res
->ai_addr
))
229 if(setsockopt(rtp_fd
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof one
) < 0)
230 fatal(errno
, "error setting SO_BROADCAST on broadcast socket");
231 info("broadcasting on %s (%s)", sockname
, ifs
->ifa_name
);
233 info("unicasting on %s", sockname
);
235 /* Enlarge the socket buffer */
237 if(getsockopt(rtp_fd
, SOL_SOCKET
, SO_SNDBUF
,
239 fatal(errno
, "error getting SO_SNDBUF");
240 if(target_sndbuf
> sndbuf
) {
241 if(setsockopt(rtp_fd
, SOL_SOCKET
, SO_SNDBUF
,
242 &target_sndbuf
, sizeof target_sndbuf
) < 0)
243 error(errno
, "error setting SO_SNDBUF to %d", target_sndbuf
);
245 info("changed socket send buffer size from %d to %d",
246 sndbuf
, target_sndbuf
);
248 info("default socket send buffer is %d",
250 /* We might well want to set additional broadcast- or multicast-related
252 if(sres
&& bind(rtp_fd
, sres
->ai_addr
, sres
->ai_addrlen
) < 0)
253 fatal(errno
, "error binding broadcast socket to %s", ssockname
);
254 if(connect(rtp_fd
, res
->ai_addr
, res
->ai_addrlen
) < 0)
255 fatal(errno
, "error connecting broadcast socket to %s", sockname
);
258 static void rtp_start(uaudio_callback
*callback
,
260 /* We only support L16 (but we do stereo and mono and will convert sign) */
261 if(uaudio_channels
== 2
263 && uaudio_rate
== 44100)
265 else if(uaudio_channels
== 1
267 && uaudio_rate
== 44100)
270 fatal(0, "asked for %d/%d/%d 16/44100/1 and 16/44100/2",
271 uaudio_bits
, uaudio_rate
, uaudio_channels
);
272 /* Various fields are required to have random initial values by RFC3550. The
273 * packet contents are highly public so there's no point asking for very
274 * strong randomness. */
275 gcry_create_nonce(&rtp_id
, sizeof rtp_id
);
276 gcry_create_nonce(&rtp_sequence
, sizeof rtp_sequence
);
278 uaudio_schedule_init();
279 uaudio_thread_start(callback
,
282 256 / uaudio_sample_size
,
283 (NETWORK_BYTES
- sizeof(struct rtp_header
))
284 / uaudio_sample_size
);
287 static void rtp_stop(void) {
288 uaudio_thread_stop();
293 static void rtp_activate(void) {
294 uaudio_schedule_reactivated
= 1;
295 uaudio_thread_activate();
298 static void rtp_deactivate(void) {
299 uaudio_thread_deactivate();
302 static void rtp_configure(void) {
305 uaudio_set("rtp-destination", config
->broadcast
.s
[0]);
306 uaudio_set("rtp-destination-port", config
->broadcast
.s
[1]);
307 if(config
->broadcast_from
.n
) {
308 uaudio_set("rtp-source", config
->broadcast_from
.s
[0]);
309 uaudio_set("rtp-source-port", config
->broadcast_from
.s
[0]);
311 uaudio_set("rtp-source", NULL
);
312 uaudio_set("rtp-source-port", NULL
);
314 snprintf(buffer
, sizeof buffer
, "%ld", config
->multicast_ttl
);
315 uaudio_set("multicast-ttl", buffer
);
316 uaudio_set("multicast-loop", config
->multicast_loop ?
"yes" : "no");
317 snprintf(buffer
, sizeof buffer
, "%ld", config
->rtp_delay_threshold
);
318 uaudio_set("delay-threshold", buffer
);
321 const struct uaudio uaudio_rtp
= {
323 .options
= rtp_options
,
326 .activate
= rtp_activate
,
327 .deactivate
= rtp_deactivate
,
328 .configure
= rtp_configure
,