ObsPlayerPanel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using DG.Tweening;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using UnityEngine;
  11. using RenderHeads.Media.AVProVideo;
  12. using UnityEngine.Networking;
  13. using UnityEngine.UI;
  14. using UnityEngine.UIElements;
  15. using Button = UnityEngine.UI.Button;
  16. using UnityAsync;
  17. public class ObsPlayerPanel : MonoBehaviour
  18. {
  19. public MediaPlayer obsPlayer;
  20. private Button closeButton;
  21. private Text titleText;
  22. private string obsName;
  23. private Button captureButton;
  24. private ExtendedButton_ObsDirCtrl _upButtonObsDirCtrl;
  25. private ExtendedButton_ObsDirCtrl _downButtonObsDirCtrl;
  26. private ExtendedButton_ObsDirCtrl _leftButtonObsDirCtrl;
  27. private ExtendedButton_ObsDirCtrl _rightButtonObsDirCtrl;
  28. private ExtendedButton_ObsDirCtrl _upRightButtonObsDirCtrl;
  29. private ExtendedButton_ObsDirCtrl _downRightButtonObsDirCtrl;
  30. private ExtendedButton_ObsDirCtrl _upLeftButtonObsDirCtrl;
  31. private ExtendedButton_ObsDirCtrl _downLeftButtonObsDirCtrl;
  32. private RectTransform centerImg;
  33. private Vector3 oriPos;
  34. private ObsCtrlType currentDonwType;
  35. private string currentChannelId="";
  36. private string currentDeviceId="";
  37. private string _targetName;
  38. public RawImage bgTex;
  39. public Button windowsButton;
  40. public Button fullScreenButton;
  41. public Button fullScreenButton2;
  42. public bool isFullScreen = false;
  43. private void Awake()
  44. {
  45. obsPlayer = this.transform.Find("AVProVideo").GetComponent<MediaPlayer>();
  46. titleText = this.transform.Find("Title").GetComponent<Text>();
  47. closeButton = this.transform.Find("CloseButton").GetComponent<Button>();
  48. closeButton.onClick.AddListener(Close);
  49. captureButton = this.transform.Find("CaptureButton").GetComponent<Button>();
  50. captureButton.onClick.AddListener(Capture);
  51. centerImg = this.transform.Find("ctrlBg_1").GetComponent<RectTransform>();
  52. oriPos = centerImg.localPosition;
  53. _upButtonObsDirCtrl = this.transform.Find("UpButton").GetComponent<ExtendedButton_ObsDirCtrl>();
  54. _upButtonObsDirCtrl.type = ObsCtrlType.up;
  55. _upButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;
  56. _upButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;
  57. _upButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;
  58. _downButtonObsDirCtrl = this.transform.Find("DownButton").GetComponent<ExtendedButton_ObsDirCtrl>();
  59. _downButtonObsDirCtrl.type = ObsCtrlType.down;
  60. _downButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;
  61. _downButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;
  62. _downButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;
  63. _leftButtonObsDirCtrl = this.transform.Find("LeftButton").GetComponent<ExtendedButton_ObsDirCtrl>();
  64. _leftButtonObsDirCtrl.type = ObsCtrlType.left;
  65. _leftButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;
  66. _leftButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;
  67. _leftButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;
  68. _rightButtonObsDirCtrl = this.transform.Find("RightButton").GetComponent<ExtendedButton_ObsDirCtrl>();
  69. _rightButtonObsDirCtrl.type = ObsCtrlType.right;
  70. _rightButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;
  71. _rightButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;
  72. _rightButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;
  73. _upRightButtonObsDirCtrl = this.transform.Find("UpRightButton").GetComponent<ExtendedButton_ObsDirCtrl>();
  74. _upRightButtonObsDirCtrl.type = ObsCtrlType.upright;
  75. _upRightButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;
  76. _upRightButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;
  77. _upRightButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;
  78. _downRightButtonObsDirCtrl = this.transform.Find("DownRightButton").GetComponent<ExtendedButton_ObsDirCtrl>();
  79. _downRightButtonObsDirCtrl.type = ObsCtrlType.downright;
  80. _downRightButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;
  81. _downRightButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;
  82. _downRightButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;
  83. _upLeftButtonObsDirCtrl = this.transform.Find("UpLeftButton").GetComponent<ExtendedButton_ObsDirCtrl>();
  84. _upLeftButtonObsDirCtrl.type = ObsCtrlType.upleft;
  85. _upLeftButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;
  86. _upLeftButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;
  87. _upLeftButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;
  88. _downLeftButtonObsDirCtrl = this.transform.Find("DownLeftButton").GetComponent<ExtendedButton_ObsDirCtrl>();
  89. _downLeftButtonObsDirCtrl.type = ObsCtrlType.downright;
  90. _downLeftButtonObsDirCtrl.onPointerDown+=OnDirButtonDown;
  91. _downLeftButtonObsDirCtrl.onPointerUp+=OnDirButtonUp;
  92. _downLeftButtonObsDirCtrl.onPointerExit+=OnDirButtonExit;
  93. fullScreenButton = this.transform.Find("FullScreenButton").GetComponent<Button>();
  94. fullScreenButton.onClick.AddListener(FullScreen);
  95. fullScreenButton2= this.transform.Find("PlayBackButton").GetComponent<Button>();
  96. fullScreenButton2.onClick.AddListener(FullScreen);
  97. windowsButton = this.transform.Find("WindowsButton").GetComponent<Button>();
  98. windowsButton.onClick.AddListener(FullScreen);
  99. obsPlayer.GetComponent<RectTransform>().sizeDelta = new Vector2(640,360);
  100. obsPlayer.GetComponent<RectTransform>().anchoredPosition = new Vector2(-62,-16);
  101. fullScreenButton.gameObject.SetActive(true);
  102. windowsButton.gameObject.SetActive(false);
  103. }
  104. public void Play()
  105. {
  106. obsPlayer.Control.Play();
  107. }
  108. public void Pause()
  109. {
  110. obsPlayer.Control.Pause();
  111. }
  112. public void FullScreen()
  113. {
  114. Debug.Log("1111");
  115. isFullScreen = !isFullScreen;
  116. if (isFullScreen)
  117. {
  118. fullScreenButton.gameObject.SetActive(false);
  119. windowsButton.gameObject.SetActive(true);
  120. obsPlayer.GetComponent<RectTransform>().sizeDelta = new Vector2(1920,1080);
  121. obsPlayer.GetComponent<RectTransform>().anchoredPosition = new Vector2(0,-86);
  122. }
  123. else
  124. {
  125. fullScreenButton.gameObject.SetActive(true);
  126. windowsButton.gameObject.SetActive(false);
  127. obsPlayer.GetComponent<RectTransform>().sizeDelta = new Vector2(640,360);
  128. obsPlayer.GetComponent<RectTransform>().anchoredPosition = new Vector2(-62,-16);
  129. }
  130. }
  131. public void Close()
  132. {
  133. Texture2D saveTex = new Texture2D(704, 576);
  134. obsPlayer.ExtractFrame(saveTex);
  135. HttpHelper._Instance.SaveFirstFrame(saveTex.EncodeToJPG(), $"{_targetName}.jpg", _targetName);
  136. obsPlayer.Control.Stop();
  137. currentChannelId = "";
  138. this.gameObject.SetActive(false);
  139. }
  140. public async void SetObsData(ObsData _data)
  141. {
  142. _targetName = _data.targetName;
  143. if (ObsItem.OBSTexLibrary.ContainsKey(ServerAddress.Server_TextureSavePath + _data.targetName + ".jpg"))
  144. {
  145. bgTex.texture = ObsItem.OBSTexLibrary[ServerAddress.Server_TextureSavePath + _data.targetName + ".jpg"];
  146. bgTex.color = Color.gray;
  147. }
  148. else {
  149. bgTex.texture = null;
  150. bgTex.color = Color.black;
  151. }
  152. string playUrl = await HttpHelper._Instance.GetObsUrl(_data.deviceId, _data.channelId);
  153. obsPlayer.GetComponent<DisplayUGUI>().color = Color.clear;
  154. obsPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,playUrl, true);
  155. obsPlayer.Play();
  156. currentChannelId = _data.channelId;
  157. currentDeviceId = _data.deviceId;
  158. await new UnityAsync.WaitUntil(() =>
  159. {
  160. return obsPlayer.MediaOpened;
  161. });
  162. obsPlayer.GetComponent<DisplayUGUI>().color = Color.white;
  163. }
  164. private void OnDirButtonDown(ObsCtrlType type)
  165. {
  166. if (currentChannelId != null && !currentChannelId.Equals(""))
  167. {
  168. currentDonwType = type;
  169. HttpHelper._Instance.SendObsCameraCtrlCmd(currentDeviceId,currentChannelId,type);
  170. switch (type)
  171. {
  172. case ObsCtrlType.left:
  173. centerImg.DOLocalMove(oriPos + Vector3.left * 15, 0.1f);
  174. break;
  175. case ObsCtrlType.right:
  176. centerImg.DOLocalMove(oriPos + Vector3.right * 15, 0.1f);
  177. break;
  178. case ObsCtrlType.up:
  179. centerImg.DOLocalMove(oriPos + Vector3.up * 15, 0.1f);
  180. break;
  181. case ObsCtrlType.down:
  182. centerImg.DOLocalMove(oriPos + Vector3.down * 15, 0.1f);
  183. break;
  184. case ObsCtrlType.upleft:
  185. centerImg.DOLocalMove(oriPos + Vector3.up * 15+Vector3.left*15, 0.1f);
  186. break;
  187. case ObsCtrlType.upright:
  188. centerImg.DOLocalMove(oriPos + Vector3.up * 15+Vector3.right*15, 0.1f);
  189. break;
  190. case ObsCtrlType.downleft:
  191. centerImg.DOLocalMove(oriPos + Vector3.down * 15+Vector3.left*15, 0.1f);
  192. break;
  193. case ObsCtrlType.downright:
  194. centerImg.DOLocalMove(oriPos + Vector3.down * 15+Vector3.right*15, 0.1f);
  195. break;
  196. }
  197. }
  198. }
  199. private void OnDirButtonUp(ObsCtrlType type)
  200. {
  201. if (currentChannelId != null &&currentDonwType != ObsCtrlType.none&& !currentChannelId.Equals(""))
  202. {
  203. if (currentDonwType == type)
  204. {
  205. currentDonwType = ObsCtrlType.none;
  206. HttpHelper._Instance.SendObsCameraCtrlCmd(currentDeviceId,currentChannelId,ObsCtrlType.stop);
  207. centerImg.DOLocalMove(oriPos, 0.1f);
  208. }
  209. }
  210. }
  211. private void OnDirButtonExit(ObsCtrlType type)
  212. {
  213. if (currentChannelId != null&&currentDonwType != ObsCtrlType.none&& !currentChannelId.Equals(""))
  214. {
  215. currentDonwType = ObsCtrlType.none;
  216. HttpHelper._Instance.SendObsCameraCtrlCmd(currentDeviceId,currentChannelId,ObsCtrlType.stop);
  217. centerImg.DOLocalMove(oriPos, 0.1f);
  218. }
  219. }
  220. public void SetTitle(string str)
  221. {
  222. obsName = str;
  223. titleText.text = str;
  224. }
  225. public void Capture()
  226. {
  227. Texture2D saveTex = new Texture2D(704,576);
  228. obsPlayer.ExtractFrame(saveTex);
  229. StartCoroutine(UpLoadFile(saveTex.EncodeToJPG(), $"{obsName}{GetCurrentUnixTimestampMillis()}.jpg"));
  230. }
  231. public string ConvertToUnicode(string input)
  232. {
  233. StringBuilder sb = new StringBuilder();
  234. for (int i = 0; i < input.Length; i++)
  235. {
  236. char c = input[i];
  237. if (c < 128) // 非汉字字符
  238. {
  239. sb.Append(c);
  240. }
  241. else // 汉字字符
  242. {
  243. sb.Append("\\u" + ((int)c).ToString("x4"));
  244. }
  245. }
  246. return sb.ToString();
  247. }
  248. long GetCurrentUnixTimestampMillis()
  249. {
  250. // Unix时间戳是从1970年1月1日开始计算
  251. DateTime unixStartTime = new DateTime(1970, 1, 1);
  252. // 当前时间
  253. DateTime now = DateTime.UtcNow;
  254. // 当前时间与Unix时间戳的时间间隔
  255. TimeSpan timeSpan = now - unixStartTime;
  256. // 将时间间隔转换为毫秒数
  257. long timestamp = (long)timeSpan.TotalMilliseconds;
  258. return timestamp;
  259. }
  260. IEnumerator UpLoadFile(byte[] uploadData,string fileName)
  261. {
  262. fileName = fileName.Replace(" ", "");
  263. //fileName = ConvertToUnicode(fileName);
  264. Debug.Log(fileName);
  265. WWWForm form = new WWWForm();
  266. form.AddBinaryData("file",uploadData,fileName);
  267. UnityWebRequest www = UnityWebRequest.Post(ServerAddress.API_TextureUploadPath, form);
  268. yield return www.SendWebRequest();
  269. Debug.Log(www.downloadHandler.text);
  270. www.Dispose();
  271. }
  272. }