Fix depend.sh on NetBSD.
[paraslash.git] / osx_write.c
index 551748a..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,17 +187,9 @@ static OSStatus osx_callback(void * inClientData,
 #define ENDIAN_FLAGS 0
 #endif
 
-static void osx_write_open(struct writer_node *wn)
-{
-       struct private_osx_write_data *powd = para_calloc(sizeof(*powd));
-
-       wn->private_data = powd;
-       init_buffers(wn);
-}
-
 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};
@@ -206,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;
@@ -275,6 +269,8 @@ e2:
 e1:
        CloseComponent(powd->audio_unit);
 e0:
+       free(powd);
+       wn->private_data = NULL;
        return ret;
 }
 
@@ -296,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);
@@ -313,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);
@@ -335,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;
        }
@@ -346,19 +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;
+       unsigned long factor;
        size_t numbytes;
        int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
 
        if (ret == 0)
                return;
-       if (ret < 0 || !powd->audio_unit)
+       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);
-       assert(numbytes > 0);
-       divisor = powd->sample_rate * wn->min_iqs / numbytes;
-       if (divisor)
-               tv_divide(divisor, &tmp, &delay);
+       factor = numbytes / powd->sample_rate / wn->min_iqs;
+       tv_scale(factor, &tmp, &delay);
        sched_request_timeout(&delay, s);
 }
 
@@ -368,7 +367,6 @@ 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;