| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 | using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Text;using System.Threading.Tasks;using DG.Tweening;using Newtonsoft.Json;using Newtonsoft.Json.Linq;using UnityEngine;using System.Security.Cryptography;using RenderHeads.Media.AVProVideo;using UnityEngine.Networking;using UnityEngine.UI;using UnityEngine.UIElements;using Button = UnityEngine.UI.Button;using UnityAsync;public class ObsPlayerPanel : MonoBehaviour{    public MediaPlayer obsPlayer;    private Button closeButton;    private Text titleText;    private string obsName;    private Button captureButton;    private ExtendedButton_ObsDirCtrl _upButtonObsDirCtrl;    private ExtendedButton_ObsDirCtrl _downButtonObsDirCtrl;    private ExtendedButton_ObsDirCtrl _leftButtonObsDirCtrl;    private ExtendedButton_ObsDirCtrl _rightButtonObsDirCtrl;        private ExtendedButton_ObsDirCtrl _upRightButtonObsDirCtrl;    private ExtendedButton_ObsDirCtrl _downRightButtonObsDirCtrl;    private ExtendedButton_ObsDirCtrl _upLeftButtonObsDirCtrl;    private ExtendedButton_ObsDirCtrl _downLeftButtonObsDirCtrl;    private RectTransform centerImg;    private Vector3 oriPos;        private ObsCtrlType currentDonwType;        private string currentChannelId="";    private string currentDeviceId="";    private string _targetName;    public RawImage bgTex;    public Button windowsButton;    public Button fullScreenButton;    public Button fullScreenButton2;    public bool isFullScreen = false;    public bool openSuccess = false;    private void Awake()    {        obsPlayer = this.transform.Find("AVProVideo").GetComponent<MediaPlayer>();        titleText = this.transform.Find("Title").GetComponent<Text>();        closeButton = this.transform.Find("CloseButton").GetComponent<Button>();        closeButton.onClick.AddListener(Close);        captureButton = this.transform.Find("CaptureButton").GetComponent<Button>();        captureButton.onClick.AddListener(Capture);        centerImg = this.transform.Find("ctrlBg_1").GetComponent<RectTransform>();        oriPos = centerImg.localPosition;                _upButtonObsDirCtrl = this.transform.Find("UpButton").GetComponent<ExtendedButton_ObsDirCtrl>();        _upButtonObsDirCtrl.type = ObsCtrlType.up;        _upButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;        _upButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;        _upButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;                _downButtonObsDirCtrl = this.transform.Find("DownButton").GetComponent<ExtendedButton_ObsDirCtrl>();        _downButtonObsDirCtrl.type = ObsCtrlType.down;        _downButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;        _downButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;        _downButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;                _leftButtonObsDirCtrl = this.transform.Find("LeftButton").GetComponent<ExtendedButton_ObsDirCtrl>();        _leftButtonObsDirCtrl.type = ObsCtrlType.left;        _leftButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;        _leftButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;        _leftButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;                _rightButtonObsDirCtrl = this.transform.Find("RightButton").GetComponent<ExtendedButton_ObsDirCtrl>();        _rightButtonObsDirCtrl.type = ObsCtrlType.right;        _rightButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;        _rightButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;        _rightButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;                _upRightButtonObsDirCtrl = this.transform.Find("UpRightButton").GetComponent<ExtendedButton_ObsDirCtrl>();        _upRightButtonObsDirCtrl.type = ObsCtrlType.upright;        _upRightButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;        _upRightButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;        _upRightButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;                _downRightButtonObsDirCtrl = this.transform.Find("DownRightButton").GetComponent<ExtendedButton_ObsDirCtrl>();        _downRightButtonObsDirCtrl.type = ObsCtrlType.downright;        _downRightButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;        _downRightButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;        _downRightButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;                _upLeftButtonObsDirCtrl = this.transform.Find("UpLeftButton").GetComponent<ExtendedButton_ObsDirCtrl>();        _upLeftButtonObsDirCtrl.type = ObsCtrlType.upleft;        _upLeftButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;        _upLeftButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;        _upLeftButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;                _downLeftButtonObsDirCtrl = this.transform.Find("DownLeftButton").GetComponent<ExtendedButton_ObsDirCtrl>();        _downLeftButtonObsDirCtrl.type = ObsCtrlType.downright;        _downLeftButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;        _downLeftButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;        _downLeftButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;        fullScreenButton = this.transform.Find("FullScreenButton").GetComponent<Button>();        fullScreenButton.onClick.AddListener(FullScreen);        fullScreenButton2= this.transform.Find("PlayBackButton").GetComponent<Button>();        fullScreenButton2.onClick.AddListener(FullScreen);        windowsButton = this.transform.Find("WindowsButton").GetComponent<Button>();        windowsButton.onClick.AddListener(FullScreen);        obsPlayer.GetComponent<RectTransform>().sizeDelta = new Vector2(640,360);        obsPlayer.GetComponent<RectTransform>().anchoredPosition = new Vector2(-62,-16);                fullScreenButton.gameObject.SetActive(true);        windowsButton.gameObject.SetActive(false);    }    public void Play()    {        obsPlayer.Control.Play();    }    public void Pause()    {        obsPlayer.Control.Pause();    }    public void FullScreen()    {        Debug.Log("1111");        isFullScreen = !isFullScreen;        if (isFullScreen)        {            fullScreenButton.gameObject.SetActive(false);            windowsButton.gameObject.SetActive(true);            obsPlayer.GetComponent<RectTransform>().sizeDelta = new Vector2(1920,1080);            obsPlayer.GetComponent<RectTransform>().anchoredPosition = new Vector2(0,-86);        }        else        {            fullScreenButton.gameObject.SetActive(true);            windowsButton.gameObject.SetActive(false);            obsPlayer.GetComponent<RectTransform>().sizeDelta = new Vector2(640,360);            obsPlayer.GetComponent<RectTransform>().anchoredPosition = new Vector2(-62,-16);        }    }    public static string GetHashedName(string originalName)    {        using (SHA256 sha256 = SHA256.Create())        {            byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(originalName));            return BitConverter.ToString(hash).Replace("-", "").ToLower();        }    }    public void Close()    {        Texture2D saveTex = new Texture2D(704, 576);        obsPlayer.ExtractFrame(saveTex);        string hashName = GetHashedName(_targetName);        HttpHelper._Instance.SaveFirstFrame(saveTex.EncodeToJPG(), $"{hashName}.jpg", hashName);        obsPlayer.Control.Stop();        currentChannelId = "";        this.gameObject.SetActive(false);    }        public async void SetObsData(ObsData _data)    {        openSuccess = false;        _targetName = _data.targetName;        if (ObsItem.OBSTexLibrary.ContainsKey(ServerAddress.Server_TextureSavePath + _data.targetName + ".jpg"))        {            bgTex.texture = ObsItem.OBSTexLibrary[ServerAddress.Server_TextureSavePath + _data.targetName + ".jpg"];            bgTex.color = Color.gray;        }        else {            bgTex.texture = null;            bgTex.color = Color.black;        }        obsPlayer.GetComponent<DisplayUGUI>().color = Color.clear;        string playUrl = await HttpHelper._Instance.GetObsUrl(_data.deviceId, _data.channelId);        if (playUrl != "")        {            obsPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL, playUrl, true);            obsPlayer.Play();            currentChannelId = _data.channelId;            currentDeviceId = _data.deviceId;            await new UnityAsync.WaitUntil(() =>            {                return obsPlayer.MediaOpened;            });            openSuccess = true;            obsPlayer.GetComponent<DisplayUGUI>().color = Color.white;        }        else {            openSuccess = false;            obsPlayer.GetComponent<DisplayUGUI>().color = Color.white;        }    }        private void OnDirButtonDown(ObsCtrlType type)    {        if (currentChannelId != null && !currentChannelId.Equals(""))        {            currentDonwType = type;            HttpHelper._Instance.SendObsCameraCtrlCmd(currentDeviceId,currentChannelId,type);            switch (type)            {                case ObsCtrlType.left:                    centerImg.DOLocalMove(oriPos + Vector3.left * 15, 0.1f);                    break;                case ObsCtrlType.right:                    centerImg.DOLocalMove(oriPos + Vector3.right * 15, 0.1f);                    break;                case ObsCtrlType.up:                    centerImg.DOLocalMove(oriPos + Vector3.up * 15, 0.1f);                    break;                case ObsCtrlType.down:                    centerImg.DOLocalMove(oriPos + Vector3.down * 15, 0.1f);                    break;                case ObsCtrlType.upleft:                    centerImg.DOLocalMove(oriPos + Vector3.up * 15+Vector3.left*15, 0.1f);                    break;                case ObsCtrlType.upright:                    centerImg.DOLocalMove(oriPos + Vector3.up * 15+Vector3.right*15, 0.1f);                    break;                case ObsCtrlType.downleft:                    centerImg.DOLocalMove(oriPos + Vector3.down * 15+Vector3.left*15, 0.1f);                    break;                case ObsCtrlType.downright:                    centerImg.DOLocalMove(oriPos + Vector3.down * 15+Vector3.right*15, 0.1f);                    break;            }        }    }    private void OnDirButtonUp(ObsCtrlType type)    {        if (currentChannelId != null &¤tDonwType != ObsCtrlType.none&&  !currentChannelId.Equals(""))        {            if (currentDonwType == type)            {                currentDonwType = ObsCtrlType.none;                HttpHelper._Instance.SendObsCameraCtrlCmd(currentDeviceId,currentChannelId,ObsCtrlType.stop);                centerImg.DOLocalMove(oriPos, 0.1f);            }        }    }    private void OnDirButtonExit(ObsCtrlType type)    {        if (currentChannelId != null&¤tDonwType != ObsCtrlType.none&& !currentChannelId.Equals(""))        {            currentDonwType = ObsCtrlType.none;            HttpHelper._Instance.SendObsCameraCtrlCmd(currentDeviceId,currentChannelId,ObsCtrlType.stop);            centerImg.DOLocalMove(oriPos, 0.1f);        }    }    public void SetTitle(string str)    {        obsName = str;        titleText.text = str;    }        public void Capture()    {        Texture2D saveTex = new Texture2D(704, 576);        obsPlayer.ExtractFrame(saveTex);        string hashName = GetHashedName(obsName);        StartCoroutine(UpLoadFile(saveTex.EncodeToJPG(), $"{obsName}_{GetCurrentUnixTimestampMillis()}.jpg"));    }    public string ConvertToUnicode(string input)    {        StringBuilder sb = new StringBuilder();        for (int i = 0; i < input.Length; i++)        {            char c = input[i];            if (c < 128) // 非汉字字符            {                sb.Append(c);            }            else // 汉字字符            {                sb.Append("\\u" + ((int)c).ToString("x4"));            }        }        return sb.ToString();    }        long GetCurrentUnixTimestampMillis()    {        // Unix时间戳是从1970年1月1日开始计算        DateTime unixStartTime = new DateTime(1970, 1, 1);        // 当前时间        DateTime now = DateTime.UtcNow;        // 当前时间与Unix时间戳的时间间隔        TimeSpan timeSpan = now - unixStartTime;        // 将时间间隔转换为毫秒数        long timestamp = (long)timeSpan.TotalMilliseconds;        return timestamp;    }    IEnumerator UpLoadFile(byte[] uploadData,string fileName)    {        fileName = fileName.Replace(" ", "");        //fileName = ConvertToUnicode(fileName);        Debug.Log(fileName);        WWWForm form = new WWWForm();        form.AddBinaryData("file",uploadData,fileName);        UnityWebRequest www = UnityWebRequest.Post(ServerAddress.API_TextureUploadPath, form);        yield return www.SendWebRequest();        Debug.Log(www.downloadHandler.text);        www.Dispose();    }}
 |