AgoraTest.cs 29 KB

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