From 784883f773ae76de995e185bd5d55c8b09f58f08 Mon Sep 17 00:00:00 2001 From: mannol Date: Sun, 28 Sep 2014 00:13:45 +0200 Subject: [PATCH] What if, like, photosynthesis is a lie?! --- src/device.c | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/device.c b/src/device.c index 63f69be..c48e449 100644 --- a/src/device.c +++ b/src/device.c @@ -61,7 +61,7 @@ typedef struct Device { int32_t call_idx; /* ToxAv call index */ uint32_t source, buffers[OPENAL_BUFS]; /* Playback source/buffers */ - size_t ref_count; + size_t *ref_count; int32_t selection; bool enable_VAD; bool muted; @@ -227,21 +227,29 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx device->frame_duration = frame_duration; device->sound_mode = channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; + if (pthread_mutex_init(device->mutex, NULL) != 0) { + free(device); + unlock; + return de_InternalError; + } + for (i = 0; i < *device_idx; i ++) { /* Check if any previous has the same selection */ if ( running[type][i]->selection == selection ) { device->dhndl = running[type][i]->dhndl; + if (type == output) { device->ctx = running[type][i]->ctx; memcpy(device->buffers, running[type][i]->buffers, sizeof(running[type][i]->buffers)); device->source = running[type][i]->source; } - device->ref_count++; - - if (pthread_mutex_init(device->mutex, NULL) != 0) { - unlock; - return de_InternalError; - } - + + if (running[type][i]->ref_count) + device->ref_count = running[type][i]->ref_count; + else + device->ref_count = running[type][i]->ref_count = calloc(1, sizeof(size_t)); + + *device->ref_count++; + unlock; return de_None; } @@ -293,11 +301,6 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx thread_paused = false; } - if (pthread_mutex_init(device->mutex, NULL) != 0) { - unlock; - return de_InternalError; - } - unlock; return de_None; } @@ -308,6 +311,7 @@ DeviceError close_device(DeviceType type, uint32_t device_idx) lock; Device* device = running[type][device_idx]; + DeviceError rc = de_None; if (!device) { unlock; @@ -316,10 +320,10 @@ DeviceError close_device(DeviceType type, uint32_t device_idx) running[type][device_idx] = NULL; - if ( !(device->ref_count--) ) { - unlock; + /* Once copied device->ref_count will be set to 1; not 2 + */ + if ( !(device->ref_count) || *(device->ref_count) ) { - DeviceError rc = de_None; if (type == input) { if ( !alcCaptureCloseDevice(device->dhndl) ) rc = de_AlError; @@ -334,14 +338,14 @@ DeviceError close_device(DeviceType type, uint32_t device_idx) alcMakeContextCurrent(NULL); if ( device->ctx ) alcDestroyContext(device->ctx); } - - free(device); - return rc; } + else if (device->ref_count) *(device->ref_count)--; + + /* Just pop the reference */ + free(device); unlock; - - return de_None; + return rc; } DeviceError register_device_callback( int32_t call_idx, uint32_t device_idx, DataHandleCallback callback, void* data, bool enable_VAD)