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>
39 #include "configuration.h"
41 /** @brief Bytes to send per network packet
43 * This is the maximum number of bytes we pass to write(2); to determine actual
44 * packet sizes, add a UDP header and an IP header (and a link layer header if
45 * it's the link layer size you care about).
47 * Don't make this too big or arithmetic will start to overflow.
49 #define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/)
51 /** @brief RTP payload type */
52 static int rtp_payload
;
54 /** @brief RTP output socket */
57 /** @brief RTP SSRC */
58 static uint32_t rtp_id
;
60 /** @brief RTP sequence number */
61 static uint16_t rtp_sequence
;
63 /** @brief Network error count
65 * If too many errors occur in too short a time, we give up.
67 static int rtp_errors
;
69 /** @brief Delay threshold in microseconds
71 * rtp_play() never attempts to introduce a delay shorter than this.
73 static int64_t rtp_delay_threshold
;
75 static const char *const rtp_options
[] = {
77 "rtp-destination-port",
86 static size_t rtp_play(void *buffer
, size_t nsamples
) {
87 struct rtp_header header
;
90 /* We do as much work as possible before checking what time it is */
92 header
.vpxcc
= 2 << 6; /* V=2, P=0, X=0, CC=0 */
93 header
.seq
= htons(rtp_sequence
++);
95 header
.mpt
= (uaudio_schedule_reactivated ?
0x80 : 0x00) | rtp_payload
;
97 /* Convert samples to network byte order */
98 uint16_t *u
= buffer
, *const limit
= u
+ nsamples
;
104 vec
[0].iov_base
= (void *)&header
;
105 vec
[0].iov_len
= sizeof header
;
106 vec
[1].iov_base
= buffer
;
107 vec
[1].iov_len
= nsamples
* uaudio_sample_size
;
108 uaudio_schedule_synchronize();
109 header
.timestamp
= htonl((uint32_t)uaudio_schedule_timestamp
);
112 written_bytes
= writev(rtp_fd
, vec
, 2);
113 } while(written_bytes
< 0 && errno
== EINTR
);
114 if(written_bytes
< 0) {
115 error(errno
, "error transmitting audio data");
118 fatal(0, "too many audio tranmission errors");
121 rtp_errors
/= 2; /* gradual decay */
122 written_bytes
-= sizeof (struct rtp_header
);
123 const size_t written_samples
= written_bytes
/ uaudio_sample_size
;
124 uaudio_schedule_update(written_samples
);
125 return written_samples
;
128 static void rtp_open(void) {
129 struct addrinfo
*res
, *sres
;
130 static const struct addrinfo pref
= {
132 .ai_family
= PF_INET
,
133 .ai_socktype
= SOCK_DGRAM
,
134 .ai_protocol
= IPPROTO_UDP
,
136 static const struct addrinfo prefbind
= {
137 .ai_flags
= AI_PASSIVE
,
138 .ai_family
= PF_INET
,
139 .ai_socktype
= SOCK_DGRAM
,
140 .ai_protocol
= IPPROTO_UDP
,
142 static const int one
= 1;
143 int sndbuf
, target_sndbuf
= 131072;
145 char *sockname
, *ssockname
;
146 struct stringlist dst
, src
;
148 /* Get configuration */
150 dst
.s
= xcalloc(2, sizeof *dst
.s
);
151 dst
.s
[0] = uaudio_get("rtp-destination", NULL
);
152 dst
.s
[1] = uaudio_get("rtp-destination-port", NULL
);
154 src
.s
= xcalloc(2, sizeof *dst
.s
);
155 src
.s
[0] = uaudio_get("rtp-source", NULL
);
156 src
.s
[1] = uaudio_get("rtp-source-port", NULL
);
158 fatal(0, "'rtp-destination' not set");
160 fatal(0, "'rtp-destination-port' not set");
163 fatal(0, "'rtp-source-port' not set");
167 rtp_delay_threshold
= atoi(uaudio_get("rtp-delay-threshold", "1000"));
168 /* ...microseconds */
170 /* Resolve addresses */
171 res
= get_address(&dst
, &pref
, &sockname
);
174 sres
= get_address(&src
, &prefbind
, &ssockname
);
178 /* Create the socket */
179 if((rtp_fd
= socket(res
->ai_family
,
181 res
->ai_protocol
)) < 0)
182 fatal(errno
, "error creating broadcast socket");
183 if(multicast(res
->ai_addr
)) {
184 /* Enable multicast options */
185 const int ttl
= atoi(uaudio_get("multicast-ttl", "1"));
186 const int loop
= !strcmp(uaudio_get("multicast-loop", "yes"), "yes");
187 switch(res
->ai_family
) {
189 if(setsockopt(rtp_fd
, IPPROTO_IP
, IP_MULTICAST_TTL
,
190 &ttl
, sizeof ttl
) < 0)
191 fatal(errno
, "error setting IP_MULTICAST_TTL on multicast socket");
192 if(setsockopt(rtp_fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
193 &loop
, sizeof loop
) < 0)
194 fatal(errno
, "error setting IP_MULTICAST_LOOP on multicast socket");
198 if(setsockopt(rtp_fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
199 &ttl
, sizeof ttl
) < 0)
200 fatal(errno
, "error setting IPV6_MULTICAST_HOPS on multicast socket");
201 if(setsockopt(rtp_fd
, IPPROTO_IP
, IPV6_MULTICAST_LOOP
,
202 &loop
, sizeof loop
) < 0)
203 fatal(errno
, "error setting IPV6_MULTICAST_LOOP on multicast socket");
207 fatal(0, "unsupported address family %d", res
->ai_family
);
209 info("multicasting on %s TTL=%d loop=%s",
210 sockname
, ttl
, loop ?
"yes" : "no");
214 if(getifaddrs(&ifs
) < 0)
215 fatal(errno
, "error calling getifaddrs");
217 /* (At least on Darwin) IFF_BROADCAST might be set but ifa_broadaddr
218 * still a null pointer. It turns out that there's a subsequent entry
219 * for he same interface which _does_ have ifa_broadaddr though... */
220 if((ifs
->ifa_flags
& IFF_BROADCAST
)
221 && ifs
->ifa_broadaddr
222 && sockaddr_equal(ifs
->ifa_broadaddr
, res
->ai_addr
))
227 if(setsockopt(rtp_fd
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof one
) < 0)
228 fatal(errno
, "error setting SO_BROADCAST on broadcast socket");
229 info("broadcasting on %s (%s)", sockname
, ifs
->ifa_name
);
231 info("unicasting on %s", sockname
);
233 /* Enlarge the socket buffer */
235 if(getsockopt(rtp_fd
, SOL_SOCKET
, SO_SNDBUF
,
237 fatal(errno
, "error getting SO_SNDBUF");
238 if(target_sndbuf
> sndbuf
) {
239 if(setsockopt(rtp_fd
, SOL_SOCKET
, SO_SNDBUF
,
240 &target_sndbuf
, sizeof target_sndbuf
) < 0)
241 error(errno
, "error setting SO_SNDBUF to %d", target_sndbuf
);
243 info("changed socket send buffer size from %d to %d",
244 sndbuf
, target_sndbuf
);
246 info("default socket send buffer is %d",
248 /* We might well want to set additional broadcast- or multicast-related
250 if(sres
&& bind(rtp_fd
, sres
->ai_addr
, sres
->ai_addrlen
) < 0)
251 fatal(errno
, "error binding broadcast socket to %s", ssockname
);
252 if(connect(rtp_fd
, res
->ai_addr
, res
->ai_addrlen
) < 0)
253 fatal(errno
, "error connecting broadcast socket to %s", sockname
);
256 static void rtp_start(uaudio_callback
*callback
,
258 /* We only support L16 (but we do stereo and mono and will convert sign) */
259 if(uaudio_channels
== 2
261 && uaudio_rate
== 44100)
263 else if(uaudio_channels
== 1
265 && uaudio_rate
== 44100)
268 fatal(0, "asked for %d/%d/%d 16/44100/1 and 16/44100/2",
269 uaudio_bits
, uaudio_rate
, uaudio_channels
);
270 /* Various fields are required to have random initial values by RFC3550. The
271 * packet contents are highly public so there's no point asking for very
272 * strong randomness. */
273 gcry_create_nonce(&rtp_id
, sizeof rtp_id
);
274 gcry_create_nonce(&rtp_sequence
, sizeof rtp_sequence
);
276 uaudio_schedule_init();
277 uaudio_thread_start(callback
,
280 256 / uaudio_sample_size
,
281 (NETWORK_BYTES
- sizeof(struct rtp_header
))
282 / uaudio_sample_size
);
285 static void rtp_stop(void) {
286 uaudio_thread_stop();
291 static void rtp_activate(void) {
292 uaudio_schedule_reactivated
= 1;
293 uaudio_thread_activate();
296 static void rtp_deactivate(void) {
297 uaudio_thread_deactivate();
300 static void rtp_configure(void) {
303 uaudio_set("rtp-destination", config
->broadcast
.s
[0]);
304 uaudio_set("rtp-destination-port", config
->broadcast
.s
[1]);
305 if(config
->broadcast_from
.n
) {
306 uaudio_set("rtp-source", config
->broadcast_from
.s
[0]);
307 uaudio_set("rtp-source-port", config
->broadcast_from
.s
[0]);
309 uaudio_set("rtp-source", NULL
);
310 uaudio_set("rtp-source-port", NULL
);
312 snprintf(buffer
, sizeof buffer
, "%ld", config
->multicast_ttl
);
313 uaudio_set("multicast-ttl", buffer
);
314 uaudio_set("multicast-loop", config
->multicast_loop ?
"yes" : "no");
315 snprintf(buffer
, sizeof buffer
, "%ld", config
->rtp_delay_threshold
);
316 uaudio_set("delay-threshold", buffer
);
319 const struct uaudio uaudio_rtp
= {
321 .options
= rtp_options
,
324 .activate
= rtp_activate
,
325 .deactivate
= rtp_deactivate
,
326 .configure
= rtp_configure
,