Placeholder-Software / Dissonance

Unity Voice Chat Asset
71 stars 5 forks source link

UnityEngine.UnityException: get_deviceType can only be called from the main thread #73

Closed WikkidEdd closed 6 years ago

WikkidEdd commented 6 years ago

Context

When building and running a UWP app there is a unity exception due to SystemInfo.deviceType being outside the main thread.

BasePreprocessingPipeline: Error: Unhandled exception killed audio preprocessor thread: UnityEngine.UnityException: get_deviceType can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. at UnityEngineProxy.InternalCalls.SystemInfo_Get_Custom_PropDeviceType() at Dissonance.Audio.Capture.WebRtcPreprocessingPipeline.WebRtcPreprocessor.CreatePreprocessor() at Dissonance.Audio.Capture.WebRtcPreprocessingPipeline.WebRtcPreprocessor.Reset() at Dissonance.Audio.Capture.WebRtcPreprocessingPipeline.ApplyReset() at Dissonance.Audio.Capture.BasePreprocessingPipeline.ThreadEntry()

This is being called from IsMobilePlatform() return SystemInfo.deviceType == DeviceType.Handheld;

Workaround

You can work around this by returning true or false depending on your target platform. This is fine for a temporary workaround, but I'm not really sure of the implications of what the plugin will do if it thinks my device is a mobile platform or not. I'm running on Hololens at the moment, but also occurs in a standard UWP app.

Steps to Reproduce

  1. Import main plugin.
  2. Import HLAPI integration
  3. Switch platform to windows store
  4. Build
  5. Run on local machine in Visual Studio
  6. Start Host
  7. Observe error in console

Your Environment

martindevans commented 6 years ago

Hi WikkidEdd,

Sorry about that. I actually can't reproduce this on UWP with Unity 5.6, so it must be a platform specific restriction on that API just for some versions of UWP/Unity!

You're absolutely correct about the workaround - add in a bit of specific code for your platform before the offending code:

private static bool IsMobilePlatform()
{
        #if UNITY_EDITOR
            return false;
        #endif

        //Platforms which we explicitly know are mobile are conditionally compiled to return true
        #if UNITY_ANDROID || UNITY_IOS || UNITY_IPHONE || UNITY_BLACKBERRY || UNITY_WP8
           return true;
        #endif

        #if MY_PLATFORM
              return aBool;
        #endif

        //If it's not one of those devices, do a runtime check for extra safety
        return SystemInfo.deviceType == DeviceType.Handheld;
}

Check out the list of defines here.

For the next release I'll replace the contents of this method entirely will a set of conditional compilation statements so this will never happen again.

martindevans commented 6 years ago

Sorry I forgot to address this:

I'm not really sure of the implications of what the plugin will do if it thinks my device is a mobile platform or not

If you're not using echo cancellation it makes absolutely no difference. Dissonance has two independent echo cancellation algorithms, one heavyweight but high quality one designed for powerful platforms (e.g. desktop PC) and one very light but fairly low quality one which can run on phones. So at worst you'll get a much lower quality of AEC if this is wrong.

WikkidEdd commented 6 years ago

Great, thanks for the feedback!

martindevans commented 6 years ago

Hi WikkidEdd, Dissonance 6.0.2 went live on the asset store last night which include this fix. I'll close this issue now, feel free to re-open it if there is still something wrong :)