RemoteClickAction.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. uint SessionCallerId;
  19. const int SCREENSHOT_SUPER_SIZE = 2;
  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. DrawControl drawControl_;
  49. private GameObject toolsCanvas;
  50. private GameObject loginCanvas;
  51. private AspectRatioFitter fit;
  52. public RawImage remoteCamImage;
  53. GameObject RemoteCameraView_;
  54. // Use this for initialization
  55. void Start ()
  56. {
  57. Client.instance.RemoteAssistCanvas = GameObject.Find("RemoteAssistCanvas");
  58. var lst = Client.instance.RemoteAssistCanvas.GetComponentsInChildren<RectTransform>(true);
  59. foreach (var l in lst)
  60. {
  61. switch (l.name)
  62. {
  63. case "background": fit = l.gameObject.GetComponent<AspectRatioFitter>();
  64. background_ = l.gameObject;
  65. break;
  66. case "RemoteCameraView": remoteCamImage = l.gameObject.GetComponent<RawImage>();
  67. remoteImageRt_ = l.gameObject.GetComponent<RectTransform>();
  68. RemoteCameraView_ = l.gameObject;
  69. break;
  70. case "ToolsCanvas": toolsCanvas = l.gameObject; break;
  71. case "RemoteCanvas": remoteCanvas_ = l.gameObject; break;
  72. case "LoginCanvas": loginCanvas = l.gameObject; break;
  73. case "CallCanvas": callCanvas_ = l.gameObject; break;
  74. case "AnswerButton": answerButton_ = l.gameObject.GetComponent<Button>(); break;
  75. case "DeclineButton": declineButton_ = l.gameObject.GetComponent<Button>(); break;
  76. case "CallerName": callerName_ = l.gameObject.GetComponent<Text>(); break;
  77. case "OnConnectCanvas": onConnectCanvas_ = l.gameObject; break;
  78. case "DrawButton": drawButton_ = l.gameObject.GetComponent<Button>(); break;
  79. case "HangupButton": hangupButton_ = l.gameObject.GetComponent<Button>(); break;
  80. case "DrawCanvas": drawCanvas_ = l.gameObject; break;
  81. case "DrawImage": drawImage_ = l.gameObject.GetComponent<RawImage>(); break;
  82. case "DrawTextPattern": drawTextPattern_ = l.gameObject; break;
  83. case "DrawControlCanvas": drawToolsCanvas_ = l.gameObject; break;
  84. case "SendImageButton": sendImageButton_ = l.gameObject.GetComponent<Button>(); break;
  85. case "CancelDrawButton": cancelDrawButton_ = l.gameObject.GetComponent<Button>(); break;
  86. }
  87. // Debug.Log("asdsa " + l.name);
  88. }
  89. answerButton_.onClick.AddListener(this.OnAnswer);
  90. declineButton_.onClick.AddListener(this.OnDecline);
  91. callCanvas_.SetActive(false);
  92. drawButton_.onClick.AddListener(this.OnDraw);
  93. hangupButton_.onClick.AddListener(this.OnHangup);
  94. onConnectCanvas_.SetActive(false);
  95. drawCanvas_.SetActive(false); //TODO
  96. drawControl_ = FindObjectOfType<DrawControl>();
  97. sendImageButton_.onClick.AddListener(this.OnSendImage);
  98. cancelDrawButton_.onClick.AddListener(this.OnCancelDraw);
  99. drawToolsCanvas_.SetActive(false); //TODO
  100. RemoteTextureTestFill();
  101. Client.instance.RemoteAssistCanvas.SetActive(false);
  102. }
  103. public uint GetCallerID()
  104. {
  105. return SessionCallerId;
  106. }
  107. private void RemoteTextureTestFill()
  108. {
  109. //RawImage image = GameObject.Find("RemoteCameraView").GetComponent<RawImage>();
  110. /*int WIDTH = (int)remoteCanvasRt_.sizeDelta.x;
  111. int HEIGHT = (int)remoteCanvasRt_.sizeDelta.y;
  112. Texture2D texture = new Texture2D(WIDTH, HEIGHT);
  113. for (int i = 0; i < HEIGHT; ++i)
  114. {
  115. texture.SetPixel(HEIGHT - i, i, Color.blue);
  116. }
  117. texture.Apply();
  118. image.texture = texture;*/
  119. remoteCamImage.texture = Texture2D.whiteTexture;
  120. //image.texture = Texture2D.whiteTexture;
  121. //image.texture = Texture2D.blackTexture;
  122. }
  123. private void Update()
  124. {
  125. switch (state_)
  126. {
  127. case STATE.CAPTURING_SCREENSHOT:
  128. CheckScreenshot();
  129. break;
  130. }
  131. }
  132. public void StartSession(uint callerID)
  133. {
  134. session_ = this;
  135. SessionCallerId = callerID;
  136. remoteCanvas_.SetActive(true);
  137. RemoteCameraView_.SetActive(true);
  138. background_.SetActive(true);
  139. drawCanvas_.SetActive(true);
  140. callCanvas_.SetActive(true);
  141. callerName_.text = "call from " + callerID + "...";
  142. }
  143. public void OnEndDraw(byte[] image)
  144. {
  145. //if (image != null)
  146. //{
  147. // videoCalled_.SendImage(image);
  148. //}
  149. //else
  150. //{
  151. // videoCalled_.Resume();
  152. //}
  153. }
  154. public void StopSession()
  155. {
  156. SessionCallerId = 0;
  157. callCanvas_.SetActive(false);
  158. onConnectCanvas_.SetActive(false);
  159. drawToolsCanvas_.SetActive(false);
  160. drawCanvas_.SetActive(false);
  161. RemoteTextureTestFill();
  162. session_ = null;
  163. }
  164. public void OnAnswer()
  165. {
  166. if (session_ != null)
  167. {
  168. Debug.Log("OnAnswer");
  169. callCanvas_.SetActive(false);
  170. onConnectCanvas_.SetActive(true);
  171. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.videoCallRequest);
  172. }
  173. }
  174. public void OnDraw()
  175. {
  176. drawControl_.Clear();
  177. onConnectCanvas_.SetActive(false);
  178. drawToolsCanvas_.SetActive(true);
  179. drawCanvas_.SetActive(true);
  180. state_ = STATE.DRAWING;
  181. }
  182. public void OnHangup()
  183. {
  184. onConnectCanvas_.SetActive(false);
  185. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
  186. if (session_ != null)
  187. {
  188. StopSession();
  189. }
  190. Client.instance.RemoteAssistCanvas.SetActive(false);
  191. }
  192. public void OnDecline()
  193. {
  194. Debug.Log("OnDecline " + SessionCallerId);
  195. callCanvas_.SetActive(false);
  196. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
  197. if (session_ != null)
  198. {
  199. StopSession();
  200. }
  201. Client.instance.RemoteAssistCanvas.SetActive(false);
  202. }
  203. private void OnSendImage()
  204. {
  205. drawToolsCanvas_.SetActive(false);
  206. //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
  207. state_ = STATE.CAPTURING_SCREENSHOT;
  208. }
  209. public void OnCancelDraw()
  210. {
  211. drawToolsCanvas_.SetActive(false);
  212. //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
  213. state_ = STATE.CAPTURING_SCREENSHOT;
  214. }
  215. private void CheckScreenshot()
  216. {
  217. if (state_ == STATE.CAPTURING_SCREENSHOT)
  218. {
  219. StartCoroutine(RecordFrame());
  220. state_ = STATE.SCREENSHOT_CAPTURING_COROUTINE;
  221. }
  222. }
  223. IEnumerator RecordFrame()
  224. {
  225. yield return new WaitForEndOfFrame();
  226. Texture2D tex = null;
  227. try
  228. {
  229. tex = ScreenCapture.CaptureScreenshotAsTexture(SCREENSHOT_SUPER_SIZE);
  230. }
  231. catch (Exception ex)
  232. {
  233. Debug.Log(ex.Message);
  234. }
  235. drawCanvas_.SetActive(false);
  236. onConnectCanvas_.SetActive(true);
  237. int width = (int)remoteImageRt_.rect.width * SCREENSHOT_SUPER_SIZE;
  238. int height = (int)remoteImageRt_.rect.height * SCREENSHOT_SUPER_SIZE;
  239. var srceenPos = remoteImageRt_.TransformPoint(new Vector2(0, 0));
  240. int x = (int)srceenPos.x * SCREENSHOT_SUPER_SIZE;
  241. int y = (int)srceenPos.y * SCREENSHOT_SUPER_SIZE;
  242. width = x + width > tex.width ? tex.width - x : width;
  243. height = y + height > tex.height ? tex.height - y : height;
  244. Color[] col = tex.GetPixels(x, y, width, height);
  245. Texture2D m2Texture = new Texture2D(width, height);
  246. m2Texture.SetPixels(col);
  247. m2Texture.Apply();
  248. byte[] compress = m2Texture.EncodeToJPG();
  249. File.WriteAllBytes("test.jpg", compress);
  250. if (session_ != null)
  251. {
  252. session_.OnEndDraw(compress);
  253. }
  254. state_ = STATE.SHOWING_REMOTE;
  255. }
  256. }