Merge branch 'refs/heads/t/kill_usleep'
[paraslash.git] / oss_write.c
index 7acde1dacc42bcef0af94380109fc14f6ea55287..35f7b6280115ec53bf88d5f2e303e34f8cc45d98 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2010 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2009-2014 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -9,9 +9,7 @@
 #include <regex.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
-#include <dirent.h>
 #include <sys/soundcard.h>
-#include <stdbool.h>
 
 #include "para.h"
 #include "fd.h"
@@ -25,9 +23,6 @@
 #include "oss_write.cmdline.h"
 #include "error.h"
 
-/** Always use 16 bit little endian. */
-#define FORMAT AFMT_S16_LE
-
 /** Data specific to the oss writer. */
 struct private_oss_write_data {
        /** The file handle of the device. */
@@ -36,26 +31,64 @@ struct private_oss_write_data {
        int bytes_per_frame;
 };
 
-static void oss_pre_select(struct sched *s, struct task *t)
+/*
+ * We keep one bit of static storage to make sure only one instance of the oss
+ * writer is running at any given time.
+ */
+static bool sound_device_busy;
+
+static bool sound_device_is_busy(void)
+{
+       return sound_device_busy;
+}
+
+static void set_sound_device_busy(void)
+{
+       assert(!sound_device_busy);
+       sound_device_busy = true;
+}
+
+static void set_sound_device_idle(void)
 {
-       struct writer_node *wn = container_of(t, struct writer_node, task);
+       assert(sound_device_busy);
+       sound_device_busy = false;
+}
+
+static int get_oss_format(enum sample_format sf)
+{
+       switch (sf) {
+       case SF_S8: return AFMT_S8;
+       case SF_U8: return AFMT_U8;
+       case SF_S16_LE: return AFMT_S16_LE;
+       case SF_S16_BE: return AFMT_S16_BE;
+       case SF_U16_LE: return AFMT_U16_LE;
+       case SF_U16_BE: return AFMT_U16_BE;
+       default: return AFMT_S16_LE;
+       }
+}
+
+static void oss_pre_select(struct sched *s, void *context)
+{
+       struct writer_node *wn = context;
        struct private_oss_write_data *powd = wn->private_data;
        int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
 
-       t->error = 0;
-       if (ret < 0)
-               sched_min_delay(s);
-       else if (ret > 0)
-               para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
+       if (ret == 0 || (sound_device_is_busy() && !powd))
+               return;
+       if (ret < 0 || !powd)
+               return sched_min_delay(s);
+       para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
 }
 
 static void oss_close(struct writer_node *wn)
 {
        struct private_oss_write_data *powd = wn->private_data;
 
-       if (powd->fd >= 0)
-               close(powd->fd);
+       if (!powd)
+               return;
+       close(powd->fd);
        free(powd);
+       set_sound_device_idle();
 }
 
 /*
@@ -69,34 +102,33 @@ static void oss_close(struct writer_node *wn)
  * incorrectly believe that the device is still in 44.1 kHz mode when actually
  * the speed is decreased to 22.05 kHz.
  */
-static int oss_init(struct writer_node *wn, unsigned sample_rate, unsigned channels)
+static int oss_init(struct writer_node *wn, unsigned sample_rate,
+               unsigned channels, int sample_format)
 {
-       int ret, format = FORMAT;
+       int ret, format;
        unsigned ch, rate;
        struct oss_write_args_info *conf = wn->conf;
-       struct private_oss_write_data *powd = wn->private_data;
+       struct private_oss_write_data *powd = para_calloc(sizeof(*powd));
 
        PARA_INFO_LOG("opening %s\n", conf->device_arg);
        ret = para_open(conf->device_arg, O_WRONLY, 0);
        if (ret < 0)
-               return ret;
+               goto err_free;
        powd->fd = ret;
        ret = mark_fd_nonblocking(powd->fd);
        if (ret < 0)
                goto err;
        /* set PCM format */
+       sample_format = format = get_oss_format(sample_format);
        ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
        if (ret < 0) {
                ret = -ERRNO_TO_PARA_ERROR(errno);
                goto err;
        }
        ret = -E_BAD_SAMPLE_FORMAT;
-       if (format != FORMAT)
+       if (format != sample_format)
                goto err;
        /* set number of channels */
-       ret = -E_BAD_CHANNEL_COUNT;
-       if (channels == 0)
-               goto err;
        ch = channels;
        ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
        if (ret < 0) {
@@ -106,7 +138,10 @@ static int oss_init(struct writer_node *wn, unsigned sample_rate, unsigned chann
        ret = -E_BAD_CHANNEL_COUNT;
        if (ch != channels)
                goto err;
-       powd->bytes_per_frame = ch * 2;
+       if (format == SF_U8 || format == SF_S8)
+               powd->bytes_per_frame = ch;
+       else
+               powd->bytes_per_frame = ch * 2;
 
        /*
         * Set sampling rate
@@ -135,88 +170,89 @@ static int oss_init(struct writer_node *wn, unsigned sample_rate, unsigned chann
                        sample_rate);
        }
        wn->min_iqs = powd->bytes_per_frame;
+       wn->private_data = powd;
        return 1;
 err:
        close(powd->fd);
-       powd->fd = -1;
+err_free:
+       free(powd);
+       PARA_ERROR_LOG("failed to init %s: %s\n", conf->device_arg,
+               para_strerror(-ret));
        return ret;
 }
 
-static void oss_post_select(__a_unused struct sched *s,
-               struct task *t)
+static int oss_post_select(__a_unused struct sched *s, void *context)
 {
-       struct writer_node *wn = container_of(t, struct writer_node, task);
+       struct writer_node *wn = context;
        struct private_oss_write_data *powd = wn->private_data;
        struct btr_node *btrn = wn->btrn;
        size_t frames, bytes;
-       int ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
+       int ret;
        char *data;
+       audio_buf_info abi;
 
+       ret = task_get_notification(wn->task);
        if (ret < 0)
                goto out;
-       if (ret == 0)
-               return;
-       if (powd->fd < 0) {
-               int32_t rate, ch;
-               ret = get_btr_sample_rate(btrn, &rate);
-               if (ret < 0)
-                       goto out;
-               ret = get_btr_channels(btrn, &ch);
-               if (ret < 0)
-                       goto out;
-               ret = oss_init(wn, rate, ch);
+       ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
+       if (ret <= 0)
+               goto out;
+       if (!powd) {
+               int32_t rate, ch, format;
+
+               if (sound_device_is_busy())
+                       return 0;
+               get_btr_sample_rate(btrn, &rate);
+               get_btr_channels(btrn, &ch);
+               get_btr_sample_format(btrn, &format);
+               ret = oss_init(wn, rate, ch, format);
                if (ret < 0)
                        goto out;
-               return;
+               set_sound_device_busy();
+               return 0;
        }
        btr_merge(btrn, wn->min_iqs);
        bytes = btr_next_buffer(btrn, &data);
        frames = bytes / powd->bytes_per_frame;
        if (!frames) { /* eof and less than a single frame available */
-               ret = -E_OSS_EOF;
+               ret = -E_WRITE_COMMON_EOF;
                goto out;
        }
        ret = 0;
        if (!FD_ISSET(powd->fd, &s->wfds))
                goto out;
-       ret = write_nonblock(powd->fd, data, frames * powd->bytes_per_frame);
+       /* get maximal number of bytes that can be written */
+       ret = ioctl(powd->fd, SNDCTL_DSP_GETOSPACE, &abi);
+       if (ret >= 0) {
+               size_t max_frames = abi.bytes / powd->bytes_per_frame;
+               if (max_frames == 0)
+                       goto out;
+               /* cap number of frames to avoid sound artefacts */
+               frames = PARA_MIN(frames, max_frames);
+       }
+       ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
        if (ret < 0)
                goto out;
        btr_consume(btrn, ret);
        ret = 0;
 out:
-       t->error = ret;
        if (ret < 0)
-               btr_remove_node(btrn);
-}
-
-static int oss_open(struct writer_node *wn)
-{
-       struct private_oss_write_data *powd;
-
-       powd = para_calloc(sizeof(*powd));
-       wn->private_data = powd;
-       powd->fd = -1;
-       return 1;
+               btr_remove_node(&wn->btrn);
+       return ret;
 }
 
-__malloc static void *oss_parse_config(const char *options)
+__malloc static void *oss_parse_config_or_die(int argc, char **argv)
 {
-       int ret;
        struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
 
-       ret = oss_cmdline_parser_string(options, conf, "oss_write");
-       if (ret)
-               goto err_out;
+       /* exits on errors */
+       oss_write_cmdline_parser(argc, argv, conf);
        return conf;
-err_out:
-       free(conf);
-       return NULL;
 }
 
 static void oss_free_config(void *conf)
 {
-       oss_cmdline_parser_free(conf);
+       oss_write_cmdline_parser_free(conf);
 }
 
 /**
@@ -230,17 +266,12 @@ void oss_write_init(struct writer *w)
 {
        struct oss_write_args_info dummy;
 
-       oss_cmdline_parser_init(&dummy);
-       w->open = oss_open;
+       oss_write_cmdline_parser_init(&dummy);
        w->close = oss_close;
        w->pre_select = oss_pre_select;
        w->post_select = oss_post_select;
-       w->parse_config = oss_parse_config;
+       w->parse_config_or_die = oss_parse_config_or_die;
        w->free_config = oss_free_config;
-       w->shutdown = NULL;
-       w->help = (struct ggo_help) {
-               .short_help = oss_write_args_info_help,
-               .detailed_help = oss_write_args_info_detailed_help
-       };
-       oss_cmdline_parser_free(&dummy);
+       w->help = (struct ggo_help)DEFINE_GGO_HELP(oss_write);
+       oss_write_cmdline_parser_free(&dummy);
 }