AgoraRTCManager.cs 25 KB

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