diff --git a/audio/drivers/dsound.c b/audio/drivers/dsound.c index 9443e37429..30b73312a0 100644 --- a/audio/drivers/dsound.c +++ b/audio/drivers/dsound.c @@ -20,6 +20,8 @@ #endif #include +#include +#include #ifdef _XBOX #define DSERR_BUFFERLOST MAKE_DSHRESULT(150) @@ -65,8 +67,8 @@ typedef struct dsound fifo_buffer_t *buffer; CRITICAL_SECTION crit; - HANDLE event; - HANDLE thread; + slock_t *event; + sthread_t *thread; unsigned buffer_size; @@ -147,7 +149,7 @@ static INLINE void release_region(dsound_t *ds, const struct audio_lock *region) IDirectSoundBuffer_Unlock(ds->dsb, region->chunk1, region->size1, region->chunk2, region->size2); } -static DWORD CALLBACK dsound_thread(PVOID data) +static void dsound_thread(void *data) { DWORD write_ptr; dsound_t *ds = (dsound_t*)data; @@ -174,7 +176,7 @@ static DWORD CALLBACK dsound_thread(PVOID data) /* No space to write, or we don't have data in our fifo, * but we can wait some time before it underruns ... */ - Sleep(1); + rarch_sleep(1); /* We could opt for using the notification interface, * but it is not guaranteed to work, so use high @@ -189,7 +191,7 @@ static DWORD CALLBACK dsound_thread(PVOID data) if (!grab_region(ds, write_ptr, ®ion)) { ds->thread_alive = false; - SetEvent(ds->event); + scond_broadcast(ds->event); break; } @@ -206,7 +208,7 @@ static DWORD CALLBACK dsound_thread(PVOID data) if (!grab_region(ds, write_ptr, ®ion)) { ds->thread_alive = false; - SetEvent(ds->event); + scond_broadcast(ds->event); break; } @@ -220,7 +222,7 @@ static DWORD CALLBACK dsound_thread(PVOID data) release_region(ds, ®ion); write_ptr = (write_ptr + region.size1 + region.size2) % ds->buffer_size; - SetEvent(ds->event); + scond_broadcast(ds->event); } } @@ -233,8 +235,8 @@ static void dsound_stop_thread(dsound_t *ds) return; ds->thread_alive = false; - WaitForSingleObject(ds->thread, INFINITE); - CloseHandle(ds->thread); + + sthread_join(ds->thread); ds->thread = NULL; } @@ -243,8 +245,8 @@ static bool dsound_start_thread(dsound_t *ds) if (!ds->thread) { ds->thread_alive = true; - ds->thread = CreateThread(NULL, 0, dsound_thread, ds, 0, NULL); - if (ds->thread == NULL) + ds->thread = sthread_create(dsound_thread, ds); + if (!ds->thread) return false; } @@ -275,8 +277,7 @@ static void dsound_free(void *data) if (ds->thread) { ds->thread_alive = false; - WaitForSingleObject(ds->thread, INFINITE); - CloseHandle(ds->thread); + sthread_join(ds->thread); } DeleteCriticalSection(&ds->crit); @@ -290,8 +291,8 @@ static void dsound_free(void *data) if (ds->ds) IDirectSound_Release(ds->ds); - if (ds->event) - CloseHandle(ds->event); + slock_free(ds->event); + ds->event = NULL; if (ds->buffer) fifo_free(ds->buffer); @@ -320,10 +321,10 @@ static BOOL CALLBACK enumerate_cb(LPGUID guid, LPCSTR desc, LPCSTR module, LPVOI static void *dsound_init(const char *device, unsigned rate, unsigned latency) { - WAVEFORMATEX wfx = {0}; - DSBUFFERDESC bufdesc = {0}; + WAVEFORMATEX wfx = {0}; + DSBUFFERDESC bufdesc = {0}; struct dsound_dev dev = {0}; - dsound_t *ds = (dsound_t*)calloc(1, sizeof(*ds)); + dsound_t *ds = (dsound_t*)calloc(1, sizeof(*ds)); if (!ds) goto error; @@ -346,32 +347,31 @@ static void *dsound_init(const char *device, unsigned rate, unsigned latency) goto error; #endif - wfx.wFormatTag = WAVE_FORMAT_PCM; - wfx.nChannels = 2; - wfx.nSamplesPerSec = rate; - wfx.wBitsPerSample = 16; - wfx.nBlockAlign = 2 * sizeof(int16_t); - wfx.nAvgBytesPerSec = rate * 2 * sizeof(int16_t); + wfx.wFormatTag = WAVE_FORMAT_PCM; + wfx.nChannels = 2; + wfx.nSamplesPerSec = rate; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = 2 * sizeof(int16_t); + wfx.nAvgBytesPerSec = rate * 2 * sizeof(int16_t); - ds->buffer_size = (latency * wfx.nAvgBytesPerSec) / 1000; - ds->buffer_size /= CHUNK_SIZE; - ds->buffer_size *= CHUNK_SIZE; + ds->buffer_size = (latency * wfx.nAvgBytesPerSec) / 1000; + ds->buffer_size /= CHUNK_SIZE; + ds->buffer_size *= CHUNK_SIZE; if (ds->buffer_size < 4 * CHUNK_SIZE) - ds->buffer_size = 4 * CHUNK_SIZE; + ds->buffer_size = 4 * CHUNK_SIZE; RARCH_LOG("[DirectSound]: Setting buffer size of %u bytes\n", ds->buffer_size); RARCH_LOG("[DirectSound]: Latency = %u ms\n", (unsigned)((1000 * ds->buffer_size) / wfx.nAvgBytesPerSec)); - bufdesc.dwSize = sizeof(DSBUFFERDESC); -#ifdef _XBOX - bufdesc.dwFlags = 0; -#else - bufdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; + bufdesc.dwSize = sizeof(DSBUFFERDESC); + bufdesc.dwFlags = 0; +#ifndef _XBOX + bufdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; #endif bufdesc.dwBufferBytes = ds->buffer_size; - bufdesc.lpwfxFormat = &wfx; + bufdesc.lpwfxFormat = &wfx; - ds->event = CreateEvent(NULL, false, false, NULL); + ds->event = scond_new(); if (!ds->event) goto error; @@ -420,7 +420,8 @@ static bool dsound_start(void *data) if (!dsound_start_thread(ds)) return false; - ds->is_paused = (IDirectSoundBuffer_Play(ds->dsb, 0, 0, DSBPLAY_LOOPING) == DS_OK) ? false : true; + ds->is_paused = (IDirectSoundBuffer_Play( + ds->dsb, 0, 0, DSBPLAY_LOOPING) == DS_OK) ? false : true; return (ds->is_paused) ? false : true; } @@ -461,15 +462,15 @@ static ssize_t dsound_write(void *data, const void *buf_, size_t size) fifo_write(ds->buffer, buf, avail); LeaveCriticalSection(&ds->crit); - buf += avail; - size -= avail; + buf += avail; + size -= avail; written += avail; if (ds->nonblock || !ds->thread_alive) break; if (avail == 0) - WaitForSingleObject(ds->event, INFINITE); + slock_lock(ds->event); } return written;