AgoraVideoAudioManager.cs 30 KB

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