AgoraVideoAudioManager.cs 28 KB

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