CustomRTC.cs 9.6 KB

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