2

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 bunch of arrays of samples that you dump into the audio queue. Windows plays them seamlessly in sequence, and as each buffer completes Windows calls a callback function. Inside this function, I load the next set of samples into the buffer, process them however, and then dump the buffer back into the audio queue. In this way, the audio plays indefinitely.

For animation purposes, I'm trying to incorporate waveOutGetPosition into the application (since the "buffer done" callbacks are irregular enough to cause jerky animation). waveOutGetPosition returns the current position of playback, so it's hyper-precise.

The problem is that in my application, making calls to waveOutGetPosition eventually causes the application to lock up - the sound stops and the call never returns. I've boiled things down to a simple app that demonstrates the problem. You can run the app here:

http://www.musigenesis.com/SO/waveOut%20demo.exe

If you just hear a tiny bit of piano over and over, it's working. It's just meant to demonstrate the problem. The source code for this project is here (all the meat is in LeapFrogPlayer.cs):

http://www.musigenesis.com/SO/WaveOutDemo.zip

The first button runs the app in leapfrog mode without making the calls to waveOutGetPosition. If you click this, the app will play forever without breaking (the X button will close it and shut it off). The second button starts the leapfrogger and also starts a forms timer that calls the waveOutGetPosition and displays the current position. Click this and the app will run for a short while and then lock up. On my laptop, it usually locks up in 15-30 seconds; at most it's taken a minute.

I have no idea how to fix this, so any help or suggestions would be most welcome. I've found very few posts on this issue, but it seems that there is a potential deadlock, either from multiple calls to waveOutGetPosition or from calls to that and waveOutWrite that occur at the same time. It's possible that I'm calling this too frequently for the system to handle.

Edit: forgot to mention, I'm running this on Windows Vista. This might not happen at all on other OSes.

Edit 2: I've found little about this problem online, except for these (unanswered) posts:

http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/c6a1e80e-4a18-47e7-af11-56a89f638ad7

Edit 3: Well, I'm now able to reproduce this problem at will. If I call waveOutGetPosition immediately after waveOutWrite (in the following line of code) the application hangs every time. It also hangs in an especially bad way - it seems to lock up my whole OS for awhile, not just the app itself. So it appears that waveOutGetPosition deadlocks if it occurs at nearly the same time as waveOutWrite, not just literally at the same time, which might explain why the locks aren't working for me. Yeesh.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334

3 Answers3

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I tried this, but it's still locking up. I'm going to try locking around the calls to waveOutPrepareHeader, also. Why do you need Build + Clean? Was it difficult to get the project to compile or something? – MusiGenesis Mar 16 '10 at 13:19
  • 2
    Worked on my machine. Use Debug + Break All, Debug + Windows + Threads to see where the threads got stuck. Nobody likes large downloads with .exe files from an untrusted Internet URL. – Hans Passant Mar 16 '10 at 13:35
  • If anything, this modification makes the problem even worse. It seems to be locking up a lot faster, now; about half the time now it locks up on the first call to waveOutGetPosition. Does this work on your computer? – MusiGenesis Mar 16 '10 at 13:37
  • Are you running Vista or something else? – MusiGenesis Mar 16 '10 at 13:39
  • Windows 7. I had no trouble getting a repro on the deadlock. – Hans Passant Mar 16 '10 at 13:42
  • Sorry, I don't know what you mean by "Use Debug + Break All, Debug + Windows + Threads". Can you explain exactly what I should do here? – MusiGenesis Mar 16 '10 at 13:44
  • I took a look at NAudio, and they put a lock around all the waveOut... calls, but NAudio doesn't seem to use waveOutGetPosition anywhere. – MusiGenesis Mar 16 '10 at 14:22
  • Could you explain how you were able to determine which threads were locking in Visual Studio? – MusiGenesis Mar 16 '10 at 15:46
  • I used Debug + Break All, then Debug + Windows + Threads. Then double-clicked the threads one by one to look at their call stacks. – Hans Passant Mar 16 '10 at 16:12
  • @nobugz: thanks, but I literally don't understand what you mean by "I used Debug + Break All ..." Do you mean you set Visual Studio to break on all errors? I have that set (I think), but my app doesn't break on any errors - it just locks up. – MusiGenesis Mar 16 '10 at 16:17
  • @nobugz: OK, I get it now. I can see the threads and which one is suspended in the Threads window. But how can I get any further information? It shows one thread in GetElapsedMilliseconds frozen on the waveOutGetPosition line, and the other thread in HandleWaveCallback frozen on the lock(mLocker) line. I assume this means the waveOutGetPosition call isn't returning, and the other thread is stock waiting for the lock to clear? – MusiGenesis Mar 16 '10 at 16:24
  • Sounds like waveOutGetPosition() is still deadlocking. I don't know why, might have something to do with another unmanaged thread doing the playback. Nothing you can do about it but giving up on that call. – Hans Passant Mar 16 '10 at 16:37
  • @nobugz: didn't you see the "larry-osterman" tag? I'm not giving up until *he* says so. :) – MusiGenesis Mar 16 '10 at 18:52
  • You can email him through his blog: http://blogs.msdn.com/larryosterman/contact.aspx – Hans Passant Mar 16 '10 at 20:15
  • He answers all my questions eventually, and I don't want to bother him. – MusiGenesis Mar 16 '10 at 23:44
  • +1 for the pointer to deadlocking in the Callback. See my input on how I modified the NAudio source to prevent Callback deadlocks. – cod3monk3y May 09 '13 at 18:21
1

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):

  1. A -> lock (waveOutLock) --- acquired
  2. B -> lock (mmdll) for callback --- acquired
  3. B -> callback into user code
  4. B -> attempt to lock (waveOutLock) -- waiting for A to release
  5. A -> resumed due to B waiting
  6. A -> call waveOutGetPosition
  7. A -> attempt to lock (mmdll) -- deadlock

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.

cod3monk3y
  • 9,508
  • 6
  • 39
  • 54
  • You may want to test that solution very heavily and make sure it's working 100%. In my real application, the work that required the call to `waveOutGetPosition` was done on a separate thread. It worked pretty well, but it would still occasionally deadlock - much less frequently than in the demo app that I posted here, but often enough to greatly concern me still. In my current version using the wndProc approach, the playback engine is 100% reliable; I have left it running for days at a time and I have not seen a single instance of the deadlock. But using the callback it would rarely run ... – MusiGenesis May 10 '13 at 04:22
  • ... for more than an hour or two continuously before locking, and it would occasionally lock after just a few seconds or minutes. So make sure you do some really long-running tests so you know you have it. – MusiGenesis May 10 '13 at 04:24
  • BTW, I've never heard any kind of stuttering like what you describe when using the wndProc approach. What's going on in your program and your system when this occurs? – MusiGenesis May 10 '13 at 04:27
  • Thanks @MusiGenesis. After discussions with the author of NAudio, I ended up forking and modifying `WaveOutEvent` to return `GetPosition`, which is ultimately all I really needed. He has made the same change in the head, and it should eventually be available in an official build. He recommended adamantly against using function callback mode. As far as what the app is doing during the stutter: it's a complicated WPF layout change with some certainly less-than-optimal components loading. The stutter happens every time, as the buffers replay themselves since they've not been filled with new data. – cod3monk3y May 15 '13 at 20:25
0

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.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334