2024-08-30 17:33:22 +08:00
|
|
|
|
using MAME.Core;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
public class UniVideoPlayer : MonoBehaviour, IVideoPlayer
|
|
|
|
|
{
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private int mWidth;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private int mHeight;
|
|
|
|
|
[SerializeField]
|
2024-08-07 18:23:55 +08:00
|
|
|
|
private int mDataLenght;
|
|
|
|
|
[SerializeField]
|
2024-07-30 17:38:24 +08:00
|
|
|
|
private Texture2D m_rawBufferWarper;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private RawImage m_drawCanvas;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private RectTransform m_drawCanvasrect;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
int[] mFrameData;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
IntPtr mFrameDataPtr;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
|
|
|
|
|
private TimeSpan lastElapsed;
|
|
|
|
|
public double videoFPS { get; private set; }
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public ulong mFrame { get; private set; }
|
2024-08-08 16:24:18 +08:00
|
|
|
|
bool bInit = false;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2024-08-30 17:33:22 +08:00
|
|
|
|
mFrame = 0;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
m_drawCanvas = GameObject.Find("GameRawImage").GetComponent<RawImage>();
|
2024-08-07 18:23:55 +08:00
|
|
|
|
m_drawCanvasrect = m_drawCanvas.GetComponent<RectTransform>();
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|
2024-08-08 16:24:18 +08:00
|
|
|
|
|
2024-08-07 18:23:55 +08:00
|
|
|
|
public void Initialize(int width, int height,IntPtr framePtr)
|
2024-07-30 17:38:24 +08:00
|
|
|
|
{
|
2024-08-07 18:23:55 +08:00
|
|
|
|
m_drawCanvas.color = Color.white;
|
|
|
|
|
|
2024-08-08 16:24:18 +08:00
|
|
|
|
if (m_rawBufferWarper == null || mWidth != width || mHeight != height)
|
|
|
|
|
{
|
|
|
|
|
mWidth = width;
|
|
|
|
|
mHeight = height;
|
|
|
|
|
mDataLenght = width * height * 4;
|
|
|
|
|
mFrameData = new int[mWidth * mHeight];
|
|
|
|
|
//MAME<4D><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD>BGRA32<33><32><EFBFBD>úú<C3BA>
|
|
|
|
|
m_rawBufferWarper = new Texture2D(mWidth, mHeight, TextureFormat.BGRA32, false);
|
|
|
|
|
m_rawBufferWarper.filterMode = FilterMode.Point;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mFrameDataPtr = framePtr;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
m_drawCanvas.texture = m_rawBufferWarper;
|
2024-08-08 16:24:18 +08:00
|
|
|
|
bInit = true;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
|
|
|
|
|
float targetWidth = ((float)mWidth / mHeight) * m_drawCanvasrect.rect.height ;
|
|
|
|
|
m_drawCanvasrect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, targetWidth);
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 16:24:18 +08:00
|
|
|
|
public void StopVideo()
|
2024-07-30 17:38:24 +08:00
|
|
|
|
{
|
2024-08-08 16:24:18 +08:00
|
|
|
|
bInit = false;
|
|
|
|
|
m_drawCanvas.color = new Color(0,0,0,0);
|
|
|
|
|
}
|
2024-07-30 17:38:24 +08:00
|
|
|
|
|
2024-08-08 16:24:18 +08:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
if (!bInit) return;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
m_rawBufferWarper.LoadRawTextureData(mFrameDataPtr, mDataLenght);
|
|
|
|
|
m_rawBufferWarper.Apply();
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public void SubmitVideo(int[] data, long frame_number)
|
2024-07-30 17:38:24 +08:00
|
|
|
|
{
|
2024-08-30 17:33:22 +08:00
|
|
|
|
mFrame = (ulong)frame_number;
|
2025-01-16 11:14:23 +08:00
|
|
|
|
UMAME.instance.mTimeSpan.SetTick(mFrame);
|
2024-07-30 17:38:24 +08:00
|
|
|
|
var current = UMAME.sw.Elapsed;
|
|
|
|
|
var delta = current - lastElapsed;
|
|
|
|
|
lastElapsed = current;
|
|
|
|
|
videoFPS = 1d / delta.TotalSeconds;
|
|
|
|
|
mFrameData = data;
|
2024-08-30 17:33:22 +08:00
|
|
|
|
|
|
|
|
|
//Debug.Log($"frame_number -> {frame_number}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] GetScreenImg()
|
|
|
|
|
{
|
|
|
|
|
return (m_drawCanvas.texture as Texture2D).EncodeToJPG();
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|