From a0208dd56c13c9a964e58f3ce0efb3d4ce45b211 Mon Sep 17 00:00:00 2001 From: Mats Date: Tue, 4 Jun 2024 14:04:40 +0200 Subject: [PATCH] (WASAPI) Only write when running and fix deadlock --- audio/drivers/wasapi.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/audio/drivers/wasapi.c b/audio/drivers/wasapi.c index 32f6d9760df..242aab605e6 100644 --- a/audio/drivers/wasapi.c +++ b/audio/drivers/wasapi.c @@ -27,6 +27,9 @@ #include "../../verbosity.h" #include "../../configuration.h" +/* Max time to wait before continuing */ +#define WASAPI_TIMEOUT 256 + typedef struct { HANDLE write_event; @@ -200,7 +203,7 @@ static ssize_t wasapi_write_sh_buffer(wasapi_t *w, const void *data, size_t size if (!write_avail) { size_t read_avail = 0; - if (!(WaitForSingleObject(w->write_event, INFINITE) == WAIT_OBJECT_0)) + if (!(WaitForSingleObject(w->write_event, WASAPI_TIMEOUT) == WAIT_OBJECT_0)) return -1; if (FAILED(_IAudioClient_GetCurrentPadding(w->client, &padding))) @@ -228,7 +231,7 @@ static ssize_t wasapi_write_sh(wasapi_t *w, const void *data, size_t size) size_t write_avail = 0; UINT32 padding = 0; - if (!(WaitForSingleObject(w->write_event, INFINITE) == WAIT_OBJECT_0)) + if (!(WaitForSingleObject(w->write_event, WASAPI_TIMEOUT) == WAIT_OBJECT_0)) return -1; if (FAILED(_IAudioClient_GetCurrentPadding(w->client, &padding))) @@ -319,6 +322,8 @@ static ssize_t wasapi_write(void *wh, const void *data, size_t size) ssize_t ir = 0; wasapi_t *w = (wasapi_t*)wh; + if (!w->running) return -1; + if (w->nonblock) { if (w->exclusive) @@ -330,7 +335,7 @@ static ssize_t wasapi_write(void *wh, const void *data, size_t size) { for (ir = -1; written < size; written += ir) { - ir = wasapi_write_ex(w, (char*)data + written, size - written, INFINITE); + ir = wasapi_write_ex(w, (char*)data + written, size - written, WASAPI_TIMEOUT); if (ir == -1) return -1; }