mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-18 21:50:25 +01:00
Follow native interoperability best practices
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/best-practices
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
#pragma warning disable CA1031
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Encoder;
|
||||
@@ -12,43 +14,38 @@ namespace MediaBrowser.MediaEncoding.Encoder;
|
||||
/// Helper class for Apple platform specific operations.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("macos")]
|
||||
public static class ApplePlatformHelper
|
||||
public static partial class ApplePlatformHelper
|
||||
{
|
||||
private static readonly string[] _av1DecodeBlacklistedCpuClass = ["M1", "M2"];
|
||||
|
||||
private static string GetSysctlValue(ReadOnlySpan<byte> name)
|
||||
private static string GetSysctlValue(string name)
|
||||
{
|
||||
IntPtr length = IntPtr.Zero;
|
||||
// Get length of the value
|
||||
int osStatus = SysctlByName(name, IntPtr.Zero, ref length, IntPtr.Zero, 0);
|
||||
|
||||
if (osStatus != 0)
|
||||
int osStatus = sysctlbyname(name, Span<byte>.Empty, ref length, IntPtr.Zero, 0);
|
||||
if (osStatus != 0 || length == 0)
|
||||
{
|
||||
throw new NotSupportedException($"Failed to get sysctl value for {System.Text.Encoding.UTF8.GetString(name)} with error {osStatus}");
|
||||
throw new NotSupportedException($"Failed to get sysctl value for {name} with error {osStatus}");
|
||||
}
|
||||
|
||||
IntPtr buffer = Marshal.AllocHGlobal(length.ToInt32());
|
||||
byte[] buffer = ArrayPool<byte>.Shared.Rent((int)length);
|
||||
try
|
||||
{
|
||||
osStatus = SysctlByName(name, buffer, ref length, IntPtr.Zero, 0);
|
||||
osStatus = sysctlbyname(name, buffer.AsSpan().Slice(0, (int)length), ref length, IntPtr.Zero, 0);
|
||||
if (osStatus != 0)
|
||||
{
|
||||
throw new NotSupportedException($"Failed to get sysctl value for {System.Text.Encoding.UTF8.GetString(name)} with error {osStatus}");
|
||||
throw new NotSupportedException($"Failed to get sysctl value for {name} with error {osStatus}");
|
||||
}
|
||||
|
||||
return Marshal.PtrToStringAnsi(buffer) ?? string.Empty;
|
||||
ReadOnlySpan<byte> data = buffer.AsSpan().Slice(0, (int)length);
|
||||
return Encoding.UTF8.GetString(data);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
ArrayPool<byte>.Shared.Return(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
private static int SysctlByName(ReadOnlySpan<byte> name, IntPtr oldp, ref IntPtr oldlenp, IntPtr newp, uint newlen)
|
||||
{
|
||||
return NativeMethods.SysctlByName(name.ToArray(), oldp, ref oldlenp, newp, newlen);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the current system has hardware acceleration for AV1 decoding.
|
||||
/// </summary>
|
||||
@@ -63,7 +60,7 @@ public static class ApplePlatformHelper
|
||||
|
||||
try
|
||||
{
|
||||
string cpuBrandString = GetSysctlValue("machdep.cpu.brand_string"u8);
|
||||
string cpuBrandString = GetSysctlValue("machdep.cpu.brand_string");
|
||||
return !_av1DecodeBlacklistedCpuClass.Any(blacklistedCpuClass => cpuBrandString.Contains(blacklistedCpuClass, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
catch (NotSupportedException e)
|
||||
@@ -78,10 +75,7 @@ public static class ApplePlatformHelper
|
||||
return false;
|
||||
}
|
||||
|
||||
private static class NativeMethods
|
||||
{
|
||||
[DllImport("libc", EntryPoint = "sysctlbyname", SetLastError = true)]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
|
||||
internal static extern int SysctlByName(byte[] name, IntPtr oldp, ref IntPtr oldlenp, IntPtr newp, uint newlen);
|
||||
}
|
||||
[LibraryImport("libc", EntryPoint = "sysctlbyname", SetLastError = true)]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
|
||||
internal static partial int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string name, Span<byte> oldp, ref IntPtr oldlenp, IntPtr newp, uint newlen);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -10,7 +10,7 @@ using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Tests.IO;
|
||||
|
||||
public class ManagedFileSystemTests
|
||||
public partial class ManagedFileSystemTests
|
||||
{
|
||||
private readonly IFixture _fixture;
|
||||
private readonly ManagedFileSystem _sut;
|
||||
@@ -117,7 +117,7 @@ public class ManagedFileSystemTests
|
||||
}
|
||||
|
||||
[SuppressMessage("Naming Rules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Have to")]
|
||||
[DllImport("libc", SetLastError = true, CharSet = CharSet.Ansi)]
|
||||
[LibraryImport("libc", SetLastError = true)]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
|
||||
private static extern int symlink(string target, string linkpath);
|
||||
private static partial int symlink([MarshalAs(UnmanagedType.LPStr)] string target, [MarshalAs(UnmanagedType.LPStr)] string linkpath);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<PropertyGroup>
|
||||
<ProjectGuid>{2E3A1B4B-4225-4AAA-8B29-0181A84E7AEE}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user