Files
video-orchestra/godot-project/scripts/VideoOrchestraManager.cs
2025-09-15 00:17:01 +09:00

149 lines
4.2 KiB
C#

using Godot;
using System;
using VideoOrchestra.Platform;
namespace VideoOrchestra
{
public partial class VideoOrchestraManager : Node
{
private const int MAX_STREAMS = 3;
private IVP9PlatformDecoder _platformDecoder;
private VP9PlatformInfo _platformInfo;
private bool _initialized = false;
[Export] public int StreamWidth { get; set; } = 1920;
[Export] public int StreamHeight { get; set; } = 1080;
[Export] public bool UseHardwareDecoding { get; set; } = true;
[Export] public bool ShowPlatformInfo { get; set; } = true;
[Signal] public delegate void StreamDecodedEventHandler(int streamId);
[Signal] public delegate void DecoderErrorEventHandler(int streamId, string error);
[Signal] public delegate void DecoderInitializedEventHandler(string platformName, bool hardwareEnabled);
public override void _Ready()
{
InitializePlatformDecoder();
}
private void InitializePlatformDecoder()
{
GD.Print("[Manager] Starting platform decoder initialization...");
try
{
_platformInfo = VP9PlatformFactory.GetPlatformInfo();
if (ShowPlatformInfo)
{
GD.Print($"[Manager] VP9 Platform Info: {_platformInfo}");
}
GD.Print("[Manager] Creating platform-specific decoder...");
_platformDecoder = VP9PlatformFactory.CreateDecoder(UseHardwareDecoding);
if (_platformDecoder == null)
{
GD.PrintErr("[Manager] Failed to create platform decoder object.");
return;
}
GD.Print($"[Manager] Decoder object created: {_platformDecoder.PlatformName}");
GD.Print("[Manager] Calling decoder.Initialize()...");
_initialized = _platformDecoder.Initialize(StreamWidth, StreamHeight, UseHardwareDecoding);
GD.Print($"[Manager] decoder.Initialize() returned: {_initialized}");
if (_initialized)
{
bool hardwareEnabled = UseHardwareDecoding && _platformDecoder.IsHardwareDecodingSupported;
GD.Print($"[Manager] VP9 Orchestra initialized successfully.");
GD.Print($"[Manager] Hardware acceleration: {(hardwareEnabled ? "Enabled" : "Disabled")}");
EmitSignal(SignalName.DecoderInitialized, _platformDecoder.PlatformName, hardwareEnabled);
}
else
{
GD.PrintErr($"[Manager] Failed to initialize {_platformDecoder.PlatformName} VP9 decoder.");
}
}
catch (PlatformNotSupportedException ex)
{
GD.PrintErr($"[Manager] Platform not supported: {ex.Message}");
}
catch (Exception ex)
{
GD.PrintErr($"[Manager] Error during decoder initialization: {ex.Message}");
}
}
public void UpdateTextures()
{
if (!_initialized || _platformDecoder == null) return;
_platformDecoder.UpdateTextures();
}
public bool DecodeFrame(byte[] frameData, int streamId)
{
if (!_initialized || streamId < 0 || streamId >= MAX_STREAMS || _platformDecoder == null)
{
return false;
}
try
{
bool success = _platformDecoder.DecodeFrame(frameData, streamId);
if (success)
{
EmitSignal(SignalName.StreamDecoded, streamId);
}
return success;
}
catch (VP9DecoderException vpEx)
{
GD.PrintErr($"[Manager] VP9 decoder error: {vpEx.Message}");
EmitSignal(SignalName.DecoderError, streamId, vpEx.Message);
return false;
}
catch (Exception ex)
{
GD.PrintErr($"[Manager] Error decoding frame for stream {streamId}: {ex.Message}");
EmitSignal(SignalName.DecoderError, streamId, ex.Message);
return false;
}
}
public ImageTexture GetStreamTexture(int streamId)
{
if (!_initialized || streamId < 0 || streamId >= MAX_STREAMS || _platformDecoder == null) return null;
return _platformDecoder.GetDecodedTexture(streamId);
}
public uint GetNativeTextureId(int streamId)
{
if (!_initialized || streamId < 0 || streamId >= MAX_STREAMS || _platformDecoder == null) return 0;
return _platformDecoder.GetNativeTextureId(streamId);
}
public VP9PlatformInfo GetPlatformInfo()
{
return _platformInfo;
}
public VP9DecoderStatus GetDecoderStatus()
{
return _platformDecoder?.GetStatus() ?? VP9DecoderStatus.Uninitialized;
}
public override void _ExitTree()
{
if (_platformDecoder != null)
{
_platformDecoder.Dispose();
_platformDecoder = null;
}
_initialized = false;
}
}
}