Files
video-orchestra/godot-project/scripts/Platform/VP9PlatformFactory.cs

143 lines
5.6 KiB
C#

using Godot;
using System;
namespace VideoOrchestra.Platform
{
/// <summary>
/// Factory for creating platform-specific VP9 decoders
/// </summary>
public static class VP9PlatformFactory
{
/// <summary>
/// Create the appropriate VP9 decoder for the current platform
/// </summary>
/// <param name="preferHardware">Prefer hardware acceleration if available</param>
/// <returns>Platform-specific VP9 decoder implementation</returns>
public static IVP9PlatformDecoder CreateDecoder(bool preferHardware = true)
{
string platform = OS.GetName().ToLower();
try
{
switch (platform)
{
case "windows":
GD.Print("Creating Windows Media Foundation VP9 decoder");
return new WindowsVP9Decoder();
case "android":
GD.Print("Creating Android MediaCodec VP9 decoder");
return new AndroidVP9Decoder();
case "ios":
GD.Print("Creating iOS VideoToolbox VP9 decoder");
return new iOSVP9Decoder();
case "macos":
case "osx":
GD.Print("Creating macOS VideoToolbox VP9 decoder");
return new macOSVP9Decoder();
case "linux":
case "x11":
GD.Print("Creating Linux software VP9 decoder");
return new LinuxVP9Decoder();
default:
GD.PrintErr($"Unsupported platform for VP9 decoding: {platform}");
throw new PlatformNotSupportedException($"Platform '{platform}' is not supported for VP9 decoding");
}
}
catch (Exception ex) when (!(ex is PlatformNotSupportedException))
{
GD.PrintErr($"Failed to create VP9 decoder for platform '{platform}': {ex.Message}");
// Fallback to software decoder if available
try
{
GD.Print("Attempting fallback to software VP9 decoder");
return new SoftwareVP9Decoder();
}
catch (Exception fallbackEx)
{
GD.PrintErr($"Fallback software decoder also failed: {fallbackEx.Message}");
throw new VP9DecoderException("Factory",
$"Failed to create VP9 decoder for platform '{platform}' and fallback failed", ex);
}
}
}
/// <summary>
/// Get information about VP9 support on the current platform
/// </summary>
/// <returns>Platform VP9 support information</returns>
public static VP9PlatformInfo GetPlatformInfo()
{
string platform = OS.GetName().ToLower();
return new VP9PlatformInfo
{
PlatformName = platform,
SupportsHardwareDecoding = GetHardwareSupportForPlatform(platform),
SupportsSoftwareDecoding = true, // All platforms support software fallback
RecommendedMaxStreams = GetRecommendedStreamCountForPlatform(platform),
SupportedPixelFormats = GetSupportedPixelFormats(platform)
};
}
private static bool GetHardwareSupportForPlatform(string platform)
{
return platform switch
{
"windows" => true, // Media Foundation
"android" => true, // MediaCodec
"ios" => true, // VideoToolbox
"macos" or "osx" => true, // VideoToolbox
"linux" or "x11" => false, // Software only for now
_ => false
};
}
private static int GetRecommendedStreamCountForPlatform(string platform)
{
return platform switch
{
"windows" => 3, // Full support
"android" => 3, // Full support
"ios" => 3, // Full support
"macos" or "osx" => 3, // Full support
"linux" or "x11" => 1, // Limited for software decoding
_ => 1
};
}
private static string[] GetSupportedPixelFormats(string platform)
{
return platform switch
{
"windows" => new[] { "NV12", "YUV420P", "BGRA32" },
"android" => new[] { "NV21", "YUV420P", "RGBA32" },
"ios" or "macos" or "osx" => new[] { "NV12", "YUV420P", "BGRA32" },
_ => new[] { "YUV420P", "RGBA32" }
};
}
}
/// <summary>
/// Information about VP9 decoding capabilities on the current platform
/// </summary>
public class VP9PlatformInfo
{
public string PlatformName { get; set; }
public bool SupportsHardwareDecoding { get; set; }
public bool SupportsSoftwareDecoding { get; set; }
public int RecommendedMaxStreams { get; set; }
public string[] SupportedPixelFormats { get; set; }
public override string ToString()
{
return $"Platform: {PlatformName}, Hardware: {SupportsHardwareDecoding}, " +
$"Software: {SupportsSoftwareDecoding}, Max Streams: {RecommendedMaxStreams}";
}
}
}