locked
Any particular reason why my WinRT C++ WASAPI component returns actual data in a C# project, but silent buffers in a WinRT C++ one?

    Question

  • I've written a component wrapping WASAPI. The exact same code works fine in C#, yet when initialized and used the same way in C++/CX, WASAPI sets the AUDCLNT_BUFFERFLAGS_SILENT flag and returns zeroed buffers.

    Do I need something specific initializing my C++ application to make this work? The Microphone capability is set in the manifest and when uninstalling and relaunching the application, it actually asks me permission for the microphone as expected.

    This is pretty confusing.


    • Edited by Tom Servo Monday, May 7, 2012 9:48 PM
    Monday, May 7, 2012 9:45 PM

Answers

  • Hey Tom,

    You shouldn't be using WASAPI form C# or any managed language. As you know this is due to the side effect of nondeterministic finalization. You may have unforeseen issues if using WASAPI from managed languages.

    As far as your C++ problems... Are you sure that you don't eventually start getting audio? This flag can be set if you are polling for the next buffer and the buffer has not been filled yet. The recommendation is to wait and try again. Try keeping the size of your buffer around 25 ms - 35 ms (I recommend that you actually do the math on this). This is due to the fact that the quanta on the X86 architecture is between 10 ms and 15 ms and we need to wait long enough to make sure the DPC have been handled and we have data. Also check and make sure that you are yielding the processor and not polling in a tight loop.

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, May 8, 2012 12:28 AM
    Moderator

All replies

  • Hey Tom,

    You shouldn't be using WASAPI form C# or any managed language. As you know this is due to the side effect of nondeterministic finalization. You may have unforeseen issues if using WASAPI from managed languages.

    As far as your C++ problems... Are you sure that you don't eventually start getting audio? This flag can be set if you are polling for the next buffer and the buffer has not been filled yet. The recommendation is to wait and try again. Try keeping the size of your buffer around 25 ms - 35 ms (I recommend that you actually do the math on this). This is due to the fact that the quanta on the X86 architecture is between 10 ms and 15 ms and we need to wait long enough to make sure the DPC have been handled and we have data. Also check and make sure that you are yielding the processor and not polling in a tight loop.

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, May 8, 2012 12:28 AM
    Moderator
  • Thanks for your answer. Yesterday evening, no matter how long I let it run, buffers were zeroed and I got the silent flag returned.

    Today it works without issues. Same code, no change. Not sure what caused it, probably some unknown unforeseen conditions.

    As far as the C# part goes, there's an allowance for jitter (since it's visualizations from sound only), buffers are collated into 50ms chunks, and invocation of the (C#) event handlers happens asynchronously in the thread pool to avoid the C# code blocking the capturing loop. By this I hope to avoid the C# parts to fubar the WASAPI handling in the C++ component. The test code worked pretty well without any special safety guards.

    By the looks of it, I'll be going pure C++ anyway. Lots of performance advantages to be had by being able to directly access the byte arrays inside the WriteableBitmap.PixelBuffer and freely casting between array types without moving everything back and forth using Buffer.BlockCopy or anything. This becomes especially apparent at higher resolutions.

    Tuesday, May 8, 2012 1:14 PM
  • Tom,

    I'm glad that you have things working. Sounds like gremlins ;)

    I would encourage you to investigate C++ Cx. I was very skeptical at first but I have really come to appreciate and even like C++ Cx. 

    Thanks,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, May 8, 2012 11:40 PM
    Moderator