From: Eduard Permyakov Subject: [PATCH v2 2/2] dsound/tests: Add test for overwriting mixing region in a buffer. Message-Id: <20210818151546.13209-2-epermyakov@codeweavers.com> Date: Wed, 18 Aug 2021 18:15:46 +0300 In-Reply-To: <20210818151546.13209-1-epermyakov@codeweavers.com> References: <20210818151546.13209-1-epermyakov@codeweavers.com> The test plays a WAV buffer using DirectSound and sets up a loopback capture to snoop on the actual rendered (sent to speakers) audio contents. This allows comparing the theoretically played audio (what is written to the DirectSound secondary buffer) and the actually played audio (what is sent to the speakers). This allows drawing some conclusions about the behavior of DirectSound, especially in the niche case of overwriting the mixing region in a playing buffer. Signed-off-by: Eduard Permyakov --- dlls/dsound/tests/dsound.c | 328 +++++++++++++++++++++++++++++++++++- dlls/dsound/tests/dsound8.c | 7 +- 2 files changed, 332 insertions(+), 3 deletions(-) diff --git a/dlls/dsound/tests/dsound.c b/dlls/dsound/tests/dsound.c index a4f59efaad9..149593dccdb 100644 --- a/dlls/dsound/tests/dsound.c +++ b/dlls/dsound/tests/dsound.c @@ -26,19 +26,23 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#define COBJMACROS #include #include "wine/test.h" #include "mmsystem.h" -#define COBJMACROS #include "dsound.h" #include "dsconf.h" #include "initguid.h" #include "ks.h" #include "ksmedia.h" +#include "mmdeviceapi.h" +#include "audioclient.h" #include "dsound_test.h" +#define PADDING (65536) + DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0); static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized, @@ -1618,6 +1622,327 @@ static void test_notifications(LPGUID lpGuid) IDirectSound_Release(dso); } +/* Test what happens when we overwrite the current mixing position (between + * the play cursor and the write cursor). While this is invalid use of the API, + * some apps do this and we should handle it the same way Windows would. + * + * The main thing we want to check here is if we overwrite the "forbidden" region + * between the play cursor and write cursor, will the newly overwritten data be + * rendered (i.e. sent to speakers) instead of the old data? + */ +static void test_mix_position_overwrite(LPGUID lpGuid) +{ + HRESULT rc; + LPDIRECTSOUND dso = NULL; + LPDIRECTSOUNDBUFFER primary = NULL, secondary = NULL; + LPDIRECTSOUNDNOTIFY notify = NULL; + DSBUFFERDESC bufdesc; + WAVEFORMATEX wfx; + DSBPOSITIONNOTIFY dsbpn[2]; + LPVOID ptr1 = NULL, ptr2 = NULL; + DWORD size1, size2, ret, status; + DWORD playpos, writepos, delta; + HANDLE event; + int i, ref; + static unsigned char wavbuff[705600] = {0}; + static unsigned char rendered[705600] = {0}; + IMMDeviceEnumerator *devenum; + IMMDevice *defdev; + IAudioClient *client = NULL; + IAudioCaptureClient *capture = NULL; + unsigned renderstats[256] = {0}; + DWORD cap_read = 0, cap_frames, cap_flags; + BYTE *cap_data; + UINT32 cap_packet_len; + + trace("testing overwriting mixing position in a buffer.\n"); + init_format(&wfx, WAVE_FORMAT_PCM, 44100, 16, 2); + memset(wavbuff, 0x77, sizeof(wavbuff)); + + /* Create the DirectSound object */ + rc = DirectSoundCreate(lpGuid, &dso, NULL); + ok(rc == DS_OK || rc == DSERR_NODRIVER || rc == DSERR_ALLOCATED, + "DirectSoundCreate() failed: %08x\n", rc); + if (rc != DS_OK) + return; + + /* Set cooperative level */ + rc = IDirectSound_SetCooperativeLevel(dso, get_hwnd(), DSSCL_EXCLUSIVE); + ok(rc == DS_OK, "IDirectSound_SetCooperativeLevel() failed: %08x\n", rc); + if (rc != DS_OK) + goto exit0; + + /* Create a loopback audio capture client to snoop on the rendered audio buffer */ + rc = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, + CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum); + ok(SUCCEEDED(rc), "Failed to get device enumerator: %08x\n", rc); + if(!SUCCEEDED(rc)) + goto exit1; + + rc = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, eRender, + eMultimedia, &defdev); + ok(SUCCEEDED(rc), "Failed to get default audio endpoint: %08x\n", rc); + if(!SUCCEEDED(rc)) + goto exit2; + + rc = IMMDevice_Activate(defdev, &IID_IAudioClient, CLSCTX_INPROC_SERVER, NULL, (void **)&client); + ok(rc == S_OK || rc == AUDCLNT_E_DEVICE_IN_USE, "Failed to activate audio client: %08x\n", rc); + if(rc != S_OK) { + trace("Could not create loopback capture audio client. Skipping...\n"); + goto exit3; + } + + rc = IAudioClient_Initialize(client, + AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 5*10000000, 0, &wfx, NULL); + ok(rc == S_OK || rc == AUDCLNT_E_UNSUPPORTED_FORMAT, "Failed to initialize audio client: %08x\n", rc); + if(rc != S_OK) { + trace("Could not create loopback capture audio client. Skipping...\n"); + goto exit4; + } + + rc = IAudioClient_GetService(client, &IID_IAudioCaptureClient, (void**)&capture); + ok(rc == S_OK || rc == AUDCLNT_E_WRONG_ENDPOINT_TYPE, "Failed to get audio client service: %08x\n", rc); + if(rc != S_OK) { + trace("Could not create loopback capture audio client. Skipping...\n"); + goto exit4; + } + + rc = IAudioClient_Start(client); /* start recording */ + ok(SUCCEEDED(rc), "Failed to start audio capture client: %08x\n", rc); + if(!SUCCEEDED(rc)) + goto exit5; + + /* Create the primary buffer */ + ZeroMemory(&bufdesc, sizeof(bufdesc)); + bufdesc.dwSize = sizeof(bufdesc); + bufdesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME; + bufdesc.dwBufferBytes = 0; + bufdesc.dwReserved = 0; + bufdesc.lpwfxFormat = NULL; + bufdesc.guid3DAlgorithm = GUID_NULL; + + rc = IDirectSound_CreateSoundBuffer(dso, &bufdesc, &primary, NULL); + ok(rc == DS_OK && primary != NULL, + "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n", rc); + if(rc != DS_OK || primary == NULL) + goto exit5; + + /* Set the primary buffer format */ + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == DS_OK, "IDirectSoundBuffer_SetFormat() failed: %08x\n", rc); + if (rc != DS_OK) + goto exit6; + + /* Set the secondary buffer description and format */ + ZeroMemory(&bufdesc, sizeof(bufdesc)); + bufdesc.dwSize = sizeof(bufdesc); + bufdesc.dwFlags = DSBCAPS_LOCSOFTWARE | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME + | DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2; + bufdesc.dwBufferBytes = sizeof(wavbuff); + bufdesc.dwReserved = 0; + bufdesc.lpwfxFormat = &wfx; + bufdesc.guid3DAlgorithm = GUID_NULL; + + rc = IDirectSound_CreateSoundBuffer(dso, &bufdesc, &secondary, NULL); + ok((rc == DS_OK && secondary != NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */ + "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n", rc); + if(rc != DS_OK) + goto exit6; + + rc = IDirectSoundBuffer_SetPan(secondary, 0); + ok(rc == DS_OK, "IDirectSoundBuffer_SetPan failed: %08x\n", rc); + if(rc != DS_OK) + goto exit7; + + /* Copy WAV contents to buffer */ + rc = IDirectSoundBuffer_Lock(secondary, 0, sizeof(wavbuff), &ptr1, &size1, &ptr2, &size2, 0); + ok(rc == DS_OK && ptr1 != NULL, + "IDirectSoundBuffer_Lock failed to lock the buffer %08x\n", rc); + ok(size1 == sizeof(wavbuff), "Could not lock entire buffer\n"); + ok(ptr2 == NULL && size2 == 0, "Unexpectedly got a second buffer segment\n"); + if(rc != DS_OK) + goto exit7; + + trace("copying %u bytes from %p to %p\n", size1, ptr1, wavbuff); + memcpy(ptr1, wavbuff, size1); + rc = IDirectSoundBuffer_Unlock(secondary, ptr1, size1, ptr2, size2); + ok(rc == DS_OK, "IDirectSoundBuffer_Unlock failed to unlock buffer: %08x\n", rc); + if(rc != DS_OK) + goto exit7; + + /* Set up a notification position */ + rc = IDirectSoundBuffer_QueryInterface(secondary, &IID_IDirectSoundNotify, (void**)¬ify); + ok(rc == DS_OK && notify != NULL, + "IDirectSoundBuffer_QueryInterface() failed to create a notification %08x\n", rc); + + event = CreateEventW(NULL, FALSE, FALSE, NULL); + ok(event != NULL, "CreateEventW failed: %08x\n", GetLastError()); + if(event == NULL) + goto exit7; + + dsbpn[0].dwOffset = 235199; + dsbpn[0].hEventNotify = event; + dsbpn[1].dwOffset = 470399; + dsbpn[1].hEventNotify = event; + rc = IDirectSoundNotify_SetNotificationPositions(notify, 2,dsbpn); + ok(rc == DS_OK,"IDirectSoundNotify_SetNotificationPositions failed: %08x\n", rc); + if(rc != DS_OK) + goto exit8; + + /* Play the buffer and wait on the notification */ + rc = IDirectSoundBuffer_Play(secondary, 0, 0, DSBPLAY_LOOPING); + ok(rc == DS_OK,"IDirectSoundBuffer_Play failed: %08x\n", rc); + if(rc != DS_OK) + goto exit8; + + rc = IDirectSoundBuffer_GetStatus(secondary, &status); + ok(rc == DS_OK,"IDirectSoundBuffer_GetStatus failed: %08x\n", rc); + ok(status == (DSBSTATUS_PLAYING | DSBSTATUS_LOOPING), "Unexpected buffer status: %08x\n", status); + + ret = WaitForMultipleObjects(1, &event, FALSE, 10000); + ok(ret == WAIT_OBJECT_0, "WaitForMultipleObjects unexpected return value: %08x\n", ret); + if(ret != WAIT_OBJECT_0) + goto exit9; + + /* Get the buffer play and write cursors. Attempt to overwrite the area between the play and + * write cursor. This is the stuff that the API docs tell you to never, ever do. + */ + rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &playpos, &writepos); + ok(rc == DS_OK, "IDirectSoundBuffer_GetCurrentPosition error: %08x\n", rc); + if(rc != DS_OK) + goto exit9; + + delta = writepos - playpos; + ok(writepos >= playpos, "Unexpected writepos (%u) and playpos (%u)\n", writepos, playpos); + trace("playpos = %u, writepos = %u, delta = %u\n", playpos, writepos, delta); + + rc = IDirectSoundBuffer_Lock(secondary, playpos, delta + PADDING, &ptr1, &size1, &ptr2, &size2, 0); + ok(rc == DS_OK && ptr1 != NULL, + "IDirectSoundBuffer_Lock failed to lock the buffer %08x\n", rc); + ok(size1 == (delta + PADDING), "Could not lock specified region (%u bytes)\n", delta + PADDING); + if(rc != DS_OK) + goto exit9; + + memset(ptr1, 0xaa, delta + PADDING); + + rc = IDirectSoundBuffer_Unlock(secondary, ptr1, size1, ptr2, size2); + ok(rc == DS_OK, "IDirectSoundBuffer_Unlock failed to unlock buffer: %08x\n", rc); + if(rc != DS_OK) + goto exit9; + + /* Make sure we've played the overwritten segment */ + ret = WaitForMultipleObjects(1, &event, FALSE, 10000); + ok(ret == WAIT_OBJECT_0, "WaitForMultipleObjects unexpected return value: %08x\n", ret); + if(ret != WAIT_OBJECT_0) + goto exit9; + + /* Now try to read back the data - did we actually overwrite it? */ + rc = IDirectSoundBuffer_Lock(secondary, playpos, delta + PADDING, &ptr1, &size1, &ptr2, &size2, 0); + ok(rc == DS_OK && ptr1 != NULL, + "IDirectSoundBuffer_Lock failed to lock the buffer %08x\n", rc); + ok(size1 == (delta + PADDING), "Could not lock specified region (%u bytes)\n", delta + PADDING); + if(rc != DS_OK) + goto exit9; + + for(i = 0; i < delta + PADDING; i++) { + ok(*(((unsigned char*)ptr1) + i) == 0xaa, + "Unexpected buffer contents at offset: %u\n", playpos + i); + } + + rc = IDirectSoundBuffer_Unlock(secondary, ptr1, size1, ptr2, size2); + ok(rc == DS_OK, "IDirectSoundBuffer_Unlock failed to unlock buffer: %08x\n", rc); + if(rc != DS_OK) + goto exit9; + + /* Now check the data in the capture buffer - were the overwritten buffer contents actually rendered? */ + rc = IAudioCaptureClient_GetNextPacketSize(capture, &cap_packet_len); + ok(SUCCEEDED(rc), "IAudioCaptureClient_GetNextPacketSize failed: %08x\n", rc); + if(!SUCCEEDED(rc)) + goto exit9; + + while(cap_read < sizeof(rendered) && cap_packet_len > 0) { + + DWORD cap_bytes, copysz; + rc = IAudioCaptureClient_GetBuffer(capture, &cap_data, &cap_frames, &cap_flags, NULL, NULL); + ok(SUCCEEDED(rc), "IAudioCaptureClient_GetBuffer failed: %08x\n", rc); + if(!SUCCEEDED(rc)) + goto exit9; + + cap_bytes = cap_frames * wfx.nBlockAlign; + copysz = (cap_read + cap_bytes > sizeof(rendered)) ? (sizeof(rendered) - cap_read) : cap_bytes; + memcpy(rendered + cap_read, cap_data, copysz); + cap_read += copysz; + + rc = IAudioCaptureClient_ReleaseBuffer(capture, cap_frames); + ok(SUCCEEDED(rc), "IAudioCaptureClient_ReleaseBuffer failed: %08x\n", rc); + if(!SUCCEEDED(rc)) + goto exit9; + + rc = IAudioCaptureClient_GetNextPacketSize(capture, &cap_packet_len); + ok(SUCCEEDED(rc), "IAudioCaptureClient_GetNextPacketSize failed: %08x\n", rc); + if(!SUCCEEDED(rc)) + goto exit9; + } + trace("captured %u bytes of sound data [%u samples]\n", cap_read, cap_read / wfx.nBlockAlign); + + for(i = 0; i < cap_read; i++) { + renderstats[rendered[i]]++; + } + trace("rendered audio statistics:\n"); + for(i = 0; i < 256; i++) { + if(renderstats[i] == 0) + continue; + trace(" 0x%02x: %u\n", i, renderstats[i]); + } + + for(i = 1; i < cap_read; i++) { + BYTE curr = rendered[i], prev = rendered[i - 1]; + if((curr >> 4) == 0x7 && (prev >> 4) == 0x0) + trace(" -> change from 0x00 to 0x77 at offset: %u\n", i); + if((curr >> 4) == 0x0 && (prev >> 4) == 0x7) + trace(" -> change from 0x77 to 0x00 at offset: %u\n", i); + + if((curr >> 4) == 0x7 && (prev >> 4) == 0xa) + trace(" -> change from 0xaa to 0x77 at offset: %u\n", i); + if((curr >> 4) == 0xa && (prev >> 4) == 0x7) + trace(" -> change from 0x77 to 0xaa at offset: %u\n", i); + } + + rc = IAudioClient_Stop(client); + ok(SUCCEEDED(rc), "Failed to stop audio capture client: %08x\n", rc); + if(!SUCCEEDED(rc)) + goto exit9; + +exit9: + rc = IDirectSoundBuffer_Stop(secondary); + ok(rc==DS_OK,"IDirectSoundBuffer_Stop failed %08x\n", rc); +exit8: + CloseHandle(event); +exit7: + ref = IDirectSoundBuffer_Release(secondary); + ok(ref == 0,"IDirectSoundBuffer_Release() secondary has %d references, " + "should have 0\n", ref); +exit6: + ref = IDirectSoundBuffer_Release(primary); + ok(ref == 0, "IDirectSoundBuffer_Release() primary has %d references, " + "should have 0\n", ref); +exit5: + IAudioCaptureClient_Release(capture); +exit4: + IAudioClient_Release(client); +exit3: + IMMDevice_Release(defdev); +exit2: + IMMDeviceEnumerator_Release(devenum); +exit1: + /* Set the CooperativeLevel back to normal */ + rc = IDirectSound_SetCooperativeLevel(dso, get_hwnd(), DSSCL_NORMAL); + ok(rc == DS_OK, "IDirectSound_SetCooperativeLevel() failed: %08x\n", rc); +exit0: + ref = IDirectSound_Release(dso); + ok(ref == 0,"IDirectSound_Release() has %d references, should have 0\n", ref); +} + static unsigned int number; static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription, @@ -1649,6 +1974,7 @@ static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription, test_duplicate(lpGuid); test_invalid_fmts(lpGuid); test_notifications(lpGuid); + test_mix_position_overwrite(lpGuid); } return TRUE; diff --git a/dlls/dsound/tests/dsound8.c b/dlls/dsound/tests/dsound8.c index e3adbfbe70e..6f4f1ca0cde 100644 --- a/dlls/dsound/tests/dsound8.c +++ b/dlls/dsound/tests/dsound8.c @@ -42,11 +42,14 @@ #include "uuids.h" #include "wingdi.h" -#include "mmdeviceapi.h" -#include "audioclient.h" #include "propkey.h" #include "devpkey.h" +#undef INITGUID +#include +#include "mmdeviceapi.h" +#include "audioclient.h" + #include "dsound_test.h" static const GUID testdmo_clsid = {0x1234}; -- 2.17.1