ObsPlayerPanel.cs 12 KB

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