ObsPlayerPanel.cs 12 KB

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