AgoraVideoAudioManager.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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 =new Dictionary<string, uint>();
  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. try
  179. {
  180. if (dicPeeridAndUid.ContainsKey(peerId))
  181. return dicPeeridAndUid[peerId];
  182. }
  183. catch
  184. {
  185. }
  186. return 0;
  187. }
  188. private IEnumerator CloseChannel( float times)
  189. {
  190. yield return new WaitForSeconds(times);
  191. LeaveChannel();
  192. }
  193. public void LeaveChannel()
  194. {
  195. Debug.Log("LeaveChannel " );
  196. int msg = RtcEngine.LeaveChannel();
  197. switch (msg)
  198. {
  199. case 0:
  200. LogText.text = "成功退出频道: " + _channelName;
  201. break;
  202. default:
  203. LogText.text = "退出频道失败: " + msg;
  204. break;
  205. }
  206. isRoom = false;
  207. myPeer = null;
  208. dicPeeridAndUid.Clear();
  209. dicPeeridAndUid = new Dictionary<string, uint>();
  210. //list_ShowView.Clear();
  211. //list_ShowView = new Dictionary<string, RawImage>();
  212. AgoraVideoAudioManager.Instance.listCustomPeer.Clear();
  213. AgoraVideoAudioManager.Instance.listCustomPeer = new List<CustomPeer>();
  214. mainViewPeerId = "";
  215. CloseAgoraMainImage(mainViewPeerId, false);
  216. }
  217. public void VuforiaLeaveChannel()
  218. {
  219. int msg = RtcEngine.LeaveChannel();
  220. switch (msg)
  221. {
  222. case 0:
  223. LogText.text = "成功退出频道: " + _channelName;
  224. break;
  225. default:
  226. LogText.text = "退出频道失败: " + msg;
  227. break;
  228. }
  229. }
  230. public void AddPeeridUid(string peerid, uint uid)
  231. {
  232. Debug.Log(" AddPeeridUid " + peerid + " " + uid);
  233. if (dicPeeridAndUid.ContainsKey(peerid))
  234. return;
  235. dicPeeridAndUid.Add(peerid, uid);
  236. }
  237. public void RemAtPeeridUid(string peerid)
  238. {
  239. if (!dicPeeridAndUid.ContainsKey(peerid))
  240. return;
  241. Debug.Log(" RemAtPeeridUid " + peerid + " " + dicPeeridAndUid[peerid]);
  242. dicPeeridAndUid.Remove(peerid);
  243. CloseAgoraMainImage(peerid, false);
  244. for (int i = 0; i <listCustomPeer.Count; i++)
  245. {
  246. if (listCustomPeer[i].peerId == peerid)
  247. listCustomPeer.RemoveAt(i);
  248. }
  249. }
  250. private void CloseAgoraMainImage(string peerid, bool isOpen)
  251. {
  252. if (peerid == mainViewPeerId)
  253. {
  254. isVideoStream = isOpen;
  255. if (isOpen)
  256. {
  257. AddListShowView(mainViewPeerId, RoomMainForms.Instance.bigView);
  258. }
  259. else
  260. {
  261. StartCoroutine(CloseBG(0.05f));
  262. }
  263. }
  264. }
  265. IEnumerator CloseBG(float times)
  266. {
  267. yield return new WaitForSeconds(times);
  268. RoomMainForms.Instance.bigView.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
  269. RoomMainForms.Instance.bigView.texture = bg;
  270. }
  271. public void ShowLocalView(RawImage rawImage)
  272. {
  273. rawImage.gameObject.SetActive(true);
  274. rawImage.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
  275. MakeVideoView(0, rawImage);
  276. }
  277. public void AddListShowView( string peerId, RawImage rawImage)
  278. {
  279. if (!dicPeeridAndUid.ContainsKey(peerId))
  280. return;
  281. for (int i = 0; i < listCustomPeer.Count; i++)
  282. {
  283. if(listCustomPeer[i].peerId == peerId )
  284. {
  285. if( listCustomPeer[i].isVideo)
  286. {
  287. Debug.Log(listCustomPeer[i].isVideo + " " + peerId);
  288. rawImage.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
  289. MakeVideoView(dicPeeridAndUid[peerId], rawImage, this._channelName);
  290. isVideoStream = true;
  291. }
  292. else
  293. {
  294. rawImage.texture = bg;
  295. rawImage.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
  296. }
  297. break;
  298. }
  299. }
  300. mainViewPeerId = peerId;
  301. }
  302. public void ShowOneView(RawImage rawImage)
  303. {
  304. if (dicPeeridAndUid.Count < 2)
  305. return;
  306. if (!isOneShow)
  307. return;
  308. isOneShow = true;
  309. rawImage.gameObject.SetActive(true);
  310. rawImage.texture = null;
  311. rawImage.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
  312. MakeVideoView(dicPeeridAndUid.Values.Skip(1).First(), rawImage, this._channelName);
  313. }
  314. public void UserJoined(uint uid)
  315. {
  316. if (uid == dicPeeridAndUid.Values.Skip(1).First())
  317. {
  318. RoomMainForms.Instance.bigView.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
  319. RoomMainForms.Instance.bigView.gameObject.SetActive(true);
  320. MakeVideoView(dicPeeridAndUid.Values.Skip(1).First(), RoomMainForms.Instance.bigView, this._channelName);
  321. foreach (var item in dicPeeridAndUid)
  322. {
  323. if (item.Value == uid)
  324. mainViewPeerId = item.Key;
  325. }
  326. }
  327. }
  328. public void SwitchCamera(bool state)
  329. {
  330. int msg = RtcEngine.SwitchCamera();
  331. switch (msg)
  332. {
  333. case 0:
  334. Debug.Log("切换摄像头成功");
  335. break;
  336. default:
  337. Debug.LogError("切换摄像头失败: " + msg);
  338. break;
  339. }
  340. }
  341. public void EnableLoacalAudio( bool isAudio)
  342. {
  343. int msg = RtcEngine.MuteLocalAudioStream(!isAudio);
  344. // int msg = RtcEngine.EnableLocalAudio(isAudio);
  345. switch (msg)
  346. {
  347. case 0:
  348. Debug.Log( isAudio ? "打开本地音频成功" : "关闭本地音频成功 ");
  349. break;
  350. default:
  351. Debug.LogError("开关本地音频失败: " + msg);
  352. break;
  353. }
  354. }
  355. public void MuteRemoteAudioStream( string peerid , bool isAudio)
  356. {
  357. if (!dicPeeridAndUid.ContainsKey(peerid))
  358. return;
  359. int msg = RtcEngine.MuteRemoteAudioStream(dicPeeridAndUid[peerid], isAudio);
  360. switch (msg)
  361. {
  362. case 0:
  363. Debug.Log( !isAudio ? "订阅远端音频成功" : "取订远端音频成功 ");
  364. break;
  365. default:
  366. Debug.LogError( "开关本地音频失败: " + msg);
  367. break;
  368. }
  369. }
  370. public void MuteRemoteVideoStream(string peerid , bool isVideo)
  371. {
  372. if (!dicPeeridAndUid.ContainsKey(peerid))
  373. return;
  374. int msg = RtcEngine.MuteRemoteVideoStream(dicPeeridAndUid[peerid], isVideo);
  375. switch (msg)
  376. {
  377. case 0:
  378. Debug.Log(!isVideo ? "订阅远端视频成功" : "取订远端视频成功 ");
  379. break;
  380. default:
  381. Debug.LogError("开关本地视频失败: " + msg);
  382. break;
  383. }
  384. // CloseAgoraMainImage(peerid, !isVideo);
  385. }
  386. public void EnableLocalVideo( bool isVideo)
  387. {
  388. int msg = RtcEngine.MuteLocalVideoStream(!isVideo);
  389. //RtcEngine.EnableVideo();
  390. // int msg = RtcEngine.EnableLocalVideo(isVideo);
  391. switch (msg)
  392. {
  393. case 0:
  394. Debug.Log( isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ");
  395. break;
  396. default:
  397. Debug.LogError( "开关本地视频失败: " + msg);
  398. break;
  399. }
  400. if (!isVideo)
  401. RoomMainForms.Instance.selfVideo.texture = null;
  402. else
  403. ShowLocalView( RoomMainForms.Instance.selfVideo);
  404. }
  405. public void OnClickEnableLocalVideo( bool isVideo)
  406. {
  407. int msg = RtcEngine.EnableLocalVideo(isVideo);
  408. switch (msg)
  409. {
  410. case 0:
  411. LogText.text = isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ";
  412. break;
  413. default:
  414. LogText.text = "开关本地视频失败: " + msg;
  415. break;
  416. }
  417. }
  418. public void RemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
  419. reason)
  420. {
  421. Debug.Log("RemoteVideoStateChanged " + uid);
  422. if (!dicPeeridAndUid.ContainsValue(uid))
  423. return;
  424. for (int i = 0; i < listCustomPeer.Count; i++)
  425. {
  426. if (dicPeeridAndUid[listCustomPeer[i].peerId] == uid)
  427. {
  428. switch (reason)
  429. {
  430. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_INTERNAL:
  431. break;
  432. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION://网络阻塞。
  433. // listCustomPeer[i].isVideo = false;
  434. break;
  435. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY:// 网络恢复正常。
  436. // listCustomPeer[i].isVideo = true;
  437. break;
  438. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED://本地用户停止接收远端视频流或本地用户禁用视频模块
  439. CloseAgoraMainImage(listCustomPeer[i].peerId, false);
  440. // listCustomPeer[i].isCloseVideo = false;
  441. break;
  442. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED://本地用户恢复接收远端视频流或本地用户启动视频模块
  443. CloseAgoraMainImage(listCustomPeer[i].peerId, true);
  444. // listCustomPeer[i].isCloseVideo = true;
  445. break;
  446. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED://远端用户停止发送视频流或远端用户禁用视频模块。
  447. listCustomPeer[i].isVideo = false;
  448. CloseAgoraMainImage(listCustomPeer[i].peerId, false);
  449. break;
  450. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED://远端用户恢复发送视频流或远端用户启用视频模块。
  451. listCustomPeer[i].isVideo = true;
  452. CloseAgoraMainImage(listCustomPeer[i].peerId, true);
  453. break;
  454. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE: //远端用户离开频道。
  455. listCustomPeer[i].isVideo = false;
  456. break;
  457. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK:
  458. break;
  459. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK_RECOVERY:
  460. break;
  461. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_LOW:
  462. break;
  463. case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_HIGH:
  464. break;
  465. default:
  466. break;
  467. }
  468. switch (state)
  469. {
  470. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STOPPED:
  471. break;
  472. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STARTING:
  473. break;
  474. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_DECODING:
  475. RoomMainForms.Instance.bigView.gameObject.SetActive(true);
  476. // RoomMainForms.Instance.bigView.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
  477. break;
  478. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FROZEN:
  479. break;
  480. case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FAILED:
  481. RoomMainForms.Instance.bigView.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
  482. RoomMainForms.Instance.bigView.texture = bg;
  483. break;
  484. default:
  485. break;
  486. }
  487. break;
  488. }
  489. }
  490. }
  491. public void RemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
  492. {
  493. Debug.Log("RemoteAudioStateChanged " + uid);
  494. if (!dicPeeridAndUid.ContainsValue(uid))
  495. return;
  496. for (int i = 0; i < listCustomPeer.Count; i++)
  497. {
  498. if (dicPeeridAndUid[listCustomPeer[i].peerId] == uid)
  499. {
  500. switch (reason)
  501. {
  502. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_INTERNAL:
  503. break;
  504. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_CONGESTION:
  505. // listCustomPeer[i].isAudio = false;
  506. break;
  507. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_RECOVERY:
  508. // listCustomPeer[i].isAudio = true;
  509. break;
  510. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_MUTED:
  511. listCustomPeer[i].isCloseAudio = true;
  512. break;
  513. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_UNMUTED:
  514. listCustomPeer[i].isCloseAudio = false;
  515. break;
  516. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_MUTED:
  517. listCustomPeer[i].isAudio = false;
  518. break;
  519. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_UNMUTED:
  520. listCustomPeer[i].isAudio = true;
  521. break;
  522. case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_OFFLINE:
  523. listCustomPeer[i].isAudio = false;
  524. break;
  525. default:
  526. break;
  527. }
  528. break;
  529. }
  530. }
  531. }
  532. private void StopPublish()
  533. {
  534. var options = new ChannelMediaOptions();
  535. options.publishMicrophoneTrack.SetValue(false);
  536. options.publishCameraTrack.SetValue(false);
  537. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  538. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  539. }
  540. private void StartPublish()
  541. {
  542. var options = new ChannelMediaOptions();
  543. options.publishMicrophoneTrack.SetValue(true);
  544. options.publishCameraTrack.SetValue(true);
  545. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  546. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  547. }
  548. private void OnDestroy()
  549. {
  550. Debug.Log("OnDestroy");
  551. if (RtcEngine == null) return;
  552. RtcEngine.InitEventHandler(null);
  553. RtcEngine.LeaveChannel();
  554. RtcEngine.Dispose();
  555. }
  556. internal string GetChannelName()
  557. {
  558. return _channelName;
  559. }
  560. internal static void MakeVideoView(uint uid, RawImage rawImage, string channelId = "")
  561. {
  562. Debug.Log("MakeVideoView " + uid);
  563. //var go = GameObject.Find(uid.ToString());
  564. //if (!ReferenceEquals(go, null))
  565. //{
  566. // return; // reuse
  567. //}
  568. // create a GameObject and assign to this new user
  569. var videoSurface = MakeImageSurface(rawImage);
  570. // var videoSurface = MakePlaneSurface(uid.ToString());
  571. if (ReferenceEquals(videoSurface, null)) return;
  572. // configure videoSurface
  573. if (uid == 0)
  574. {
  575. videoSurface.SetForUser(uid, channelId);
  576. }
  577. else
  578. {
  579. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  580. }
  581. //videoSurface.OnTextureSizeModify += (int width, int height) =>
  582. //{
  583. // float scale = (float)height / (float)width;
  584. // videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  585. // Debug.Log("OnTextureSizeModify: " + width + " " + height);
  586. //};
  587. videoSurface.SetEnable(true);
  588. }
  589. //internal static void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  590. //{
  591. // Debug.Log(info.uid);
  592. // disUserPeer_Uid.Add("", info);
  593. //}
  594. internal static void OnUserJoined(uint uid)
  595. {
  596. // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  597. // Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed))
  598. //Agora.Rtc.UserInfo userInfo = new Agora.Rtc.UserInfo();
  599. //AgoraVideoAudioManager.Instance.RtcEngine.GetUserInfoByUid(uid, ref userInfo);
  600. AgoraVideoAudioManager.Instance.UserJoined(uid);
  601. }
  602. internal static void OnRemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
  603. reason)
  604. {
  605. AgoraVideoAudioManager.Instance.RemoteVideoStateChanged(uid, state, reason);
  606. }
  607. internal static void OnRemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
  608. {
  609. AgoraVideoAudioManager.Instance.RemoteAudioStateChanged(uid, state, reason);
  610. }
  611. #region -- Video Render UI Logic ---
  612. // VIDEO TYPE 1: 3D Object
  613. private static VideoSurface MakePlaneSurface(string goName)
  614. {
  615. var go = GameObject.CreatePrimitive(PrimitiveType.Quad);
  616. //for (int i = 0; i < list_UserInfo.Count; i++)
  617. //{
  618. // if (list_UserInfo[i].uid.ToString() == goName)
  619. // {
  620. // string userAccount = list_UserInfo[i].userAccount;
  621. // if (AgoraVideoAudioManager.Instance.list_ShowView.ContainsKey(list_UserInfo[i].userAccount))
  622. // {
  623. // go = AgoraVideoAudioManager.Instance.list_ShowView[list_UserInfo[i].userAccount].gameObject;
  624. // }
  625. // else
  626. // Debug.LogError(" Agora ShowView is NULL ");
  627. // }
  628. //}
  629. if (go == null)
  630. {
  631. go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  632. return null;
  633. }
  634. go.name = goName;
  635. // set up transform
  636. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  637. var yPos = Random.Range(3.0f, 5.0f);
  638. var xPos = Random.Range(-2.0f, 2.0f);
  639. go.transform.position = Vector3.zero;
  640. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  641. // configure videoSurface
  642. var videoSurface = go.AddComponent<VideoSurface>();
  643. return videoSurface;
  644. }
  645. // Video TYPE 2: RawImage
  646. private static VideoSurface MakeImageSurface(RawImage rawImage)
  647. {
  648. //GameObject go = rawImage.gameObject;
  649. //if (go == null)
  650. //{
  651. // go = new GameObject();
  652. // //go.name = goName;
  653. // // to be renderered onto
  654. // go.AddComponent<RawImage>();
  655. // // set up transform
  656. // //go.transform.Rotate(0f, 0.0f, 180.0f);
  657. // //go.transform.localPosition = Vector3.zero;
  658. // //go.transform.localScale = new Vector3(2f, 3f, 1f);
  659. //}
  660. //if (go == null)
  661. //{
  662. // return null;
  663. //}
  664. // make the object draggable
  665. if (rawImage.gameObject.GetComponent<UIElementDrag>() != null)
  666. Destroy(rawImage.gameObject.GetComponent<UIElementDrag>());
  667. rawImage.gameObject.AddComponent<UIElementDrag>();
  668. //var canvas = GameObject.Find("VideoCanvas");
  669. //if (canvas != null)
  670. //{
  671. // go.transform.parent = canvas.transform;
  672. // Debug.Log("add video view");
  673. //}
  674. //else
  675. //{
  676. // Debug.Log("Canvas is null video view");
  677. //}
  678. // configure videoSurface
  679. if (rawImage.gameObject.GetComponent<VideoSurface>() != null)
  680. Destroy(rawImage.gameObject.GetComponent<VideoSurface>());
  681. var videoSurface = rawImage.gameObject.AddComponent<VideoSurface>();
  682. return videoSurface;
  683. }
  684. internal static void DestroyVideoView(uint uid)
  685. {
  686. var go = GameObject.Find(uid.ToString());
  687. if (!ReferenceEquals(go, null))
  688. {
  689. Destroy(go);
  690. }
  691. }
  692. #endregion
  693. }
  694. #region -- Agora Event ---
  695. public class AgoraVideoManagerHandler : IRtcEngineEventHandler
  696. {
  697. private readonly AgoraVideoAudioManager _videoSample;
  698. internal AgoraVideoManagerHandler(AgoraVideoAudioManager videoSample)
  699. {
  700. _videoSample = videoSample;
  701. }
  702. public override void OnError(int err, string msg)
  703. {
  704. _videoSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  705. }
  706. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  707. {
  708. int build = 0;
  709. Debug.Log("Agora: OnJoinChannelSuccess ");
  710. _videoSample.Log.UpdateLog(string.Format("sdk version: ${0}",
  711. _videoSample.RtcEngine.GetVersion(ref build)));
  712. _videoSample.Log.UpdateLog(string.Format("sdk build: ${0}",
  713. build));
  714. _videoSample.Log.UpdateLog(
  715. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  716. connection.channelId, connection.localUid, elapsed));
  717. // _videoSample.ClickSelf();
  718. // AgoraVideoAudioManager.MakeVideoView(0);
  719. }
  720. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  721. {
  722. _videoSample.Log.UpdateLog("OnRejoinChannelSuccess");
  723. }
  724. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  725. {
  726. _videoSample.Log.UpdateLog("OnLeaveChannel");
  727. AgoraVideoAudioManager.DestroyVideoView(0);
  728. }
  729. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  730. {
  731. _videoSample.Log.UpdateLog("OnClientRoleChanged");
  732. }
  733. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  734. {
  735. Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  736. _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  737. AgoraVideoAudioManager.OnUserJoined(uid);
  738. // AgoraVideoAudioManager.MakeVideoView(uid, _videoSample.GetChannelName());
  739. }
  740. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  741. {
  742. _videoSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  743. (int)reason));
  744. AgoraVideoAudioManager.DestroyVideoView(uid);
  745. }
  746. //public override void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  747. //{
  748. // _videoSample.Log.UpdateLog(string.Format(" 用户 :${0} 加入房间", uid));
  749. // AgoraVideoAudioManager.OnUserInfoUpdated(uid, info);
  750. //}
  751. public override void OnUplinkNetworkInfoUpdated(UplinkNetworkInfo info)
  752. {
  753. _videoSample.Log.UpdateLog("OnUplinkNetworkInfoUpdated");
  754. }
  755. public override void OnDownlinkNetworkInfoUpdated(DownlinkNetworkInfo info)
  756. {
  757. _videoSample.Log.UpdateLog("OnDownlinkNetworkInfoUpdated");
  758. }
  759. public override void OnRemoteVideoStateChanged(RtcConnection connection, uint remoteUid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON reason, int elapsed)
  760. {
  761. AgoraVideoAudioManager.OnRemoteVideoStateChanged(remoteUid, state, reason);
  762. }
  763. public override void OnRemoteAudioStateChanged(RtcConnection connection, uint remoteUid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason, int elapsed)
  764. {
  765. AgoraVideoAudioManager.OnRemoteAudioStateChanged(remoteUid, state, reason);
  766. }
  767. }
  768. #endregion