feat: Simplify macOS VLC initialization and remove redundant path checks
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
<PackageReference Include="LibVLCSharp.Avalonia" Version="3.9.6" />
|
<PackageReference Include="LibVLCSharp.Avalonia" Version="3.9.6" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||||
<PackageReference Include="System.Reactive" Version="6.0.2" />
|
<PackageReference Include="System.Reactive" Version="6.0.2" />
|
||||||
|
<PackageReference Include="VideoLAN.LibVLC.Mac" Version="3.1.3.1" Condition="$([MSBuild]::IsOSPlatform('macos'))" />
|
||||||
<PackageReference Include="VideoLAN.LibVLC.Windows" Version="3.0.23" Condition="$([MSBuild]::IsOSPlatform('windows'))" />
|
<PackageReference Include="VideoLAN.LibVLC.Windows" Version="3.0.23" Condition="$([MSBuild]::IsOSPlatform('windows'))" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
@@ -30,32 +31,4 @@
|
|||||||
<AvaloniaResource Include="Assets\**\*" />
|
<AvaloniaResource Include="Assets\**\*" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="$([MSBuild]::IsOSPlatform('macos'))">
|
|
||||||
<None Include="natives/macos/**">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
<Link>natives/macos/%(RecursiveDir)%(Filename)%(Extension)</Link>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<Target Name="EnsureBundledMacVlc" BeforeTargets="Build" Condition="$([MSBuild]::IsOSPlatform('macos')) and ( !Exists('$(MSBuildProjectDirectory)/natives/macos/lib/libvlccore.dylib') or !Exists('$(MSBuildProjectDirectory)/natives/macos/plugins') )">
|
|
||||||
<Message Text="Bundling VLC native macOS runtime files (one-time download)..." Importance="high" />
|
|
||||||
<Exec Command="sh "$(MSBuildProjectDirectory)/scripts/fetch-vlc-macos.sh" "$(MSBuildProjectDirectory)"" />
|
|
||||||
</Target>
|
|
||||||
|
|
||||||
<Target Name="CopyBundledMacVlcToOutput" AfterTargets="Build" Condition="$([MSBuild]::IsOSPlatform('macos')) and Exists('$(MSBuildProjectDirectory)/natives/macos/lib/libvlccore.dylib')">
|
|
||||||
<ItemGroup>
|
|
||||||
<BundledMacVlcFiles Include="$(MSBuildProjectDirectory)/natives/macos/**/*" />
|
|
||||||
<BundledMacVlcRootLibs Include="$(MSBuildProjectDirectory)/natives/macos/lib/libvlc.dylib;$(MSBuildProjectDirectory)/natives/macos/lib/libvlccore.dylib" />
|
|
||||||
<BundledMacVlcRootPlugins Include="$(MSBuildProjectDirectory)/natives/macos/plugins/**/*" />
|
|
||||||
</ItemGroup>
|
|
||||||
<Copy SourceFiles="@(BundledMacVlcFiles)"
|
|
||||||
DestinationFiles="@(BundledMacVlcFiles->'$(TargetDir)natives/macos/%(RecursiveDir)%(Filename)%(Extension)')"
|
|
||||||
SkipUnchangedFiles="true" />
|
|
||||||
<Copy SourceFiles="@(BundledMacVlcRootLibs)"
|
|
||||||
DestinationFiles="@(BundledMacVlcRootLibs->'$(TargetDir)%(Filename)%(Extension)')"
|
|
||||||
SkipUnchangedFiles="true" />
|
|
||||||
<Copy SourceFiles="@(BundledMacVlcRootPlugins)"
|
|
||||||
DestinationFiles="@(BundledMacVlcRootPlugins->'$(TargetDir)plugins/%(RecursiveDir)%(Filename)%(Extension)')"
|
|
||||||
SkipUnchangedFiles="true" />
|
|
||||||
</Target>
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ using Avalonia.Threading;
|
|||||||
using LibVLCSharp.Shared;
|
using LibVLCSharp.Shared;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.IO;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
@@ -40,40 +39,9 @@ public partial class PlayerView : UserControl
|
|||||||
Log("Attach to visual tree -> initializing VLC");
|
Log("Attach to visual tree -> initializing VLC");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string? macPluginDirectory = null;
|
Log("Core.Initialize() start");
|
||||||
if (OperatingSystem.IsMacOS())
|
Core.Initialize();
|
||||||
{
|
Log("Core.Initialize() completed");
|
||||||
if (!TryGetMacLibVlcPaths(out var libVlcDirectory, out macPluginDirectory))
|
|
||||||
{
|
|
||||||
const string message = "VLC native dependencies not found. Build once with internet to auto-bundle natives/macos (lib + plugins).";
|
|
||||||
Log(message);
|
|
||||||
_viewModel?.SetPlaybackStatus(message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(macPluginDirectory) && Directory.Exists(macPluginDirectory))
|
|
||||||
{
|
|
||||||
Environment.SetEnvironmentVariable("VLC_PLUGIN_PATH", macPluginDirectory);
|
|
||||||
Log($"VLC_PLUGIN_PATH set to: {macPluginDirectory}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var dyldLibraryPath = PrependPath(Environment.GetEnvironmentVariable("DYLD_LIBRARY_PATH"), libVlcDirectory);
|
|
||||||
var dyldFallbackPath = PrependPath(Environment.GetEnvironmentVariable("DYLD_FALLBACK_LIBRARY_PATH"), libVlcDirectory);
|
|
||||||
Environment.SetEnvironmentVariable("DYLD_LIBRARY_PATH", dyldLibraryPath);
|
|
||||||
Environment.SetEnvironmentVariable("DYLD_FALLBACK_LIBRARY_PATH", dyldFallbackPath);
|
|
||||||
Log($"DYLD_LIBRARY_PATH set to: {dyldLibraryPath}");
|
|
||||||
Log($"DYLD_FALLBACK_LIBRARY_PATH set to: {dyldFallbackPath}");
|
|
||||||
|
|
||||||
Log($"Core.Initialize(path) start: {libVlcDirectory}");
|
|
||||||
Core.Initialize(libVlcDirectory);
|
|
||||||
Log("Core.Initialize(path) completed");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log("Core.Initialize() start");
|
|
||||||
Core.Initialize();
|
|
||||||
Log("Core.Initialize() completed");
|
|
||||||
}
|
|
||||||
|
|
||||||
_libVlc = new LibVLC(enableDebugLogs: true);
|
_libVlc = new LibVLC(enableDebugLogs: true);
|
||||||
Log("LibVLC instance created");
|
Log("LibVLC instance created");
|
||||||
@@ -103,77 +71,6 @@ public partial class PlayerView : UserControl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool TryGetMacLibVlcPaths(out string libDirectory, out string pluginDirectory)
|
|
||||||
{
|
|
||||||
var outputBase = AppContext.BaseDirectory;
|
|
||||||
var candidates = new[]
|
|
||||||
{
|
|
||||||
outputBase,
|
|
||||||
Path.Combine(outputBase, "natives", "macos", "lib"),
|
|
||||||
Path.Combine(outputBase, "lib"),
|
|
||||||
Path.Combine(outputBase, "libvlc", "osx-x64", "lib"),
|
|
||||||
"/Applications/VLC.app/Contents/MacOS/lib",
|
|
||||||
"/opt/homebrew/lib",
|
|
||||||
"/usr/local/lib"
|
|
||||||
};
|
|
||||||
|
|
||||||
foreach (var candidateDir in candidates)
|
|
||||||
{
|
|
||||||
var libvlc = Path.Combine(candidateDir, "libvlc.dylib");
|
|
||||||
var libvlccore = Path.Combine(candidateDir, "libvlccore.dylib");
|
|
||||||
if (File.Exists(libvlc) && File.Exists(libvlccore))
|
|
||||||
{
|
|
||||||
var pluginCandidates = new[]
|
|
||||||
{
|
|
||||||
Path.Combine(candidateDir, "plugins"),
|
|
||||||
Path.Combine(Path.GetDirectoryName(candidateDir) ?? string.Empty, "plugins")
|
|
||||||
};
|
|
||||||
|
|
||||||
pluginDirectory = string.Empty;
|
|
||||||
foreach (var candidatePluginDir in pluginCandidates)
|
|
||||||
{
|
|
||||||
if (Directory.Exists(candidatePluginDir))
|
|
||||||
{
|
|
||||||
pluginDirectory = candidatePluginDir;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
libDirectory = candidateDir;
|
|
||||||
Log($"Found macOS VLC libs in: {candidateDir}");
|
|
||||||
if (!string.IsNullOrWhiteSpace(pluginDirectory))
|
|
||||||
{
|
|
||||||
Log($"Found macOS VLC plugins in: {pluginDirectory}");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log("macOS VLC plugins directory not found next to libraries");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
libDirectory = string.Empty;
|
|
||||||
pluginDirectory = string.Empty;
|
|
||||||
Log("macOS VLC libs not found. Checked directories: " + string.Join("; ", candidates));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string PrependPath(string? existingPaths, string pathToPrepend)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(pathToPrepend))
|
|
||||||
return existingPaths ?? string.Empty;
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(existingPaths))
|
|
||||||
return pathToPrepend;
|
|
||||||
|
|
||||||
var parts = existingPaths.Split(':', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
|
||||||
if (parts.Contains(pathToPrepend))
|
|
||||||
return existingPaths;
|
|
||||||
|
|
||||||
return pathToPrepend + ":" + existingPaths;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDetachedFromVisualTree(object? sender, Avalonia.VisualTreeAttachmentEventArgs e)
|
private void OnDetachedFromVisualTree(object? sender, Avalonia.VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
Log("Detach from visual tree -> disposing player resources");
|
Log("Detach from visual tree -> disposing player resources");
|
||||||
|
|||||||
Reference in New Issue
Block a user