AgoraVideoAudioManager.cs 30 KB

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