Modifying or disconnecting audio devices causes Unity3D to crash when using WasapiLoopbackCapture
Hi, I've been dealing with this issue ever since I started using CSCore in my game, and haven't been able to figure out what's going on.
When capturing audio, the engine crashes with a memory access violation at 0x00000002. Normally in Unity, the game will catch exceptions automatically, but in this instance it hard crashes whatever process is hosting the code. This includes the Editor, and the built game. This only seems to happen inside Unity engine, because if I run the same code with the Rosyln compiler, it works as expected.
CSCore was never ment for Unity3D. Nevertheless, some people use it in combination with unity3d.
If've never used Unity3D so it's hard to determine the reason for that.
Are you using the latest version of cscore (v1.2.1.2)?
First of all there might be a problem with the callback in the MMDeviceEnumerator.
Could you may just create an empty game and keep an instance of MMDeviceEnumerator while disconnecting the device?
I would guess that this causes the crash. But I can't really tell.
Yep, that did it. Using this code as the only code in my game, it crashed when the audio device was removed. It also seems to be crashing when I do anything related to Windows audio, including right clicking on the speaker taskbar icon.
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
// Use this for initialization
void Start () {
MMDevice defaultDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
Debug.Log("Default device: " + defaultDevice);
}
It also crashes just by constructing an MMDeviceEnumerator in my code, but won't crash if it has been disposed of when the device is disconnected.
I've attached two binaries. You can try them out whether they work. But to be honest, it is quite hard to solve issues like these...
Alright, I did some testing with this in an empty Unity project. This is kind of long, so bear with me.
For clarification: "Freeze" means that Unity triggered a breakpoint in my debugger OR showed the "stopped working" message on Windows. "Crash" means that the Unity runtime detected the error and halted the application on it's own.
Note: I wasn't able to catch any of the errors in my own code.
When using CSCore_1:
- The program DID NOT freeze when unplugging an audio device while an
MMDeviceEnumeratorinstance was active
When using CSCore_2:
- The program DID freeze when unplugging an audio device while an
MMDeviceEnumeratorinstance was active.
When using CSCore master (v1.2.1.2):
- The program DID crash and trigger the Unity error handler when unplugging an audio device while an
MMDeviceEnumeratorinstance was active
However, when using any of the binaries, this happened:
- The program DID NOT freeze when a
WasapiLoopbackCapturewas initialized, but not recording. - The program DID freeze when a
WasapiLoopbackCapturewas recording. When it crashed while recording, if I continued in the debugger, the program would fire theStoppedevent and continue as normal, and would not break any more until the process was restarted, even when unplugging the device again.
It seems like CSCore_1 fixes the issue with the MMDeviceEnumerator, but the WasapiLoopbackCapture is causing problems. It's odd that it only happens the first time the device is unplugged, and works perfectly fine after continuing in the debugger. By the way, thanks so much for helping me out with this issue.
I'm sorry for the late response. So none of both versions did the trick? Are there any details regarding the "crash" you can continue after?
The first version fixed the issue with the MMDeviceEnumerator, so whatever you changed did the trick! I’m not sure how you did it but I think if you did something similar to the WasapiLoopbackCapture class it’d fix both of the issues.
And about the “crash”, if I debug the unity editor itself inside visual studio, it will trigger a breakpoint from the process. However, if I choose to continue execution of the program, it functions perfectly fine, and doesn’t crash again.
On Jan 12, 2018, at 4:45 AM, Florian Rosmann [email protected] wrote:
I'm sorry for the late response. So none of both versions did the trick? Are there any details regarding the "crash" you can continue after?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
it’d fix both of the issues. So is there one issue left?
Correct. The program still crashes when using the WasapiLoopbackCapture. The CSCore_1 binary that you sent only fixes the issues with MMDeviceEnumerator.
Hey, I understand you probably don't have the time to fix this issue but can you send me the source code (or the diffs) for the two DLLs you send earlier in the thread? I'd like to take a look at them and see if I can fix it myself.
Hey, I'm not sure if what i am experiencing is the same issue as this but it's certainly close.
I am attempting to use CSCore with Unity to make a system audio visualizer. Everything works exactly as desired in the unity editor but when i build the project and try and run the standalone version i get this:
KERNELBASE.dll caused an Access Violation (0xc0000005) in module KERNELBASE.dll at 0033:43ce4008.
I have very little experience using non Unity DLLs like this but i copied a lot of the code i needed from this example project (which does produce the same error).
Best i can tell the error occurs when initializing a new WasapiLoopbackCapture:
systemCapture = new WasapiLoopbackCapture();
systemCapture.Initialize();
Any help would be greatly appreciated, Thanks, -Tainin
Hey @Tainin, have you tried using either of the two DLLs posted above? They helped fix my issue partially, and they may solve yours. Hopefully we can bring some more attention to this issue and help get it resolved! 😄
Yes @Technoguyfication, I tried both of them, and unfortunately, as far as i can tell neither of them changed anything.. Thanks for the response, and yes, hopefully we both get a solution soon.
Well the problem is that I don't know how exactly the unity runtime works. In fact I was doing this one: https://github.com/filoe/cscore/commit/9e1126a3598fa99d46996e88c6d4a4d18adfa8b8#diff-47ada3f00013edb738fcefb8747bfab5
Hi @filoe , thank you for your work on this library.
My apologies for reviving an old thread, but I thought I'd add my data around hot-plugging devices & using the DLL with Unity, in the hopes of reaching a complete solution:
- CSCore_1: Doesn't crash after a hotplug, events fire, BUT the DLL hangs within DomainUnload. I'm just creating a MMDeviceEnumerator, unplugging something, Disposing of the enumerator, and then trying to quit Unity. It fails on the last step, only if I have an MMDeviceEnumerator alive during the connection/disconnection.
- CSCore_2: Doesn't crash after a hotplug, the DLL doesn't hang in DomainUnload, BUT events don't fire (Added, Removed, StateChanged).
As you can see, both have their improvements over the master release which does for me throw AccessViolation exceptions as soon as a device is unplugged.