AgoraVideoAudioManager.cs 35 KB

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