chiark / gitweb /
synchronize with disorder.dev
[disorder] / server / speaker-network.c
index 59630ffc1bab747adf485a834a77f2d64861d634..40abca59f14aeeba85f4eee8ad9dfa32a112a379 100644 (file)
@@ -118,14 +118,7 @@ static void network_init(void) {
                    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: {
@@ -189,7 +182,6 @@ static void network_init(void) {
     fatal(errno, "error connecting broadcast socket to %s", sockname);
   /* Select an SSRC */
   gcry_randomize(&rtp_id, sizeof rtp_id, GCRY_STRONG_RANDOM);
-  info("selected network backend, sending to %s", sockname);
 }
 
 /** @brief Play over the network */
@@ -307,14 +299,16 @@ static size_t network_play(size_t frames) {
 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)
@@ -327,8 +321,17 @@ static void network_beforepoll(void) {
   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 */