Core.cs 4.62 KB
Newer Older
1
using System;
2
using System.Diagnostics;
3 4
using System.IO;
using System.Runtime.InteropServices;
5 6 7
#if ANDROID
using Java.Interop;
#endif
8

9
namespace LibVLCSharp.Shared
10 11 12 13 14
{
    public static class Core
    {
        struct Native
        {
Martin Finkel's avatar
Martin Finkel committed
15 16 17
            [DllImport("kernel32.dll", SetLastError = true)]
            internal static extern IntPtr LoadPackagedLibrary(string dllToLoad);

18
            [DllImport("kernel32.dll", SetLastError = true)]
19
            internal static extern IntPtr LoadLibrary(string dllToLoad);
Martin Finkel's avatar
Martin Finkel committed
20
#if ANDROID
21
            [DllImport(Constants.LibraryName, EntryPoint = "JNI_OnLoad")]
22 23
            internal static extern int JniOnLoad(IntPtr javaVm, IntPtr reserved = default(IntPtr));
#endif
24 25 26 27 28 29 30
        }

        static IntPtr _libvlccoreHandle;
        static IntPtr _libvlcHandle;

        public static void Initialize()
        {
31
#if MODERN_DESKTOP
32
            InitializeDesktop();
33 34 35
#elif ANDROID
            InitializeAndroid();
#endif
36 37
        }

38 39 40 41 42 43 44 45
#if ANDROID
        static void InitializeAndroid()
        {
            var initLibvlc = Native.JniOnLoad(JniRuntime.CurrentRuntime.InvocationPointer);
            if(initLibvlc == 0)
                throw new VLCException("failed to initialize libvlc with JniOnLoad " +
                                       $"{nameof(JniRuntime.CurrentRuntime.InvocationPointer)}: {JniRuntime.CurrentRuntime.InvocationPointer}");
        }
46
#elif MODERN_DESKTOP
47
        //TODO: Add Unload library func using handles
48
        static void InitializeDesktop()
49
        {
50
            var myPath = typeof(LibVLC).Assembly.Location;
51 52 53 54
            var appExecutionDirectory = Path.GetDirectoryName(myPath);
            if (appExecutionDirectory == null)
                throw new NullReferenceException(nameof(appExecutionDirectory));

55 56
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
                var arch = Environment.Is64BitProcess ? ArchitectureNames.Win64 : ArchitectureNames.Win86;

                var librariesFolder = Path.Combine(appExecutionDirectory, Constants.LibrariesRepositoryFolderName, arch);

                _libvlccoreHandle = PreloadNativeLibrary(librariesFolder, $"{Constants.CoreLibraryName}.dll");
                
                if(_libvlccoreHandle == IntPtr.Zero)
                {
                    throw new VLCException($"Failed to load required native library {Constants.CoreLibraryName}.dll");
                }

                _libvlcHandle = PreloadNativeLibrary(librariesFolder, $"{Constants.LibraryName}.dll");
                
                if(_libvlcHandle == IntPtr.Zero)
                {
                    throw new VLCException($"Failed to load required native library {Constants.LibraryName}.dll");
                }
74
            }
75 76
        }

Martin Finkel's avatar
Martin Finkel committed
77
        //TODO: check if Store app
78
        static IntPtr PreloadNativeLibrary(string nativeLibrariesPath, string libraryName)
79
        {
80 81 82 83 84 85 86
            Debug.WriteLine($"Loading {libraryName}");
            var libraryPath = Path.Combine(nativeLibrariesPath, libraryName);
            if(!File.Exists(libraryPath))
            {
                Debug.WriteLine($"Cannot find {libraryPath}");
                return IntPtr.Zero;
            }
87

88
            return Native.LoadLibrary(libraryPath);// TODO: cross-platform load
89
        }
Martin Finkel's avatar
Martin Finkel committed
90
#endif
91
    }
92

93
    internal static class Constants
94 95 96
    {
#if IOS
        internal const string LibraryName = "@rpath/DynamicMobileVLCKit.framework/DynamicMobileVLCKit";
97 98
#elif MAC
        internal const string LibraryName = "@rpath/VLCKit.framework/VLCKit";
Martin Finkel's avatar
Martin Finkel committed
99 100 101 102 103 104
#elif UNITY_ANDROID
        /// <summary>
        /// The vlc-unity C++ plugin which handles rendering (opengl/d3d) libvlc callbacks
        /// </summary>
        internal const string UnityPlugin = "VlcUnityWrapper";
        internal const string LibraryName = "libvlcjni";
105
#else
Martin Finkel's avatar
Martin Finkel committed
106
        internal const string LibraryName = "libvlc";
107
#endif
108 109 110 111 112 113
        internal const string CoreLibraryName = "libvlccore";

        /// <summary>
        /// The name of the folder that contains the per-architecture folders
        /// </summary>
        internal const string LibrariesRepositoryFolderName = "libvlc";
114 115 116 117

        internal const string Windows = "msvcrt";
        internal const string Linux = "libc";
        internal const string Mac = "libSystem";
118 119 120 121 122 123 124 125 126 127 128 129 130
    }

    internal static class ArchitectureNames
    {
        internal const string Win64 = "win-x64";
        internal const string Win86 = "win-x86";
        internal const string Winrt64 = "winrt-x64";
        internal const string Winrt86 = "winrt-x86";
        
        internal const string Lin64 = "linux-x64";
        internal const string LinArm = "linux-arm";

        internal const string MacOS64 = "osx-x64";
131
    }
132
}