Fix depend.sh on NetBSD.
[paraslash.git] / osx_write.c
index cfd02e7..f27a49b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2010 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -187,18 +187,9 @@ static OSStatus osx_callback(void * inClientData,
 #define ENDIAN_FLAGS 0
 #endif
 
-static int osx_write_open(struct writer_node *wn)
-{
-       struct private_osx_write_data *powd = para_calloc(sizeof(*powd));
-
-       wn->private_data = powd;
-       init_buffers(wn);
-       return 0;
-}
-
 static int core_audio_init(struct writer_node *wn)
 {
-       struct private_osx_write_data *powd = wn->private_data;
+       struct private_osx_write_data *powd = para_calloc(sizeof(*powd));
        ComponentDescription desc;
        Component comp;
        AURenderCallbackStruct inputCallback = {osx_callback, powd};
@@ -207,6 +198,8 @@ static int core_audio_init(struct writer_node *wn)
        struct btr_node *btrn = wn->btrn;
        int32_t val;
 
+       wn->private_data = powd;
+       init_buffers(wn);
        /* where did that default audio output go? */
        desc.componentType = kAudioUnitType_Output;
        desc.componentSubType = kAudioUnitSubType_DefaultOutput;
@@ -276,22 +269,18 @@ e2:
 e1:
        CloseComponent(powd->audio_unit);
 e0:
+       free(powd);
+       wn->private_data = NULL;
        return ret;
 }
 
-__malloc static void *osx_write_parse_config(const char *options)
+__malloc static void *osx_write_parse_config_or_die(const char *options)
 {
-       struct osx_write_args_info *conf
-               = para_calloc(sizeof(struct osx_write_args_info));
-       PARA_INFO_LOG("options: %s\n", options);
-       int ret = osx_cmdline_parser_string(options, conf, "osx_write");
-       if (ret)
-               goto err_out;
-       return conf;
-err_out:
-       free(conf);
-       return NULL;
+       struct osx_write_args_info *conf = para_calloc(sizeof(*conf));
 
+       /* exits on errors */
+       osx_cmdline_parser_string(options, conf, "osx_write");
+       return conf;
 }
 
 static void osx_free_config(void *conf)
@@ -303,6 +292,8 @@ static void osx_write_close(struct writer_node *wn)
 {
        struct private_osx_write_data *powd = wn->private_data;
 
+       if (!powd)
+               return;
        PARA_INFO_LOG("closing writer node %p\n", wn);
        AudioOutputUnitStop(powd->audio_unit);
        AudioUnitUninitialize(powd->audio_unit);
@@ -320,14 +311,15 @@ static void osx_write_post_select(__a_unused struct sched *s, struct task *t)
        size_t bytes;
        int ret = 0;
 
-       while (powd->to->remaining <= 0) {
+       while (!powd || powd->to->remaining <= 0) {
                ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
                if (ret <= 0)
                        break;
-               if (powd->sample_rate == 0) {
+               if (!powd) {
                        ret = core_audio_init(wn);
                        if (ret < 0)
                                break;
+                       powd = wn->private_data;
                }
                btr_merge(btrn, 8192);
                bytes = btr_next_buffer(btrn, &data);
@@ -342,7 +334,7 @@ static void osx_write_post_select(__a_unused struct sched *s, struct task *t)
                }
                powd->to = powd->to->next;
        }
-       if (ret < 0 && powd->from->remaining <= 0) {
+       if (ret < 0 && (!powd || powd->from->remaining <= 0)) {
                btr_remove_node(btrn);
                t->error = ret;
        }
@@ -353,17 +345,19 @@ static void osx_write_pre_select(struct sched *s, struct task *t)
        struct writer_node *wn = container_of(t, struct writer_node, task);
        struct private_osx_write_data *powd = wn->private_data;
        struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
-       unsigned long divisor;
-       size_t numbytes = powd->to->remaining * sizeof(short);
+       unsigned long factor;
+       size_t numbytes;
        int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
 
-       if (ret < 0)
-               sched_min_delay(s);
-       if (ret <= 0 || numbytes < wn->min_iqs)
+       if (ret == 0)
                return;
-       divisor = powd->sample_rate * wn->min_iqs / numbytes;
-       if (divisor)
-               tv_divide(divisor, &tmp, &delay);
+       if (ret < 0 || !powd)
+               return sched_min_delay(s);
+       assert(powd->sample_rate > 0);
+       assert(wn->min_iqs > 0);
+       numbytes = powd->to->remaining * sizeof(short);
+       factor = numbytes / powd->sample_rate / wn->min_iqs;
+       tv_scale(factor, &tmp, &delay);
        sched_request_timeout(&delay, s);
 }
 
@@ -373,11 +367,10 @@ void osx_write_init(struct writer *w)
        struct osx_write_args_info dummy;
 
        osx_cmdline_parser_init(&dummy);
-       w->open = osx_write_open;
        w->close = osx_write_close;
        w->pre_select = osx_write_pre_select;
        w->post_select = osx_write_post_select;
-       w->parse_config = osx_write_parse_config;
+       w->parse_config_or_die = osx_write_parse_config_or_die;
        w->free_config = osx_free_config;
        w->shutdown = NULL; /* nothing to do */
        w->help = (struct ggo_help) {