From 3eaee5609cbebbd9598e641a53e672be8640477a Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 5 Aug 2011 00:40:38 -0700 Subject: [PATCH] Make the primary buffer a non-pointer member of the device --- dsound8.c | 62 +++++++++++++++++++++++++++----------------------------- dsound_private.h | 55 +++++++++++++++++++++++++------------------------ primary.c | 16 +++++++-------- 3 files changed, 66 insertions(+), 67 deletions(-) diff --git a/dsound8.c b/dsound8.c index 324cefd..5276883 100644 --- a/dsound8.c +++ b/dsound8.c @@ -363,9 +363,7 @@ HRESULT DSOUND_Create8(REFIID riid, LPVOID *ds) static void DS8Impl_Destroy(DS8Impl *This) { - if(This->primary) - DS8Primary_Destroy(This->primary); - This->primary = NULL; + DS8Primary_Destroy(&This->primary); if(This->share) DSShare_Release(This->share); This->share = NULL; @@ -450,7 +448,7 @@ static HRESULT WINAPI DS8_CreateSoundBuffer(IDirectSound8 *iface, LPCDSBUFFERDES return DSERR_INVALIDPARAM; } - if(!This->primary) + if(!This->share) { WARN("Device not initialized\n"); return DSERR_UNINITIALIZED; @@ -486,10 +484,10 @@ static HRESULT WINAPI DS8_CreateSoundBuffer(IDirectSound8 *iface, LPCDSBUFFERDES return DSERR_INVALIDPARAM; } - EnterCriticalSection(This->primary->crst); + EnterCriticalSection(This->primary.crst); if((desc->dwFlags&DSBCAPS_PRIMARYBUFFER)) { - IDirectSoundBuffer *prim = &This->primary->IDirectSoundBuffer_iface; + IDirectSoundBuffer *prim = &This->primary.IDirectSoundBuffer_iface; hr = S_OK; if(IDirectSoundBuffer_AddRef(prim) == 1) @@ -507,7 +505,7 @@ static HRESULT WINAPI DS8_CreateSoundBuffer(IDirectSound8 *iface, LPCDSBUFFERDES { DS8Buffer *dsb; - hr = DS8Buffer_Create(&dsb, This->primary, NULL); + hr = DS8Buffer_Create(&dsb, &This->primary, NULL); if(SUCCEEDED(hr)) { hr = IDirectSoundBuffer8_Initialize(&dsb->IDirectSoundBuffer8_iface, &This->IDirectSound_iface, desc); @@ -520,7 +518,7 @@ static HRESULT WINAPI DS8_CreateSoundBuffer(IDirectSound8 *iface, LPCDSBUFFERDES } } } - LeaveCriticalSection(This->primary->crst); + LeaveCriticalSection(This->primary.crst); TRACE("%08"LONGFMT"x\n", hr); return hr; @@ -532,7 +530,7 @@ static HRESULT WINAPI DS8_GetCaps(IDirectSound8 *iface, LPDSCAPS caps) TRACE("(%p)->(%p)\n", iface, caps); - if(!This->primary) + if(!This->share) { WARN("Device not initialized\n"); return DSERR_UNINITIALIZED; @@ -611,7 +609,7 @@ static HRESULT WINAPI DS8_DuplicateSoundBuffer(IDirectSound8 *iface, IDirectSoun hr = DSERR_INVALIDPARAM; } if(SUCCEEDED(hr)) - hr = DS8Buffer_Create(&buf, This->primary, in); + hr = DS8Buffer_Create(&buf, &This->primary, in); if(SUCCEEDED(hr)) { *out = &buf->IDirectSoundBuffer_iface; @@ -683,14 +681,14 @@ static HRESULT WINAPI DS8_SetCooperativeLevel(IDirectSound8 *iface, HWND hwnd, D return DSERR_INVALIDPARAM; } - EnterCriticalSection(This->primary->crst); + EnterCriticalSection(This->primary.crst); if(level == DSSCL_WRITEPRIMARY && (This->prio_level != DSSCL_WRITEPRIMARY)) { DWORD i, state; - for(i = 0; i < This->primary->nbuffers; ++i) + for(i = 0; i < This->primary.nbuffers; ++i) { - DS8Buffer *buf = This->primary->buffers[i]; + DS8Buffer *buf = This->primary.buffers[i]; if(FAILED(IDirectSoundBuffer_GetStatus(&buf->IDirectSoundBuffer8_iface, &state)) || (state&DSBSTATUS_PLAYING)) { @@ -701,14 +699,14 @@ static HRESULT WINAPI DS8_SetCooperativeLevel(IDirectSound8 *iface, HWND hwnd, D /* Mark buffer as lost */ buf->bufferlost = 1; } - if(This->primary->write_emu) + if(This->primary.write_emu) { ERR("Why was there a write_emu?\n"); /* Delete it */ - IDirectSoundBuffer8_Release(This->primary->write_emu); - This->primary->write_emu = NULL; + IDirectSoundBuffer8_Release(This->primary.write_emu); + This->primary.write_emu = NULL; } - if(This->primary->flags) + if(This->primary.flags) { /* Primary has open references.. create write_emu */ DSBUFFERDESC desc; @@ -716,34 +714,34 @@ static HRESULT WINAPI DS8_SetCooperativeLevel(IDirectSound8 *iface, HWND hwnd, D memset(&desc, 0, sizeof(desc)); desc.dwSize = sizeof(desc); - desc.dwFlags = DSBCAPS_LOCHARDWARE | (This->primary->flags&DSBCAPS_CTRLPAN); - desc.dwBufferBytes = This->primary->buf_size; - desc.lpwfxFormat = &This->primary->format.Format; + desc.dwFlags = DSBCAPS_LOCHARDWARE | (This->primary.flags&DSBCAPS_CTRLPAN); + desc.dwBufferBytes = This->primary.buf_size; + desc.lpwfxFormat = &This->primary.format.Format; - hr = DS8Buffer_Create(&emu, This->primary, NULL); + hr = DS8Buffer_Create(&emu, &This->primary, NULL); if(SUCCEEDED(hr)) { - This->primary->write_emu = &emu->IDirectSoundBuffer8_iface; - hr = IDirectSoundBuffer8_Initialize(This->primary->write_emu, &This->IDirectSound_iface, &desc); + This->primary.write_emu = &emu->IDirectSoundBuffer8_iface; + hr = IDirectSoundBuffer8_Initialize(This->primary.write_emu, &This->IDirectSound_iface, &desc); if(FAILED(hr)) { - IDirectSoundBuffer8_Release(This->primary->write_emu); - This->primary->write_emu = NULL; + IDirectSoundBuffer8_Release(This->primary.write_emu); + This->primary.write_emu = NULL; } } } } - else if(This->prio_level == DSSCL_WRITEPRIMARY && level != DSSCL_WRITEPRIMARY && This->primary->write_emu) + else if(This->prio_level == DSSCL_WRITEPRIMARY && level != DSSCL_WRITEPRIMARY && This->primary.write_emu) { TRACE("Nuking write_emu\n"); /* Delete it */ - IDirectSoundBuffer8_Release(This->primary->write_emu); - This->primary->write_emu = NULL; + IDirectSoundBuffer8_Release(This->primary.write_emu); + This->primary.write_emu = NULL; } if(SUCCEEDED(hr)) This->prio_level = level; out: - LeaveCriticalSection(This->primary->crst); + LeaveCriticalSection(This->primary.crst); return hr; } @@ -783,7 +781,7 @@ static HRESULT WINAPI DS8_GetSpeakerConfig(IDirectSound8 *iface, DWORD *config) return DSERR_INVALIDPARAM; *config = 0; - if(!This->primary) + if(!This->share) { WARN("Device not initialized\n"); return DSERR_UNINITIALIZED; @@ -852,7 +850,7 @@ static HRESULT WINAPI DS8_Initialize(IDirectSound8 *iface, const GUID *devguid) if(!openal_loaded) return DSERR_NODRIVER; - if(This->primary) + if(This->share) { WARN("Device already initialized\n"); return DSERR_ALREADYINITIALIZED; @@ -910,7 +908,7 @@ static HRESULT WINAPI DS8_VerifyCertification(IDirectSound8 *iface, DWORD *certi return DSERR_INVALIDPARAM; *certified = 0; - if(!This->primary) + if(!This->share) { WARN("Device not initialized\n"); return DSERR_UNINITIALIZED; diff --git a/dsound_private.h b/dsound_private.h index 4145683..3e8694f 100644 --- a/dsound_private.h +++ b/dsound_private.h @@ -295,6 +295,11 @@ extern LPALCGETCURRENTCONTEXT get_context; extern BOOL local_contexts; +typedef struct DS8Impl DS8Impl; +typedef struct DS8Primary DS8Primary; +typedef struct DS8Buffer DS8Buffer; + + /* Sample types */ #define AL_BYTE 0x1400 #define AL_UNSIGNED_BYTE 0x1401 @@ -353,7 +358,7 @@ enum { MAX_EXTENSIONS }; -typedef struct { +typedef struct ExtALFuncs { PFNALBUFFERSUBDATASOFTPROC BufferSubData; PFNALBUFFERDATASTATICPROC BufferDataStatic; @@ -376,7 +381,7 @@ typedef struct { } ExtALFuncs; #define MAX_SOURCES 256 -typedef struct { +typedef struct DeviceShare { LONG ref; ALCdevice *device; @@ -397,27 +402,6 @@ typedef struct { GUID guid; } DeviceShare; -/* Device implementation */ -typedef struct DS8Primary DS8Primary; -typedef struct DS8Buffer DS8Buffer; - -typedef struct DS8Impl { - IDirectSound8 IDirectSound8_iface; - IDirectSound IDirectSound_iface; - - LONG ref; - BOOL is_8; - - DeviceShare *share; - - /* Taken from the share */ - ALCdevice *device; - - DS8Primary *primary; - - DWORD speaker_config; - DWORD prio_level; -} DS8Impl; struct DS8Primary { IDirectSoundBuffer IDirectSoundBuffer_iface; @@ -474,6 +458,26 @@ struct DS8Primary { }; +/* Device implementation */ +struct DS8Impl { + IDirectSound8 IDirectSound8_iface; + IDirectSound IDirectSound_iface; + + LONG ref; + BOOL is_8; + + DeviceShare *share; + + /* Taken from the share */ + ALCdevice *device; + + DS8Primary primary; + + DWORD speaker_config; + DWORD prio_level; +}; + + typedef struct DS8Data { LONG ref; @@ -496,8 +500,7 @@ typedef struct DS8Data { * bufferdatastatic and buffersubdata are not available */ #define QBUFFERS 3 -struct DS8Buffer -{ +struct DS8Buffer { IDirectSoundBuffer8 IDirectSoundBuffer8_iface; IDirectSoundBuffer IDirectSoundBuffer_iface; IDirectSound3DBuffer IDirectSound3DBuffer_iface; @@ -536,7 +539,7 @@ struct DS8Buffer DSBPOSITIONNOTIFY *notify; }; -extern HRESULT DS8Primary_Create(DS8Primary **prim, DS8Impl *parent); +extern HRESULT DS8Primary_Create(DS8Primary *prim, DS8Impl *parent); extern void DS8Primary_Destroy(DS8Primary *prim); extern HRESULT DS8Buffer_Create(DS8Buffer **ppv, DS8Primary *parent, IDirectSoundBuffer *orig); diff --git a/primary.c b/primary.c index 1239a3e..ffc9b34 100644 --- a/primary.c +++ b/primary.c @@ -217,21 +217,17 @@ static DWORD CALLBACK ThreadProc(void *dwUser) } -HRESULT DS8Primary_Create(DS8Primary **ppv, DS8Impl *parent) +HRESULT DS8Primary_Create(DS8Primary *This, DS8Impl *parent) { - DS8Primary *This = NULL; DS3DLISTENER *listener; WAVEFORMATEX *wfx; HRESULT hr; - *ppv = NULL; - This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This)); - if(!This) return DSERR_OUTOFMEMORY; - This->IDirectSoundBuffer_iface.lpVtbl = (IDirectSoundBufferVtbl*)&DS8Primary_Vtbl; This->IDirectSound3DListener_iface.lpVtbl = (IDirectSound3DListenerVtbl*)&DS8Primary3D_Vtbl; This->IKsPropertySet_iface.lpVtbl = (IKsPropertySetVtbl*)&DS8PrimaryProp_Vtbl; + This->parent = parent; This->crst = &parent->share->crst; This->ctx = parent->share->ctx; This->SupportedExt = parent->share->SupportedExt; @@ -254,7 +250,7 @@ HRESULT DS8Primary_Create(DS8Primary **ppv, DS8Impl *parent) wfx->cbSize = 0; This->stopped = TRUE; - This->parent = parent; + /* Apparently primary buffer size is always 32k, * tested on windows with 192k 24 bits sound @ 6 channels * where it will run out in 60 ms and it isn't pointer aligned @@ -306,7 +302,6 @@ HRESULT DS8Primary_Create(DS8Primary **ppv, DS8Impl *parent) if(This->thread_hdl == NULL) goto fail; - *ppv = This; return S_OK; fail: @@ -318,6 +313,9 @@ void DS8Primary_Destroy(DS8Primary *This) { TRACE("Destroying primary %p\n", This); + if(!This->parent) + return; + if(This->timer_id) { timeKillEvent(This->timer_id); @@ -339,7 +337,7 @@ void DS8Primary_Destroy(DS8Primary *This) HeapFree(GetProcessHeap(), 0, This->notifies); HeapFree(GetProcessHeap(), 0, This->buffers); - HeapFree(GetProcessHeap(), 0, This); + memset(This, 0, sizeof(*This)); } static inline DS8Primary *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface) -- 2.11.4.GIT