This commit is contained in:
2026-02-23 00:58:54 +01:00
parent 6868138754
commit af319b7bd3
9 changed files with 119 additions and 52 deletions

View File

@@ -1,10 +1,32 @@
#include "ao_playasync.h"
#include <pthread.h>
#ifdef WIN32
#include <windows.h>
#define USE_WINDOWS_THREADS
#define sleep_ms(ms) Sleep(ms)
#else
#define USE_PTHREADS
#define sleep_ms(ms) usleep(ms * 1000)
#endif
#ifdef USE_WINDOWS_THREADS
#define MUTEX_LOCK(m) WaitForSingleObject(m, INFINITE)
#define MUTEX_UNLOCK(m) ReleaseMutex(m)
#endif
#ifdef USE_PTHREADS
#include <pthread.h>
#define MUTEX_LOCK(m) pthread_mutex_lock(&m)
#define MUTEX_UNLOCK(m) pthread_mutex_unlock(&m)
#endif
#ifndef WIN32
#include <unistd.h>
#endif
#include <assert.h>
#include <malloc.h>
#include <string.h>
//#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -25,12 +47,21 @@ typedef struct _queue_ {
struct _queue_ *prev;
} Queue_t;
typedef int(*ao_play_func_t)(void *, char *, uint32_t);
typedef struct {
Queue_t *play_queue;
Queue_t *last_frame;
ao_device *ao_device;
#ifdef USE_WINDOWS_THREADS
HANDLE mutex;
HANDLE thread;
DWORD thread_id;
#endif
#ifdef USE_PTHREADS
pthread_mutex_t mutex;
pthread_t thread;
#endif
double at_second;
ao_play_func_t ao_play_f;
int buf_size;
@@ -111,58 +142,48 @@ static void clear(AO_Handle *h)
}
}
#ifdef USE_PTHREADS
static void *run(void *arg)
#endif
#ifdef USE_WINDOWS_THREADS
static DWORD run(LPVOID arg)
#endif
{
AO_Handle *handle = (AO_Handle *) arg;
int go_on = (1 == 1);
while(go_on) {
pthread_mutex_lock(&handle->mutex);
MUTEX_LOCK(handle->mutex);
int has_frames = (handle->play_queue != NULL);
if (has_frames) {
Queue_t *q = get(handle);
handle->at_second = q->at_second;
pthread_mutex_unlock(&handle->mutex);
MUTEX_UNLOCK(handle->mutex);
if (q->command == STOP) {
go_on = (1 == 0);
} else {
//fprintf(stderr, "playing buf at %lf\n", handle->at_second);
handle->ao_play_f(handle->ao_device, q->buf, q->buflen);
}
del_elem(q);
} else {
pthread_mutex_unlock(&handle->mutex);
usleep(5000); // sleep for 5ms
MUTEX_UNLOCK(handle->mutex);
sleep_ms(5); // sleep for 5ms
}
}
#ifdef USE_PTHREADS
return NULL;
#endif
#ifdef USE_WINDOWS_THREADS
return 0;
#endif
}
/*
static void get_ao_play(void)
{
char *lib = "libao.so";
void *handle = dlopen(lib, RTLD_LAZY);
if (!handle) {
fprintf(stderr, "Cannot open library %s: %s\n", lib, dlerror());
exit(EXIT_FAILURE);
}
ao_play = dlsym(handle, "ao_play");
char *err;
err = dlerror();
if (err != NULL) {
fprintf(stderr, "Cannot get function ao_play: %s\n", err);
exit(EXIT_FAILURE);
}
}
*/
void *ao_create_async(void *ao_device_yeah, void *ao_play_f)
{
//if (ao_play == NULL) { get_ao_play(); }
@@ -173,12 +194,22 @@ void *ao_create_async(void *ao_device_yeah, void *ao_play_f)
handle->play_queue = NULL;
handle->last_frame = NULL;
handle->at_second = -1;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
handle->mutex = m;
handle->ao_play_f = ao_play_f;
handle->buf_size = 0;
#ifdef USE_PTHREADS
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
handle->mutex = m;
pthread_create(&handle->thread, NULL, run, handle);
#endif
#ifdef USE_WINDOWS_THREADS
handle->mutex = CreateMutex(NULL, // default security attributes
FALSE, // initially not owned
NULL);
handle->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) run, handle, 0, &handle->thread_id);
#endif
return (void *) handle;
}
@@ -187,14 +218,21 @@ void ao_stop_async(void *ao_handle)
{
AO_Handle *h = (AO_Handle *) ao_handle;
pthread_mutex_lock(&h->mutex);
MUTEX_LOCK(h->mutex);
clear(h);
Queue_t *q = new_elem(STOP, 0.0, 0, NULL);
add(h, q);
pthread_mutex_unlock(&h->mutex);
MUTEX_UNLOCK(h->mutex);
#ifdef USE_PTHREADS
void *retval;
pthread_join(h->thread, &retval);
#endif
#ifdef USE_WINDOWS_THREADS
WaitForSingleObject(h->thread, INFINITE);
CloseHandle(h->thread);
CloseHandle(h->mutex);
#endif
free(h);
}
@@ -202,26 +240,26 @@ void ao_play_async(void *ao_handle, double at_second, int buf_size, void *mem)
{
AO_Handle *h = (AO_Handle *) ao_handle;
Queue_t *q = new_elem(PLAY, at_second, buf_size, mem);
pthread_mutex_lock(&h->mutex);
MUTEX_LOCK(h->mutex);
add(h, q);
pthread_mutex_unlock(&h->mutex);
MUTEX_UNLOCK(h->mutex);
}
double ao_is_at_second_async(void *ao_handle)
{
AO_Handle *h = (AO_Handle *) ao_handle;
pthread_mutex_lock(&h->mutex);
MUTEX_LOCK(h->mutex);
double s = h->at_second;
pthread_mutex_unlock(&h->mutex);
MUTEX_UNLOCK(h->mutex);
return s;
}
int ao_bufsize_async(void *ao_handle)
{
AO_Handle *h = (AO_Handle *) ao_handle;
pthread_mutex_lock(&h->mutex);
MUTEX_LOCK(h->mutex);
int s = h->buf_size;
pthread_mutex_unlock(&h->mutex);
MUTEX_UNLOCK(h->mutex);
return s;
}