ApplePlatformHelper.cs 2.9 KB

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