RemoteClickAction.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public interface IVideoCallSession
  9. {
  10. void OnAnswer();
  11. void OnDecline();
  12. void OnHangup();
  13. void OnDraw();
  14. void OnEndDraw(byte[] image);
  15. }
  16. public class RemoteClickAction : MonoBehaviour, IVideoCallSession
  17. {
  18. public uint SessionCallerId;
  19. const int SCREENSHOT_SUPER_SIZE = 1;
  20. private enum STATE
  21. {
  22. RELEASED,
  23. SHOWING_REMOTE,
  24. DRAWING,
  25. CAPTURING_SCREENSHOT,
  26. SCREENSHOT_CAPTURING_COROUTINE
  27. };
  28. private STATE state_ = STATE.RELEASED;
  29. //bool pointerDown_ = false;
  30. private IVideoCallSession session_;
  31. private RectTransform remoteImageRt_;
  32. private GameObject callCanvas_;
  33. private Button answerButton_;
  34. private Button declineButton_;
  35. public Text callerName_;
  36. private GameObject onConnectCanvas_;
  37. private Button drawButton_;
  38. private Button hangupButton_;
  39. private GameObject background_;
  40. private GameObject remoteCanvas_;
  41. private GameObject drawCanvas_;
  42. private RawImage drawImage_;
  43. private RectTransform curDrawTextRt_;
  44. private GameObject drawTextPattern_;
  45. private GameObject drawToolsCanvas_;
  46. private Button sendImageButton_;
  47. private Button cancelDrawButton_;
  48. private Button undoButton_;
  49. private Button redoButton_;
  50. DrawControl drawControl_;
  51. private GameObject toolsCanvas;
  52. private GameObject loginCanvas;
  53. private AspectRatioFitter fit;
  54. public RawImage remoteCamImage;
  55. GameObject RemoteCameraView_;
  56. // Use this for initialization
  57. void Start ()
  58. {
  59. Client.instance.RemoteAssistCanvas = GameObject.Find("RemoteAssistCanvas");
  60. var lst = Client.instance.RemoteAssistCanvas.GetComponentsInChildren<RectTransform>(true);
  61. drawControl_ = FindObjectOfType<DrawControl>();
  62. foreach (var l in lst)
  63. {
  64. switch (l.name)
  65. {
  66. case "background": fit = l.gameObject.GetComponent<AspectRatioFitter>();
  67. background_ = l.gameObject;
  68. break;
  69. case "RemoteCameraView": remoteCamImage = l.gameObject.GetComponent<RawImage>();
  70. remoteImageRt_ = l.gameObject.GetComponent<RectTransform>();
  71. RemoteCameraView_ = l.gameObject;
  72. break;
  73. case "ToolsCanvas": toolsCanvas = l.gameObject; break;
  74. case "RemoteCanvas": remoteCanvas_ = l.gameObject; break;
  75. case "LoginCanvas": loginCanvas = l.gameObject; break;
  76. case "CallCanvas": callCanvas_ = l.gameObject; break;
  77. case "AnswerButton": answerButton_ = l.gameObject.GetComponent<Button>(); break;
  78. case "DeclineButton": declineButton_ = l.gameObject.GetComponent<Button>(); break;
  79. case "CallerName": callerName_ = l.gameObject.GetComponent<Text>(); break;
  80. case "OnConnectCanvas": onConnectCanvas_ = l.gameObject; break;
  81. case "DrawButton": drawButton_ = l.gameObject.GetComponent<Button>(); break;
  82. case "HangupButton": hangupButton_ = l.gameObject.GetComponent<Button>(); break;
  83. case "UndoButton": undoButton_ = l.gameObject.GetComponent<Button>(); break;
  84. case "DrawCanvas": drawCanvas_ = l.gameObject;
  85. DrawControl.drawScript_ = drawCanvas_.GetComponent<DrawScript>();
  86. break;
  87. case "DrawImage": drawImage_ = l.gameObject.GetComponent<RawImage>(); break;
  88. case "DrawTextPattern": drawTextPattern_ = l.gameObject; break;
  89. case "DrawControlCanvas": drawToolsCanvas_ = l.gameObject; break;
  90. case "SendImageButton": sendImageButton_ = l.gameObject.GetComponent<Button>(); break;
  91. case "CancelDrawButton": cancelDrawButton_ = l.gameObject.GetComponent<Button>(); break;
  92. case "ArrowToggle":
  93. drawControl_.arrowToggle_ = l.gameObject.GetComponent<Toggle>(); break;
  94. case "TextToggle":
  95. drawControl_.textToggle_ = l.gameObject.GetComponent<Toggle>(); break;
  96. case "BrushToggle":
  97. drawControl_.brushToggle_ = l.gameObject.GetComponent<Toggle>(); break;
  98. case "TargetToggle":
  99. drawControl_.targetToggle_ = l.gameObject.GetComponent<Toggle>(); break;
  100. }
  101. // Debug.Log("asdsa " + l.name);
  102. }
  103. drawControl_.Init();
  104. undoButton_.onClick.AddListener(DrawControl.drawScript_.Undo);
  105. answerButton_.onClick.AddListener(this.OnAnswer);
  106. declineButton_.onClick.AddListener(this.OnDecline);
  107. drawButton_.onClick.AddListener(OnDraw);
  108. hangupButton_.onClick.AddListener(this.OnHangup);
  109. callCanvas_.SetActive(false);
  110. onConnectCanvas_.SetActive(false);
  111. drawCanvas_.SetActive(false); //TODO
  112. sendImageButton_.onClick.AddListener(this.OnSendImage);
  113. cancelDrawButton_.onClick.AddListener(this.OnCancelDraw);
  114. drawToolsCanvas_.SetActive(false); //TODO
  115. RemoteTextureTestFill();
  116. Client.instance.RemoteAssistCanvas.SetActive(false);
  117. }
  118. public uint GetCallerID()
  119. {
  120. return SessionCallerId;
  121. }
  122. private void RemoteTextureTestFill()
  123. {
  124. //RawImage image = GameObject.Find("RemoteCameraView").GetComponent<RawImage>();
  125. /*int WIDTH = (int)remoteCanvasRt_.sizeDelta.x;
  126. int HEIGHT = (int)remoteCanvasRt_.sizeDelta.y;
  127. Texture2D texture = new Texture2D(WIDTH, HEIGHT);
  128. for (int i = 0; i < HEIGHT; ++i)
  129. {
  130. texture.SetPixel(HEIGHT - i, i, Color.blue);
  131. }
  132. texture.Apply();
  133. image.texture = texture;*/
  134. remoteCamImage.texture = Texture2D.whiteTexture;
  135. //image.texture = Texture2D.whiteTexture;
  136. //image.texture = Texture2D.blackTexture;
  137. }
  138. private void Update()
  139. {
  140. switch (state_)
  141. {
  142. case STATE.CAPTURING_SCREENSHOT:
  143. CheckScreenshot();
  144. break;
  145. }
  146. }
  147. public void StartSession(uint callerID)
  148. {
  149. session_ = this;
  150. SessionCallerId = callerID;
  151. remoteCanvas_.SetActive(true);
  152. RemoteCameraView_.SetActive(true);
  153. background_.SetActive(true);
  154. drawCanvas_.SetActive(true);
  155. callCanvas_.SetActive(true);
  156. var user = User.Find(callerID);
  157. if (user != null)
  158. callerName_.text = "Звонок от [" + callerID + "] "+ user.name_user;
  159. else
  160. callerName_.text = "Звонок от сотрудника [ID: " + callerID + "]";
  161. }
  162. public void OnEndDraw(byte[] image)
  163. {
  164. Client.instance.ImageSend(image, SessionCallerId);
  165. //if (image != null)
  166. //{
  167. // videoCalled_.SendImage(image);
  168. //}
  169. //else
  170. //{
  171. // videoCalled_.Resume();
  172. //}
  173. }
  174. public void StopSession()
  175. {
  176. SessionCallerId = 0;
  177. callCanvas_.SetActive(false);
  178. onConnectCanvas_.SetActive(false);
  179. drawToolsCanvas_.SetActive(false);
  180. drawCanvas_.SetActive(false);
  181. RemoteTextureTestFill();
  182. session_ = null;
  183. }
  184. public void OnAnswer()
  185. {
  186. if (session_ != null)
  187. {
  188. callCanvas_.SetActive(false);
  189. onConnectCanvas_.SetActive(true);
  190. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.videoCallRequest);
  191. }
  192. }
  193. public void OnDraw()
  194. {
  195. drawControl_.Clear();
  196. onConnectCanvas_.SetActive(false);
  197. toolsCanvas.SetActive(true);
  198. drawToolsCanvas_.SetActive(true);
  199. drawCanvas_.SetActive(true);
  200. state_ = STATE.DRAWING;
  201. Client.ImageStreamControlCommandSend(SessionCallerId, ImageCMD.pause);
  202. }
  203. public void OnHangup()
  204. {
  205. onConnectCanvas_.SetActive(false);
  206. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
  207. if (session_ != null)
  208. {
  209. StopSession();
  210. }
  211. Client.instance.RemoteAssistCanvas.SetActive(false);
  212. }
  213. public void OnDecline()
  214. {
  215. Debug.Log("OnDecline " + SessionCallerId);
  216. callCanvas_.SetActive(false);
  217. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
  218. if (session_ != null)
  219. {
  220. StopSession();
  221. }
  222. Client.instance.RemoteAssistCanvas.SetActive(false);
  223. }
  224. private void OnSendImage()
  225. {
  226. drawToolsCanvas_.SetActive(false);
  227. //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
  228. state_ = STATE.CAPTURING_SCREENSHOT;
  229. }
  230. public void OnCancelDraw()
  231. {
  232. drawToolsCanvas_.SetActive(false);
  233. //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
  234. state_ = STATE.CAPTURING_SCREENSHOT;
  235. }
  236. private void CheckScreenshot()
  237. {
  238. if (state_ == STATE.CAPTURING_SCREENSHOT)
  239. {
  240. StartCoroutine(RecordFrame());
  241. state_ = STATE.SCREENSHOT_CAPTURING_COROUTINE;
  242. }
  243. }
  244. IEnumerator RecordFrame()
  245. {
  246. yield return new WaitForEndOfFrame();
  247. Texture2D tex = null;
  248. try
  249. {
  250. tex = ScreenCapture.CaptureScreenshotAsTexture(SCREENSHOT_SUPER_SIZE);
  251. }
  252. catch (Exception ex)
  253. {
  254. Debug.Log(ex.Message);
  255. }
  256. drawCanvas_.SetActive(false);
  257. onConnectCanvas_.SetActive(true);
  258. int width = (int)remoteImageRt_.rect.width * SCREENSHOT_SUPER_SIZE;
  259. int height = (int)remoteImageRt_.rect.height * SCREENSHOT_SUPER_SIZE;
  260. var srceenPos = remoteImageRt_.TransformPoint(new Vector2(0, 0));
  261. int x = (int)srceenPos.x * SCREENSHOT_SUPER_SIZE;
  262. int y = (int)srceenPos.y * SCREENSHOT_SUPER_SIZE;
  263. Debug.Log($"RecordFrame w {width} h {height} x {x} y {y}");
  264. width = x + width > tex.width ? tex.width - x : width;
  265. height = y + height > tex.height ? tex.height - y : height;
  266. Debug.Log($"RecordFrame2 w {width} h {height} x {x} y {y}");
  267. Color[] col = tex.GetPixels(x, y, width, height);
  268. Texture2D m2Texture = new Texture2D(width, height);
  269. m2Texture.SetPixels(col);
  270. m2Texture.Apply();
  271. byte[] compress = m2Texture.EncodeToJPG(90);
  272. //File.WriteAllBytes("test.jpg", compress);
  273. if (session_ != null)
  274. {
  275. session_.OnEndDraw(compress);
  276. }
  277. state_ = STATE.SHOWING_REMOTE;
  278. }
  279. }