AgoraVideoAudioManager.cs 31 KB

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