AgoraRTCManager.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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")
  110. {
  111. RtcEngine.SwitchCamera();
  112. isSwitchCamera = !isSwitchCamera;
  113. }
  114. StartCoroutine(InitVideoAndAduio(1f));
  115. StartCoroutine(RenderTexturesScreenCapture());
  116. }
  117. Texture2D screenShot;
  118. IEnumerator RenderTexturesScreenCapture()
  119. {
  120. Debug.Log("RenderTexturesScreenCapture发送图片1");
  121. yield return new WaitForEndOfFrame();
  122. if (screenShot == null)
  123. {
  124. screenShot = new Texture2D(1280, 720, TextureFormat.RGBA32, false);
  125. StartCoroutine(GetRenederFPS());
  126. }
  127. while (true)
  128. {
  129. // if (isSendVideo)
  130. // continue;
  131. // img.texture = RemoteRtc.Instance.cam.activeTexture;
  132. // RenderTexture.active = ca.activeTexture;
  133. // screenShot.ReadPixels(new Rect(0, 0, ca.activeTexture.width, ca.activeTexture.height), 0, 0);
  134. // screenShot.Apply();
  135. //Camera.main.targetTexture = null;
  136. // RenderTexture.active = null;
  137. yield return new WaitForSeconds(0.05f);
  138. if (bts != null)
  139. {
  140. // screenShot = HorizontalFlipTexture(screenShot2);
  141. var timetick = System.DateTime.Now.Ticks / 10000;
  142. ExternalVideoFrame externalVideoFrame = new ExternalVideoFrame();
  143. externalVideoFrame.type = VIDEO_BUFFER_TYPE.VIDEO_BUFFER_RAW_DATA;
  144. externalVideoFrame.format = VIDEO_PIXEL_FORMAT.VIDEO_PIXEL_RGBA;
  145. externalVideoFrame.buffer = bts;// screenShot.GetRawTextureData();
  146. externalVideoFrame.stride = (int)screenShot.width;
  147. externalVideoFrame.height = (int)screenShot.height;
  148. externalVideoFrame.rotation = 180;
  149. externalVideoFrame.cropLeft = 1;
  150. externalVideoFrame.cropRight = 1;
  151. externalVideoFrame.timestamp = timetick;
  152. RtcEngine.PushVideoFrame(externalVideoFrame);
  153. Debug.Log("发送中。。。。。");
  154. }
  155. }
  156. }
  157. public Camera cam2;
  158. byte[] bts;
  159. IEnumerator GetRenederFPS()
  160. {
  161. while (true)
  162. {
  163. var req = AsyncGPUReadback.Request(cam2.activeTexture);
  164. yield return new WaitUntil(() => req.done);
  165. if (!req.hasError)
  166. {
  167. if (bts == null)
  168. {
  169. bts = new byte[req.layerDataSize];
  170. }
  171. req.GetData<byte>().CopyTo(bts);
  172. // screenShot.LoadRawTextureData(bts);
  173. // screenShot.Apply();
  174. // tex.SetPixels32(req.GetData<Color32>().ToArray());
  175. // img.texture = screenShot;
  176. }
  177. else
  178. {
  179. Debug.LogError("Error AsyncGPUReadbackRequest.hasError");
  180. }
  181. }
  182. }
  183. public void LeaveChannel()
  184. {
  185. Debug.Log("LeaveChannel ");
  186. int msg = RtcEngine.LeaveChannel();
  187. switch (msg)
  188. {
  189. case 0:
  190. Debug.Log( "成功退出频道: " + _channelName);
  191. break;
  192. default:
  193. Debug.Log("退出频道失败: " + msg);
  194. break;
  195. }
  196. }
  197. public void ClearDatas()
  198. {
  199. myPeer = null;
  200. dicPeeridAndUid.Clear();
  201. dicPeeridAndUid = new Dictionary<string, uint>();
  202. listCustomPeer.Clear();
  203. listCustomPeer = new List<AgoraCustomPeer>();
  204. }
  205. private IEnumerator InitVideoAndAduio(float times)
  206. {
  207. yield return new WaitForSeconds(times);
  208. MuteLocalVideoStream(CustomInfo.isSendVideo);
  209. MuteLocalAudioStream(CustomInfo.isSendAudio);
  210. MyPeer.isAudio = true;
  211. MyPeer.isVideo = true;
  212. }
  213. public void AddDicPeerData(string peerid, uint uid)
  214. {
  215. if (dicPeeridAndUid.ContainsKey(peerid))
  216. return;
  217. dicPeeridAndUid.Add(peerid, uid);
  218. AgoraCustomPeer peer = new AgoraCustomPeer();
  219. peer.AgoraInit(peerid);
  220. listCustomPeer.Add(peer);
  221. if (peerid == PeerId)
  222. MyPeer = peer;
  223. }
  224. public void RemDicPeerData(string peerid)
  225. {
  226. Debug.Log(" RemAtPeeridUid " + peerid + " " + uid);
  227. dicPeeridAndUid.Remove(peerid);
  228. for (int i = 0; i < listCustomPeer.Count; i++)
  229. {
  230. if (listCustomPeer[i].peerId == peerid)
  231. listCustomPeer.Remove(listCustomPeer[i]);
  232. }
  233. }
  234. public void SetCustomPeerName(string peerid, string name)
  235. {
  236. Debug.Log("SetListCustomPeer " + peerid + " " + name);
  237. for (int i = 0; i < listCustomPeer.Count; i++)
  238. {
  239. if (peerid == listCustomPeer[i].peerId)
  240. {
  241. listCustomPeer[i].SetName(name);
  242. break;
  243. }
  244. }
  245. }
  246. public AgoraCustomPeer GetCustomPeer(string peerid)
  247. {
  248. Debug.Log("GetListCustomPeer " + peerid);
  249. for (int i = 0; i < listCustomPeer.Count; i++)
  250. {
  251. if (listCustomPeer[i].peerId == peerid)
  252. return listCustomPeer[i];
  253. }
  254. return null;
  255. }
  256. public void AgoarRawImageVideoView(string peerId, RawImage rawImage)
  257. {
  258. try
  259. {
  260. if (peerId == CommonMethod.MyPeer.PeerId)
  261. {
  262. rawImage.gameObject.SetActive(true);
  263. rawImage.rectTransform.localEulerAngles += new Vector3(0, 180, 180);
  264. MakeVideoView(0, rawImage);
  265. }
  266. else if (dicPeeridAndUid.ContainsKey(peerId))
  267. {
  268. Debug.Log(" AddListShowView " + peerId);
  269. // list_ShowView.Add(peerId, rawImage);
  270. rawImage.gameObject.SetActive(true);
  271. rawImage.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
  272. MakeVideoView(dicPeeridAndUid[peerId], rawImage, this._channelName);
  273. }
  274. }
  275. catch (System.Exception e)
  276. {
  277. HttpsSendLog.Instance.SendLog("Agora", e.ToString());
  278. }
  279. }
  280. public void MuteLocalAudioStream(bool isAudio)
  281. {
  282. int msg = RtcEngine.MuteLocalAudioStream(!isAudio);
  283. // int msg = RtcEngine.EnableLocalAudio(isAudio);
  284. switch (msg)
  285. {
  286. case 0:
  287. Debug.Log(isAudio ? "打开本地音频成功" : "关闭本地音频成功 ");
  288. MyPeer.isOpenAduio = !isAudio;
  289. break;
  290. default:
  291. Debug.LogError("开关本地音频失败: " + msg);
  292. break;
  293. }
  294. }
  295. public void MuteRemoteAudioStream(string peerid, bool isAudio)
  296. {
  297. Debug.Log(peerid);
  298. if (!dicPeeridAndUid.ContainsKey(peerid))
  299. return;
  300. int msg = RtcEngine.MuteRemoteAudioStream(dicPeeridAndUid[peerid], !isAudio);
  301. switch (msg)
  302. {
  303. case 0:
  304. Debug.Log(isAudio ? "订阅远端音频成功" : "取订远端音频成功 ");
  305. break;
  306. default:
  307. Debug.LogError("开关本地音频失败: " + msg);
  308. break;
  309. }
  310. }
  311. public void MuteLocalVideoStream(bool isVideo)
  312. {
  313. int msg = RtcEngine.MuteLocalVideoStream(!isVideo);
  314. //RtcEngine.EnableVideo();
  315. //int msg = RtcEngine.EnableLocalVideo(isVideo);
  316. switch (msg)
  317. {
  318. case 0:
  319. Debug.Log(isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ");
  320. // myPeer.isCloseVideo = !isVideo;
  321. MyPeer.isOpenVideo = !isVideo;
  322. break;
  323. default:
  324. Debug.LogError("开关本地视频失败: " + msg);
  325. break;
  326. }
  327. }
  328. public void MuteRemoteVideoStream(string peerid, bool isVideo)
  329. {
  330. // Debug.Log(peerid);
  331. if (!dicPeeridAndUid.ContainsKey(peerid))
  332. return;
  333. int msg = RtcEngine.MuteRemoteVideoStream(dicPeeridAndUid[peerid], !isVideo);
  334. switch (msg)
  335. {
  336. case 0:
  337. Debug.Log(isVideo ? "订阅远端视频成功" : "取订远端视频成功 ");
  338. break;
  339. default:
  340. Debug.LogError("开关本地音频失败: " + msg);
  341. break;
  342. }
  343. }
  344. public void RemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
  345. reason)
  346. {
  347. Debug.Log(uid + " " + state + " " + reason);
  348. Debug.Log("RemoteVideoStateChanged " + uid);
  349. if (!dicPeeridAndUid.ContainsValue(uid))
  350. return;
  351. for (int i = 0; i < listCustomPeer.Count; i++)
  352. {
  353. if (dicPeeridAndUid[listCustomPeer[i].peerId] == uid)
  354. {
  355. switch (state)
  356. {
  357. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STOPPED:
  358. // listCustomPeer[i].isVideo = false;
  359. break;
  360. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STARTING:
  361. listCustomPeer[i].isVideo = true;
  362. break;
  363. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_DECODING:
  364. listCustomPeer[i].isOpenVideo = false;
  365. break;
  366. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FROZEN:
  367. break;
  368. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FAILED:
  369. // listCustomPeer[i].isVideo = false;
  370. break;
  371. default:
  372. break;
  373. }
  374. switch (reason)
  375. {
  376. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_INTERNAL:
  377. break;
  378. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION://网络阻塞。
  379. // listCustomPeer[i].isVideo = false;
  380. break;
  381. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY:// 网络恢复正常。
  382. // listCustomPeer[i].isVideo = true;
  383. break;
  384. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED://本地用户停止接收远端视频流或本地用户禁用视频模块
  385. listCustomPeer[i].isOpenVideo = true;
  386. break;
  387. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED://本地用户恢复接收远端视频流或本地用户启动视频模块
  388. listCustomPeer[i].isOpenVideo = false;
  389. break;
  390. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED://远端用户停止发送视频流或远端用户禁用视频模块。
  391. listCustomPeer[i].isVideo = false;
  392. // CloseAgoraMainImage(listCustomPeer[i].peerId, false);
  393. break;
  394. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED://远端用户恢复发送视频流或远端用户启用视频模块。
  395. listCustomPeer[i].isVideo = true;
  396. // CloseAgoraMainImage(listCustomPeer[i].peerId, true);
  397. break;
  398. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE: //远端用户离开频道。
  399. listCustomPeer[i].isVideo = false;
  400. break;
  401. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK:
  402. break;
  403. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK_RECOVERY:
  404. break;
  405. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_LOW:
  406. break;
  407. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_HIGH:
  408. break;
  409. default:
  410. break;
  411. }
  412. break;
  413. }
  414. }
  415. }
  416. public void RemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
  417. {
  418. Debug.Log(uid + " " + state + " " + reason);
  419. // Debug.Log("RemoteAudioStateChanged " + uid);
  420. if (!dicPeeridAndUid.ContainsValue(uid))
  421. return;
  422. // Debug.Log("RemoteAudioStateChanged " + 1);
  423. for (int i = 0; i < listCustomPeer.Count; i++)
  424. {
  425. Debug.Log(listCustomPeer[i].peerId);
  426. if (dicPeeridAndUid[listCustomPeer[i].peerId] == uid)
  427. {
  428. // Debug.Log("RemoteAudioStateChanged " + 2);
  429. switch (state)
  430. {
  431. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STOPPED:
  432. // listCustomPeer[i].isAudio = false;
  433. break;
  434. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STARTING:
  435. listCustomPeer[i].isAudio = true;
  436. break;
  437. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_DECODING:
  438. listCustomPeer[i].isOpenAduio = false;
  439. break;
  440. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_FROZEN:
  441. break;
  442. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_FAILED:
  443. // listCustomPeer[i].isAudio = false;
  444. break;
  445. default:
  446. break;
  447. }
  448. switch (reason)
  449. {
  450. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_INTERNAL:
  451. break;
  452. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_CONGESTION:
  453. // listCustomPeer[i].isAudio = false;
  454. break;
  455. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_RECOVERY:
  456. // listCustomPeer[i].isAudio = true;
  457. break;
  458. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_MUTED:
  459. listCustomPeer[i].isOpenAduio = true;
  460. break;
  461. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_UNMUTED:
  462. listCustomPeer[i].isOpenAduio = false;
  463. break;
  464. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_MUTED:
  465. listCustomPeer[i].isAudio = false;
  466. break;
  467. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_UNMUTED:
  468. listCustomPeer[i].isAudio = true;
  469. break;
  470. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_OFFLINE:
  471. listCustomPeer[i].isAudio = false;
  472. break;
  473. default:
  474. break;
  475. }
  476. break;
  477. }
  478. }
  479. }
  480. private void OnDestroy()
  481. {
  482. Debug.Log("OnDestroy");
  483. if (RtcEngine == null) return;
  484. RtcEngine.InitEventHandler(null);
  485. RtcEngine.LeaveChannel();
  486. RtcEngine.Dispose();
  487. }
  488. internal static void MakeVideoView(uint uid, RawImage rawImage, string channelId = "")
  489. {
  490. Debug.Log("MakeVideoView " + uid);
  491. //var go = GameObject.Find(uid.ToString());
  492. //if (!ReferenceEquals(go, null))
  493. //{
  494. // return; // reuse
  495. //}
  496. // create a GameObject and assign to this new user
  497. // var videoSurface = MakePlaneSurface(uid.ToString());
  498. // configure videoSurface
  499. if (uid == 0)
  500. {
  501. rawImage.transform.localEulerAngles = Vector3.zero;
  502. //videoSurface.SetForUser(uid, channelId);
  503. rawImage.texture = AgoraRTCManager.Instance.cam2.activeTexture;
  504. }
  505. else
  506. {
  507. rawImage.transform.localEulerAngles = new Vector3(-180,0,0);
  508. var videoSurface = MakeImageSurface(rawImage);
  509. if (ReferenceEquals(videoSurface, null)) return;
  510. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  511. videoSurface.SetEnable(true);
  512. }
  513. //videoSurface.OnTextureSizeModify += (int width, int height) =>
  514. //{
  515. // float scale = (float)height / (float)width;
  516. // videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  517. // Debug.Log("OnTextureSizeModify: " + width + " " + height);
  518. //};
  519. }
  520. internal static void OnUserJoined(uint uid)
  521. {
  522. // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  523. // Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed))
  524. Agora.Rtc.UserInfo userInfo = new Agora.Rtc.UserInfo();
  525. AgoraRTCManager.Instance.RtcEngine.GetUserInfoByUid(uid, ref userInfo);
  526. }
  527. internal static void OnRemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
  528. reason)
  529. {
  530. AgoraRTCManager.Instance.RemoteVideoStateChanged(uid, state, reason);
  531. }
  532. internal static void OnRemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
  533. {
  534. AgoraRTCManager.Instance.RemoteAudioStateChanged(uid, state, reason);
  535. }
  536. #region -- Video Render UI Logic ---
  537. // VIDEO TYPE 1: 3D Object
  538. private static VideoSurface MakePlaneSurface(string goName)
  539. {
  540. var go = GameObject.CreatePrimitive(PrimitiveType.Quad);
  541. //for (int i = 0; i < list_UserInfo.Count; i++)
  542. //{
  543. // if (list_UserInfo[i].uid.ToString() == goName)
  544. // {
  545. // string userAccount = list_UserInfo[i].userAccount;
  546. // if (AgoraVideoAudioManager.Instance.list_ShowView.ContainsKey(list_UserInfo[i].userAccount))
  547. // {
  548. // go = AgoraVideoAudioManager.Instance.list_ShowView[list_UserInfo[i].userAccount].gameObject;
  549. // }
  550. // else
  551. // Debug.LogError(" Agora ShowView is NULL ");
  552. // }
  553. //}
  554. if (go == null)
  555. {
  556. go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  557. return null;
  558. }
  559. go.name = goName;
  560. // set up transform
  561. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  562. var yPos = Random.Range(3.0f, 5.0f);
  563. var xPos = Random.Range(-2.0f, 2.0f);
  564. go.transform.position = Vector3.zero;
  565. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  566. // configure videoSurface
  567. var videoSurface = go.AddComponent<VideoSurface>();
  568. return videoSurface;
  569. }
  570. // Video TYPE 2: RawImage
  571. private static VideoSurface MakeImageSurface(RawImage rawImage)
  572. {
  573. rawImage.gameObject.AddComponent<UIElementDrag>();
  574. var videoSurface = rawImage.gameObject.AddComponent<VideoSurface>();
  575. return videoSurface;
  576. }
  577. #endregion
  578. }
  579. #region -- Agora Event ---
  580. public class AgoraRTCManagerHandler : IRtcEngineEventHandler
  581. {
  582. private readonly AgoraRTCManager _videoSample;
  583. internal AgoraRTCManagerHandler(AgoraRTCManager videoSample)
  584. {
  585. _videoSample = videoSample;
  586. }
  587. public override void OnError(int err, string msg)
  588. {
  589. // _videoSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  590. }
  591. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  592. {
  593. int build = 0;
  594. Debug.Log("Agora: OnJoinChannelSuccess ");
  595. _videoSample.RtcEngine.GetVersion(ref build);
  596. }
  597. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  598. {
  599. Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  600. // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  601. AgoraRTCManager.OnUserJoined(uid);
  602. // AgoraVideoAudioManager.MakeVideoView(uid, _videoSample.GetChannelName());
  603. }
  604. public override void OnRemoteVideoStateChanged(RtcConnection connection, uint remoteUid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON reason, int elapsed)
  605. {
  606. AgoraRTCManager.OnRemoteVideoStateChanged(remoteUid, state, reason);
  607. }
  608. public override void OnRemoteAudioStateChanged(RtcConnection connection, uint remoteUid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason, int elapsed)
  609. {
  610. AgoraRTCManager.OnRemoteAudioStateChanged(remoteUid, state, reason);
  611. }
  612. public override void OnLocalVideoStateChanged(RtcConnection connection, LOCAL_VIDEO_STREAM_STATE state, LOCAL_VIDEO_STREAM_ERROR errorCode)
  613. {
  614. }
  615. public override void OnLocalAudioStateChanged(RtcConnection connection, LOCAL_AUDIO_STREAM_STATE state, LOCAL_AUDIO_STREAM_ERROR error)
  616. {
  617. }
  618. }
  619. #endregion