CustomRTC.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class CustomRTC : SCRtcManager
  7. {
  8. public static CustomRTC Instance;
  9. public TextMesh text;
  10. public GameObject myQuad;
  11. public RenderTexture myRenderTexture;
  12. //public GameObject peerQuad;
  13. public Camera cam;
  14. public LoginManager loginManager;
  15. public CustomPeerList cpl;
  16. public CustomMe me;
  17. public ItemList list;
  18. public MeshRenderer bigMr;
  19. public string bigNameID = "";
  20. public Texture2D t2;
  21. public Texture2D t2fang;
  22. private void Start()
  23. {
  24. Instance = this;
  25. //initRtc();
  26. }
  27. private void Update()
  28. {
  29. SCRtcFactory.Instance.Update();
  30. }
  31. //加入Rtc
  32. public void initRtc()
  33. {
  34. InitListener();
  35. cpl = new CustomPeerList();
  36. list.initList(cpl);
  37. me = new CustomMe();
  38. me.myQuad = myQuad;
  39. // changeVideo3();
  40. //
  41. }
  42. public void showNextTexture()
  43. {
  44. bool isChoose = false;
  45. CustomPeer hasBig = null;
  46. foreach (CustomPeer value in cpl.getPeers().Values)
  47. {
  48. Debug.Log("Word");
  49. if(value.peerId!=bigNameID&&value.cIdV!=""&&!value.isBig)
  50. {
  51. bigMr.material.mainTexture = value.tex;
  52. bigNameID = value.peerId;
  53. isChoose = true;
  54. }
  55. if (value.peerId != bigNameID && value.cIdV != "" )
  56. {
  57. hasBig = value;
  58. }
  59. }
  60. if(!isChoose)
  61. {
  62. Debug.Log("LLL");
  63. bigMr.material.mainTexture = t2fang;
  64. bigNameID = "";
  65. }
  66. if(list.configs.Count<=0&& hasBig!=null)
  67. {
  68. hasBig.bigView.GetComponent<BigVideoManager>().close();
  69. }
  70. }
  71. public void joinRoom()
  72. {
  73. Debug.Log("joinRoom" + SCRtcFactory.Instance.mSCRtcConfig.routerRtpCapabilities);
  74. SCRtcFactory.Instance.mSCRtcHandle.joinRoom(SCRtcFactory.Instance.mSCRtcConfig.routerRtpCapabilities);
  75. }
  76. public void changeName(string name)
  77. {
  78. SCRtcFactory.Instance.mSCRtcConfig.disPlayName = name;
  79. SCRtcFactory.Instance.mSCRtcHandle.changeName(SCRtcFactory.Instance.mSCRtcConfig.disPlayName);
  80. }
  81. //改变自己的Video
  82. public void ChangeMyVideo(int width, int hight, int FPS)
  83. {
  84. SCRtcFactory.Instance.mSCRtcHandle.changeVideo(width, hight, FPS);
  85. }
  86. public void changeMySize(int w, int h, int Fps = 30)
  87. {
  88. CustomInfo.mWidth = w;
  89. CustomInfo.mHight = h;
  90. // openCamera.Instance.restart(Fps);
  91. if (myRenderTexture != null)
  92. {
  93. myRenderTexture.Release();
  94. }
  95. myRenderTexture = new RenderTexture(w, h, 1);
  96. cam.targetTexture = myRenderTexture;
  97. // SCRtcFactory.Instance.mSCRtcMe.changeQuad(w, h);
  98. SCRtcFactory.Instance.mSCRtcMe.setMyCamera(myRenderTexture);
  99. }
  100. public void changeFps1()
  101. {
  102. SCRtcFactory.Instance.mSCRtcConfig.FPS = 30;
  103. openCamera.Instance.restart(SCRtcFactory.Instance.mSCRtcConfig.FPS);
  104. Debug.Log("FPS"+30);
  105. SCRtcFactory.Instance.mSCRtcHandle.changeVideo(CustomInfo.mWidth, CustomInfo.mHight, 30);
  106. }
  107. public void changeFps2()
  108. {
  109. SCRtcFactory.Instance.mSCRtcConfig.FPS = 15;
  110. openCamera.Instance.restart(SCRtcFactory.Instance.mSCRtcConfig.FPS);
  111. Debug.Log("FPS"+15);
  112. SCRtcFactory.Instance.mSCRtcHandle.changeVideo(CustomInfo.mWidth, CustomInfo.mHight, 15);
  113. }
  114. public void changeFps3()
  115. {
  116. SCRtcFactory.Instance.mSCRtcConfig.FPS = 1;
  117. openCamera.Instance.restart(SCRtcFactory.Instance.mSCRtcConfig.FPS);
  118. Debug.Log("FPS"+1);
  119. SCRtcFactory.Instance.mSCRtcHandle.changeVideo(CustomInfo.mWidth, CustomInfo.mHight, 1);
  120. }
  121. public void changeVideo1()
  122. {
  123. changeMySize(1920, 1080);
  124. SCRtcFactory.Instance.mSCRtcHandle.changeVideo(CustomInfo.mWidth, CustomInfo.mHight, 30);
  125. Debug.Log("超清");
  126. }
  127. public void changeVideo2()
  128. {
  129. changeMySize(1280, 720);
  130. SCRtcFactory.Instance.mSCRtcHandle.changeVideo(CustomInfo.mWidth, CustomInfo.mHight, 30);
  131. Debug.Log("高清");
  132. }
  133. public void changeVideo3()
  134. {
  135. changeMySize(640, 480);
  136. SCRtcFactory.Instance.mSCRtcHandle.changeVideo(CustomInfo.mWidth, CustomInfo.mHight, 30);
  137. Debug.Log("标清");
  138. }
  139. public void changeVideo4()
  140. {
  141. changeMySize(360, 240);
  142. SCRtcFactory.Instance.mSCRtcHandle.changeVideo(CustomInfo.mWidth, CustomInfo.mHight, 30);
  143. }
  144. //开启或关闭Video
  145. public void sendVideo(bool boo)
  146. {
  147. SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
  148. }
  149. //开启或关闭全部Video
  150. public void enableAudioOnly(bool boo)
  151. {
  152. SCRtcFactory.Instance.mSCRtcHandle.enableAudioOnly(boo);
  153. }
  154. //开启或关闭全部Audio
  155. public void enableVideoOnly(bool boo)
  156. {
  157. SCRtcFactory.Instance.mSCRtcHandle.enableVideoOnly(boo);
  158. }
  159. //开启或关闭Audio
  160. public void sendMic(bool boo)
  161. {
  162. SCRtcFactory.Instance.mSCRtcHandle.sendMic(boo);
  163. }
  164. //开启扬声器
  165. public void openSpeaker()
  166. {
  167. SCRtcFactory.Instance.mSCRtcHandle.openSpeaker();
  168. }
  169. //关闭扬声器
  170. public void closeSpeaker()
  171. {
  172. SCRtcFactory.Instance.mSCRtcHandle.closeSpeaker();
  173. }
  174. public TextMesh volume;
  175. double volumeD;
  176. public void setAddVolume(int f)
  177. {
  178. volumeD += 0.1f;
  179. volume.text = volumeD.ToString();
  180. Debug.Log("setVolume" + volumeD.ToString());
  181. SCRtcFactory.Instance.mSCRtcHandle.audioMeChange(volumeD);
  182. }
  183. public void setVolume(int f)
  184. {
  185. volumeD -= 0.1f;
  186. volume.text = volumeD.ToString();
  187. Debug.Log("setVolume" + volumeD.ToString());
  188. SCRtcFactory.Instance.mSCRtcHandle.audioMeChange(volumeD);
  189. }
  190. //关闭RTC
  191. public void Close()
  192. {
  193. Debug.Log("退出房间CloseStart");
  194. SCRtcFactory.Instance.Close();
  195. if (SCRtcFactory.Instance.mSCRtcPeers != null)
  196. {
  197. foreach (CustomPeer value in SCRtcFactory.Instance.mSCRtcPeers.getPeers().Values)
  198. {
  199. if(value.isBig)
  200. Destroy(value.bigView);
  201. }
  202. SCRtcFactory.Instance.mSCRtcPeers.initPeers();
  203. }
  204. ViewManager.Instance.ShowView(ViewManager.LOGINROOM_VIEW);
  205. loginManager.ClearRoomInput();
  206. loginManager.ClearPassInput();
  207. bigNameID = "";
  208. }
  209. public override void OnRtcListener(JsonData data)
  210. {
  211. base.OnRtcListener(data);
  212. if (text != null)
  213. text.text = text.text.Insert(0, "\n" + data.ToJson() + "Start");
  214. //base.OnRtcListener(data);//这个方法让父类的方法又执行了一次
  215. if (data != null)
  216. {
  217. switch (data["action"].ToString())
  218. {
  219. case "OnSDKInited":
  220. OnSDKInited(data);
  221. break;
  222. case "OnListener":
  223. OnListener(data);
  224. break;
  225. case "onDisconnected":
  226. ViewManager.Instance.ShowTuSi("ServerException");
  227. onDisconnected(data);
  228. break;
  229. case "onNotification":
  230. onNotification(data);
  231. break;
  232. case "onRequest":
  233. onRequest(data);
  234. break;
  235. case "onFail":
  236. onFail(data);
  237. break;
  238. }
  239. }
  240. if (text != null)
  241. text.text = text.text.Insert(0, "\nEnd");
  242. }
  243. public void OnSDKInited(JsonData data)
  244. {
  245. }
  246. public void OnListener(JsonData data)
  247. {
  248. string pid;
  249. SCRtcPeer p;
  250. switch (data["data"]["action"].ToString())
  251. {
  252. case "addPeer":
  253. break;
  254. case "addVideo":
  255. break;
  256. case "changeResolution":
  257. break;
  258. }
  259. }
  260. public void onDisconnected(JsonData data)
  261. {
  262. }
  263. public void onNotification(JsonData data)
  264. {
  265. switch (data["method"].ToString())
  266. {
  267. case "consumerClosed":
  268. Debug.Log("PPT1");
  269. CustomPeer cp = (CustomPeer)cpl.getPeer(data["data"]["peerId"].ToString());
  270. //if (cp.cIdA == data["data"]["consumerId"].ToString())
  271. //{
  272. // return;
  273. //}
  274. cp.changeInfo(data["data"]["consumerId"].ToString(), cp);
  275. // list.Refresh();
  276. break;
  277. case "init":
  278. string nameS = PlayerPrefs.GetString("name");
  279. if (nameS != null && nameS != "")
  280. {
  281. changeName(nameS);
  282. }
  283. else
  284. {
  285. ViewManager.Instance.ShowView(ViewManager.SETNAME_VIEW);
  286. }
  287. break;
  288. case "closed":
  289. list.initList(cpl);
  290. if(bigNameID == data["data"]["peerId"].ToString())
  291. {
  292. Debug.Log("PPT");
  293. showNextTexture();
  294. //list.Refresh();
  295. }
  296. if(list.configs.Count<=0)
  297. {
  298. showNextTexture();
  299. }
  300. break;
  301. case "joined":
  302. break;
  303. case "joinRoom":
  304. loginManager.JoinSuccess();
  305. // ViewManager.Instance.ShowView(ViewManager.ROOM_VIEW);
  306. break;
  307. case "changeDisplayName":
  308. joinRoom();
  309. break;
  310. }
  311. }
  312. public void onRequest(JsonData data)
  313. {
  314. }
  315. public void onFail(JsonData data)
  316. {
  317. Debug.Log("data===========>" + data.ToJson());
  318. Debug.Log("cccc" + data["data"].ToString());
  319. loginManager.JoinFail(data);
  320. // Close();
  321. }
  322. }