Convert the DSound backend to the new API

This commit is contained in:
Chris Robinson 2014-04-19 22:54:21 -07:00
parent bb969c3ccc
commit 32a2f95885
4 changed files with 292 additions and 223 deletions

View File

@ -86,7 +86,7 @@ static struct BackendInfo BackendList[] = {
{ "mmdevapi", NULL, alcMMDevApiInit, alcMMDevApiDeinit, alcMMDevApiProbe, EmptyFuncs }, { "mmdevapi", NULL, alcMMDevApiInit, alcMMDevApiDeinit, alcMMDevApiProbe, EmptyFuncs },
#endif #endif
#ifdef HAVE_DSOUND #ifdef HAVE_DSOUND
{ "dsound", NULL, alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs }, { "dsound", ALCdsoundBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
#endif #endif
#ifdef HAVE_WINMM #ifdef HAVE_WINMM
{ "winmm", NULL, alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs }, { "winmm", NULL, alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },

View File

@ -126,6 +126,7 @@ static const struct ALCbackendFactoryVtable T##_ALCbackendFactory_vtable = { \
ALCbackendFactory *ALCpulseBackendFactory_getFactory(void); ALCbackendFactory *ALCpulseBackendFactory_getFactory(void);
ALCbackendFactory *ALCalsaBackendFactory_getFactory(void); ALCbackendFactory *ALCalsaBackendFactory_getFactory(void);
ALCbackendFactory *ALCossBackendFactory_getFactory(void); ALCbackendFactory *ALCossBackendFactory_getFactory(void);
ALCbackendFactory *ALCdsoundBackendFactory_getFactory(void);
ALCbackendFactory *ALCnullBackendFactory_getFactory(void); ALCbackendFactory *ALCnullBackendFactory_getFactory(void);
ALCbackendFactory *ALCloopbackFactory_getFactory(void); ALCbackendFactory *ALCloopbackFactory_getFactory(void);

View File

@ -38,6 +38,8 @@
#include "compat.h" #include "compat.h"
#include "alstring.h" #include "alstring.h"
#include "backends/base.h"
#ifndef DSSPEAKER_5POINT1 #ifndef DSSPEAKER_5POINT1
# define DSSPEAKER_5POINT1 0x00000006 # define DSSPEAKER_5POINT1 0x00000006
#endif #endif
@ -68,41 +70,6 @@ static HRESULT (WINAPI *pDirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW pDSEnum
#define DirectSoundCaptureEnumerateW pDirectSoundCaptureEnumerateW #define DirectSoundCaptureEnumerateW pDirectSoundCaptureEnumerateW
typedef struct {
// DirectSound Playback Device
IDirectSound *DS;
IDirectSoundBuffer *PrimaryBuffer;
IDirectSoundBuffer *Buffer;
IDirectSoundNotify *Notifies;
HANDLE NotifyEvent;
volatile int killNow;
althrd_t thread;
} DSoundPlaybackData;
typedef struct {
// DirectSound Capture Device
IDirectSoundCapture *DSC;
IDirectSoundCaptureBuffer *DSCbuffer;
DWORD BufferBytes;
DWORD Cursor;
RingBuffer *Ring;
} DSoundCaptureData;
typedef struct {
al_string name;
GUID guid;
} DevMap;
DECL_VECTOR(DevMap)
vector_DevMap PlaybackDevices;
vector_DevMap CaptureDevices;
#define MAX_UPDATES 128
static ALCboolean DSoundLoad(void) static ALCboolean DSoundLoad(void)
{ {
if(!ds_handle) if(!ds_handle)
@ -132,6 +99,17 @@ static ALCboolean DSoundLoad(void)
} }
#define MAX_UPDATES 128
typedef struct {
al_string name;
GUID guid;
} DevMap;
DECL_VECTOR(DevMap)
vector_DevMap PlaybackDevices;
vector_DevMap CaptureDevices;
static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCWSTR desc, LPCWSTR UNUSED(drvname), LPVOID data) static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCWSTR desc, LPCWSTR UNUSED(drvname), LPVOID data)
{ {
vector_DevMap *devices = data; vector_DevMap *devices = data;
@ -180,10 +158,49 @@ static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCWSTR desc, LPCWSTR UNUSED
} }
FORCE_ALIGN static int DSoundPlaybackProc(void *ptr) typedef struct ALCdsoundPlayback {
DERIVE_FROM_TYPE(ALCbackend);
IDirectSound *DS;
IDirectSoundBuffer *PrimaryBuffer;
IDirectSoundBuffer *Buffer;
IDirectSoundNotify *Notifies;
HANDLE NotifyEvent;
volatile int killNow;
althrd_t thread;
} ALCdsoundPlayback;
static int ALCdsoundPlayback_mixerProc(void *ptr);
static void ALCdsoundPlayback_Construct(ALCdsoundPlayback *self, ALCdevice *device);
static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, Destruct)
static ALCenum ALCdsoundPlayback_open(ALCdsoundPlayback *self, const ALCchar *name);
static void ALCdsoundPlayback_close(ALCdsoundPlayback *self);
static ALCboolean ALCdsoundPlayback_reset(ALCdsoundPlayback *self);
static ALCboolean ALCdsoundPlayback_start(ALCdsoundPlayback *self);
static void ALCdsoundPlayback_stop(ALCdsoundPlayback *self);
static DECLARE_FORWARD2(ALCdsoundPlayback, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, ALCuint, availableSamples)
static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, ALint64, getLatency)
static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, lock)
static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, unlock)
DECLARE_DEFAULT_ALLOCATORS(ALCdsoundPlayback)
DEFINE_ALCBACKEND_VTABLE(ALCdsoundPlayback);
static void ALCdsoundPlayback_Construct(ALCdsoundPlayback *self, ALCdevice *device)
{ {
ALCdevice *Device = (ALCdevice*)ptr; ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
DSoundPlaybackData *data = (DSoundPlaybackData*)Device->ExtraData; SET_VTABLE2(ALCdsoundPlayback, ALCbackend, self);
}
FORCE_ALIGN static int ALCdsoundPlayback_mixerProc(void *ptr)
{
ALCdsoundPlayback *self = ptr;
ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
DSBCAPS DSBCaps; DSBCAPS DSBCaps;
DWORD LastCursor = 0; DWORD LastCursor = 0;
DWORD PlayCursor; DWORD PlayCursor;
@ -200,43 +217,43 @@ FORCE_ALIGN static int DSoundPlaybackProc(void *ptr)
memset(&DSBCaps, 0, sizeof(DSBCaps)); memset(&DSBCaps, 0, sizeof(DSBCaps));
DSBCaps.dwSize = sizeof(DSBCaps); DSBCaps.dwSize = sizeof(DSBCaps);
err = IDirectSoundBuffer_GetCaps(data->Buffer, &DSBCaps); err = IDirectSoundBuffer_GetCaps(self->Buffer, &DSBCaps);
if(FAILED(err)) if(FAILED(err))
{ {
ERR("Failed to get buffer caps: 0x%lx\n", err); ERR("Failed to get buffer caps: 0x%lx\n", err);
ALCdevice_Lock(Device); ALCdevice_Lock(device);
aluHandleDisconnect(Device); aluHandleDisconnect(device);
ALCdevice_Unlock(Device); ALCdevice_Unlock(device);
return 1; return 1;
} }
FrameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType); FrameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
FragSize = Device->UpdateSize * FrameSize; FragSize = device->UpdateSize * FrameSize;
IDirectSoundBuffer_GetCurrentPosition(data->Buffer, &LastCursor, NULL); IDirectSoundBuffer_GetCurrentPosition(self->Buffer, &LastCursor, NULL);
while(!data->killNow) while(!self->killNow)
{ {
// Get current play cursor // Get current play cursor
IDirectSoundBuffer_GetCurrentPosition(data->Buffer, &PlayCursor, NULL); IDirectSoundBuffer_GetCurrentPosition(self->Buffer, &PlayCursor, NULL);
avail = (PlayCursor-LastCursor+DSBCaps.dwBufferBytes) % DSBCaps.dwBufferBytes; avail = (PlayCursor-LastCursor+DSBCaps.dwBufferBytes) % DSBCaps.dwBufferBytes;
if(avail < FragSize) if(avail < FragSize)
{ {
if(!Playing) if(!Playing)
{ {
err = IDirectSoundBuffer_Play(data->Buffer, 0, 0, DSBPLAY_LOOPING); err = IDirectSoundBuffer_Play(self->Buffer, 0, 0, DSBPLAY_LOOPING);
if(FAILED(err)) if(FAILED(err))
{ {
ERR("Failed to play buffer: 0x%lx\n", err); ERR("Failed to play buffer: 0x%lx\n", err);
ALCdevice_Lock(Device); ALCdevice_Lock(device);
aluHandleDisconnect(Device); aluHandleDisconnect(device);
ALCdevice_Unlock(Device); ALCdevice_Unlock(device);
return 1; return 1;
} }
Playing = TRUE; Playing = TRUE;
} }
avail = WaitForSingleObjectEx(data->NotifyEvent, 2000, FALSE); avail = WaitForSingleObjectEx(self->NotifyEvent, 2000, FALSE);
if(avail != WAIT_OBJECT_0) if(avail != WAIT_OBJECT_0)
ERR("WaitForSingleObjectEx error: 0x%lx\n", avail); ERR("WaitForSingleObjectEx error: 0x%lx\n", avail);
continue; continue;
@ -246,18 +263,18 @@ FORCE_ALIGN static int DSoundPlaybackProc(void *ptr)
// Lock output buffer // Lock output buffer
WriteCnt1 = 0; WriteCnt1 = 0;
WriteCnt2 = 0; WriteCnt2 = 0;
err = IDirectSoundBuffer_Lock(data->Buffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0); err = IDirectSoundBuffer_Lock(self->Buffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
// If the buffer is lost, restore it and lock // If the buffer is lost, restore it and lock
if(err == DSERR_BUFFERLOST) if(err == DSERR_BUFFERLOST)
{ {
WARN("Buffer lost, restoring...\n"); WARN("Buffer lost, restoring...\n");
err = IDirectSoundBuffer_Restore(data->Buffer); err = IDirectSoundBuffer_Restore(self->Buffer);
if(SUCCEEDED(err)) if(SUCCEEDED(err))
{ {
Playing = FALSE; Playing = FALSE;
LastCursor = 0; LastCursor = 0;
err = IDirectSoundBuffer_Lock(data->Buffer, 0, DSBCaps.dwBufferBytes, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0); err = IDirectSoundBuffer_Lock(self->Buffer, 0, DSBCaps.dwBufferBytes, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
} }
} }
@ -265,18 +282,18 @@ FORCE_ALIGN static int DSoundPlaybackProc(void *ptr)
if(SUCCEEDED(err)) if(SUCCEEDED(err))
{ {
// If we have an active context, mix data directly into output buffer otherwise fill with silence // If we have an active context, mix data directly into output buffer otherwise fill with silence
aluMixData(Device, WritePtr1, WriteCnt1/FrameSize); aluMixData(device, WritePtr1, WriteCnt1/FrameSize);
aluMixData(Device, WritePtr2, WriteCnt2/FrameSize); aluMixData(device, WritePtr2, WriteCnt2/FrameSize);
// Unlock output buffer only when successfully locked // Unlock output buffer only when successfully locked
IDirectSoundBuffer_Unlock(data->Buffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2); IDirectSoundBuffer_Unlock(self->Buffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
} }
else else
{ {
ERR("Buffer lock error: %#lx\n", err); ERR("Buffer lock error: %#lx\n", err);
ALCdevice_Lock(Device); ALCdevice_Lock(device);
aluHandleDisconnect(Device); aluHandleDisconnect(device);
ALCdevice_Unlock(Device); ALCdevice_Unlock(device);
return 1; return 1;
} }
@ -288,9 +305,9 @@ FORCE_ALIGN static int DSoundPlaybackProc(void *ptr)
return 0; return 0;
} }
static ALCenum DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName) static ALCenum ALCdsoundPlayback_open(ALCdsoundPlayback *self, const ALCchar *deviceName)
{ {
DSoundPlaybackData *data = NULL; ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
LPGUID guid = NULL; LPGUID guid = NULL;
HRESULT hr, hrcom; HRESULT hr, hrcom;
@ -328,60 +345,55 @@ static ALCenum DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
return ALC_INVALID_VALUE; return ALC_INVALID_VALUE;
} }
//Initialise requested device
data = calloc(1, sizeof(DSoundPlaybackData));
if(!data)
return ALC_OUT_OF_MEMORY;
hr = DS_OK; hr = DS_OK;
data->NotifyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); self->NotifyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if(data->NotifyEvent == NULL) if(self->NotifyEvent == NULL)
hr = E_FAIL; hr = E_FAIL;
//DirectSound Init code //DirectSound Init code
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
hr = DirectSoundCreate(guid, &data->DS, NULL); hr = DirectSoundCreate(guid, &self->DS, NULL);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
hr = IDirectSound_SetCooperativeLevel(data->DS, GetForegroundWindow(), DSSCL_PRIORITY); hr = IDirectSound_SetCooperativeLevel(self->DS, GetForegroundWindow(), DSSCL_PRIORITY);
if(FAILED(hr)) if(FAILED(hr))
{ {
if(data->DS) if(self->DS)
IDirectSound_Release(data->DS); IDirectSound_Release(self->DS);
if(data->NotifyEvent) self->DS = NULL;
CloseHandle(data->NotifyEvent); if(self->NotifyEvent)
free(data); CloseHandle(self->NotifyEvent);
self->NotifyEvent = NULL;
ERR("Device init failed: 0x%08lx\n", hr); ERR("Device init failed: 0x%08lx\n", hr);
return ALC_INVALID_VALUE; return ALC_INVALID_VALUE;
} }
al_string_copy_cstr(&device->DeviceName, deviceName); al_string_copy_cstr(&device->DeviceName, deviceName);
device->ExtraData = data;
return ALC_NO_ERROR; return ALC_NO_ERROR;
} }
static void DSoundClosePlayback(ALCdevice *device) static void ALCdsoundPlayback_close(ALCdsoundPlayback *self)
{ {
DSoundPlaybackData *data = device->ExtraData; if(self->Notifies)
IDirectSoundNotify_Release(self->Notifies);
self->Notifies = NULL;
if(self->Buffer)
IDirectSoundBuffer_Release(self->Buffer);
self->Buffer = NULL;
if(self->PrimaryBuffer != NULL)
IDirectSoundBuffer_Release(self->PrimaryBuffer);
self->PrimaryBuffer = NULL;
if(data->Notifies) IDirectSound_Release(self->DS);
IDirectSoundNotify_Release(data->Notifies); self->DS = NULL;
data->Notifies = NULL; CloseHandle(self->NotifyEvent);
if(data->Buffer) self->NotifyEvent = NULL;
IDirectSoundBuffer_Release(data->Buffer);
data->Buffer = NULL;
if(data->PrimaryBuffer != NULL)
IDirectSoundBuffer_Release(data->PrimaryBuffer);
data->PrimaryBuffer = NULL;
IDirectSound_Release(data->DS);
CloseHandle(data->NotifyEvent);
free(data);
device->ExtraData = NULL;
} }
static ALCboolean DSoundResetPlayback(ALCdevice *device) static ALCboolean ALCdsoundPlayback_reset(ALCdsoundPlayback *self)
{ {
DSoundPlaybackData *data = (DSoundPlaybackData*)device->ExtraData; ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
DSBUFFERDESC DSBDescription; DSBUFFERDESC DSBDescription;
WAVEFORMATEXTENSIBLE OutputType; WAVEFORMATEXTENSIBLE OutputType;
DWORD speakers; DWORD speakers;
@ -389,15 +401,15 @@ static ALCboolean DSoundResetPlayback(ALCdevice *device)
memset(&OutputType, 0, sizeof(OutputType)); memset(&OutputType, 0, sizeof(OutputType));
if(data->Notifies) if(self->Notifies)
IDirectSoundNotify_Release(data->Notifies); IDirectSoundNotify_Release(self->Notifies);
data->Notifies = NULL; self->Notifies = NULL;
if(data->Buffer) if(self->Buffer)
IDirectSoundBuffer_Release(data->Buffer); IDirectSoundBuffer_Release(self->Buffer);
data->Buffer = NULL; self->Buffer = NULL;
if(data->PrimaryBuffer != NULL) if(self->PrimaryBuffer != NULL)
IDirectSoundBuffer_Release(data->PrimaryBuffer); IDirectSoundBuffer_Release(self->PrimaryBuffer);
data->PrimaryBuffer = NULL; self->PrimaryBuffer = NULL;
switch(device->FmtType) switch(device->FmtType)
{ {
@ -420,7 +432,7 @@ static ALCboolean DSoundResetPlayback(ALCdevice *device)
break; break;
} }
hr = IDirectSound_GetSpeakerConfig(data->DS, &speakers); hr = IDirectSound_GetSpeakerConfig(self->DS, &speakers);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
if(!(device->Flags&DEVICE_CHANNELS_REQUEST)) if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
@ -513,21 +525,21 @@ retry_open:
else else
OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
if(data->PrimaryBuffer) if(self->PrimaryBuffer)
IDirectSoundBuffer_Release(data->PrimaryBuffer); IDirectSoundBuffer_Release(self->PrimaryBuffer);
data->PrimaryBuffer = NULL; self->PrimaryBuffer = NULL;
} }
else else
{ {
if(SUCCEEDED(hr) && !data->PrimaryBuffer) if(SUCCEEDED(hr) && !self->PrimaryBuffer)
{ {
memset(&DSBDescription,0,sizeof(DSBUFFERDESC)); memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
DSBDescription.dwSize=sizeof(DSBUFFERDESC); DSBDescription.dwSize=sizeof(DSBUFFERDESC);
DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER; DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
hr = IDirectSound_CreateSoundBuffer(data->DS, &DSBDescription, &data->PrimaryBuffer, NULL); hr = IDirectSound_CreateSoundBuffer(self->DS, &DSBDescription, &self->PrimaryBuffer, NULL);
} }
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
hr = IDirectSoundBuffer_SetFormat(data->PrimaryBuffer,&OutputType.Format); hr = IDirectSoundBuffer_SetFormat(self->PrimaryBuffer,&OutputType.Format);
} }
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
@ -545,7 +557,7 @@ retry_open:
DSBDescription.dwBufferBytes=device->UpdateSize * device->NumUpdates * DSBDescription.dwBufferBytes=device->UpdateSize * device->NumUpdates *
OutputType.Format.nBlockAlign; OutputType.Format.nBlockAlign;
DSBDescription.lpwfxFormat=&OutputType.Format; DSBDescription.lpwfxFormat=&OutputType.Format;
hr = IDirectSound_CreateSoundBuffer(data->DS, &DSBDescription, &data->Buffer, NULL); hr = IDirectSound_CreateSoundBuffer(self->DS, &DSBDescription, &self->Buffer, NULL);
if(FAILED(hr) && device->FmtType == DevFmtFloat) if(FAILED(hr) && device->FmtType == DevFmtFloat)
{ {
device->FmtType = DevFmtShort; device->FmtType = DevFmtShort;
@ -555,7 +567,7 @@ retry_open:
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
hr = IDirectSoundBuffer_QueryInterface(data->Buffer, &IID_IDirectSoundNotify, (LPVOID *)&data->Notifies); hr = IDirectSoundBuffer_QueryInterface(self->Buffer, &IID_IDirectSoundNotify, (LPVOID *)&self->Notifies);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
DSBPOSITIONNOTIFY notifies[MAX_UPDATES]; DSBPOSITIONNOTIFY notifies[MAX_UPDATES];
@ -565,62 +577,93 @@ retry_open:
{ {
notifies[i].dwOffset = i * device->UpdateSize * notifies[i].dwOffset = i * device->UpdateSize *
OutputType.Format.nBlockAlign; OutputType.Format.nBlockAlign;
notifies[i].hEventNotify = data->NotifyEvent; notifies[i].hEventNotify = self->NotifyEvent;
} }
if(IDirectSoundNotify_SetNotificationPositions(data->Notifies, device->NumUpdates, notifies) != DS_OK) if(IDirectSoundNotify_SetNotificationPositions(self->Notifies, device->NumUpdates, notifies) != DS_OK)
hr = E_FAIL; hr = E_FAIL;
} }
} }
if(FAILED(hr)) if(FAILED(hr))
{ {
if(data->Notifies != NULL) if(self->Notifies != NULL)
IDirectSoundNotify_Release(data->Notifies); IDirectSoundNotify_Release(self->Notifies);
data->Notifies = NULL; self->Notifies = NULL;
if(data->Buffer != NULL) if(self->Buffer != NULL)
IDirectSoundBuffer_Release(data->Buffer); IDirectSoundBuffer_Release(self->Buffer);
data->Buffer = NULL; self->Buffer = NULL;
if(data->PrimaryBuffer != NULL) if(self->PrimaryBuffer != NULL)
IDirectSoundBuffer_Release(data->PrimaryBuffer); IDirectSoundBuffer_Release(self->PrimaryBuffer);
data->PrimaryBuffer = NULL; self->PrimaryBuffer = NULL;
return ALC_FALSE; return ALC_FALSE;
} }
ResetEvent(data->NotifyEvent); ResetEvent(self->NotifyEvent);
SetDefaultWFXChannelOrder(device); SetDefaultWFXChannelOrder(device);
return ALC_TRUE; return ALC_TRUE;
} }
static ALCboolean DSoundStartPlayback(ALCdevice *device) static ALCboolean ALCdsoundPlayback_start(ALCdsoundPlayback *self)
{ {
DSoundPlaybackData *data = (DSoundPlaybackData*)device->ExtraData; self->killNow = 0;
if(althrd_create(&self->thread, ALCdsoundPlayback_mixerProc, self) != althrd_success)
data->killNow = 0;
if(althrd_create(&data->thread, DSoundPlaybackProc, device) != althrd_success)
return ALC_FALSE; return ALC_FALSE;
return ALC_TRUE; return ALC_TRUE;
} }
static void DSoundStopPlayback(ALCdevice *device) static void ALCdsoundPlayback_stop(ALCdsoundPlayback *self)
{ {
DSoundPlaybackData *data = device->ExtraData;
int res; int res;
if(data->killNow) if(self->killNow)
return; return;
data->killNow = 1; self->killNow = 1;
althrd_join(data->thread, &res); althrd_join(self->thread, &res);
IDirectSoundBuffer_Stop(data->Buffer); IDirectSoundBuffer_Stop(self->Buffer);
} }
static ALCenum DSoundOpenCapture(ALCdevice *device, const ALCchar *deviceName)
typedef struct ALCdsoundCapture {
DERIVE_FROM_TYPE(ALCbackend);
IDirectSoundCapture *DSC;
IDirectSoundCaptureBuffer *DSCbuffer;
DWORD BufferBytes;
DWORD Cursor;
RingBuffer *Ring;
} ALCdsoundCapture;
static void ALCdsoundCapture_Construct(ALCdsoundCapture *self, ALCdevice *device);
static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, Destruct)
static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *name);
static void ALCdsoundCapture_close(ALCdsoundCapture *self);
static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, ALCboolean, reset)
static ALCboolean ALCdsoundCapture_start(ALCdsoundCapture *self);
static void ALCdsoundCapture_stop(ALCdsoundCapture *self);
static ALCenum ALCdsoundCapture_captureSamples(ALCdsoundCapture *self, ALCvoid *buffer, ALCuint samples);
static ALCuint ALCdsoundCapture_availableSamples(ALCdsoundCapture *self);
static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, ALint64, getLatency)
static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, lock)
static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, unlock)
DECLARE_DEFAULT_ALLOCATORS(ALCdsoundCapture)
DEFINE_ALCBACKEND_VTABLE(ALCdsoundCapture);
static void ALCdsoundCapture_Construct(ALCdsoundCapture *self, ALCdevice *device)
{ {
DSoundCaptureData *data = NULL; ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
SET_VTABLE2(ALCdsoundCapture, ALCbackend, self);
}
static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *deviceName)
{
ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
WAVEFORMATEXTENSIBLE InputType; WAVEFORMATEXTENSIBLE InputType;
DSCBUFFERDESC DSCBDescription; DSCBUFFERDESC DSCBDescription;
LPGUID guid = NULL; LPGUID guid = NULL;
@ -676,16 +719,8 @@ static ALCenum DSoundOpenCapture(ALCdevice *device, const ALCchar *deviceName)
break; break;
} }
//Initialise requested device
data = calloc(1, sizeof(DSoundCaptureData));
if(!data)
return ALC_OUT_OF_MEMORY;
hr = DS_OK;
//DirectSoundCapture Init code //DirectSoundCapture Init code
if(SUCCEEDED(hr)) hr = DirectSoundCaptureCreate(guid, &self->DSC, NULL);
hr = DirectSoundCaptureCreate(guid, &data->DSC, NULL);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
memset(&InputType, 0, sizeof(InputType)); memset(&InputType, 0, sizeof(InputType));
@ -770,12 +805,12 @@ static ALCenum DSoundOpenCapture(ALCdevice *device, const ALCchar *deviceName)
DSCBDescription.dwBufferBytes = samples * InputType.Format.nBlockAlign; DSCBDescription.dwBufferBytes = samples * InputType.Format.nBlockAlign;
DSCBDescription.lpwfxFormat = &InputType.Format; DSCBDescription.lpwfxFormat = &InputType.Format;
hr = IDirectSoundCapture_CreateCaptureBuffer(data->DSC, &DSCBDescription, &data->DSCbuffer, NULL); hr = IDirectSoundCapture_CreateCaptureBuffer(self->DSC, &DSCBDescription, &self->DSCbuffer, NULL);
} }
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
data->Ring = CreateRingBuffer(InputType.Format.nBlockAlign, device->UpdateSize * device->NumUpdates); self->Ring = CreateRingBuffer(InputType.Format.nBlockAlign, device->UpdateSize * device->NumUpdates);
if(data->Ring == NULL) if(self->Ring == NULL)
hr = DSERR_OUTOFMEMORY; hr = DSERR_OUTOFMEMORY;
} }
@ -783,158 +818,156 @@ static ALCenum DSoundOpenCapture(ALCdevice *device, const ALCchar *deviceName)
{ {
ERR("Device init failed: 0x%08lx\n", hr); ERR("Device init failed: 0x%08lx\n", hr);
DestroyRingBuffer(data->Ring); DestroyRingBuffer(self->Ring);
data->Ring = NULL; self->Ring = NULL;
if(data->DSCbuffer != NULL) if(self->DSCbuffer != NULL)
IDirectSoundCaptureBuffer_Release(data->DSCbuffer); IDirectSoundCaptureBuffer_Release(self->DSCbuffer);
data->DSCbuffer = NULL; self->DSCbuffer = NULL;
if(data->DSC) if(self->DSC)
IDirectSoundCapture_Release(data->DSC); IDirectSoundCapture_Release(self->DSC);
data->DSC = NULL; self->DSC = NULL;
free(data);
return ALC_INVALID_VALUE; return ALC_INVALID_VALUE;
} }
data->BufferBytes = DSCBDescription.dwBufferBytes; self->BufferBytes = DSCBDescription.dwBufferBytes;
SetDefaultWFXChannelOrder(device); SetDefaultWFXChannelOrder(device);
al_string_copy_cstr(&device->DeviceName, deviceName); al_string_copy_cstr(&device->DeviceName, deviceName);
device->ExtraData = data;
return ALC_NO_ERROR; return ALC_NO_ERROR;
} }
static void DSoundCloseCapture(ALCdevice *device) static void ALCdsoundCapture_close(ALCdsoundCapture *self)
{ {
DSoundCaptureData *data = device->ExtraData; DestroyRingBuffer(self->Ring);
self->Ring = NULL;
DestroyRingBuffer(data->Ring); if(self->DSCbuffer != NULL)
data->Ring = NULL;
if(data->DSCbuffer != NULL)
{ {
IDirectSoundCaptureBuffer_Stop(data->DSCbuffer); IDirectSoundCaptureBuffer_Stop(self->DSCbuffer);
IDirectSoundCaptureBuffer_Release(data->DSCbuffer); IDirectSoundCaptureBuffer_Release(self->DSCbuffer);
data->DSCbuffer = NULL; self->DSCbuffer = NULL;
} }
IDirectSoundCapture_Release(data->DSC); IDirectSoundCapture_Release(self->DSC);
data->DSC = NULL; self->DSC = NULL;
free(data);
device->ExtraData = NULL;
} }
static void DSoundStartCapture(ALCdevice *device) static ALCboolean ALCdsoundCapture_start(ALCdsoundCapture *self)
{ {
DSoundCaptureData *data = device->ExtraData;
HRESULT hr; HRESULT hr;
hr = IDirectSoundCaptureBuffer_Start(data->DSCbuffer, DSCBSTART_LOOPING); hr = IDirectSoundCaptureBuffer_Start(self->DSCbuffer, DSCBSTART_LOOPING);
if(FAILED(hr)) if(FAILED(hr))
{ {
ERR("start failed: 0x%08lx\n", hr); ERR("start failed: 0x%08lx\n", hr);
aluHandleDisconnect(device); aluHandleDisconnect(STATIC_CAST(ALCbackend, self)->mDevice);
return ALC_FALSE;
} }
return ALC_TRUE;
} }
static void DSoundStopCapture(ALCdevice *device) static void ALCdsoundCapture_stop(ALCdsoundCapture *self)
{ {
DSoundCaptureData *data = device->ExtraData;
HRESULT hr; HRESULT hr;
hr = IDirectSoundCaptureBuffer_Stop(data->DSCbuffer); hr = IDirectSoundCaptureBuffer_Stop(self->DSCbuffer);
if(FAILED(hr)) if(FAILED(hr))
{ {
ERR("stop failed: 0x%08lx\n", hr); ERR("stop failed: 0x%08lx\n", hr);
aluHandleDisconnect(device); aluHandleDisconnect(STATIC_CAST(ALCbackend, self)->mDevice);
} }
} }
static ALCenum DSoundCaptureSamples(ALCdevice *Device, ALCvoid *pBuffer, ALCuint lSamples) static ALCenum ALCdsoundCapture_captureSamples(ALCdsoundCapture *self, ALCvoid *buffer, ALCuint samples)
{ {
DSoundCaptureData *data = Device->ExtraData; ReadRingBuffer(self->Ring, buffer, samples);
ReadRingBuffer(data->Ring, pBuffer, lSamples);
return ALC_NO_ERROR; return ALC_NO_ERROR;
} }
static ALCuint DSoundAvailableSamples(ALCdevice *Device) static ALCuint ALCdsoundCapture_availableSamples(ALCdsoundCapture *self)
{ {
DSoundCaptureData *data = Device->ExtraData; ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
DWORD ReadCursor, LastCursor, BufferBytes, NumBytes; DWORD ReadCursor, LastCursor, BufferBytes, NumBytes;
VOID *ReadPtr1, *ReadPtr2; VOID *ReadPtr1, *ReadPtr2;
DWORD ReadCnt1, ReadCnt2; DWORD ReadCnt1, ReadCnt2;
DWORD FrameSize; DWORD FrameSize;
HRESULT hr; HRESULT hr;
if(!Device->Connected) if(!device->Connected)
goto done; goto done;
FrameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType); FrameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
BufferBytes = data->BufferBytes; BufferBytes = self->BufferBytes;
LastCursor = data->Cursor; LastCursor = self->Cursor;
hr = IDirectSoundCaptureBuffer_GetCurrentPosition(data->DSCbuffer, NULL, &ReadCursor); hr = IDirectSoundCaptureBuffer_GetCurrentPosition(self->DSCbuffer, NULL, &ReadCursor);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
NumBytes = (ReadCursor-LastCursor + BufferBytes) % BufferBytes; NumBytes = (ReadCursor-LastCursor + BufferBytes) % BufferBytes;
if(NumBytes == 0) if(NumBytes == 0)
goto done; goto done;
hr = IDirectSoundCaptureBuffer_Lock(data->DSCbuffer, LastCursor, NumBytes, hr = IDirectSoundCaptureBuffer_Lock(self->DSCbuffer, LastCursor, NumBytes,
&ReadPtr1, &ReadCnt1, &ReadPtr1, &ReadCnt1,
&ReadPtr2, &ReadCnt2, 0); &ReadPtr2, &ReadCnt2, 0);
} }
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
WriteRingBuffer(data->Ring, ReadPtr1, ReadCnt1/FrameSize); WriteRingBuffer(self->Ring, ReadPtr1, ReadCnt1/FrameSize);
if(ReadPtr2 != NULL) if(ReadPtr2 != NULL)
WriteRingBuffer(data->Ring, ReadPtr2, ReadCnt2/FrameSize); WriteRingBuffer(self->Ring, ReadPtr2, ReadCnt2/FrameSize);
hr = IDirectSoundCaptureBuffer_Unlock(data->DSCbuffer, hr = IDirectSoundCaptureBuffer_Unlock(self->DSCbuffer,
ReadPtr1, ReadCnt1, ReadPtr1, ReadCnt1,
ReadPtr2, ReadCnt2); ReadPtr2, ReadCnt2);
data->Cursor = (LastCursor+ReadCnt1+ReadCnt2) % BufferBytes; self->Cursor = (LastCursor+ReadCnt1+ReadCnt2) % BufferBytes;
} }
if(FAILED(hr)) if(FAILED(hr))
{ {
ERR("update failed: 0x%08lx\n", hr); ERR("update failed: 0x%08lx\n", hr);
aluHandleDisconnect(Device); aluHandleDisconnect(device);
} }
done: done:
return RingBufferSize(data->Ring); return RingBufferSize(self->Ring);
} }
static const BackendFuncs DSoundFuncs = { typedef struct ALCdsoundBackendFactory {
DSoundOpenPlayback, DERIVE_FROM_TYPE(ALCbackendFactory);
DSoundClosePlayback, } ALCdsoundBackendFactory;
DSoundResetPlayback, #define ALCDSOUNDBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCdsoundBackendFactory, ALCbackendFactory) } }
DSoundStartPlayback,
DSoundStopPlayback, ALCbackendFactory *ALCdsoundBackendFactory_getFactory(void);
DSoundOpenCapture,
DSoundCloseCapture, static ALCboolean ALCdsoundBackendFactory_init(ALCdsoundBackendFactory *self);
DSoundStartCapture, static void ALCdsoundBackendFactory_deinit(ALCdsoundBackendFactory *self);
DSoundStopCapture, static ALCboolean ALCdsoundBackendFactory_querySupport(ALCdsoundBackendFactory *self, ALCbackend_Type type);
DSoundCaptureSamples, static void ALCdsoundBackendFactory_probe(ALCdsoundBackendFactory *self, enum DevProbe type);
DSoundAvailableSamples, static ALCbackend* ALCdsoundBackendFactory_createBackend(ALCdsoundBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
ALCdevice_GetLatencyDefault DEFINE_ALCBACKENDFACTORY_VTABLE(ALCdsoundBackendFactory);
};
ALCboolean alcDSoundInit(BackendFuncs *FuncList) ALCbackendFactory *ALCdsoundBackendFactory_getFactory(void)
{
static ALCdsoundBackendFactory factory = ALCDSOUNDBACKENDFACTORY_INITIALIZER;
return STATIC_CAST(ALCbackendFactory, &factory);
}
static ALCboolean ALCdsoundBackendFactory_init(ALCdsoundBackendFactory* UNUSED(self))
{ {
VECTOR_INIT(PlaybackDevices); VECTOR_INIT(PlaybackDevices);
VECTOR_INIT(CaptureDevices); VECTOR_INIT(CaptureDevices);
if(!DSoundLoad()) if(!DSoundLoad())
return ALC_FALSE; return ALC_FALSE;
*FuncList = DSoundFuncs;
return ALC_TRUE; return ALC_TRUE;
} }
void alcDSoundDeinit(void) static void ALCdsoundBackendFactory_deinit(ALCdsoundBackendFactory* UNUSED(self))
{ {
DevMap *iter, *end; DevMap *iter, *end;
@ -955,7 +988,14 @@ void alcDSoundDeinit(void)
ds_handle = NULL; ds_handle = NULL;
} }
void alcDSoundProbe(enum DevProbe type) static ALCboolean ALCdsoundBackendFactory_querySupport(ALCdsoundBackendFactory* UNUSED(self), ALCbackend_Type type)
{
if(type == ALCbackend_Playback || type == ALCbackend_Capture)
return ALC_TRUE;
return ALC_FALSE;
}
static void ALCdsoundBackendFactory_probe(ALCdsoundBackendFactory* UNUSED(self), enum DevProbe type)
{ {
DevMap *iter, *end; DevMap *iter, *end;
HRESULT hr, hrcom; HRESULT hr, hrcom;
@ -1005,3 +1045,34 @@ void alcDSoundProbe(enum DevProbe type)
if(SUCCEEDED(hrcom)) if(SUCCEEDED(hrcom))
CoUninitialize(); CoUninitialize();
} }
static ALCbackend* ALCdsoundBackendFactory_createBackend(ALCdsoundBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
{
if(type == ALCbackend_Playback)
{
ALCdsoundPlayback *backend;
backend = ALCdsoundPlayback_New(sizeof(*backend));
if(!backend) return NULL;
memset(backend, 0, sizeof(*backend));
ALCdsoundPlayback_Construct(backend, device);
return STATIC_CAST(ALCbackend, backend);
}
if(type == ALCbackend_Capture)
{
ALCdsoundCapture *backend;
backend = ALCdsoundCapture_New(sizeof(*backend));
if(!backend) return NULL;
memset(backend, 0, sizeof(*backend));
ALCdsoundCapture_Construct(backend, device);
return STATIC_CAST(ALCbackend, backend);
}
return NULL;
}

View File

@ -506,9 +506,6 @@ void alc_sndio_probe(enum DevProbe type);
ALCboolean alcMMDevApiInit(BackendFuncs *func_list); ALCboolean alcMMDevApiInit(BackendFuncs *func_list);
void alcMMDevApiDeinit(void); void alcMMDevApiDeinit(void);
void alcMMDevApiProbe(enum DevProbe type); void alcMMDevApiProbe(enum DevProbe type);
ALCboolean alcDSoundInit(BackendFuncs *func_list);
void alcDSoundDeinit(void);
void alcDSoundProbe(enum DevProbe type);
ALCboolean alcWinMMInit(BackendFuncs *FuncList); ALCboolean alcWinMMInit(BackendFuncs *FuncList);
void alcWinMMDeinit(void); void alcWinMMDeinit(void);
void alcWinMMProbe(enum DevProbe type); void alcWinMMProbe(enum DevProbe type);