chiark / gitweb /
Merge from disorder.dev.
[disorder] / lib / uaudio-oss.c
index 081e1eb4bef09c6b595706cc57f816b7a4c223f5..d0c90626fa8b21a8e974dc1699044caf0895fc63 100644 (file)
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+#include <time.h>
 
 #include "mem.h"
 #include "log.h"
 #include "uaudio.h"
 
 #include "mem.h"
 #include "log.h"
 #include "uaudio.h"
+#include "configuration.h"
 
 #ifndef AFMT_U16_NE
 # if BYTE_ORDER == BIG_ENDIAN
 
 #ifndef AFMT_U16_NE
 # if BYTE_ORDER == BIG_ENDIAN
@@ -60,22 +62,13 @@ static const char *const oss_options[] = {
   NULL
 };
 
   NULL
 };
 
-/** @brief Actually play sound via OSS */
-static size_t oss_play(void *buffer, size_t samples) {
-  const size_t bytes = samples * uaudio_sample_size;
-  int rc = write(oss_fd, buffer, bytes);
-  if(rc < 0)
-    fatal(errno, "error writing to sound device");
-  return rc / uaudio_sample_size;
-}
-
 /** @brief Open the OSS sound device */
 static void oss_open(void) {
   const char *device = uaudio_get("device", NULL);
 
 #if EMPEG_HOST
   if(!device || !*device || !strcmp(device, "default"))
 /** @brief Open the OSS sound device */
 static void oss_open(void) {
   const char *device = uaudio_get("device", NULL);
 
 #if EMPEG_HOST
   if(!device || !*device || !strcmp(device, "default"))
-    device "/dev/audio";
+    device "/dev/audio";
 #else
   if(!device || !*device || !strcmp(device, "default")) {
     if(access("/dev/dsp", W_OK) == 0)
 #else
   if(!device || !*device || !strcmp(device, "default")) {
     if(access("/dev/dsp", W_OK) == 0)
@@ -104,17 +97,40 @@ static void oss_open(void) {
 #endif
 }
 
 #endif
 }
 
-static void oss_activate(void) {
-  oss_open();
-  uaudio_thread_activate();
+/** @brief Close the OSS sound device */
+static void oss_close(void) {
+  if(oss_fd != -1) {
+    close(oss_fd);
+    oss_fd = -1;
+  }
 }
 
 }
 
-static void oss_deactivate(void) {
-  uaudio_thread_deactivate();
-  close(oss_fd);
-  oss_fd = -1;
+/** @brief Actually play sound via OSS */
+static size_t oss_play(void *buffer, size_t samples, unsigned flags) {
+  /* cf uaudio-alsa.c:alsa-play() */
+  if(flags & UAUDIO_PAUSED) {
+    if(flags & UAUDIO_PAUSE)
+      oss_close();
+    if(samples > 64)
+      samples /= 2;
+    const uint64_t ns = ((uint64_t)samples * 1000000000
+                         / (uaudio_rate * uaudio_channels));
+    struct timespec ts[1];
+    ts->tv_sec = ns / 1000000000;
+    ts->tv_nsec = ns % 1000000000;
+    while(nanosleep(ts, ts) < 0 && errno == EINTR)
+      ;
+    return samples;
+  }
+  if(flags & UAUDIO_RESUME)
+    oss_open();
+  const size_t bytes = samples * uaudio_sample_size;
+  int rc = write(oss_fd, buffer, bytes);
+  if(rc < 0)
+    fatal(errno, "error writing to sound device");
+  return rc / uaudio_sample_size;
 }
 }
-  
+
 static void oss_start(uaudio_callback *callback,
                       void *userdata) {
   if(uaudio_channels != 1 && uaudio_channels != 2)
 static void oss_start(uaudio_callback *callback,
                       void *userdata) {
   if(uaudio_channels != 1 && uaudio_channels != 2)
@@ -127,18 +143,21 @@ static void oss_start(uaudio_callback *callback,
   /* Very specific buffer size requirements here apparently */
   uaudio_thread_start(callback, userdata, oss_play, 
                       4608 / uaudio_sample_size,
   /* Very specific buffer size requirements here apparently */
   uaudio_thread_start(callback, userdata, oss_play, 
                       4608 / uaudio_sample_size,
-                      4608 / uaudio_sample_size);
+                      4608 / uaudio_sample_size,
+                      0);
 #else
   /* We could SNDCTL_DSP_GETBLKSIZE but only when the device is already open,
    * which is kind of inconvenient.  We go with 1-4Kbyte for now. */
   uaudio_thread_start(callback, userdata, oss_play, 
                       32 / uaudio_sample_size,
 #else
   /* We could SNDCTL_DSP_GETBLKSIZE but only when the device is already open,
    * which is kind of inconvenient.  We go with 1-4Kbyte for now. */
   uaudio_thread_start(callback, userdata, oss_play, 
                       32 / uaudio_sample_size,
-                      4096 / uaudio_sample_size);
+                      4096 / uaudio_sample_size,
+                      0);
 #endif
 }
 
 static void oss_stop(void) {
   uaudio_thread_stop();
 #endif
 }
 
 static void oss_stop(void) {
   uaudio_thread_stop();
+  oss_close();                          /* might not have been paused */
 }
 
 /** @brief Channel names */
 }
 
 /** @brief Channel names */
@@ -195,17 +214,24 @@ static void oss_set_volume(int *left, int *right) {
   }
 }
 
   }
 }
 
+static void oss_configure(void) {
+  uaudio_set("device", config->device);
+  uaudio_set("mixer-device", config->mixer);
+  uaudio_set("mixer-channel", config->channel);
+}
+
 const struct uaudio uaudio_oss = {
   .name = "oss",
   .options = oss_options,
   .start = oss_start,
   .stop = oss_stop,
 const struct uaudio uaudio_oss = {
   .name = "oss",
   .options = oss_options,
   .start = oss_start,
   .stop = oss_stop,
-  .activate = oss_activate,
-  .deactivate = oss_deactivate,
+  .activate = uaudio_thread_activate,
+  .deactivate = uaudio_thread_deactivate,
   .open_mixer = oss_open_mixer,
   .close_mixer = oss_close_mixer,
   .get_volume = oss_get_volume,
   .set_volume = oss_set_volume,
   .open_mixer = oss_open_mixer,
   .close_mixer = oss_close_mixer,
   .get_volume = oss_get_volume,
   .set_volume = oss_set_volume,
+  .configure = oss_configure,
 };
 
 #endif
 };
 
 #endif