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 Base for timestamp */
63 static uint32_t rtp_base
;
65 /** @brief RTP sequence number */
66 static uint16_t rtp_sequence
;
68 /** @brief Network error count
70 * If too many errors occur in too short a time, we give up.
72 static int rtp_errors
;
74 /** @brief Set while paused */
75 static volatile int rtp_paused
;
77 static const char *const rtp_options
[] = {
79 "rtp-destination-port",
87 static void rtp_get_netconfig(const char *af
,
90 struct netaddress
*na
) {
93 vec
[0] = uaudio_get(af
, NULL
);
94 vec
[1] = uaudio_get(addr
, NULL
);
95 vec
[2] = uaudio_get(port
, NULL
);
99 if(netaddress_parse(na
, 3, vec
))
100 disorder_fatal(0, "invalid RTP address");
103 static void rtp_set_netconfig(const char *af
,
106 const struct netaddress
*na
) {
107 uaudio_set(af
, NULL
);
108 uaudio_set(addr
, NULL
);
109 uaudio_set(port
, NULL
);
114 netaddress_format(na
, &nvec
, &vec
);
116 uaudio_set(af
, vec
[0]);
120 uaudio_set(addr
, vec
[1]);
124 uaudio_set(port
, vec
[2]);
131 static size_t rtp_play(void *buffer
, size_t nsamples
, unsigned flags
) {
132 struct rtp_header header
;
136 if(flags
& (UAUDIO_PAUSE
|UAUDIO_RESUME
))
137 fprintf(stderr
, "rtp_play %zu samples%s%s%s%s\n", nsamples
,
138 flags
& UAUDIO_PAUSE ?
" UAUDIO_PAUSE" : "",
139 flags
& UAUDIO_RESUME ?
" UAUDIO_RESUME" : "",
140 flags
& UAUDIO_PLAYING ?
" UAUDIO_PLAYING" : "",
141 flags
& UAUDIO_PAUSED ?
" UAUDIO_PAUSED" : "");
144 /* We do as much work as possible before checking what time it is */
145 /* Fill out header */
146 header
.vpxcc
= 2 << 6; /* V=2, P=0, X=0, CC=0 */
147 header
.seq
= htons(rtp_sequence
++);
148 header
.ssrc
= rtp_id
;
149 header
.mpt
= rtp_payload
;
150 /* If we've come out of a pause, set the marker bit */
151 if(flags
& UAUDIO_RESUME
)
154 /* Convert samples to network byte order */
155 uint16_t *u
= buffer
, *const limit
= u
+ nsamples
;
161 vec
[0].iov_base
= (void *)&header
;
162 vec
[0].iov_len
= sizeof header
;
163 vec
[1].iov_base
= buffer
;
164 vec
[1].iov_len
= nsamples
* uaudio_sample_size
;
165 const uint32_t timestamp
= uaudio_schedule_sync();
166 header
.timestamp
= htonl(rtp_base
+ (uint32_t)timestamp
);
167 /* If we're paused don't actually end a packet, we just pretend */
168 if(flags
& UAUDIO_PAUSED
) {
169 uaudio_schedule_sent(nsamples
);
174 written_bytes
= writev(rtp_fd
, vec
, 2);
175 } while(written_bytes
< 0 && errno
== EINTR
);
176 if(written_bytes
< 0) {
177 disorder_error(errno
, "error transmitting audio data");
180 disorder_fatal(0, "too many audio tranmission errors");
183 rtp_errors
/= 2; /* gradual decay */
184 /* TODO what can we sensibly do about short writes here? Really that's just
185 * an error and we ought to be using smaller packets. */
186 uaudio_schedule_sent(nsamples
);
190 static void rtp_open(void) {
191 struct addrinfo
*res
, *sres
;
192 static const int one
= 1;
193 int sndbuf
, target_sndbuf
= 131072;
195 struct netaddress dst
[1], src
[1];
197 /* Get configuration */
198 rtp_get_netconfig("rtp-destination-af",
200 "rtp-destination-port",
202 rtp_get_netconfig("rtp-source-af",
206 /* ...microseconds */
208 /* Resolve addresses */
209 res
= netaddress_resolve(dst
, 0, IPPROTO_UDP
);
213 sres
= netaddress_resolve(src
, 1, IPPROTO_UDP
);
218 /* Create the socket */
219 if((rtp_fd
= socket(res
->ai_family
,
221 res
->ai_protocol
)) < 0)
222 disorder_fatal(errno
, "error creating broadcast socket");
223 if(multicast(res
->ai_addr
)) {
224 /* Enable multicast options */
225 const int ttl
= atoi(uaudio_get("multicast-ttl", "1"));
226 const int loop
= !strcmp(uaudio_get("multicast-loop", "yes"), "yes");
227 switch(res
->ai_family
) {
229 if(setsockopt(rtp_fd
, IPPROTO_IP
, IP_MULTICAST_TTL
,
230 &ttl
, sizeof ttl
) < 0)
231 disorder_fatal(errno
, "error setting IP_MULTICAST_TTL on multicast socket");
232 if(setsockopt(rtp_fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
233 &loop
, sizeof loop
) < 0)
234 disorder_fatal(errno
, "error setting IP_MULTICAST_LOOP on multicast socket");
238 if(setsockopt(rtp_fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
239 &ttl
, sizeof ttl
) < 0)
240 disorder_fatal(errno
, "error setting IPV6_MULTICAST_HOPS on multicast socket");
241 if(setsockopt(rtp_fd
, IPPROTO_IP
, IPV6_MULTICAST_LOOP
,
242 &loop
, sizeof loop
) < 0)
243 disorder_fatal(errno
, "error setting IPV6_MULTICAST_LOOP on multicast socket");
247 disorder_fatal(0, "unsupported address family %d", res
->ai_family
);
249 disorder_info("multicasting on %s TTL=%d loop=%s",
250 format_sockaddr(res
->ai_addr
), ttl
, loop ?
"yes" : "no");
254 if(getifaddrs(&ifs
) < 0)
255 disorder_fatal(errno
, "error calling getifaddrs");
257 /* (At least on Darwin) IFF_BROADCAST might be set but ifa_broadaddr
258 * still a null pointer. It turns out that there's a subsequent entry
259 * for he same interface which _does_ have ifa_broadaddr though... */
260 if((ifs
->ifa_flags
& IFF_BROADCAST
)
261 && ifs
->ifa_broadaddr
262 && sockaddr_equal(ifs
->ifa_broadaddr
, res
->ai_addr
))
267 if(setsockopt(rtp_fd
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof one
) < 0)
268 disorder_fatal(errno
, "error setting SO_BROADCAST on broadcast socket");
269 disorder_info("broadcasting on %s (%s)",
270 format_sockaddr(res
->ai_addr
), ifs
->ifa_name
);
272 disorder_info("unicasting on %s", format_sockaddr(res
->ai_addr
));
274 /* Enlarge the socket buffer */
276 if(getsockopt(rtp_fd
, SOL_SOCKET
, SO_SNDBUF
,
278 disorder_fatal(errno
, "error getting SO_SNDBUF");
279 if(target_sndbuf
> sndbuf
) {
280 if(setsockopt(rtp_fd
, SOL_SOCKET
, SO_SNDBUF
,
281 &target_sndbuf
, sizeof target_sndbuf
) < 0)
282 disorder_error(errno
, "error setting SO_SNDBUF to %d", target_sndbuf
);
284 disorder_info("changed socket send buffer size from %d to %d",
285 sndbuf
, target_sndbuf
);
287 disorder_info("default socket send buffer is %d", sndbuf
);
288 /* We might well want to set additional broadcast- or multicast-related
290 if(sres
&& bind(rtp_fd
, sres
->ai_addr
, sres
->ai_addrlen
) < 0)
291 disorder_fatal(errno
, "error binding broadcast socket to %s",
292 format_sockaddr(sres
->ai_addr
));
293 if(connect(rtp_fd
, res
->ai_addr
, res
->ai_addrlen
) < 0)
294 disorder_fatal(errno
, "error connecting broadcast socket to %s",
295 format_sockaddr(res
->ai_addr
));
298 static void rtp_start(uaudio_callback
*callback
,
300 /* We only support L16 (but we do stereo and mono and will convert sign) */
301 if(uaudio_channels
== 2
303 && uaudio_rate
== 44100)
305 else if(uaudio_channels
== 1
307 && uaudio_rate
== 44100)
310 disorder_fatal(0, "asked for %d/%d/%d 16/44100/1 and 16/44100/2",
311 uaudio_bits
, uaudio_rate
, uaudio_channels
);
312 /* Various fields are required to have random initial values by RFC3550. The
313 * packet contents are highly public so there's no point asking for very
314 * strong randomness. */
315 gcry_create_nonce(&rtp_id
, sizeof rtp_id
);
316 gcry_create_nonce(&rtp_base
, sizeof rtp_base
);
317 gcry_create_nonce(&rtp_sequence
, sizeof rtp_sequence
);
319 uaudio_schedule_init();
320 uaudio_thread_start(callback
,
323 256 / uaudio_sample_size
,
324 (NETWORK_BYTES
- sizeof(struct rtp_header
))
325 / uaudio_sample_size
,
329 static void rtp_stop(void) {
330 uaudio_thread_stop();
335 static void rtp_configure(void) {
338 rtp_set_netconfig("rtp-destination-af",
340 "rtp-destination-port", &config
->broadcast
);
341 rtp_set_netconfig("rtp-source-af",
343 "rtp-source-port", &config
->broadcast_from
);
344 snprintf(buffer
, sizeof buffer
, "%ld", config
->multicast_ttl
);
345 uaudio_set("multicast-ttl", buffer
);
346 uaudio_set("multicast-loop", config
->multicast_loop ?
"yes" : "no");
349 const struct uaudio uaudio_rtp
= {
351 .options
= rtp_options
,
354 .activate
= uaudio_thread_activate
,
355 .deactivate
= uaudio_thread_deactivate
,
356 .configure
= rtp_configure
,