AgoraVideoAudioManager.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. using Agora.Rtc;
  2. using Agora.Util;
  3. using SC.XR.Unity;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.Serialization;
  8. using UnityEngine.UI;
  9. using XRTool.Util;
  10. using Logger = Agora.Util.Logger;
  11. public class AgoraVideoAudioManager : SingletonMono<AgoraVideoAudioManager>
  12. {
  13. [FormerlySerializedAs("appIdInput")]
  14. [SerializeField]
  15. private AppIdInput _appIdInput;
  16. [Header("_____________Basic Configuration_____________")]
  17. [FormerlySerializedAs("APP_ID")]
  18. [SerializeField]
  19. private string _appID = "";
  20. [FormerlySerializedAs("TOKEN")]
  21. [SerializeField]
  22. private string _token = "";
  23. [FormerlySerializedAs("CHANNEL_NAME")]
  24. [SerializeField]
  25. private string _channelName = "";
  26. internal IRtcEngine RtcEngine = null;
  27. internal Logger Log;
  28. private bool isAudio;
  29. private bool isVideo;
  30. //private static List<Agora.Rtc.UserInfo> list_UserInfo;
  31. private Dictionary<string, uint> dicPeeridAndUid;
  32. public Text LogText;
  33. private bool isRoom;
  34. private CustomPeer myPeer;
  35. public uint uid;
  36. public string userAccount;
  37. // private Dictionary<string, RawImage> list_ShowView;
  38. public MenuIcon menuIcon;
  39. public bool isSwitchCamera;
  40. // Use this for initialization
  41. public List<CustomPeer> listCustomPeer;
  42. private void Start()
  43. {
  44. LoadAssetData();
  45. if (CheckAppId())
  46. {
  47. InitEngine();
  48. //SetChinnelName("0003003");
  49. //JoinChannel();
  50. //SetupUI();
  51. }
  52. //list_UserInfo = new List<Agora.Rtc.UserInfo>();
  53. // list_ShowView = new Dictionary<string, RawImage>();
  54. dicPeeridAndUid = new Dictionary<string, uint>();
  55. isRoom = false;
  56. isSwitchCamera = false;
  57. listCustomPeer = new List<CustomPeer>();
  58. }
  59. // Update is called once per frame
  60. private void Update()
  61. {
  62. PermissionHelper.RequestMicrophontPermission();
  63. PermissionHelper.RequestCameraPermission();
  64. //if(isRoom&&CommonMethod.MyPeer!=null&&list_ShowView.Count>0)
  65. //{
  66. // if(myPeer==null)
  67. // {
  68. // myPeer = (CustomPeer)StudioRtc.Instance.customPeerList.getPeerName(CommonMethod.MyPeer.PeerId);
  69. // Debug.Log(" Get MyCustomPeer");
  70. // }
  71. // if (myPeer == null) return;
  72. // if (isAudio != myPeer.isAudio)
  73. // {
  74. // isAudio = myPeer.isAudio;
  75. // EnableLoacalAudio(isAudio);
  76. // }
  77. // if(isVideo!= myPeer.isVideo)
  78. // {
  79. // isVideo = myPeer.isVideo;
  80. // EnableLocalVideo(isVideo);
  81. // }
  82. //}
  83. }
  84. //Show data in AgoraBasicProfile
  85. [ContextMenu("ShowAgoraBasicProfileData")]
  86. private void LoadAssetData()
  87. {
  88. if (_appIdInput == null) return;
  89. _appID = _appIdInput.appID;
  90. _token = _appIdInput.token;
  91. _channelName = _appIdInput.channelName;
  92. }
  93. private bool CheckAppId()
  94. {
  95. Log = new Logger(LogText);
  96. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  97. }
  98. private void InitEngine()
  99. {
  100. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  101. AgoraVideoManagerHandler handler = new AgoraVideoManagerHandler(this);
  102. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  103. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION,
  104. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  105. RtcEngine.Initialize(context);
  106. RtcEngine.InitEventHandler(handler);
  107. }
  108. public void RegisterLocalUserAccount(string peerId)
  109. {
  110. RtcEngine.RegisterLocalUserAccount(_appID, peerId);
  111. }
  112. public void SetChinnelName(string roomid)
  113. {
  114. _channelName = roomid;
  115. }
  116. public void JoinChannel( )
  117. {
  118. Debug.Log(" JoinChannel " + _channelName);
  119. RtcEngine.EnableAudio();
  120. RtcEngine.EnableVideo();
  121. // RtcEngine.DisableAudio();
  122. // RtcEngine.DisableVideo();
  123. VideoEncoderConfiguration config = new VideoEncoderConfiguration();
  124. config.dimensions = new VideoDimensions(1280, 720);
  125. config.frameRate = 15;
  126. config.bitrate = 0;
  127. RtcEngine.SetVideoEncoderConfiguration(config);
  128. RtcEngine.SetChannelProfile(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION);
  129. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  130. // _channelName = roomid;
  131. RtcEngine.JoinChannel(_token, _channelName,"", uid);
  132. //RtcEngine.MuteLocalVideoStream(false);
  133. //RtcEngine.MuteLocalAudioStream(false);
  134. //RtcEngine.MuteAllRemoteAudioStreams(true);
  135. //RtcEngine.MuteAllRemoteVideoStreams(true);
  136. isRoom = true;
  137. //if(!isSwitchCamera)
  138. //{
  139. // RtcEngine.SwitchCamera();
  140. // isSwitchCamera = !isSwitchCamera;
  141. //}
  142. //if (_channelName== "0003003")
  143. //{
  144. // StartCoroutine(CloseChannel(10f));
  145. //}
  146. }
  147. public void OpenAgoraAudio()
  148. {
  149. Debug.Log(" 打开 OpenAgoraAudio ");
  150. // RtcEngine.SwitchCamera();
  151. //RtcEngine.MuteAllRemoteAudioStreams(false);
  152. //RtcEngine.MuteAllRemoteVideoStreams(false);
  153. // RtcEngine.EnableAudio();
  154. }
  155. private IEnumerator CloseChannel( float times)
  156. {
  157. yield return new WaitForSeconds(times);
  158. LeaveChannel();
  159. }
  160. public void LeaveChannel()
  161. {
  162. Debug.Log("LeaveChannel " );
  163. int msg = RtcEngine.LeaveChannel();
  164. switch (msg)
  165. {
  166. case 0:
  167. LogText.text = "成功退出频道: " + _channelName;
  168. break;
  169. default:
  170. LogText.text = "退出频道失败: " + msg;
  171. break;
  172. }
  173. isRoom = false;
  174. myPeer = null;
  175. dicPeeridAndUid.Clear();
  176. dicPeeridAndUid = new Dictionary<string, uint>();
  177. //list_ShowView.Clear();
  178. //list_ShowView = new Dictionary<string, RawImage>();
  179. AgoraVideoAudioManager.Instance.listCustomPeer.Clear();
  180. AgoraVideoAudioManager.Instance.listCustomPeer = new List<CustomPeer>();
  181. }
  182. public void VuforiaLeaveChannel()
  183. {
  184. int msg = RtcEngine.LeaveChannel();
  185. switch (msg)
  186. {
  187. case 0:
  188. LogText.text = "成功退出频道: " + _channelName;
  189. break;
  190. default:
  191. LogText.text = "退出频道失败: " + msg;
  192. break;
  193. }
  194. }
  195. public void AddPeeridUid(string peerid, uint uid)
  196. {
  197. Debug.Log(" AddPeeridUid " + peerid + " " + uid);
  198. if (dicPeeridAndUid.ContainsKey(peerid))
  199. return;
  200. dicPeeridAndUid.Add(peerid, uid);
  201. }
  202. public void RemAtPeeridUid(string peerid)
  203. {
  204. if (!dicPeeridAndUid.ContainsKey(peerid))
  205. return;
  206. Debug.Log(" RemAtPeeridUid " + peerid + " " + dicPeeridAndUid[peerid]);
  207. dicPeeridAndUid.Remove(peerid);
  208. CloseAgoraMainImage(peerid, false);
  209. for (int i = 0; i < listCustomPeer.Count; i++)
  210. {
  211. if (listCustomPeer[i].peerId == peerid)
  212. listCustomPeer.RemoveAt(i);
  213. }
  214. }
  215. private void CloseAgoraMainImage(string peerid, bool isOpen)
  216. {
  217. //if (peerid == mainViewPeerId)
  218. // RoomMain.Instance.agoraRawImage.gameObject.SetActive(isOpen);
  219. }
  220. //public void RemAtListShowView(string peerId)
  221. //{
  222. // if (list_ShowView.ContainsKey(peerId))
  223. // list_ShowView.Remove(peerId);
  224. //}
  225. public void AddListShowView( string peerId, RawImage rawImage)
  226. {
  227. if (peerId == CommonMethod.MyPeer.PeerId)
  228. {
  229. rawImage.gameObject.SetActive(true);
  230. rawImage.rectTransform.localEulerAngles += new Vector3(0, 180, 180);
  231. MakeVideoView(0, rawImage);
  232. }
  233. if (peerId == CommonMethod.MyPeer.PeerId && !dicPeeridAndUid.ContainsKey(peerId))
  234. return;
  235. Debug.Log(" AddListShowView " + peerId);
  236. // list_ShowView.Add(peerId, rawImage);
  237. rawImage.gameObject.SetActive(true);
  238. rawImage.rectTransform.localEulerAngles += new Vector3(0, 180, 180);
  239. MakeVideoView(dicPeeridAndUid[peerId], rawImage, this._channelName);
  240. //for (int i = 0; i < list_UserInfo.Count; i++)
  241. //{
  242. // if(list_UserInfo[i].userAccount == peerId)
  243. // {
  244. // Debug.Log(" 查找到 ");
  245. // MakeVideoView(list_UserInfo[i].uid, rawImage,this._channelName );
  246. // return;
  247. // }
  248. //}
  249. //Debug.Log(" 未找到UID ");
  250. //for (int i = 0; i < list_UserInfo.Count; i++)
  251. //{
  252. // Debug.Log(list_UserInfo[i].uid + " " + list_UserInfo[i].userAccount);
  253. //}
  254. }
  255. public void EnableLoacalAudio( bool isAudio)
  256. {
  257. int msg = RtcEngine.MuteLocalAudioStream(!isAudio);
  258. // int msg = RtcEngine.EnableLocalAudio(isAudio);
  259. switch (msg)
  260. {
  261. case 0:
  262. Debug.Log( isAudio ? "打开本地音频成功" : "关闭本地音频成功 ");
  263. break;
  264. default:
  265. Debug.LogError("开关本地音频失败: " + msg);
  266. break;
  267. }
  268. }
  269. public void MuteRemoteAudioStream( string peerid , bool isAudio)
  270. {
  271. if (!dicPeeridAndUid.ContainsKey(peerid))
  272. return;
  273. int msg = RtcEngine.MuteRemoteAudioStream(dicPeeridAndUid[peerid], !isAudio);
  274. switch (msg)
  275. {
  276. case 0:
  277. Debug.Log( isAudio ? "订阅远端音频成功" : "取订远端音频成功 ");
  278. break;
  279. default:
  280. Debug.LogError( "开关本地音频失败: " + msg);
  281. break;
  282. }
  283. }
  284. public void MuteRemoteVideoStream(string peerid, bool isVideo)
  285. {
  286. if (!dicPeeridAndUid.ContainsKey(peerid))
  287. return;
  288. int msg = RtcEngine.MuteRemoteVideoStream(dicPeeridAndUid[peerid], !isVideo);
  289. switch (msg)
  290. {
  291. case 0:
  292. Debug.Log(isVideo ? "订阅远端视频成功" : "取订远端视频成功 ");
  293. break;
  294. default:
  295. Debug.LogError("开关本地音频失败: " + msg);
  296. break;
  297. }
  298. }
  299. public void EnableLocalVideo( bool isVideo)
  300. {
  301. //int msg = RtcEngine.MuteLocalVideoStream(!isVideo);
  302. //RtcEngine.EnableVideo();
  303. int msg = RtcEngine.EnableLocalVideo(isVideo);
  304. switch (msg)
  305. {
  306. case 0:
  307. Debug.Log( isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ");
  308. break;
  309. default:
  310. Debug.LogError( "开关本地视频失败: " + msg);
  311. break;
  312. }
  313. }
  314. public void OnClickEnableLocalVideo( bool isVideo)
  315. {
  316. int msg = RtcEngine.EnableLocalVideo(isVideo);
  317. switch (msg)
  318. {
  319. case 0:
  320. LogText.text = isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ";
  321. break;
  322. default:
  323. LogText.text = "开关本地视频失败: " + msg;
  324. break;
  325. }
  326. }
  327. public void RemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
  328. reason)
  329. {
  330. Debug.Log("RemoteVideoStateChanged " + uid);
  331. //if (!dicPeeridAndUid.ContainsValue(uid))
  332. // return;
  333. for (int i = 0; i < listCustomPeer.Count; i++)
  334. {
  335. if (dicPeeridAndUid.ContainsKey(listCustomPeer[i].name) && dicPeeridAndUid[listCustomPeer[i].name] == uid)
  336. {
  337. Debug.Log("RemoteVideoStateChanged " + 2);
  338. switch (state)
  339. {
  340. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STOPPED:
  341. break;
  342. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STARTING:
  343. listCustomPeer[i].isVideo = true;
  344. break;
  345. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_DECODING:
  346. break;
  347. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FROZEN:
  348. break;
  349. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FAILED:
  350. break;
  351. default:
  352. break;
  353. }
  354. switch (reason)
  355. {
  356. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_INTERNAL:
  357. break;
  358. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION://网络阻塞。
  359. // listCustomPeer[i].isVideo = false;
  360. break;
  361. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY:// 网络恢复正常。
  362. // listCustomPeer[i].isVideo = true;
  363. break;
  364. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED://本地用户停止接收远端视频流或本地用户禁用视频模块
  365. listCustomPeer[i].isCloseVideo = true;
  366. break;
  367. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED://本地用户恢复接收远端视频流或本地用户启动视频模块
  368. listCustomPeer[i].isCloseVideo = false;
  369. break;
  370. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED://远端用户停止发送视频流或远端用户禁用视频模块。
  371. listCustomPeer[i].isVideo = false;
  372. break;
  373. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED://远端用户恢复发送视频流或远端用户启用视频模块。
  374. listCustomPeer[i].isVideo = true;
  375. break;
  376. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE: //远端用户离开频道。
  377. listCustomPeer[i].isVideo = false;
  378. break;
  379. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK:
  380. break;
  381. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK_RECOVERY:
  382. break;
  383. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_LOW:
  384. break;
  385. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_HIGH:
  386. break;
  387. default:
  388. break;
  389. }
  390. break;
  391. }
  392. }
  393. }
  394. public void RemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
  395. {
  396. Debug.Log("RemoteAudioStateChanged " + uid);
  397. for (int i = 0; i < listCustomPeer.Count; i++)
  398. {
  399. if (dicPeeridAndUid.ContainsKey(listCustomPeer[i].name) && dicPeeridAndUid[listCustomPeer[i].name] == uid)
  400. {
  401. Debug.Log("RemoteAudioStateChanged " + 2);
  402. switch (state)
  403. {
  404. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STOPPED:
  405. break;
  406. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STARTING:
  407. listCustomPeer[i].isAudio = true;
  408. break;
  409. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_DECODING:
  410. break;
  411. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_FROZEN:
  412. break;
  413. case REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_FAILED:
  414. break;
  415. default:
  416. break;
  417. }
  418. switch (reason)
  419. {
  420. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_INTERNAL:
  421. break;
  422. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_CONGESTION:
  423. // listCustomPeer[i].isAudio = false;
  424. break;
  425. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_RECOVERY:
  426. // listCustomPeer[i].isAudio = true;
  427. break;
  428. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_MUTED:
  429. listCustomPeer[i].isCloseAudio = true;
  430. break;
  431. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_UNMUTED:
  432. listCustomPeer[i].isCloseAudio = false;
  433. break;
  434. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_MUTED:
  435. listCustomPeer[i].isAudio = false;
  436. break;
  437. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_UNMUTED:
  438. listCustomPeer[i].isAudio = true;
  439. break;
  440. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_OFFLINE:
  441. listCustomPeer[i].isAudio = false;
  442. break;
  443. default:
  444. break;
  445. }
  446. break;
  447. }
  448. }
  449. }
  450. private void StopPublish()
  451. {
  452. var options = new ChannelMediaOptions();
  453. options.publishMicrophoneTrack.SetValue(false);
  454. options.publishCameraTrack.SetValue(false);
  455. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  456. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  457. }
  458. private void StartPublish()
  459. {
  460. var options = new ChannelMediaOptions();
  461. options.publishMicrophoneTrack.SetValue(true);
  462. options.publishCameraTrack.SetValue(true);
  463. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  464. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  465. }
  466. private void OnDestroy()
  467. {
  468. Debug.Log("OnDestroy");
  469. if (RtcEngine == null) return;
  470. RtcEngine.InitEventHandler(null);
  471. RtcEngine.LeaveChannel();
  472. RtcEngine.Dispose();
  473. }
  474. internal static void MakeVideoView(uint uid, RawImage rawImage, string channelId = "")
  475. {
  476. Debug.Log("MakeVideoView " + uid);
  477. //var go = GameObject.Find(uid.ToString());
  478. //if (!ReferenceEquals(go, null))
  479. //{
  480. // return; // reuse
  481. //}
  482. // create a GameObject and assign to this new user
  483. var videoSurface = MakeImageSurface(rawImage);
  484. // var videoSurface = MakePlaneSurface(uid.ToString());
  485. if (ReferenceEquals(videoSurface, null)) return;
  486. // configure videoSurface
  487. if (uid == 0)
  488. {
  489. videoSurface.SetForUser(uid, channelId);
  490. }
  491. else
  492. {
  493. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  494. }
  495. //videoSurface.OnTextureSizeModify += (int width, int height) =>
  496. //{
  497. // float scale = (float)height / (float)width;
  498. // videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  499. // Debug.Log("OnTextureSizeModify: " + width + " " + height);
  500. //};
  501. videoSurface.SetEnable(true);
  502. }
  503. //internal static void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  504. //{
  505. // Debug.Log(info.uid);
  506. // disUserPeer_Uid.Add("", info);
  507. //}
  508. internal static void OnUserJoined(uint uid)
  509. {
  510. // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  511. // Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed))
  512. Agora.Rtc.UserInfo userInfo = new Agora.Rtc.UserInfo();
  513. AgoraVideoAudioManager.Instance.RtcEngine.GetUserInfoByUid(uid, ref userInfo);
  514. }
  515. #region -- Video Render UI Logic ---
  516. // VIDEO TYPE 1: 3D Object
  517. private static VideoSurface MakePlaneSurface(string goName)
  518. {
  519. var go = GameObject.CreatePrimitive(PrimitiveType.Quad);
  520. //for (int i = 0; i < list_UserInfo.Count; i++)
  521. //{
  522. // if (list_UserInfo[i].uid.ToString() == goName)
  523. // {
  524. // string userAccount = list_UserInfo[i].userAccount;
  525. // if (AgoraVideoAudioManager.Instance.list_ShowView.ContainsKey(list_UserInfo[i].userAccount))
  526. // {
  527. // go = AgoraVideoAudioManager.Instance.list_ShowView[list_UserInfo[i].userAccount].gameObject;
  528. // }
  529. // else
  530. // Debug.LogError(" Agora ShowView is NULL ");
  531. // }
  532. //}
  533. if (go == null)
  534. {
  535. go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  536. return null;
  537. }
  538. go.name = goName;
  539. // set up transform
  540. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  541. var yPos = Random.Range(3.0f, 5.0f);
  542. var xPos = Random.Range(-2.0f, 2.0f);
  543. go.transform.position = Vector3.zero;
  544. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  545. // configure videoSurface
  546. var videoSurface = go.AddComponent<VideoSurface>();
  547. return videoSurface;
  548. }
  549. // Video TYPE 2: RawImage
  550. private static VideoSurface MakeImageSurface(RawImage rawImage)
  551. {
  552. rawImage.gameObject.AddComponent<UIElementDrag>();
  553. var videoSurface = rawImage.gameObject.AddComponent<VideoSurface>();
  554. return videoSurface;
  555. }
  556. internal static void DestroyVideoView(uint uid)
  557. {
  558. var go = GameObject.Find(uid.ToString());
  559. if (!ReferenceEquals(go, null))
  560. {
  561. Destroy(go);
  562. }
  563. }
  564. internal static void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  565. {
  566. Debug.Log(uid.ToString() + " " + info.uid + " " + info.userAccount);
  567. // list_UserInfo.Add(info);
  568. }
  569. internal static void OnLocalUserRegistered(uint uid, string userAccount)
  570. {
  571. //AgoraVideoAudioManager.Instance.uid = uid;
  572. //AgoraVideoAudioManager.Instance.userAccount = userAccount;
  573. }
  574. internal static void OnRemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
  575. reason)
  576. {
  577. AgoraVideoAudioManager.Instance.RemoteVideoStateChanged(uid, state, reason);
  578. }
  579. internal static void OnRemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
  580. {
  581. AgoraVideoAudioManager.Instance.RemoteAudioStateChanged(uid, state, reason);
  582. }
  583. #endregion
  584. }
  585. #region -- Agora Event ---
  586. public class AgoraVideoManagerHandler : IRtcEngineEventHandler
  587. {
  588. private readonly AgoraVideoAudioManager _videoSample;
  589. internal AgoraVideoManagerHandler(AgoraVideoAudioManager videoSample)
  590. {
  591. _videoSample = videoSample;
  592. }
  593. public override void OnError(int err, string msg)
  594. {
  595. _videoSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  596. }
  597. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  598. {
  599. int build = 0;
  600. Debug.Log("Agora: OnJoinChannelSuccess ");
  601. _videoSample.Log.UpdateLog(string.Format("sdk version: ${0}",
  602. _videoSample.RtcEngine.GetVersion(ref build)));
  603. _videoSample.Log.UpdateLog(string.Format("sdk build: ${0}",
  604. build));
  605. _videoSample.Log.UpdateLog(
  606. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  607. connection.channelId, connection.localUid, elapsed));
  608. // _videoSample.ClickSelf();
  609. // AgoraVideoAudioManager.MakeVideoView(0);
  610. }
  611. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  612. {
  613. _videoSample.Log.UpdateLog("OnRejoinChannelSuccess");
  614. }
  615. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  616. {
  617. _videoSample.Log.UpdateLog("OnLeaveChannel");
  618. AgoraVideoAudioManager.DestroyVideoView(0);
  619. }
  620. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  621. {
  622. _videoSample.Log.UpdateLog("OnClientRoleChanged");
  623. }
  624. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  625. {
  626. Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  627. _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  628. AgoraVideoAudioManager.OnUserJoined(uid);
  629. // AgoraVideoAudioManager.MakeVideoView(uid, _videoSample.GetChannelName());
  630. }
  631. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  632. {
  633. _videoSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  634. (int)reason));
  635. AgoraVideoAudioManager.DestroyVideoView(uid);
  636. }
  637. public override void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  638. {
  639. _videoSample.Log.UpdateLog(string.Format(" 用户 :${0} 加入房间", uid));
  640. AgoraVideoAudioManager.OnUserInfoUpdated(uid, info);
  641. }
  642. public override void OnUplinkNetworkInfoUpdated(UplinkNetworkInfo info)
  643. {
  644. _videoSample.Log.UpdateLog("OnUplinkNetworkInfoUpdated");
  645. }
  646. public override void OnDownlinkNetworkInfoUpdated(DownlinkNetworkInfo info)
  647. {
  648. _videoSample.Log.UpdateLog("OnDownlinkNetworkInfoUpdated");
  649. }
  650. public override void OnLocalUserRegistered(uint uid, string userAccount)
  651. {
  652. AgoraVideoAudioManager.OnLocalUserRegistered(uid, userAccount);
  653. }
  654. public override void OnRemoteVideoStateChanged(RtcConnection connection, uint remoteUid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON reason, int elapsed)
  655. {
  656. AgoraVideoAudioManager.OnRemoteVideoStateChanged(remoteUid, state, reason);
  657. }
  658. public override void OnRemoteAudioStateChanged(RtcConnection connection, uint remoteUid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason, int elapsed)
  659. {
  660. AgoraVideoAudioManager.OnRemoteAudioStateChanged(remoteUid, state, reason);
  661. }
  662. }
  663. #endregion