I\'m working on an app that plays audio continuously using the waveOut...
API from winmm.dll
. The app uses \"leapfrog\" buffers, which are basically a
It deadlocks inside the mmsys API code. Calling waveOutGetPosition() inside the callback deadlocks when the main thread is busy executing waveOutWrite(). It is fixable, you'll need a lock so these two functions cannot execute at the same time. Add this field to LeapFrogPlayer:
private object mLocker = new object();
And use it in GetElapsedMilliseconds():
if (!noAPIcall)
{
lock (mLocker) {
ret = WaveOutX.waveOutGetPosition(_hWaveOut, ref _timestruct,
_timestructsize);
}
}
and HandleWaveCallback():
// play the next buffer
lock (mLocker) {
int ret = WaveOutX.waveOutWrite(_hWaveOut, ref _header[_currentBuffer],
Marshal.SizeOf(_header[_currentBuffer]));
if (ret != WaveOutX.MMSYSERR_NOERROR) {
throw new Exception("error writing audio");
}
}
This might have side-effects, I didn't notice any though. Take a look at the NAudio project.
Please use Build + Clean the next time you create an uploadable .zip of your project.
The solution to this was very simple (thanks to Larry Osterman): replace the callback with a WndProc.
The waveOutOpen method can take either a delegate (for callback) or a window handle. I was using the delegate approach, which is apparently inherently prone to deadlocking (makes sense, especially in managed code). I was able to simply have my player class inherit from Control
and override the WndProc
method, and do the same stuff in this method that I was doing in the callback. Now I can call waveOutGetPosition
forever and it never locks up.
I'm using NAudio
and querying WaveOut.GetPosition()
frequently, and also seeing frequent deadlocks when using the Callback
strategy. This is essentially the same problem the OP was having, so I figure this solution might help someone else.
I tried using window-based strategies (as noted in the answer) but the audio would stutter when lots of messages were being pushed through the message queue. So I switched to the Callback
strategy. Then I started getting deadlocks.
I'm querying audio position at 60 fps to sync an animation, so I'm hitting the deadlock quite regularly (about 20 seconds into a run on average). Note: I'm sure I can reduce the amount that I call the API, but that's not my point here!
It seems that winmm.dll
calls are all locking internally on the same object/handle. If that assumption holds, then I'm nearly guaranteed a deadlock in NAudio. Here's the scenario with two threads: A
(UI thread); and B
(callback thread in winmm.dll
) and two locks waveOutLock
(as in NAudio) and mmdll
(the lock I'm assuming winmm.dll is using):
My solution was to delegate the work done in the callback to my own thread so that the callback can return immediately and release the (hypothetical) mmdll
lock. This seems to work perfectly for me, as the deadlock is gone.
For those interested, I've forked and modified the NAudio source to include my change. I used the thread pool, and the audio is occasionally a little crackly. This may be due to thread pool thread management, so there may be solution that performs better.