Draining the WASAPI capture buffer fully

About six years ago I wrote a blog post about how to do WASAPI loopback capture. Since then, a few issues have come to light.

One big issue is that we’re using MMCSS registration directly. Nowadays the much-preferred approach is to use a Media Foundation work queue; yes, you can use a Media Foundation work queue even if you’re doing WASAPI directly. The official WASAPI sample demonstrates how to use this approach.

Another big issue is that the code isn’t draining the buffer fully; it assumes there is only a single packet per engine period. Here’s the capture loop as it stands:

for (…) {
if (data available) { ReadAPacket(); }

There are two reasons this is a problem. First, if you miss a period, you will never catch up. Second, the engine is free to write many small packets into the buffer if it likes.

The code SHOULD look like this:

for (…) {
while (data available) { ReadAPacket(); }

Browse source

Download binary

One thought on “Draining the WASAPI capture buffer fully

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s