From a9ed3326f84cbb7585b5875e745f87eb96551ad4 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 6 Nov 2010 11:43:59 +0100 Subject: [PATCH] osx_write: Make osx_write_open() a no-op. Move the allocation of the private_osx_write_data struct to core_audio_init() and adjust the the check whether core audio has been initialized accordingly. --- osx_write.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/osx_write.c b/osx_write.c index 551748a4..507ad912 100644 --- a/osx_write.c +++ b/osx_write.c @@ -187,17 +187,13 @@ static OSStatus osx_callback(void * inClientData, #define ENDIAN_FLAGS 0 #endif -static void osx_write_open(struct writer_node *wn) +static void osx_write_open(__a_unused 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 +202,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 +273,8 @@ e2: e1: CloseComponent(powd->audio_unit); e0: + free(powd); + wn->private_data = NULL; return ret; } @@ -296,6 +296,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 +315,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 +338,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 +349,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); } -- 2.39.2