res->ai_socktype,
res->ai_protocol)) < 0)
fatal(errno, "error creating broadcast socket");
- if((res->ai_family == PF_INET
- && IN_MULTICAST(
- ntohl(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr)
- ))
- || (res->ai_family == PF_INET6
- && IN6_IS_ADDR_MULTICAST(
- &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr
- ))) {
+ if(multicast(res->ai_addr)) {
/* Multicasting */
switch(res->ai_family) {
case PF_INET: {
const int mttl = config->multicast_ttl;
if(setsockopt(bfd, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, sizeof mttl) < 0)
fatal(errno, "error setting IP_MULTICAST_TTL on multicast socket");
+ if(setsockopt(bfd, IPPROTO_IP, IP_MULTICAST_LOOP,
+ &config->multicast_loop, sizeof one) < 0)
+ fatal(errno, "error setting IP_MULTICAST_LOOP on multicast socket");
break;
}
case PF_INET6: {
if(setsockopt(bfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&mttl, sizeof mttl) < 0)
fatal(errno, "error setting IPV6_MULTICAST_HOPS on multicast socket");
+ if(setsockopt(bfd, IPPROTO_IP, IPV6_MULTICAST_LOOP,
+ &config->multicast_loop, sizeof (int)) < 0)
+ fatal(errno, "error setting IPV6_MULTICAST_LOOP on multicast socket");
break;
}
default:
static int bfd_slot;
/** @brief Set up poll array for network play */
-static void network_beforepoll(void) {
+static void network_beforepoll(int *timeoutp) {
struct timeval now;
uint64_t target_us;
uint64_t target_rtp_time;
+ const int64_t samples_per_second = config->sample_format.rate
+ * config->sample_format.channels;
const int64_t samples_ahead = ((uint64_t)RTP_AHEAD_MS
- * config->sample_format.rate
- * config->sample_format.channels
+ * samples_per_second
/ 1000);
+ int64_t lead, ahead_ms;
/* If we're starting then initialize the base time */
if(!rtp_time)
target_rtp_time = (target_us * config->sample_format.rate
* config->sample_format.channels)
/ 1000000;
- if((int64_t)(rtp_time - target_rtp_time) < samples_ahead)
+ lead = rtp_time - target_rtp_time;
+ if(lead < samples_ahead)
+ /* We've not reached the desired lead, write as fast as we can */
bfd_slot = addfd(bfd, POLLOUT);
+ else {
+ /* We've reached the desired lead, we can afford to wait a bit even if the
+ * IP stack thinks it can accept more. */
+ ahead_ms = 1000 * (lead - samples_ahead) / samples_per_second;
+ if(ahead_ms < *timeoutp)
+ *timeoutp = ahead_ms;
+ }
}
/** @brief Process poll() results for network play */