I've looked pretty thoroughly for a similar question but haven't really found the exact case I have.
I have an unmanaged dll for an audio app that creates a thread and waits for an event from the sound card to fill the output buffer.
The app works fine with the onboard sound card but if I switch to any usb audio device the thread just hangs.
Here's the main code bits: I initialize my Event in my Initialize function
::Initialize()
{
_AudioSamplesReadyEvent = CreateEventEx(NULL, L"InputReady", 0, EVENT_MODIFY_STATE | SYNCHRONIZE);
}
And pass it to the sound device after it has been initialized
::InitiAudioEngine()
{
hr = _AudioClient->SetEventHandle(_AudioSamplesReadyEvent);
}
Here's the thread
//HRESULT hr = CoInitializeEx(NULL, COINIT_SPEED_OVER_MEMORY); //gets called in the constructor of my renderthread class
DWORD DoRenderThread()
{
HANDLE waitArray[2] = {_ShutdownEvent, _AudioSamplesReadyEvent};
HANDLE mmcssHandle = NULL;
DWORD mmcssTaskIndex = 0;
mmcssHandle = AvSetMmThreadCharacteristics(L"Audio", &mmcssTaskIndex);
while (_StillPlaying)
{
HRESULT hr;
DWORD waitResult = WaitForMultipleObjects(2, waitArray, FALSE, INFINITE);
switch (waitResult)
{
case WAIT_OBJECT_0 + 0: // _ShutdownEvent
_StillPlaying = false; // We're done, exit the loop.
break;
case WAIT_OBJECT_0 + 1: // _AudioSamplesReadyEvent
// We need to provide the next buffer of samples to the audio renderer.
BYTE *pData;
//invoke the callback to the managed app to fill our buffer
_Handler(NULL, 0);
hr = _RenderClient->GetBuffer(_BufferSizePerPeriod, &pData);
if (SUCCEEDED(hr))
{
// Copy data from the render buffer to the output buffer and bump our render pointer.
if(_FrameSize == QUAD_FRAME)
{
//we have to remux the stream data in to an L,R,LR,RR frame
//inserting silence for the rear channels
const int bufmuxsz = FIXED_FRAME_SIZE * QUAD_FRAME;
BYTE muxarray[bufmuxsz] = {0};
BYTE tempbuffer[FIXED_FRAME_SIZE * STEREO_FRAME];
CopyMemory(tempbuffer, _Buffer, FIXED_FRAME_SIZE * STEREO_FRAME);
int i = 0;
for(int x = 0; x < bufmuxsz; ++x)
{
muxarray[x++] = tempbuffer[i++];
muxarray[x++] = tempbuffer[i++];
muxarray[x++] = tempbuffer[i++];
muxarray[x++] = tempbuffer[i++];
x += 3; //the array is initialized to '0' so skip the assignments
}
CopyMemory(pData, muxarray, _BufferSizePerPeriod*_FrameSize);
}
else
{
CopyMemory(pData, _Buffer, _BufferSizePerPeriod * _FrameSize);
}
hr = _RenderClient->ReleaseBuffer(_BufferSizePerPeriod, 0);
if (!SUCCEEDED(hr))
{
printf("Playback Failed!!\n");
_StillPlaying = false;
}
}
else
{
_StillPlaying = false;
}
break;
}
}
AvRevertMmThreadCharacteristics(mmcssHandle);
CoUninitialize();
return 0;
}
My 2 events that I wait for are a shutdown event and the samplesready event from the sound card. It always hits once but then hangs . Again this is only with usb audio interfaces.