microsoft / Windows-classic-samples

This repo contains samples that demonstrate the API used in Windows classic desktop applications.
Other
5.1k stars 3.23k forks source link

WASAPI Capture shared event driven mode in Windows server 2016 to Windows Vista #313

Closed Preethiravi196 closed 1 year ago

Preethiravi196 commented 1 year ago

I am building an application to capture audio from the device using WASAPI Where all the API used support Windows vista and above devices.

Initially, I captured audio from the device with WASAPI Capture as shared timer driven mode where there will be a thread which will capture audio from the device for every 0 milliseconds. Where it worked in windows vista and above devices.

Since the above approach is not much efficient over CPU consumption and not every optimized, I tried to capture the audio from the device with WASAPI Capture as shared event driven mode where audio will be captured only when an event is triggered from the system when data is available. This approach did reduce the CPU consumption fully but is not working in Windows VISTA to Windows Server 2016 devices.

Initialization of audio client interface

Audio interface is initialized with shared mode and stream flag given is for AUDCLNT_STREAMFLAGS_EVENTCALLBACK since I am using event driven mode. GetExtensibleMixFormat method gives the default mixformat that is fetched using GetMixFormat() of windows.

int enginelatency = 20;

HRESULT hr = _AudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_LOOPBACK , enginelatency * 10000, 0, (WAVEFORMATEX*)mix_format_wasapicap_ptr->GetExtensibleMixFormat(), NULL);

Initializing the event

Where _AudioSamplesReadyEvent is a handle that is given to SetEventHandle method. This event will be triggered by the system when an audio buffer is ready to be processed by the client.

hr = _AudioClient->SetEventHandle(_AudioSamplesReadyEvent);

Though all the API's used are supported by Windows VISTA and above devices, not able to figure out why event is not triggered in Windows VISTA to Windows Server 2016 devices.

Also I have done error handling properly and not able to catch any errors. I'm able to find that _AudioSamplesReadyEvent event is not getting triggered so I can not capture the audio.

Any suggestions are appreciated.

oldnewthing commented 1 year ago

This appears to be a question about the feature itself, not a question about the sample. You can follow the instructions on Windows developer support for support options. (Click "Microsoft Q&A".)