chiark / gitweb /
uaudio: pulseaudio support
[disorder] / clients / playrtp.c
index c07c153e1938b71b5d0b409b4cac40844d8ed421..600aae164e59bac9137216054b35cba83625a6c7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2007-2009 Richard Kettlewell
+ * Copyright (C) 2007-2009, 2011, 2013 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -207,6 +207,7 @@ static const struct option options[] = {
 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
   { "core-audio", no_argument, 0, 'c' },
 #endif
+  { "api", required_argument, 0, 'A' },
   { "dump", required_argument, 0, 'r' },
   { "command", required_argument, 0, 'e' },
   { "pause-mode", required_argument, 0, 'P' },
@@ -247,10 +248,10 @@ static void *control_thread(void attribute((unused)) *arg) {
   strcpy(sa.sun_path, control_socket);
   sfd = xsocket(PF_UNIX, SOCK_STREAM, 0);
   if(bind(sfd, (const struct sockaddr *)&sa, sizeof sa) < 0)
-    fatal(errno, "error binding to %s", control_socket);
+    disorder_fatal(errno, "error binding to %s", control_socket);
   if(listen(sfd, 128) < 0)
-    fatal(errno, "error calling listen on %s", control_socket);
-  info("listening on %s", control_socket);
+    disorder_fatal(errno, "error calling listen on %s", control_socket);
+  disorder_info("listening on %s", control_socket);
   for(;;) {
     salen = sizeof sa;
     cfd = accept(sfd, (struct sockaddr *)&sa, &salen);
@@ -260,17 +261,17 @@ static void *control_thread(void attribute((unused)) *arg) {
       case EAGAIN:
         break;
       default:
-        fatal(errno, "error calling accept on %s", control_socket);
+        disorder_fatal(errno, "error calling accept on %s", control_socket);
       }
     }
     if(!(fp = fdopen(cfd, "r+"))) {
-      error(errno, "error calling fdopen for %s connection", control_socket);
+      disorder_error(errno, "error calling fdopen for %s connection", control_socket);
       close(cfd);
       continue;
     }
     if(!inputline(control_socket, fp, &line, '\n')) {
       if(!strcmp(line, "stop")) {
-        info("stopped via %s", control_socket);
+        disorder_info("stopped via %s", control_socket);
         exit(0);                          /* terminate immediately */
       }
       if(!strcmp(line, "query"))
@@ -278,7 +279,7 @@ static void *control_thread(void attribute((unused)) *arg) {
       xfree(line);
     }
     if(fclose(fp) < 0)
-      error(errno, "error closing %s connection", control_socket);
+      disorder_error(errno, "error closing %s connection", control_socket);
   }
 }
 
@@ -371,19 +372,19 @@ static void *listen_thread(void attribute((unused)) *arg) {
       case EINTR:
         continue;
       default:
-        fatal(errno, "error reading from socket");
+        disorder_fatal(errno, "error reading from socket");
       }
     }
     /* Ignore too-short packets */
     if((size_t)n <= sizeof (struct rtp_header)) {
-      info("ignored a short packet");
+      disorder_info("ignored a short packet");
       continue;
     }
     timestamp = htonl(header.timestamp);
     seq = htons(header.seq);
     /* Ignore packets in the past */
     if(active && lt(timestamp, next_timestamp)) {
-      info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32,
+      disorder_info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32,
            timestamp, next_timestamp);
       continue;
     }
@@ -402,8 +403,7 @@ static void *listen_thread(void attribute((unused)) *arg) {
       break;
       /* TODO support other RFC3551 media types (when the speaker does) */
     default:
-      fatal(0, "unsupported RTP payload type %d",
-            header.mpt & 0x7F);
+      disorder_fatal(0, "unsupported RTP payload type %d", header.mpt & 0x7F);
     }
     /* See if packet is silent */
     const uint16_t *s = p->samples_raw;
@@ -449,7 +449,7 @@ void playrtp_fill_buffer(void) {
     //fprintf(stderr, "%8u/%u (%u) DROPPING\n", nsamples, maxbuffer, minbuffer);
     drop_first_packet();
   }
-  info("Buffering...");
+  disorder_info("Buffering...");
   /* Wait until there's at least minbuffer samples available */
   while(nsamples < minbuffer) {
     //fprintf(stderr, "%8u/%u (%u) FILLING\n", nsamples, maxbuffer, minbuffer);
@@ -493,15 +493,19 @@ static void help(void) {
           "  --max, -x FRAMES        Buffer maximum size\n"
           "  --rcvbuf, -R BYTES      Socket receive buffer size\n"
           "  --config, -C PATH       Set configuration file\n"
-#if HAVE_ALSA_ASOUNDLIB_H
-          "  --alsa, -a              Use ALSA to play audio\n"
-#endif
-#if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST
-          "  --oss, -o               Use OSS to play audio\n"
-#endif
-#if HAVE_COREAUDIO_AUDIOHARDWARE_H
-          "  --core-audio, -c        Use Core Audio to play audio\n"
-#endif
+          "  --api, -A API           Select audio API.  Possibilities:\n"
+          "                            ");
+  int first = 1;
+  for(int n = 0; uaudio_apis[n]; ++n) {
+    if(uaudio_apis[n]->flags & UAUDIO_API_CLIENT) {
+      if(first)
+        first = 0;
+      else
+        xprintf(", ");
+      xprintf("%s", uaudio_apis[n]->name);
+    }
+  }
+  xprintf("\n"
           "  --command, -e COMMAND   Pipe audio to command.\n"
           "  --pause-mode, -P silence  For -e: pauses send silence (default)\n"
           "  --pause-mode, -P suspend  For -e: pauses suspend writes\n"
@@ -604,8 +608,8 @@ static size_t playrtp_callback(void *buffer,
    * basis.
    */
   if(nsamples > minbuffer && silent) {
-    info("dropping %zu samples (%"PRIu32" > %"PRIu32")",
-         samples, nsamples, minbuffer);
+    disorder_info("dropping %zu samples (%"PRIu32" > %"PRIu32")",
+                  samples, nsamples, minbuffer);
     samples = 0;
   }
   /* Junk obsolete packets */
@@ -648,9 +652,8 @@ int main(int argc, char **argv) {
    * timestamps in the logs */
   logdate = 1;
   mem_init();
-  if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
-  backend = uaudio_apis[0];
-  while((n = getopt_long(argc, argv, "hVdD:m:x:L:R:aocC:re:P:M", options, 0)) >= 0) {
+  if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale");
+  while((n = getopt_long(argc, argv, "hVdD:m:x:L:R:aocC:re:P:MA:", options, 0)) >= 0) {
     switch(n) {
     case 'h': help();
     case 'V': version("disorder-playrtp");
@@ -661,24 +664,45 @@ int main(int argc, char **argv) {
     case 'L': logfp = fopen(optarg, "w"); break;
     case 'R': target_rcvbuf = atoi(optarg); break;
 #if HAVE_ALSA_ASOUNDLIB_H
-    case 'a': backend = &uaudio_alsa; break;
+    case 'a':
+      disorder_error(0, "deprecated option; use --api alsa instead");
+      backend = &uaudio_alsa; break;
 #endif
 #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST
-    case 'o': backend = &uaudio_oss; break;
+    case 'o':
+      disorder_error(0, "deprecated option; use --api oss instead");
+      backend = &uaudio_oss; 
+      break;
 #endif
 #if HAVE_COREAUDIO_AUDIOHARDWARE_H      
-    case 'c': backend = &uaudio_coreaudio; break;
+    case 'c':
+      disorder_error(0, "deprecated option; use --api coreaudio instead");
+      backend = &uaudio_coreaudio;
+      break;
 #endif
+    case 'A': backend = uaudio_find(optarg); break;
     case 'C': configfile = optarg; break;
     case 's': control_socket = optarg; break;
     case 'r': dumpfile = optarg; break;
     case 'e': backend = &uaudio_command; uaudio_set("command", optarg); break;
     case 'P': uaudio_set("pause-mode", optarg); break;
     case 'M': monitor = 1; break;
-    default: fatal(0, "invalid option");
+    default: disorder_fatal(0, "invalid option");
     }
   }
-  if(config_read(0, NULL)) fatal(0, "cannot read configuration");
+  if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration");
+  if(!backend) {
+    backend = uaudio_default(uaudio_apis, UAUDIO_API_CLIENT);
+    if(!backend)
+      disorder_fatal(0, "no default uaudio API found");
+    disorder_info("default audio API %s", backend->name);
+  }
+  if(backend == &uaudio_rtp) {
+    /* This means that you have NO local sound output.  This can happen if you
+     * use a non-Apple GCC on a Mac (because it doesn't know how to compile
+     * CoreAudio/AudioHardware.h). */
+    disorder_fatal(0, "cannot play RTP through RTP");
+  }
   if(!maxbuffer)
     maxbuffer = 2 * minbuffer;
   argc -= optind;
@@ -701,8 +725,10 @@ int main(int argc, char **argv) {
     sl.s = argv;
     break;
   default:
-    fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]");
+    disorder_fatal(0, "usage: disorder-playrtp [OPTIONS] [[ADDRESS] PORT]");
   }
+  disorder_info("version "VERSION" process ID %lu",
+                (unsigned long)getpid());
   /* Look up address and port */
   if(!(res = get_address(&sl, &prefs, &sockname)))
     exit(1);
@@ -710,7 +736,7 @@ int main(int argc, char **argv) {
   if((rtpfd = socket(res->ai_family,
                      res->ai_socktype,
                      res->ai_protocol)) < 0)
-    fatal(errno, "error creating socket");
+    disorder_fatal(errno, "error creating socket");
   /* Allow multiple listeners */
   xsetsockopt(rtpfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
   is_multicast = multicast(res->ai_addr);
@@ -728,11 +754,13 @@ int main(int argc, char **argv) {
       mgroup.in6.sin6_port = 0;
       break;
     default:
-      fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family);
+      disorder_fatal(0, "unsupported address family %d",
+                     (int)res->ai_addr->sa_family);
     }
     /* Bind to to the multicast group address */
     if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0)
-      fatal(errno, "error binding socket to %s", format_sockaddr(res->ai_addr));
+      disorder_fatal(errno, "error binding socket to %s",
+                     format_sockaddr(res->ai_addr));
     /* Add multicast group membership */
     switch(mgroup.sa.sa_family) {
     case PF_INET:
@@ -740,21 +768,21 @@ int main(int argc, char **argv) {
       mreq.imr_interface.s_addr = 0;      /* use primary interface */
       if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                     &mreq, sizeof mreq) < 0)
-        fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP");
+        disorder_fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP");
       break;
     case PF_INET6:
       mreq6.ipv6mr_multiaddr = mgroup.in6.sin6_addr;
       memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface);
       if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
                     &mreq6, sizeof mreq6) < 0)
-        fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP");
+        disorder_fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP");
       break;
     default:
-      fatal(0, "unsupported address family %d", res->ai_family);
+      disorder_fatal(0, "unsupported address family %d", res->ai_family);
     }
     /* Report what we did */
-    info("listening on %s multicast group %s",
-         format_sockaddr(res->ai_addr), format_sockaddr(&mgroup.sa));
+    disorder_info("listening on %s multicast group %s",
+                  format_sockaddr(res->ai_addr), format_sockaddr(&mgroup.sa));
   } else {
     /* Bind to 0/port */
     switch(res->ai_addr->sa_family) {
@@ -771,35 +799,36 @@ int main(int argc, char **argv) {
       break;
     }
     default:
-      fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family);
+      disorder_fatal(0, "unsupported family %d", (int)res->ai_addr->sa_family);
     }
     if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0)
-      fatal(errno, "error binding socket to %s", format_sockaddr(res->ai_addr));
+      disorder_fatal(errno, "error binding socket to %s",
+                     format_sockaddr(res->ai_addr));
     /* Report what we did */
-    info("listening on %s", format_sockaddr(res->ai_addr));
+    disorder_info("listening on %s", format_sockaddr(res->ai_addr));
   }
   len = sizeof rcvbuf;
   if(getsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len) < 0)
-    fatal(errno, "error calling getsockopt SO_RCVBUF");
+    disorder_fatal(errno, "error calling getsockopt SO_RCVBUF");
   if(target_rcvbuf > rcvbuf) {
     if(setsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF,
                   &target_rcvbuf, sizeof target_rcvbuf) < 0)
-      error(errno, "error calling setsockopt SO_RCVBUF %d", 
-            target_rcvbuf);
+      disorder_error(errno, "error calling setsockopt SO_RCVBUF %d", 
+                     target_rcvbuf);
       /* We try to carry on anyway */
     else
-      info("changed socket receive buffer from %d to %d",
-           rcvbuf, target_rcvbuf);
+      disorder_info("changed socket receive buffer from %d to %d",
+                    rcvbuf, target_rcvbuf);
   } else
-    info("default socket receive buffer %d", rcvbuf);
+    disorder_info("default socket receive buffer %d", rcvbuf);
   //info("minbuffer %u maxbuffer %u", minbuffer, maxbuffer);
   if(logfp)
-    info("WARNING: -L option can impact performance");
+    disorder_info("WARNING: -L option can impact performance");
   if(control_socket) {
     pthread_t tid;
 
     if((err = pthread_create(&tid, 0, control_thread, 0)))
-      fatal(err, "pthread_create control_thread");
+      disorder_fatal(err, "pthread_create control_thread");
   }
   if(dumpfile) {
     int fd;
@@ -807,39 +836,40 @@ int main(int argc, char **argv) {
     size_t written;
 
     if((fd = open(dumpfile, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0)
-      fatal(errno, "opening %s", dumpfile);
+      disorder_fatal(errno, "opening %s", dumpfile);
     /* Fill with 0s to a suitable size */
     memset(buffer, 0, sizeof buffer);
     for(written = 0; written < dump_size * sizeof(int16_t);
         written += sizeof buffer) {
       if(write(fd, buffer, sizeof buffer) < 0)
-        fatal(errno, "clearing %s", dumpfile);
+        disorder_fatal(errno, "clearing %s", dumpfile);
     }
     /* Map the buffer into memory for convenience */
     dump_buffer = mmap(0, dump_size * sizeof(int16_t), PROT_READ|PROT_WRITE,
                        MAP_SHARED, fd, 0);
     if(dump_buffer == (void *)-1)
-      fatal(errno, "mapping %s", dumpfile);
-    info("dumping to %s", dumpfile);
+      disorder_fatal(errno, "mapping %s", dumpfile);
+    disorder_info("dumping to %s", dumpfile);
   }
   /* Set up output.  Currently we only support L16 so there's no harm setting
    * the format before we know what it is! */
   uaudio_set_format(44100/*Hz*/, 2/*channels*/,
                     16/*bits/channel*/, 1/*signed*/);
+  uaudio_set("application", "disorder-playrtp");
   backend->start(playrtp_callback, NULL);
   /* We receive and convert audio data in a background thread */
   if((err = pthread_create(&ltid, 0, listen_thread, 0)))
-    fatal(err, "pthread_create listen_thread");
+    disorder_fatal(err, "pthread_create listen_thread");
   /* We have a second thread to add received packets to the queue */
   if((err = pthread_create(&ltid, 0, queue_thread, 0)))
-    fatal(err, "pthread_create queue_thread");
+    disorder_fatal(err, "pthread_create queue_thread");
   pthread_mutex_lock(&lock);
   time_t lastlog = 0;
   for(;;) {
     /* Wait for the buffer to fill up a bit */
     playrtp_fill_buffer();
     /* Start playing now */
-    info("Playing...");
+    disorder_info("Playing...");
     next_timestamp = pheap_first(&packets)->timestamp;
     active = 1;
     pthread_mutex_unlock(&lock);
@@ -858,16 +888,16 @@ int main(int argc, char **argv) {
          || (nsamples > 0
              && contains(pheap_first(&packets), next_timestamp))) {
       if(monitor) {
-        time_t now = time(0);
+        time_t now = xtime(0);
 
         if(now >= lastlog + 60) {
           int offset = nsamples - minbuffer;
           double offtime = (double)offset / (uaudio_rate * uaudio_channels);
-          info("%+d samples off (%d.%02ds, %d bytes)",
-               offset,
-               (int)fabs(offtime) * (offtime < 0 ? -1 : 1),
-               (int)(fabs(offtime) * 100) % 100,
-               offset * uaudio_bits / CHAR_BIT);
+          disorder_info("%+d samples off (%d.%02ds, %d bytes)",
+                        offset,
+                        (int)fabs(offtime) * (offtime < 0 ? -1 : 1),
+                        (int)(fabs(offtime) * 100) % 100,
+                        offset * uaudio_bits / CHAR_BIT);
           lastlog = now;
         }
       }