chiark / gitweb /
update disorderd.8 to reflect current code
[disorder] / server / speaker.c
index 7f4578de7a69b71f835dcc272020cce046cb5275..8acd9a6e42399960c7b3e3a83a7c460ab01e837f 100644 (file)
@@ -117,6 +117,8 @@ static const struct option options[] = {
   { "config", required_argument, 0, 'c' },
   { "debug", no_argument, 0, 'd' },
   { "no-debug", no_argument, 0, 'D' },
+  { "syslog", no_argument, 0, 's' },
+  { "no-syslog", no_argument, 0, 'S' },
   { 0, 0, 0, 0 }
 };
 
@@ -129,6 +131,7 @@ static void help(void) {
          "  --version, -V           Display version number\n"
          "  --config PATH, -c PATH  Set configuration file\n"
          "  --debug, -d             Turn on debugging\n"
+          "  --[no-]syslog           Force logging\n"
           "\n"
           "Speaker process for DisOrder.  Not intended to be run\n"
           "directly.\n");
@@ -192,7 +195,7 @@ static void destroy(struct track *t) {
  * main loop whenever the track's file descriptor is readable, assuming the
  * buffer has not reached the maximum allowed occupancy.
  */
-static int fill(struct track *t) {
+static int speaker_fill(struct track *t) {
   size_t where, left;
   int n;
 
@@ -215,9 +218,12 @@ static int fill(struct track *t) {
     if(n == 0) {
       D(("fill %s: eof detected", t->id));
       t->eof = 1;
+      t->playable = 1;
       return -1;
     }
     t->used += n;
+    if(t->used == sizeof t->buffer)
+      t->playable = 1;
   }
   return 0;
 }
@@ -333,6 +339,9 @@ static void play(size_t frames) {
    * empty) wrap it back to the start. */
   if(!playing->used || playing->start == (sizeof playing->buffer))
     playing->start = 0;
+  /* If the buffer emptied out mark the track as unplayably */
+  if(!playing->used)
+    playing->playable = 0;
   frames -= written_frames;
   return;
 }
@@ -372,7 +381,7 @@ int addfd(int fd, int events) {
 
 /** @brief Table of speaker backends */
 static const struct speaker_backend *backends[] = {
-#if API_ALSA
+#if HAVE_ALSA_ASOUNDLIB_H
   &alsa_backend,
 #endif
   &command_backend,
@@ -389,13 +398,12 @@ static const struct speaker_backend *backends[] = {
 /** @brief Return nonzero if we want to play some audio
  *
  * We want to play audio if there is a current track; and it is not paused; and
- * there are at least @ref FRAMES frames of audio to play, or we are in sight
- * of the end of the current track.
+ * it is playable according to the rules for @ref track::playable.
  */
 static int playable(void) {
   return playing
          && !paused
-         && (playing->used >= FRAMES || playing->eof);
+         && playing->playable;
 }
 
 /** @brief Main event loop */
@@ -432,7 +440,7 @@ static void mainloop(void) {
        * instead, but the post-poll code will cope even if it's
        * device_closed. */
       if(device_state == device_open)
-        backend->beforepoll();
+        backend->beforepoll(&timeout);
     }
     /* If any other tracks don't have a full buffer, try to read sample data
      * from them.  We do this last of all, so that if we run out of slots,
@@ -492,7 +500,7 @@ static void mainloop(void) {
           t = findtrack(id, 1/*create*/);
           write(fd, "", 1);             /* write an ack */
           if(t->fd != -1) {
-            error(0, "got a connection for a track that already has one");
+            error(0, "%s: already got a connection", id);
             xclose(fd);
           } else {
             nonblock(fd);
@@ -568,7 +576,7 @@ static void mainloop(void) {
       if(t->fd != -1
          && t->slot != -1
          && (fds[t->slot].revents & (POLLIN | POLLHUP)))
-         fill(t);
+         speaker_fill(t);
     /* Maybe we finished playing a track somewhere in the above */
     maybe_finished();
     /* If we don't need the sound device for now then close it for the benefit
@@ -582,26 +590,28 @@ static void mainloop(void) {
 }
 
 int main(int argc, char **argv) {
-  int n;
+  int n, logsyslog = !isatty(2);
   struct sockaddr_un addr;
   static const int one = 1;
   struct speaker_message sm;
+  const char *d;
 
   set_progname(argv);
   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
-  while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) {
+  while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
     switch(n) {
     case 'h': help();
     case 'V': version();
     case 'c': configfile = optarg; break;
     case 'd': debugging = 1; break;
     case 'D': debugging = 0; break;
+    case 'S': logsyslog = 0; break;
+    case 's': logsyslog = 1; break;
     default: fatal(0, "invalid option");
     }
   }
-  if(getenv("DISORDER_DEBUG_SPEAKER")) debugging = 1;
-  /* If stderr is a TTY then log there, otherwise to syslog. */
-  if(!isatty(2)) {
+  if((d = getenv("DISORDER_DEBUG_SPEAKER"))) debugging = atoi(d);
+  if(logsyslog) {
     openlog(progname, LOG_PID, LOG_DAEMON);
     log_default = &log_syslog;
   }