| | | 1 | | #pragma warning disable CA1031 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Runtime.Versioning; |
| | | 8 | | using System.Text; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | |
| | | 11 | | namespace MediaBrowser.MediaEncoding.Encoder; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Helper class for Apple platform specific operations. |
| | | 15 | | /// </summary> |
| | | 16 | | [SupportedOSPlatform("macos")] |
| | | 17 | | public static partial class ApplePlatformHelper |
| | | 18 | | { |
| | 0 | 19 | | private static readonly string[] _av1DecodeBlacklistedCpuClass = ["M1", "M2"]; |
| | | 20 | | |
| | | 21 | | internal static string GetSysctlValue(string name) |
| | | 22 | | { |
| | 0 | 23 | | nuint length = 0; |
| | | 24 | | // Get length of the value |
| | 0 | 25 | | int osStatus = sysctlbyname(name, Span<byte>.Empty, ref length, IntPtr.Zero, 0); |
| | 0 | 26 | | if (osStatus != 0 || length == 0) |
| | | 27 | | { |
| | 0 | 28 | | throw new NotSupportedException($"Failed to get sysctl value for {name} with error {osStatus}"); |
| | | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | byte[] buffer = ArrayPool<byte>.Shared.Rent((int)length); |
| | | 32 | | try |
| | | 33 | | { |
| | 0 | 34 | | osStatus = sysctlbyname(name, buffer.AsSpan()[..(int)length], ref length, IntPtr.Zero, 0); |
| | 0 | 35 | | if (osStatus != 0) |
| | | 36 | | { |
| | 0 | 37 | | throw new NotSupportedException($"Failed to get sysctl value for {name} with error {osStatus}"); |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | if (length < 1) |
| | | 41 | | { |
| | 0 | 42 | | return string.Empty; |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | ReadOnlySpan<byte> data = buffer.AsSpan()[..(int)(length - 1)]; |
| | 0 | 46 | | return Encoding.UTF8.GetString(data); |
| | | 47 | | } |
| | | 48 | | finally |
| | | 49 | | { |
| | 0 | 50 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 0 | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Check if the current system has hardware acceleration for AV1 decoding. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="logger">The logger used for error logging.</param> |
| | | 58 | | /// <returns>Boolean indicates the hwaccel support.</returns> |
| | | 59 | | public static bool HasAv1HardwareAccel(ILogger logger) |
| | | 60 | | { |
| | 0 | 61 | | if (!RuntimeInformation.OSArchitecture.Equals(Architecture.Arm64)) |
| | | 62 | | { |
| | 0 | 63 | | return false; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | try |
| | | 67 | | { |
| | 0 | 68 | | string cpuBrandString = GetSysctlValue("machdep.cpu.brand_string"); |
| | 0 | 69 | | return !_av1DecodeBlacklistedCpuClass.Any(blacklistedCpuClass => cpuBrandString.Contains(blacklistedCpuClass |
| | | 70 | | } |
| | 0 | 71 | | catch (NotSupportedException e) |
| | | 72 | | { |
| | 0 | 73 | | logger.LogError("Error getting CPU brand string: {Message}", e.Message); |
| | 0 | 74 | | } |
| | 0 | 75 | | catch (Exception e) |
| | | 76 | | { |
| | 0 | 77 | | logger.LogError("Unknown error occured: {Exception}", e); |
| | 0 | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | return false; |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | [LibraryImport("libc", EntryPoint = "sysctlbyname", SetLastError = true)] |
| | | 84 | | [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] |
| | | 85 | | internal static partial int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string name, Span<byte> oldp, ref nuint ol |
| | | 86 | | } |