RemoteClickAction.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. private 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. callerName_.text = "Звонок от [" + callerID + "] "+ user.name;
  158. }
  159. public void OnEndDraw(byte[] image)
  160. {
  161. Client.instance.ImageSend(image, SessionCallerId);
  162. //if (image != null)
  163. //{
  164. // videoCalled_.SendImage(image);
  165. //}
  166. //else
  167. //{
  168. // videoCalled_.Resume();
  169. //}
  170. }
  171. public void StopSession()
  172. {
  173. SessionCallerId = 0;
  174. callCanvas_.SetActive(false);
  175. onConnectCanvas_.SetActive(false);
  176. drawToolsCanvas_.SetActive(false);
  177. drawCanvas_.SetActive(false);
  178. RemoteTextureTestFill();
  179. session_ = null;
  180. }
  181. public void OnAnswer()
  182. {
  183. if (session_ != null)
  184. {
  185. callCanvas_.SetActive(false);
  186. onConnectCanvas_.SetActive(true);
  187. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.videoCallRequest);
  188. }
  189. }
  190. public void OnDraw()
  191. {
  192. drawControl_.Clear();
  193. onConnectCanvas_.SetActive(false);
  194. toolsCanvas.SetActive(true);
  195. drawToolsCanvas_.SetActive(true);
  196. drawCanvas_.SetActive(true);
  197. state_ = STATE.DRAWING;
  198. Client.instance.ImageStreamStopSend(SessionCallerId);
  199. }
  200. public void OnHangup()
  201. {
  202. onConnectCanvas_.SetActive(false);
  203. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
  204. if (session_ != null)
  205. {
  206. StopSession();
  207. }
  208. Client.instance.RemoteAssistCanvas.SetActive(false);
  209. }
  210. public void OnDecline()
  211. {
  212. Debug.Log("OnDecline " + SessionCallerId);
  213. callCanvas_.SetActive(false);
  214. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
  215. if (session_ != null)
  216. {
  217. StopSession();
  218. }
  219. Client.instance.RemoteAssistCanvas.SetActive(false);
  220. }
  221. private void OnSendImage()
  222. {
  223. drawToolsCanvas_.SetActive(false);
  224. //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
  225. state_ = STATE.CAPTURING_SCREENSHOT;
  226. }
  227. public void OnCancelDraw()
  228. {
  229. drawToolsCanvas_.SetActive(false);
  230. //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
  231. state_ = STATE.CAPTURING_SCREENSHOT;
  232. }
  233. private void CheckScreenshot()
  234. {
  235. if (state_ == STATE.CAPTURING_SCREENSHOT)
  236. {
  237. StartCoroutine(RecordFrame());
  238. state_ = STATE.SCREENSHOT_CAPTURING_COROUTINE;
  239. }
  240. }
  241. IEnumerator RecordFrame()
  242. {
  243. yield return new WaitForEndOfFrame();
  244. Texture2D tex = null;
  245. try
  246. {
  247. tex = ScreenCapture.CaptureScreenshotAsTexture(SCREENSHOT_SUPER_SIZE);
  248. }
  249. catch (Exception ex)
  250. {
  251. Debug.Log(ex.Message);
  252. }
  253. drawCanvas_.SetActive(false);
  254. onConnectCanvas_.SetActive(true);
  255. int width = (int)remoteImageRt_.rect.width * SCREENSHOT_SUPER_SIZE;
  256. int height = (int)remoteImageRt_.rect.height * SCREENSHOT_SUPER_SIZE;
  257. var srceenPos = remoteImageRt_.TransformPoint(new Vector2(0, 0));
  258. int x = (int)srceenPos.x * SCREENSHOT_SUPER_SIZE;
  259. int y = (int)srceenPos.y * SCREENSHOT_SUPER_SIZE;
  260. Debug.Log($"RecordFrame w {width} h {height} x {x} y {y}");
  261. width = x + width > tex.width ? tex.width - x : width;
  262. height = y + height > tex.height ? tex.height - y : height;
  263. Debug.Log($"RecordFrame2 w {width} h {height} x {x} y {y}");
  264. Color[] col = tex.GetPixels(x, y, width, height);
  265. Texture2D m2Texture = new Texture2D(width, height);
  266. m2Texture.SetPixels(col);
  267. m2Texture.Apply();
  268. byte[] compress = m2Texture.EncodeToJPG(90);
  269. File.WriteAllBytes("test.jpg", compress);
  270. if (session_ != null)
  271. {
  272. session_.OnEndDraw(compress);
  273. }
  274. state_ = STATE.SHOWING_REMOTE;
  275. }
  276. }