From 2b37cff9108ba5fc30ead957c892304720b0410b Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Tue, 7 Jul 2020 16:56:41 +0200 Subject: [PATCH 01/10] cocoa sample: update libvlc version --- samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj | 2 +- samples/LibVLCSharp.Mac.Sample/packages.config | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj b/samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj index 90212d44b..0df17b9e3 100644 --- a/samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj +++ b/samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj @@ -99,5 +99,5 @@ - + \ No newline at end of file diff --git a/samples/LibVLCSharp.Mac.Sample/packages.config b/samples/LibVLCSharp.Mac.Sample/packages.config index 017cfce43..0df563dae 100644 --- a/samples/LibVLCSharp.Mac.Sample/packages.config +++ b/samples/LibVLCSharp.Mac.Sample/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file From c481e3bb9573ac1bd0cf7525e7e8a9da688357a1 Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Tue, 7 Jul 2020 18:22:49 +0200 Subject: [PATCH 02/10] works on cocoa target --- src/LibVLCSharp/Shared/Core/Constants.cs | 4 ++ src/LibVLCSharp/Shared/Core/Core.Apple.cs | 48 ++++++++++++++++++++++- src/LibVLCSharp/Shared/Core/Core.cs | 7 +++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/LibVLCSharp/Shared/Core/Constants.cs b/src/LibVLCSharp/Shared/Core/Constants.cs index 1328de87d..2d76737f3 100644 --- a/src/LibVLCSharp/Shared/Core/Constants.cs +++ b/src/LibVLCSharp/Shared/Core/Constants.cs @@ -8,6 +8,8 @@ internal static class Constants internal const string LibraryName = "@rpath/DynamicMobileVLCKit.framework/DynamicMobileVLCKit"; #elif TVOS internal const string LibraryName = "@rpath/DynamicTVVLCKit.framework/DynamicTVVLCKit"; +#elif MAC + internal const string LibraryName = "@executable_path/../MonoBundle/lib/libvlc.dylib"; #else internal const string LibraryName = "libvlc"; #endif @@ -27,6 +29,8 @@ internal static class Constants internal const string MacLibraryExtension = ".dylib"; internal const string Lib = "lib"; internal const string LibVLC = "libvlc"; + internal const string VLCPLUGINPATH = "VLC_PLUGIN_PATH"; + internal const string Plugins = "plugins"; } internal static class ArchitectureNames diff --git a/src/LibVLCSharp/Shared/Core/Core.Apple.cs b/src/LibVLCSharp/Shared/Core/Core.Apple.cs index 932780ee4..b276c7b39 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Apple.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Apple.cs @@ -1,5 +1,6 @@ #if APPLE using System; +using System.IO; namespace LibVLCSharp.Shared { @@ -9,6 +10,7 @@ namespace LibVLCSharp.Shared /// public static partial class Core { +#if IOS || TVOS /// /// Load the native libvlc library (if necessary, depending on platform) /// Ensure that you installed the VideoLAN.LibVLC.[YourPlatform] package in your target project @@ -24,6 +26,50 @@ public static void Initialize(string? libvlcDirectoryPath = null) { EnsureVersionsMatch(); } +#elif MAC + static IntPtr LibvlcHandle; + static IntPtr LibvlccoreHandle; + + /// + /// Load the native libvlc library (if necessary, depending on platform) + /// Ensure that you installed the VideoLAN.LibVLC.[YourPlatform] package in your target project + /// This will throw a if the native libvlc libraries cannot be found or loaded. + /// It may also throw a if the LibVLC and LibVLCSharp major versions do not match. + /// See https://code.videolan.org/videolan/LibVLCSharp/-/blob/master/docs/versioning.md for more info about the versioning strategy. + /// + /// The path to the directory that contains libvlc and libvlccore + /// No need to specify unless running netstandard 1.1, or using custom location for libvlc + /// This parameter is NOT supported on Linux, use LD_LIBRARY_PATH instead. + /// + public static void Initialize(string? libvlcDirectoryPath = null) + { + InitializeMac(libvlcDirectoryPath); + + EnsureVersionsMatch(); + } + + private static void InitializeMac(string? libvlcDirectoryPath) + { + if (!string.IsNullOrEmpty(libvlcDirectoryPath)) + { + bool loadResult; + + var libvlcPath = LibVLCPath(libvlcDirectoryPath!); + loadResult = LoadNativeLibrary(libvlcPath, out LibvlcHandle); + if (!loadResult) + Log($"Failed to load required native libraries at {libvlcPath}"); + return; + } + + PluginPath(Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.Plugins)); + + //var pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), "plugins"); + //Debug.WriteLine($"VLC_PLUGIN_PATH: {pluginPath}"); + //Environment.SetEnvironmentVariable("VLC_PLUGIN_PATH", pluginPath); + + LoadLibVLC(); + } +#endif // MAC } } -#endif +#endif // APPLE \ No newline at end of file diff --git a/src/LibVLCSharp/Shared/Core/Core.cs b/src/LibVLCSharp/Shared/Core/Core.cs index 7dd225da9..6afbe1c22 100644 --- a/src/LibVLCSharp/Shared/Core/Core.cs +++ b/src/LibVLCSharp/Shared/Core/Core.cs @@ -47,6 +47,9 @@ static void EnsureVersionsMatch() static string LibVLCPath(string dir) => Path.Combine(dir, $"{Constants.LibraryName}{LibraryExtension}"); static string LibVLCCorePath(string dir) => Path.Combine(dir, $"{Constants.CoreLibraryName}{LibraryExtension}"); static string LibraryExtension => PlatformHelper.IsWindows ? Constants.WindowsLibraryExtension : Constants.MacLibraryExtension; +#if !NETSTANDARD1_1 + static void PluginPath(string pluginPath) => Environment.SetEnvironmentVariable(Constants.VLCPLUGINPATH, pluginPath); +#endif static void Log(string message) { #if !UWP10_0 && !NETSTANDARD1_1 @@ -56,7 +59,7 @@ static void Log(string message) #endif } -#if (NETFRAMEWORK || NETSTANDARD) && !NETSTANDARD1_1 +#if (MAC || NETFRAMEWORK || NETSTANDARD) && !NETSTANDARD1_1 static bool Loaded => LibvlcHandle != IntPtr.Zero; static List<(string libvlccore, string libvlc)> ComputeLibVLCSearchPaths() { @@ -144,7 +147,7 @@ static void LoadLibVLC(string? libvlcDirectoryPath = null) { throw new VLCException("Failed to load required native libraries. " + $"{Environment.NewLine}Have you installed the latest LibVLC package from nuget for your target platform?" + - $"{Environment.NewLine}Search paths include {string.Join("; ", paths.Select(p => $"{p.libvlc},{p.libvlccore}"))}"); + $"{Environment.NewLine}Search paths include {string.Join($"; {Environment.NewLine}", paths.Select(p => $"{p.libvlc},{p.libvlccore}"))}"); } } #endif From 68009892733e483aa2dd592c8f5191126349af65 Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Wed, 8 Jul 2020 16:15:10 +0200 Subject: [PATCH 03/10] sorry --- .../LibVLCSharp.NetCore.Sample.csproj | 2 +- src/LibVLCSharp/LibVLCSharp.csproj | 6 +-- src/LibVLCSharp/Shared/Core/Constants.cs | 4 +- src/LibVLCSharp/Shared/Core/Core.Desktop.cs | 48 ++++++++++++++++++- src/LibVLCSharp/Shared/Core/Core.cs | 15 +++--- 5 files changed, 62 insertions(+), 13 deletions(-) diff --git a/samples/LibVLCSharp.NetCore.Sample/LibVLCSharp.NetCore.Sample.csproj b/samples/LibVLCSharp.NetCore.Sample/LibVLCSharp.NetCore.Sample.csproj index 1a90fac39..ddf01ded2 100644 --- a/samples/LibVLCSharp.NetCore.Sample/LibVLCSharp.NetCore.Sample.csproj +++ b/samples/LibVLCSharp.NetCore.Sample/LibVLCSharp.NetCore.Sample.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/LibVLCSharp/LibVLCSharp.csproj b/src/LibVLCSharp/LibVLCSharp.csproj index 2be4d7ccb..2bac0a1d9 100644 --- a/src/LibVLCSharp/LibVLCSharp.csproj +++ b/src/LibVLCSharp/LibVLCSharp.csproj @@ -28,9 +28,9 @@ This package also contains the views for the following platforms: If you need Xamarin.Forms support, see LibVLCSharp.Forms. LibVLC needs to be installed separately, see VideoLAN.LibVLC.* packages. - netstandard2.1;netstandard2.0;netstandard1.1 - $(TargetFrameworks);MonoAndroid81;Xamarin.iOS10;Xamarin.Mac20;Xamarin.TVOS10 - $(TargetFrameworks);uap10.0;uap10.0.16299;net40;net471 + netstandard2.1;netstandard2.0;netstandard1.1;netcoreapp2.0 + + $(TargetsForTfmSpecificBuildOutput);IncludeAWindow LibVLCSharp en diff --git a/src/LibVLCSharp/Shared/Core/Constants.cs b/src/LibVLCSharp/Shared/Core/Constants.cs index 2d76737f3..35f47cfc6 100644 --- a/src/LibVLCSharp/Shared/Core/Constants.cs +++ b/src/LibVLCSharp/Shared/Core/Constants.cs @@ -11,9 +11,9 @@ internal static class Constants #elif MAC internal const string LibraryName = "@executable_path/../MonoBundle/lib/libvlc.dylib"; #else - internal const string LibraryName = "libvlc"; + internal const string LibraryName = "@rpath/libvlc.dylib"; #endif - internal const string CoreLibraryName = "libvlccore"; + internal const string CoreLibraryName = "@rpath/libvlccore.dylib"; /// /// The name of the folder that contains the per-architecture folders diff --git a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs index 82caa2c1d..d68e810e1 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs @@ -1,10 +1,13 @@ -#if NETFRAMEWORK || NETSTANDARD +#if NETFRAMEWORK || NETSTANDARD || NETCOREAPP using System; using System.Diagnostics; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.InteropServices; +[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.ApplicationDirectory | DllImportSearchPath.SafeDirectories)] + namespace LibVLCSharp.Shared { /// @@ -85,10 +88,53 @@ static void InitializeDesktop(string? libvlcDirectoryPath = null) return; } + if (PlatformHelper.IsMac) + { +#if !NETSTANDARD1_1 + var pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), + Constants.LibVLC, ArchitectureNames.MacOS64, Constants.Plugins); + Console.Out.WriteLine("PluginPath: " + pluginPath); + PluginPath(pluginPath); +#endif + + } #if !NETSTANDARD1_1 LoadLibVLC(libvlcDirectoryPath); #endif } + +#if NETCOREAPP2_0 + internal class CustomMacAssemblyLoadContext : System.Runtime.Loader.AssemblyLoadContext + { + public CustomMacAssemblyLoadContext() + { + Default.Resolving += OnResolving; + } + + [DllImport(Constants.LibSystem, EntryPoint = "dlopen")] + internal static extern IntPtr Dlopen(string libraryPath, int mode = 1); + + public void LoadLibVLC(string libvlc) => LoadUnmanagedDll(libvlc); + + protected override Assembly Load(AssemblyName assemblyName) + { + throw new NotImplementedException(); + } + + protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) + { + Console.Out.WriteLine("LoadUnmanagedDll called with " + unmanagedDllName); + return Dlopen(unmanagedDllName); + } + + Assembly OnResolving(System.Runtime.Loader.AssemblyLoadContext context, AssemblyName name) + { + Console.Out.WriteLine("OnResolving called ==========="); + return Load(name); + } } +#endif + } + } #endif // NETFRAMEWORK || NETSTANDARD diff --git a/src/LibVLCSharp/Shared/Core/Core.cs b/src/LibVLCSharp/Shared/Core/Core.cs index 6afbe1c22..02db9c7b5 100644 --- a/src/LibVLCSharp/Shared/Core/Core.cs +++ b/src/LibVLCSharp/Shared/Core/Core.cs @@ -18,15 +18,17 @@ public static partial class Core partial struct Native { #if !UWP10_0 && !NETSTANDARD1_1 - [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl, + [DllImport("@rpath/libvlc.dylib", CallingConvention = CallingConvention.Cdecl, //works too + //[DllImport("libvlc/osx-x64/lib/libvlc", CallingConvention = CallingConvention.Cdecl, // works + //[DllImport("libvlc/osx-x64/lib/libvlc", CallingConvention = CallingConvention.Cdecl, EntryPoint = "libvlc_get_version")] internal static extern IntPtr LibVLCVersion(); #endif [DllImport(Constants.Kernel32, SetLastError = true)] internal static extern IntPtr LoadLibrary(string dllToLoad); - [DllImport(Constants.LibSystem, EntryPoint = "dlopen")] - internal static extern IntPtr Dlopen(string libraryPath, int mode = 1); + [DllImport("libdl", EntryPoint = "dlopen")] + internal static extern IntPtr Dlopen(string libraryPath, int mode = 0x002); } #if !UWP10_0 && !NETSTANDARD1_1 @@ -37,6 +39,7 @@ partial struct Native static void EnsureVersionsMatch() { var libvlcMajorVersion = int.Parse(Native.LibVLCVersion().FromUtf8()?.Split('.').FirstOrDefault() ?? "0"); + Console.Out.WriteLine("dlsym OK <============="); var libvlcsharpMajorVersion = Assembly.GetExecutingAssembly().GetName().Version.Major; if (libvlcMajorVersion != libvlcsharpMajorVersion) throw new VLCException($"Version mismatch between LibVLC {libvlcMajorVersion} and LibVLCSharp {libvlcsharpMajorVersion}. " + @@ -44,8 +47,8 @@ static void EnsureVersionsMatch() } #endif - static string LibVLCPath(string dir) => Path.Combine(dir, $"{Constants.LibraryName}{LibraryExtension}"); - static string LibVLCCorePath(string dir) => Path.Combine(dir, $"{Constants.CoreLibraryName}{LibraryExtension}"); + static string LibVLCPath(string dir) => Path.Combine(dir, $"{Constants.LibVLC}{LibraryExtension}"); + static string LibVLCCorePath(string dir) => Path.Combine(dir, $"libvlccore{LibraryExtension}"); static string LibraryExtension => PlatformHelper.IsWindows ? Constants.WindowsLibraryExtension : Constants.MacLibraryExtension; #if !NETSTANDARD1_1 static void PluginPath(string pluginPath) => Environment.SetEnvironmentVariable(Constants.VLCPLUGINPATH, pluginPath); @@ -59,7 +62,7 @@ static void Log(string message) #endif } -#if (MAC || NETFRAMEWORK || NETSTANDARD) && !NETSTANDARD1_1 +#if (MAC || NETFRAMEWORK || NETSTANDARD || NETCOREAPP) && !NETSTANDARD1_1 static bool Loaded => LibvlcHandle != IntPtr.Zero; static List<(string libvlccore, string libvlc)> ComputeLibVLCSearchPaths() { From 39d69556b34ac956cc8527e625c30f46c651cfab Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Tue, 8 Sep 2020 12:56:26 +0200 Subject: [PATCH 04/10] fix libvlc loading for cocoa --- src/LibVLCSharp/Shared/Core/Core.Desktop.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs index d68e810e1..9b6c1750f 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs @@ -91,8 +91,7 @@ static void InitializeDesktop(string? libvlcDirectoryPath = null) if (PlatformHelper.IsMac) { #if !NETSTANDARD1_1 - var pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), - Constants.LibVLC, ArchitectureNames.MacOS64, Constants.Plugins); + var pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.Plugins); Console.Out.WriteLine("PluginPath: " + pluginPath); PluginPath(pluginPath); #endif From ec45c8af87284dbfb58f66bca5cb0ff66b286094 Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Thu, 10 Sep 2020 18:24:20 +0200 Subject: [PATCH 05/10] libvlc loading netcore/cocoa MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the LibraryName in the case of net core on Mac needs to be different than “libvlc" and this case cannot be determined at compile time --- src/LibVLCSharp/Shared/Core/Constants.cs | 5 ++++- src/LibVLCSharp/Shared/Core/Core.Apple.cs | 7 ++----- src/LibVLCSharp/Shared/Core/Core.Desktop.cs | 13 ++++++++++++- src/LibVLCSharp/Shared/Core/Core.cs | 10 +++++----- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/LibVLCSharp/Shared/Core/Constants.cs b/src/LibVLCSharp/Shared/Core/Constants.cs index 35f47cfc6..5b578bc3d 100644 --- a/src/LibVLCSharp/Shared/Core/Constants.cs +++ b/src/LibVLCSharp/Shared/Core/Constants.cs @@ -11,7 +11,10 @@ internal static class Constants #elif MAC internal const string LibraryName = "@executable_path/../MonoBundle/lib/libvlc.dylib"; #else - internal const string LibraryName = "@rpath/libvlc.dylib"; + internal const string LibraryName = "libvlc"; + // for netcore on mac, this is needed. + // but this case can't be determined at build time, so we might need LibraryName to be a variable + //internal const string LibraryName = "libvlc/osx-x64/lib/libvlc"; #endif internal const string CoreLibraryName = "@rpath/libvlccore.dylib"; diff --git a/src/LibVLCSharp/Shared/Core/Core.Apple.cs b/src/LibVLCSharp/Shared/Core/Core.Apple.cs index b276c7b39..0cd6a5b19 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Apple.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Apple.cs @@ -61,11 +61,8 @@ private static void InitializeMac(string? libvlcDirectoryPath) return; } - PluginPath(Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.Plugins)); - - //var pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), "plugins"); - //Debug.WriteLine($"VLC_PLUGIN_PATH: {pluginPath}"); - //Environment.SetEnvironmentVariable("VLC_PLUGIN_PATH", pluginPath); + var pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location)); + PluginPath(pluginPath); LoadLibVLC(); } diff --git a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs index 9b6c1750f..df51f49cb 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs @@ -91,7 +91,18 @@ static void InitializeDesktop(string? libvlcDirectoryPath = null) if (PlatformHelper.IsMac) { #if !NETSTANDARD1_1 - var pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.Plugins); + var framework = RuntimeInformation.FrameworkDescription; + var pluginPath = string.Empty; + if(framework.StartsWith(".NET")) + { + pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.LibVLC, + ArchitectureNames.MacOS64, Constants.Plugins); + } + else + { + pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.Plugins); + } + Console.Out.WriteLine("PluginPath: " + pluginPath); PluginPath(pluginPath); #endif diff --git a/src/LibVLCSharp/Shared/Core/Core.cs b/src/LibVLCSharp/Shared/Core/Core.cs index 02db9c7b5..b1d4b839b 100644 --- a/src/LibVLCSharp/Shared/Core/Core.cs +++ b/src/LibVLCSharp/Shared/Core/Core.cs @@ -18,9 +18,7 @@ public static partial class Core partial struct Native { #if !UWP10_0 && !NETSTANDARD1_1 - [DllImport("@rpath/libvlc.dylib", CallingConvention = CallingConvention.Cdecl, //works too - //[DllImport("libvlc/osx-x64/lib/libvlc", CallingConvention = CallingConvention.Cdecl, // works - //[DllImport("libvlc/osx-x64/lib/libvlc", CallingConvention = CallingConvention.Cdecl, + [DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "libvlc_get_version")] internal static extern IntPtr LibVLCVersion(); #endif @@ -29,6 +27,9 @@ partial struct Native [DllImport("libdl", EntryPoint = "dlopen")] internal static extern IntPtr Dlopen(string libraryPath, int mode = 0x002); + + [DllImport(Constants.Libc)] + internal static extern int setenv(string name, string value, int overwrite = 1); } #if !UWP10_0 && !NETSTANDARD1_1 @@ -39,7 +40,6 @@ partial struct Native static void EnsureVersionsMatch() { var libvlcMajorVersion = int.Parse(Native.LibVLCVersion().FromUtf8()?.Split('.').FirstOrDefault() ?? "0"); - Console.Out.WriteLine("dlsym OK <============="); var libvlcsharpMajorVersion = Assembly.GetExecutingAssembly().GetName().Version.Major; if (libvlcMajorVersion != libvlcsharpMajorVersion) throw new VLCException($"Version mismatch between LibVLC {libvlcMajorVersion} and LibVLCSharp {libvlcsharpMajorVersion}. " + @@ -51,7 +51,7 @@ static void EnsureVersionsMatch() static string LibVLCCorePath(string dir) => Path.Combine(dir, $"libvlccore{LibraryExtension}"); static string LibraryExtension => PlatformHelper.IsWindows ? Constants.WindowsLibraryExtension : Constants.MacLibraryExtension; #if !NETSTANDARD1_1 - static void PluginPath(string pluginPath) => Environment.SetEnvironmentVariable(Constants.VLCPLUGINPATH, pluginPath); + static void PluginPath(string pluginPath) => Native.setenv(Constants.VLCPLUGINPATH, pluginPath); #endif static void Log(string message) { From b9a707803c8065db4a2ece67be79519b0a00c6fa Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Thu, 10 Sep 2020 18:42:59 +0200 Subject: [PATCH 06/10] some cleanup and todos --- src/LibVLCSharp/LibVLCSharp.csproj | 4 +-- src/LibVLCSharp/Shared/Core/Constants.cs | 2 ++ src/LibVLCSharp/Shared/Core/Core.Apple.cs | 1 + src/LibVLCSharp/Shared/Core/Core.Desktop.cs | 34 --------------------- src/LibVLCSharp/Shared/Core/Core.cs | 2 +- 5 files changed, 6 insertions(+), 37 deletions(-) diff --git a/src/LibVLCSharp/LibVLCSharp.csproj b/src/LibVLCSharp/LibVLCSharp.csproj index 2bac0a1d9..599ef6a73 100644 --- a/src/LibVLCSharp/LibVLCSharp.csproj +++ b/src/LibVLCSharp/LibVLCSharp.csproj @@ -29,8 +29,8 @@ If you need Xamarin.Forms support, see LibVLCSharp.Forms. LibVLC needs to be installed separately, see VideoLAN.LibVLC.* packages. netstandard2.1;netstandard2.0;netstandard1.1;netcoreapp2.0 - - + $(TargetFrameworks);MonoAndroid81;Xamarin.iOS10;Xamarin.Mac20;Xamarin.TVOS10 + $(TargetFrameworks);uap10.0;uap10.0.16299;net40;net471 $(TargetsForTfmSpecificBuildOutput);IncludeAWindow LibVLCSharp en diff --git a/src/LibVLCSharp/Shared/Core/Constants.cs b/src/LibVLCSharp/Shared/Core/Constants.cs index 5b578bc3d..95da85020 100644 --- a/src/LibVLCSharp/Shared/Core/Constants.cs +++ b/src/LibVLCSharp/Shared/Core/Constants.cs @@ -16,6 +16,7 @@ internal static class Constants // but this case can't be determined at build time, so we might need LibraryName to be a variable //internal const string LibraryName = "libvlc/osx-x64/lib/libvlc"; #endif + // TODO: fix internal const string CoreLibraryName = "@rpath/libvlccore.dylib"; /// @@ -25,6 +26,7 @@ internal static class Constants internal const string Msvcrt = "msvcrt"; internal const string Libc = "libc"; + internal const string Libdl = "libdl"; internal const string LibSystem = "libSystem"; internal const string Kernel32 = "kernel32"; internal const string LibX11 = "libX11"; diff --git a/src/LibVLCSharp/Shared/Core/Core.Apple.cs b/src/LibVLCSharp/Shared/Core/Core.Apple.cs index 0cd6a5b19..f10faf302 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Apple.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Apple.cs @@ -48,6 +48,7 @@ public static void Initialize(string? libvlcDirectoryPath = null) EnsureVersionsMatch(); } + // TODO: is this still called? private static void InitializeMac(string? libvlcDirectoryPath) { if (!string.IsNullOrEmpty(libvlcDirectoryPath)) diff --git a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs index df51f49cb..9854b64c2 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs @@ -6,8 +6,6 @@ using System.Reflection; using System.Runtime.InteropServices; -[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.ApplicationDirectory | DllImportSearchPath.SafeDirectories)] - namespace LibVLCSharp.Shared { /// @@ -112,38 +110,6 @@ static void InitializeDesktop(string? libvlcDirectoryPath = null) LoadLibVLC(libvlcDirectoryPath); #endif } - -#if NETCOREAPP2_0 - internal class CustomMacAssemblyLoadContext : System.Runtime.Loader.AssemblyLoadContext - { - public CustomMacAssemblyLoadContext() - { - Default.Resolving += OnResolving; - } - - [DllImport(Constants.LibSystem, EntryPoint = "dlopen")] - internal static extern IntPtr Dlopen(string libraryPath, int mode = 1); - - public void LoadLibVLC(string libvlc) => LoadUnmanagedDll(libvlc); - - protected override Assembly Load(AssemblyName assemblyName) - { - throw new NotImplementedException(); - } - - protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) - { - Console.Out.WriteLine("LoadUnmanagedDll called with " + unmanagedDllName); - return Dlopen(unmanagedDllName); - } - - Assembly OnResolving(System.Runtime.Loader.AssemblyLoadContext context, AssemblyName name) - { - Console.Out.WriteLine("OnResolving called ==========="); - return Load(name); - } - } -#endif } } diff --git a/src/LibVLCSharp/Shared/Core/Core.cs b/src/LibVLCSharp/Shared/Core/Core.cs index b1d4b839b..4bf50efa8 100644 --- a/src/LibVLCSharp/Shared/Core/Core.cs +++ b/src/LibVLCSharp/Shared/Core/Core.cs @@ -25,7 +25,7 @@ partial struct Native [DllImport(Constants.Kernel32, SetLastError = true)] internal static extern IntPtr LoadLibrary(string dllToLoad); - [DllImport("libdl", EntryPoint = "dlopen")] + [DllImport(Constants.Libdl, EntryPoint = "dlopen")] internal static extern IntPtr Dlopen(string libraryPath, int mode = 0x002); [DllImport(Constants.Libc)] From 522f71fb9c51d2f7fa3c0bd33a4b6dd33caf7060 Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Thu, 10 Sep 2020 18:54:17 +0200 Subject: [PATCH 07/10] revert packaging updates --- samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj | 2 +- samples/LibVLCSharp.Mac.Sample/packages.config | 2 +- .../LibVLCSharp.NetCore.Sample.csproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj b/samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj index 0df17b9e3..90212d44b 100644 --- a/samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj +++ b/samples/LibVLCSharp.Mac.Sample/LibVLCSharp.Mac.Sample.csproj @@ -99,5 +99,5 @@ - + \ No newline at end of file diff --git a/samples/LibVLCSharp.Mac.Sample/packages.config b/samples/LibVLCSharp.Mac.Sample/packages.config index 0df563dae..017cfce43 100644 --- a/samples/LibVLCSharp.Mac.Sample/packages.config +++ b/samples/LibVLCSharp.Mac.Sample/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/samples/LibVLCSharp.NetCore.Sample/LibVLCSharp.NetCore.Sample.csproj b/samples/LibVLCSharp.NetCore.Sample/LibVLCSharp.NetCore.Sample.csproj index ddf01ded2..1a90fac39 100644 --- a/samples/LibVLCSharp.NetCore.Sample/LibVLCSharp.NetCore.Sample.csproj +++ b/samples/LibVLCSharp.NetCore.Sample/LibVLCSharp.NetCore.Sample.csproj @@ -8,7 +8,7 @@ - + From d949891fd45a3ce3896df67c74758bc147b1094d Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Thu, 10 Sep 2020 18:54:53 +0200 Subject: [PATCH 08/10] remove netcoreapp used for testing --- src/LibVLCSharp/LibVLCSharp.csproj | 2 +- src/LibVLCSharp/Shared/Core/Core.Desktop.cs | 4 ++-- src/LibVLCSharp/Shared/Core/Core.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/LibVLCSharp/LibVLCSharp.csproj b/src/LibVLCSharp/LibVLCSharp.csproj index 599ef6a73..2be4d7ccb 100644 --- a/src/LibVLCSharp/LibVLCSharp.csproj +++ b/src/LibVLCSharp/LibVLCSharp.csproj @@ -28,7 +28,7 @@ This package also contains the views for the following platforms: If you need Xamarin.Forms support, see LibVLCSharp.Forms. LibVLC needs to be installed separately, see VideoLAN.LibVLC.* packages. - netstandard2.1;netstandard2.0;netstandard1.1;netcoreapp2.0 + netstandard2.1;netstandard2.0;netstandard1.1 $(TargetFrameworks);MonoAndroid81;Xamarin.iOS10;Xamarin.Mac20;Xamarin.TVOS10 $(TargetFrameworks);uap10.0;uap10.0.16299;net40;net471 $(TargetsForTfmSpecificBuildOutput);IncludeAWindow diff --git a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs index 9854b64c2..c9aa40b6a 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs @@ -1,4 +1,4 @@ -#if NETFRAMEWORK || NETSTANDARD || NETCOREAPP +#if NETFRAMEWORK || NETSTANDARD using System; using System.Diagnostics; using System.IO; @@ -100,7 +100,7 @@ static void InitializeDesktop(string? libvlcDirectoryPath = null) { pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.Plugins); } - + Console.Out.WriteLine("PluginPath: " + pluginPath); PluginPath(pluginPath); #endif diff --git a/src/LibVLCSharp/Shared/Core/Core.cs b/src/LibVLCSharp/Shared/Core/Core.cs index 4bf50efa8..c02f99b3d 100644 --- a/src/LibVLCSharp/Shared/Core/Core.cs +++ b/src/LibVLCSharp/Shared/Core/Core.cs @@ -62,7 +62,7 @@ static void Log(string message) #endif } -#if (MAC || NETFRAMEWORK || NETSTANDARD || NETCOREAPP) && !NETSTANDARD1_1 +#if (MAC || NETFRAMEWORK || NETSTANDARD) && !NETSTANDARD1_1 static bool Loaded => LibvlcHandle != IntPtr.Zero; static List<(string libvlccore, string libvlc)> ComputeLibVLCSearchPaths() { From 8b9dfe24621441714cc23efe295c15bead978460 Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Mon, 14 Sep 2020 14:27:52 +0200 Subject: [PATCH 09/10] add net40 condition --- src/LibVLCSharp/Shared/Core/Core.Desktop.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs index c9aa40b6a..1a1028bee 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs @@ -88,7 +88,7 @@ static void InitializeDesktop(string? libvlcDirectoryPath = null) if (PlatformHelper.IsMac) { -#if !NETSTANDARD1_1 +#if !NETSTANDARD1_1 && !NET40 var framework = RuntimeInformation.FrameworkDescription; var pluginPath = string.Empty; if(framework.StartsWith(".NET")) From 2925ebc8ff367da0c2040eb94090a62714eb7b4c Mon Sep 17 00:00:00 2001 From: Martin Finkel Date: Mon, 14 Sep 2020 16:00:56 +0200 Subject: [PATCH 10/10] Add helper for detecting whether we are running on dotnet or mono --- src/LibVLCSharp/Shared/Core/Core.Desktop.cs | 4 ++-- src/LibVLCSharp/Shared/Helpers/PlatformHelper.cs | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs index 1a1028bee..19bd2f902 100644 --- a/src/LibVLCSharp/Shared/Core/Core.Desktop.cs +++ b/src/LibVLCSharp/Shared/Core/Core.Desktop.cs @@ -89,15 +89,15 @@ static void InitializeDesktop(string? libvlcDirectoryPath = null) if (PlatformHelper.IsMac) { #if !NETSTANDARD1_1 && !NET40 - var framework = RuntimeInformation.FrameworkDescription; var pluginPath = string.Empty; - if(framework.StartsWith(".NET")) + if(PlatformHelper.IsDotNet) { pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.LibVLC, ArchitectureNames.MacOS64, Constants.Plugins); } else { + // in this case, we have a real macOS app bundle thanks to Mono integration with cocoa pluginPath = Path.Combine(Path.GetDirectoryName(typeof(LibVLC).Assembly.Location), Constants.Plugins); } diff --git a/src/LibVLCSharp/Shared/Helpers/PlatformHelper.cs b/src/LibVLCSharp/Shared/Helpers/PlatformHelper.cs index cee99770e..aae4cc9d4 100644 --- a/src/LibVLCSharp/Shared/Helpers/PlatformHelper.cs +++ b/src/LibVLCSharp/Shared/Helpers/PlatformHelper.cs @@ -64,5 +64,12 @@ public static bool IsMac /// Returns true if running in 64bit process, false otherwise /// public static bool IsX64BitProcess => IntPtr.Size == 8; + +#if !NET40 && !UWP10_0 + /// + /// Returns true if running on dotnet, false otherwise (such as on Mono) + /// + public static bool IsDotNet => RuntimeInformation.FrameworkDescription.StartsWith(".NET"); +#endif } }