AgoraRTCManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. using Agora.Rtc;
  2. using Agora.Util;
  3. using SC.XR.Unity;
  4. using ShadowStudio.Model;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.Rendering;
  9. using UnityEngine.Serialization;
  10. using UnityEngine.UI;
  11. using XRTool.Util;
  12. using static Reporter;
  13. public class AgoraRTCManager : SingletonMono<AgoraRTCManager>
  14. {
  15. [FormerlySerializedAs("appIdInput")]
  16. [SerializeField]
  17. private AppIdInput _appIdInput;
  18. [Header("_____________Basic Configuration_____________")]
  19. [FormerlySerializedAs("APP_ID")]
  20. [SerializeField]
  21. private string _appID = "";
  22. [FormerlySerializedAs("TOKEN")]
  23. [SerializeField]
  24. private string _token = "";
  25. [FormerlySerializedAs("CHANNEL_NAME")]
  26. [SerializeField]
  27. private string _channelName = "";
  28. private uint uid;
  29. private string peerid;
  30. internal IRtcEngine RtcEngine = null;
  31. private AgoraCustomPeer myPeer;
  32. private Dictionary<string, uint> dicPeeridAndUid;
  33. private List<AgoraCustomPeer> listCustomPeer;
  34. public bool isSwitchCamera;
  35. public uint Uid
  36. {
  37. get { return uid; }
  38. set { uid = value; }
  39. }
  40. public AgoraCustomPeer MyPeer
  41. {
  42. get { return myPeer; }
  43. set { if(value!=null) myPeer = value; }
  44. }
  45. public string ChannelName
  46. {
  47. get { return _channelName; }
  48. set { if (value != null) _channelName = value; }
  49. }
  50. public string PeerId
  51. {
  52. get { return peerid; }
  53. set { if (value != null) peerid = value; }
  54. }
  55. private void Start()
  56. {
  57. LoadAssetData();
  58. //if (_appID.Length > 10)
  59. //{
  60. //}
  61. InitEngine();
  62. dicPeeridAndUid = new Dictionary<string, uint>();
  63. isSwitchCamera = false;
  64. listCustomPeer = new List<AgoraCustomPeer>();
  65. }
  66. private void Update()
  67. {
  68. PermissionHelper.RequestMicrophontPermission();
  69. PermissionHelper.RequestCameraPermission();
  70. }
  71. //Show data in AgoraBasicProfile
  72. [ContextMenu("ShowAgoraBasicProfileData")]
  73. private void LoadAssetData()
  74. {
  75. if (_appIdInput == null) return;
  76. _appID = _appIdInput.appID;
  77. _token = _appIdInput.token;
  78. _channelName = _appIdInput.channelName;
  79. }
  80. private void InitEngine()
  81. {
  82. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  83. AgoraRTCManagerHandler handler = new AgoraRTCManagerHandler(this);
  84. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  85. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION,
  86. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  87. RtcEngine.Initialize(context);
  88. RtcEngine.InitEventHandler(handler);
  89. RtcEngine.SetDefaultAudioRouteToSpeakerphone(true);
  90. }
  91. public void JoinChannel()
  92. {
  93. Debug.Log(" JoinChannel " + _channelName);
  94. RtcEngine.EnableAudio();
  95. if(DeviceType.type!="LeiNiao")
  96. {
  97. RtcEngine.EnableVideo();
  98. }
  99. // RtcEngine.DisableAudio();
  100. // RtcEngine.DisableVideo();
  101. if (DeviceType.type != "LeiNiao")
  102. {
  103. VideoEncoderConfiguration config = new VideoEncoderConfiguration();
  104. config.dimensions = new VideoDimensions(1280, 720);
  105. config.frameRate = 15;
  106. config.bitrate = 0;
  107. // config.orientationMode = ORIENTATION_MODE.ORIENTATION_MODE_ADAPTIVE;
  108. RtcEngine.SetVideoEncoderConfiguration(config);
  109. }
  110. RtcEngine.SetChannelProfile(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION);
  111. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  112. // _channelName = roomid;
  113. if (DeviceType.type != "LeiNiao")
  114. {
  115. RtcEngine.SetExternalVideoSource(true, true, EXTERNAL_VIDEO_SOURCE_TYPE.VIDEO_FRAME, new SenderOptions());
  116. }
  117. RtcEngine.JoinChannel(_token, _channelName, "", uid);
  118. if (DeviceType.type != "LeiNiao")
  119. {
  120. if (!isSwitchCamera && (DeviceType.type != "Rhinox" || DeviceType.type != "Phone"))
  121. {
  122. RtcEngine.SwitchCamera();
  123. isSwitchCamera = !isSwitchCamera;
  124. }
  125. }
  126. StartCoroutine(InitVideoAndAduio(1f));
  127. if (!RTCINIT)
  128. {
  129. RTCINIT = true;
  130. if (DeviceType.type != "LeiNiao")
  131. {
  132. StartCoroutine(RenderTexturesScreenCapture());
  133. }
  134. }
  135. }
  136. static bool RTCINIT;
  137. Texture2D screenShot;
  138. IEnumerator RenderTexturesScreenCapture()
  139. {
  140. Debug.Log("RenderTexturesScreenCapture发送图片1");
  141. yield return new WaitForEndOfFrame();
  142. if (screenShot == null)
  143. {
  144. screenShot = new Texture2D(1280, 720, TextureFormat.RGBA32, false);
  145. StartCoroutine(GetRenederFPS());
  146. }
  147. while (true)
  148. {
  149. // if (isSendVideo)
  150. // continue;
  151. // img.texture = RemoteRtc.Instance.cam.activeTexture;
  152. // RenderTexture.active = ca.activeTexture;
  153. // screenShot.ReadPixels(new Rect(0, 0, ca.activeTexture.width, ca.activeTexture.height), 0, 0);
  154. // screenShot.Apply();
  155. //Camera.main.targetTexture = null;
  156. // RenderTexture.active = null;
  157. yield return new WaitForSeconds(0.05f);
  158. if (bts != null)
  159. {
  160. // screenShot = HorizontalFlipTexture(screenShot2);
  161. var timetick = System.DateTime.Now.Ticks / 10000;
  162. ExternalVideoFrame externalVideoFrame = new ExternalVideoFrame();
  163. externalVideoFrame.type = VIDEO_BUFFER_TYPE.VIDEO_BUFFER_RAW_DATA;
  164. externalVideoFrame.format = VIDEO_PIXEL_FORMAT.VIDEO_PIXEL_RGBA;
  165. externalVideoFrame.buffer = bts;// screenShot.GetRawTextureData();
  166. externalVideoFrame.stride = (int)screenShot.width;
  167. externalVideoFrame.height = (int)screenShot.height;
  168. externalVideoFrame.rotation = 180;
  169. externalVideoFrame.cropLeft = 1;
  170. externalVideoFrame.cropRight = 1;
  171. externalVideoFrame.timestamp = timetick;
  172. RtcEngine.PushVideoFrame(externalVideoFrame);
  173. // Debug.Log("发送中。。。。。");
  174. }
  175. }
  176. }
  177. public Camera cam2;
  178. byte[] bts;
  179. IEnumerator GetRenederFPS()
  180. {
  181. while (true)
  182. {
  183. var req = AsyncGPUReadback.Request(cam2.activeTexture);
  184. yield return new WaitUntil(() => req.done);
  185. if (!req.hasError)
  186. {
  187. if (bts == null)
  188. {
  189. bts = new byte[req.layerDataSize];
  190. }
  191. req.GetData<byte>().CopyTo(bts);
  192. // screenShot.LoadRawTextureData(bts);
  193. // screenShot.Apply();
  194. // tex.SetPixels32(req.GetData<Color32>().ToArray());
  195. // img.texture = screenShot;
  196. }
  197. else
  198. {
  199. Debug.LogError("Error AsyncGPUReadbackRequest.hasError");
  200. }
  201. }
  202. }
  203. public void LeaveChannel()
  204. {
  205. Debug.Log("LeaveChannel ");
  206. int msg = RtcEngine.LeaveChannel();
  207. switch (msg)
  208. {
  209. case 0:
  210. Debug.Log( "鎴愬姛閫€鍑洪�閬擄細 " + _channelName);
  211. break;
  212. default:
  213. Debug.Log("閫€鍑洪�閬撳け璐ワ細 " + msg);
  214. break;
  215. }
  216. }
  217. public void ClearDatas()
  218. {
  219. myPeer = null;
  220. dicPeeridAndUid.Clear();
  221. dicPeeridAndUid = new Dictionary<string, uint>();
  222. listCustomPeer.Clear();
  223. listCustomPeer = new List<AgoraCustomPeer>();
  224. }
  225. private IEnumerator InitVideoAndAduio(float times)
  226. {
  227. yield return new WaitForSeconds(times);
  228. MuteLocalVideoStream(CustomInfo.isSendVideo);
  229. MuteLocalAudioStream(CustomInfo.isSendAudio);
  230. MyPeer.isAudio = true;
  231. MyPeer.isVideo = true;
  232. }
  233. public void AddDicPeerData(string peerid, uint uid)
  234. {
  235. if (dicPeeridAndUid.ContainsKey(peerid))
  236. return;
  237. dicPeeridAndUid.Add(peerid, uid);
  238. AgoraCustomPeer peer = new AgoraCustomPeer();
  239. peer.AgoraInit(peerid);
  240. listCustomPeer.Add(peer);
  241. if (peerid == PeerId)
  242. MyPeer = peer;
  243. }
  244. public void RemDicPeerData(string peerid)
  245. {
  246. Debug.Log(" RemAtPeeridUid " + peerid + " " + uid);
  247. dicPeeridAndUid.Remove(peerid);
  248. for (int i = 0; i < listCustomPeer.Count; i++)
  249. {
  250. if (listCustomPeer[i].peerId == peerid)
  251. listCustomPeer.Remove(listCustomPeer[i]);
  252. }
  253. }
  254. public void SetCustomPeerName(string peerid, string name)
  255. {
  256. Debug.Log("SetListCustomPeer " + peerid + " " + name);
  257. for (int i = 0; i < listCustomPeer.Count; i++)
  258. {
  259. if (peerid == listCustomPeer[i].peerId)
  260. {
  261. listCustomPeer[i].SetName(name);
  262. break;
  263. }
  264. }
  265. }
  266. public AgoraCustomPeer GetCustomPeer(string peerid)
  267. {
  268. Debug.Log("GetListCustomPeer " + peerid);
  269. for (int i = 0; i < listCustomPeer.Count; i++)
  270. {
  271. if (listCustomPeer[i].peerId == peerid)
  272. return listCustomPeer[i];
  273. }
  274. return null;
  275. }
  276. public void AgoarRawImageVideoView(string peerId, RawImage rawImage)
  277. {
  278. Debug.Log(" AgoarRawImageVideoView " + peerId);
  279. try
  280. {
  281. if (peerId == CommonMethod.MyPeer.PeerId)
  282. {
  283. rawImage.gameObject.SetActive(true);
  284. rawImage.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
  285. MakeVideoView(0, rawImage);
  286. }
  287. else if (dicPeeridAndUid.ContainsKey(peerId))
  288. {
  289. // list_ShowView.Add(peerId, rawImage);
  290. rawImage.gameObject.SetActive(true);
  291. rawImage.rectTransform.localEulerAngles = new Vector3(0, 0, 180);
  292. MakeVideoView(dicPeeridAndUid[peerId], rawImage, this._channelName);
  293. }
  294. }
  295. catch (System.Exception e)
  296. {
  297. HttpsSendLog.Instance.SendLog("Agora", e.ToString());
  298. }
  299. }
  300. public void MuteLocalAudioStream(bool isAudio)
  301. {
  302. int msg = RtcEngine.MuteLocalAudioStream(!isAudio);
  303. // int msg = RtcEngine.EnableLocalAudio(isAudio);
  304. switch (msg)
  305. {
  306. case 0:
  307. Debug.Log(isAudio ? "鎵撳紑鏈�湴闊抽�鎴愬姛" : "鍏抽棴鏈�湴闊抽�鎴愬姛 ");
  308. MyPeer.isOpenAduio = !isAudio;
  309. break;
  310. default:
  311. Debug.LogError("寮€鍏虫湰鍦伴煶棰戝け璐ワ細 " + msg);
  312. break;
  313. }
  314. }
  315. public void MuteRemoteAudioStream(string peerid, bool isAudio)
  316. {
  317. Debug.Log(peerid);
  318. if (!dicPeeridAndUid.ContainsKey(peerid))
  319. return;
  320. int msg = RtcEngine.MuteRemoteAudioStream(dicPeeridAndUid[peerid], !isAudio);
  321. switch (msg)
  322. {
  323. case 0:
  324. Debug.Log(isAudio ? "璁㈤槄杩滅�闊抽�鎴愬姛" : "鍙栬�杩滅�闊抽�鎴愬姛 ");
  325. break;
  326. default:
  327. Debug.LogError("寮€鍏虫湰鍦伴煶棰戝け璐ワ細 " + msg);
  328. break;
  329. }
  330. }
  331. public void MuteLocalVideoStream(bool isVideo)
  332. {
  333. if(isVideo)
  334. {
  335. XRRGBCamera.Instance.playCamera(CustomInfo.mWidth, CustomInfo.mHight);
  336. }
  337. else
  338. {
  339. XRRGBCamera.Instance.stopCamera();
  340. }
  341. int msg = RtcEngine.MuteLocalVideoStream(!isVideo);
  342. //RtcEngine.EnableVideo();
  343. //int msg = RtcEngine.EnableLocalVideo(isVideo);
  344. switch (msg)
  345. {
  346. case 0:
  347. Debug.Log(isVideo ? "鎵撳紑鏈�湴瑙嗛�鎴愬姛 " : "鍏抽棴鏈�湴瑙嗛�鎴愬姛 ");
  348. // myPeer.isCloseVideo = !isVideo;
  349. MyPeer.isOpenVideo = !isVideo;
  350. break;
  351. default:
  352. Debug.LogError("寮€鍏虫湰鍦拌�棰戝け璐ワ細 " + msg);
  353. break;
  354. }
  355. }
  356. public void MuteRemoteVideoStream(string peerid, bool isVideo)
  357. {
  358. // Debug.Log(peerid);
  359. if (!dicPeeridAndUid.ContainsKey(peerid))
  360. return;
  361. int msg = RtcEngine.MuteRemoteVideoStream(dicPeeridAndUid[peerid], !isVideo);
  362. switch (msg)
  363. {
  364. case 0:
  365. Debug.Log(isVideo ? "璁㈤槄杩滅�瑙嗛�鎴愬姛" : "鍙栬�杩滅�瑙嗛�鎴愬姛 ");
  366. break;
  367. default:
  368. Debug.LogError("寮€鍏虫湰鍦伴煶棰戝け璐ワ細 " + msg);
  369. break;
  370. }
  371. }
  372. public void RemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
  373. reason)
  374. {
  375. Debug.Log(uid + " " + state + " " + reason);
  376. Debug.Log("RemoteVideoStateChanged " + uid);
  377. if (!dicPeeridAndUid.ContainsValue(uid))
  378. return;
  379. for (int i = 0; i < listCustomPeer.Count; i++)
  380. {
  381. if (dicPeeridAndUid[listCustomPeer[i].peerId] == uid)
  382. {
  383. switch (state)
  384. {
  385. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STOPPED:
  386. // listCustomPeer[i].isVideo = false;
  387. break;
  388. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STARTING:
  389. listCustomPeer[i].isVideo = true;
  390. break;
  391. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_DECODING:
  392. listCustomPeer[i].isOpenVideo = false;
  393. break;
  394. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FROZEN:
  395. break;
  396. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FAILED:
  397. // listCustomPeer[i].isVideo = false;
  398. break;
  399. default:
  400. break;
  401. }
  402. switch (reason)
  403. {
  404. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_INTERNAL:
  405. break;
  406. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION://缃戠粶闃诲�銆?
  407. // listCustomPeer[i].isVideo = false;
  408. break;
  409. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY:// 缃戠粶鎭㈠�姝e父銆?
  410. // listCustomPeer[i].isVideo = true;
  411. break;
  412. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED://鏈�湴鐢ㄦ埛鍋滄�鎺ユ敹杩滅�瑙嗛�娴佹垨鏈�湴鐢ㄦ埛绂佺敤瑙嗛�妯″潡
  413. listCustomPeer[i].isOpenVideo = true;
  414. break;
  415. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED://鏈�湴鐢ㄦ埛鎭㈠�鎺ユ敹杩滅�瑙嗛�娴佹垨鏈�湴鐢ㄦ埛鍚�姩瑙嗛�妯″潡
  416. listCustomPeer[i].isOpenVideo = false;
  417. break;
  418. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED://杩滅�鐢ㄦ埛鍋滄�鍙戦€佽�棰戞祦鎴栬繙绔�敤鎴风�鐢ㄨ�棰戞ā鍧椼€?
  419. listCustomPeer[i].isVideo = false;
  420. // CloseAgoraMainImage(listCustomPeer[i].peerId, false);
  421. break;
  422. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED://杩滅�鐢ㄦ埛鎭㈠�鍙戦€佽�棰戞祦鎴栬繙绔�敤鎴峰惎鐢ㄨ�棰戞ā鍧椼€?
  423. listCustomPeer[i].isVideo = true;
  424. // CloseAgoraMainImage(listCustomPeer[i].peerId, true);
  425. break;
  426. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE: //杩滅�鐢ㄦ埛绂诲紑棰戦亾銆?
  427. listCustomPeer[i].isVideo = false;
  428. break;
  429. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK:
  430. break;
  431. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK_RECOVERY:
  432. break;
  433. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_LOW:
  434. break;
  435. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_HIGH:
  436. break;
  437. default:
  438. break;
  439. }
  440. break;
  441. }
  442. }
  443. }
  444. public void RemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
  445. {
  446. Debug.Log(uid + " " + state + " " + reason);
  447. // Debug.Log("RemoteAudioStateChanged " + uid);
  448. if (!dicPeeridAndUid.ContainsValue(uid))
  449. return;
  450. // Debug.Log("RemoteAudioStateChanged " + 1);
  451. for (int i = 0; i < listCustomPeer.Count; i++)
  452. {
  453. Debug.Log(listCustomPeer[i].peerId);
  454. if (dicPeeridAndUid[listCustomPeer[i].peerId] == uid)
  455. {
  456. // Debug.Log("RemoteAudioStateChanged " + 2);
  457. switch (state)
  458. {
  459. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STOPPED:
  460. // listCustomPeer[i].isAudio = false;
  461. break;
  462. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STARTING:
  463. listCustomPeer[i].isAudio = true;
  464. break;
  465. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_DECODING:
  466. listCustomPeer[i].isOpenAduio = false;
  467. break;
  468. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_FROZEN:
  469. break;
  470. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_FAILED:
  471. // listCustomPeer[i].isAudio = false;
  472. break;
  473. default:
  474. break;
  475. }
  476. switch (reason)
  477. {
  478. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_INTERNAL:
  479. break;
  480. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_CONGESTION:
  481. // listCustomPeer[i].isAudio = false;
  482. break;
  483. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_RECOVERY:
  484. // listCustomPeer[i].isAudio = true;
  485. break;
  486. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_MUTED:
  487. listCustomPeer[i].isOpenAduio = true;
  488. break;
  489. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_UNMUTED:
  490. listCustomPeer[i].isOpenAduio = false;
  491. break;
  492. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_MUTED:
  493. listCustomPeer[i].isAudio = false;
  494. break;
  495. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_UNMUTED:
  496. listCustomPeer[i].isAudio = true;
  497. break;
  498. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_OFFLINE:
  499. listCustomPeer[i].isAudio = false;
  500. break;
  501. default:
  502. break;
  503. }
  504. break;
  505. }
  506. }
  507. }
  508. private void OnDestroy()
  509. {
  510. Debug.Log("OnDestroy");
  511. if (RtcEngine == null) return;
  512. RtcEngine.InitEventHandler(null);
  513. RtcEngine.LeaveChannel();
  514. RtcEngine.Dispose();
  515. }
  516. internal static void MakeVideoView(uint uid, RawImage rawImage, string channelId = "")
  517. {
  518. Debug.Log("MakeVideoView " + uid);
  519. //var go = GameObject.Find(uid.ToString());
  520. //if (!ReferenceEquals(go, null))
  521. //{
  522. // return; // reuse
  523. //}
  524. // create a GameObject and assign to this new user
  525. // var videoSurface = MakePlaneSurface(uid.ToString());
  526. // configure videoSurface
  527. if (uid == 0)
  528. {
  529. rawImage.transform.localEulerAngles = Vector3.zero;
  530. //videoSurface.SetForUser(uid, channelId);
  531. rawImage.texture = AgoraRTCManager.Instance.cam2.activeTexture;
  532. }
  533. else
  534. {
  535. rawImage.transform.localEulerAngles = new Vector3(-180,180,0);
  536. var videoSurface = MakeImageSurface(rawImage);
  537. if (ReferenceEquals(videoSurface, null)) return;
  538. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  539. videoSurface.SetEnable(true);
  540. }
  541. //videoSurface.OnTextureSizeModify += (int width, int height) =>
  542. //{
  543. // float scale = (float)height / (float)width;
  544. // videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  545. // Debug.Log("OnTextureSizeModify: " + width + " " + height);
  546. //};
  547. }
  548. internal static void OnUserJoined(uint uid)
  549. {
  550. // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  551. // Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed))
  552. Agora.Rtc.UserInfo userInfo = new Agora.Rtc.UserInfo();
  553. AgoraRTCManager.Instance.RtcEngine.GetUserInfoByUid(uid, ref userInfo);
  554. }
  555. internal static void OnRemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
  556. reason)
  557. {
  558. AgoraRTCManager.Instance.RemoteVideoStateChanged(uid, state, reason);
  559. }
  560. internal static void OnRemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
  561. {
  562. AgoraRTCManager.Instance.RemoteAudioStateChanged(uid, state, reason);
  563. }
  564. #region -- Video Render UI Logic ---
  565. // VIDEO TYPE 1: 3D Object
  566. private static VideoSurface MakePlaneSurface(string goName)
  567. {
  568. var go = GameObject.CreatePrimitive(PrimitiveType.Quad);
  569. //for (int i = 0; i < list_UserInfo.Count; i++)
  570. //{
  571. // if (list_UserInfo[i].uid.ToString() == goName)
  572. // {
  573. // string userAccount = list_UserInfo[i].userAccount;
  574. // if (AgoraVideoAudioManager.Instance.list_ShowView.ContainsKey(list_UserInfo[i].userAccount))
  575. // {
  576. // go = AgoraVideoAudioManager.Instance.list_ShowView[list_UserInfo[i].userAccount].gameObject;
  577. // }
  578. // else
  579. // Debug.LogError(" Agora ShowView is NULL ");
  580. // }
  581. //}
  582. if (go == null)
  583. {
  584. go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  585. return null;
  586. }
  587. go.name = goName;
  588. // set up transform
  589. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  590. var yPos = Random.Range(3.0f, 5.0f);
  591. var xPos = Random.Range(-2.0f, 2.0f);
  592. go.transform.position = Vector3.zero;
  593. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  594. // configure videoSurface
  595. var videoSurface = go.AddComponent<VideoSurface>();
  596. return videoSurface;
  597. }
  598. // Video TYPE 2: RawImage
  599. private static VideoSurface MakeImageSurface(RawImage rawImage)
  600. {
  601. rawImage.gameObject.AddComponent<UIElementDrag>();
  602. var videoSurface = rawImage.gameObject.AddComponent<VideoSurface>();
  603. return videoSurface;
  604. }
  605. #endregion
  606. }
  607. #region -- Agora Event ---
  608. public class AgoraRTCManagerHandler : IRtcEngineEventHandler
  609. {
  610. private readonly AgoraRTCManager _videoSample;
  611. internal AgoraRTCManagerHandler(AgoraRTCManager videoSample)
  612. {
  613. _videoSample = videoSample;
  614. }
  615. public override void OnError(int err, string msg)
  616. {
  617. // _videoSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  618. }
  619. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  620. {
  621. int build = 0;
  622. Debug.Log("Agora: OnJoinChannelSuccess ");
  623. _videoSample.RtcEngine.GetVersion(ref build);
  624. }
  625. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  626. {
  627. Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  628. // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  629. AgoraRTCManager.OnUserJoined(uid);
  630. // AgoraVideoAudioManager.MakeVideoView(uid, _videoSample.GetChannelName());
  631. }
  632. public override void OnRemoteVideoStateChanged(RtcConnection connection, uint remoteUid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON reason, int elapsed)
  633. {
  634. AgoraRTCManager.OnRemoteVideoStateChanged(remoteUid, state, reason);
  635. }
  636. public override void OnRemoteAudioStateChanged(RtcConnection connection, uint remoteUid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason, int elapsed)
  637. {
  638. AgoraRTCManager.OnRemoteAudioStateChanged(remoteUid, state, reason);
  639. }
  640. public override void OnLocalVideoStateChanged(RtcConnection connection, LOCAL_VIDEO_STREAM_STATE state, LOCAL_VIDEO_STREAM_ERROR errorCode)
  641. {
  642. }
  643. public override void OnLocalAudioStateChanged(RtcConnection connection, LOCAL_AUDIO_STREAM_STATE state, LOCAL_AUDIO_STREAM_ERROR error)
  644. {
  645. }
  646. }
  647. #endregion