Skip to content

Commit

Permalink
(WASAPI) Only write when running and fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Myaats authored and LibretroAdmin committed Jun 4, 2024
1 parent c978d79 commit a0208dd
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions audio/drivers/wasapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down

0 comments on commit a0208dd

Please sign in to comment.