AgoraVideoAudioManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. using Agora.Rtc;
  2. using Agora.Util;
  3. using SC.XR.Unity;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.Serialization;
  8. using UnityEngine.UI;
  9. using XRTool.Util;
  10. using Logger = Agora.Util.Logger;
  11. public class AgoraVideoAudioManager : SingletonMono<AgoraVideoAudioManager>
  12. {
  13. [FormerlySerializedAs("appIdInput")]
  14. [SerializeField]
  15. private AppIdInput _appIdInput;
  16. [Header("_____________Basic Configuration_____________")]
  17. [FormerlySerializedAs("APP_ID")]
  18. [SerializeField]
  19. private string _appID = "";
  20. [FormerlySerializedAs("TOKEN")]
  21. [SerializeField]
  22. private string _token = "";
  23. [FormerlySerializedAs("CHANNEL_NAME")]
  24. [SerializeField]
  25. private string _channelName = "";
  26. internal IRtcEngine RtcEngine = null;
  27. internal Logger Log;
  28. private bool isAudio;
  29. private bool isVideo;
  30. //private static List<Agora.Rtc.UserInfo> list_UserInfo;
  31. private Dictionary<string, uint> dicPeeridAndUid;
  32. public Text LogText;
  33. private bool isRoom;
  34. private CustomPeer myPeer;
  35. public uint uid;
  36. public string userAccount;
  37. // private Dictionary<string, RawImage> list_ShowView;
  38. public MenuIcon menuIcon;
  39. public bool isSwitchCamera;
  40. // Use this for initialization
  41. private void Start()
  42. {
  43. LoadAssetData();
  44. if (CheckAppId())
  45. {
  46. InitEngine();
  47. //SetChinnelName("0003003");
  48. //JoinChannel();
  49. //SetupUI();
  50. }
  51. //list_UserInfo = new List<Agora.Rtc.UserInfo>();
  52. // list_ShowView = new Dictionary<string, RawImage>();
  53. dicPeeridAndUid = new Dictionary<string, uint>();
  54. isRoom = false;
  55. isSwitchCamera = false;
  56. }
  57. // Update is called once per frame
  58. private void Update()
  59. {
  60. PermissionHelper.RequestMicrophontPermission();
  61. PermissionHelper.RequestCameraPermission();
  62. //if(isRoom&&CommonMethod.MyPeer!=null&&list_ShowView.Count>0)
  63. //{
  64. // if(myPeer==null)
  65. // {
  66. // myPeer = (CustomPeer)StudioRtc.Instance.customPeerList.getPeerName(CommonMethod.MyPeer.PeerId);
  67. // Debug.Log(" Get MyCustomPeer");
  68. // }
  69. // if (myPeer == null) return;
  70. // if (isAudio != myPeer.isAudio)
  71. // {
  72. // isAudio = myPeer.isAudio;
  73. // EnableLoacalAudio(isAudio);
  74. // }
  75. // if(isVideo!= myPeer.isVideo)
  76. // {
  77. // isVideo = myPeer.isVideo;
  78. // EnableLocalVideo(isVideo);
  79. // }
  80. //}
  81. }
  82. //Show data in AgoraBasicProfile
  83. [ContextMenu("ShowAgoraBasicProfileData")]
  84. private void LoadAssetData()
  85. {
  86. if (_appIdInput == null) return;
  87. _appID = _appIdInput.appID;
  88. _token = _appIdInput.token;
  89. _channelName = _appIdInput.channelName;
  90. }
  91. private bool CheckAppId()
  92. {
  93. Log = new Logger(LogText);
  94. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  95. }
  96. private void InitEngine()
  97. {
  98. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  99. AgoraVideoManagerHandler handler = new AgoraVideoManagerHandler(this);
  100. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  101. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION,
  102. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  103. RtcEngine.Initialize(context);
  104. RtcEngine.InitEventHandler(handler);
  105. }
  106. public void RegisterLocalUserAccount(string peerId)
  107. {
  108. RtcEngine.RegisterLocalUserAccount(_appID, peerId);
  109. }
  110. public void SetChinnelName(string roomid)
  111. {
  112. _channelName = roomid;
  113. }
  114. public void JoinChannel( )
  115. {
  116. Debug.Log(" JoinChannel " + _channelName);
  117. RtcEngine.EnableAudio();
  118. RtcEngine.EnableVideo();
  119. // RtcEngine.DisableAudio();
  120. // RtcEngine.DisableVideo();
  121. VideoEncoderConfiguration config = new VideoEncoderConfiguration();
  122. config.dimensions = new VideoDimensions(1280, 720);
  123. config.frameRate = 15;
  124. config.bitrate = 0;
  125. RtcEngine.SetVideoEncoderConfiguration(config);
  126. RtcEngine.SetChannelProfile(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION);
  127. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  128. // _channelName = roomid;
  129. RtcEngine.JoinChannel(_token, _channelName,"", uid);
  130. //RtcEngine.MuteLocalVideoStream(false);
  131. //RtcEngine.MuteLocalAudioStream(false);
  132. //RtcEngine.MuteAllRemoteAudioStreams(true);
  133. //RtcEngine.MuteAllRemoteVideoStreams(true);
  134. isRoom = true;
  135. //if(!isSwitchCamera)
  136. //{
  137. // RtcEngine.SwitchCamera();
  138. // isSwitchCamera = !isSwitchCamera;
  139. //}
  140. //if (_channelName== "0003003")
  141. //{
  142. // StartCoroutine(CloseChannel(10f));
  143. //}
  144. }
  145. public void OpenAgoraAudio()
  146. {
  147. Debug.Log(" 打开 OpenAgoraAudio ");
  148. // RtcEngine.SwitchCamera();
  149. //RtcEngine.MuteAllRemoteAudioStreams(false);
  150. //RtcEngine.MuteAllRemoteVideoStreams(false);
  151. // RtcEngine.EnableAudio();
  152. }
  153. private IEnumerator CloseChannel( float times)
  154. {
  155. yield return new WaitForSeconds(times);
  156. LeaveChannel();
  157. }
  158. public void LeaveChannel()
  159. {
  160. Debug.Log("LeaveChannel " );
  161. int msg = RtcEngine.LeaveChannel();
  162. switch (msg)
  163. {
  164. case 0:
  165. LogText.text = "成功退出频道: " + _channelName;
  166. break;
  167. default:
  168. LogText.text = "退出频道失败: " + msg;
  169. break;
  170. }
  171. isRoom = false;
  172. myPeer = null;
  173. dicPeeridAndUid.Clear();
  174. dicPeeridAndUid = new Dictionary<string, uint>();
  175. //list_ShowView.Clear();
  176. //list_ShowView = new Dictionary<string, RawImage>();
  177. }
  178. public void VuforiaLeaveChannel()
  179. {
  180. int msg = RtcEngine.LeaveChannel();
  181. switch (msg)
  182. {
  183. case 0:
  184. LogText.text = "成功退出频道: " + _channelName;
  185. break;
  186. default:
  187. LogText.text = "退出频道失败: " + msg;
  188. break;
  189. }
  190. }
  191. public void AddPeeridUid(string peerid, uint uid)
  192. {
  193. Debug.Log(" AddPeeridUid " + peerid + " " + uid);
  194. if (dicPeeridAndUid.ContainsKey(peerid))
  195. return;
  196. dicPeeridAndUid.Add(peerid, uid);
  197. }
  198. public void RemAtPeeridUid(string peerid)
  199. {
  200. Debug.Log(" RemAtPeeridUid " + peerid + " " + uid);
  201. dicPeeridAndUid.Remove(peerid);
  202. }
  203. //public void RemAtListShowView(string peerId)
  204. //{
  205. // if (list_ShowView.ContainsKey(peerId))
  206. // list_ShowView.Remove(peerId);
  207. //}
  208. public void AddListShowView( string peerId, RawImage rawImage)
  209. {
  210. if (peerId == CommonMethod.MyPeer.PeerId)
  211. {
  212. rawImage.gameObject.SetActive(true);
  213. rawImage.rectTransform.localEulerAngles += new Vector3(0, 180, 180);
  214. MakeVideoView(0, rawImage);
  215. }
  216. if (peerId == CommonMethod.MyPeer.PeerId && !dicPeeridAndUid.ContainsKey(peerId))
  217. return;
  218. Debug.Log(" AddListShowView " + peerId);
  219. // list_ShowView.Add(peerId, rawImage);
  220. rawImage.gameObject.SetActive(true);
  221. rawImage.rectTransform.localEulerAngles += new Vector3(0, 180, 180);
  222. MakeVideoView(dicPeeridAndUid[peerId], rawImage, this._channelName);
  223. //for (int i = 0; i < list_UserInfo.Count; i++)
  224. //{
  225. // if(list_UserInfo[i].userAccount == peerId)
  226. // {
  227. // Debug.Log(" 查找到 ");
  228. // MakeVideoView(list_UserInfo[i].uid, rawImage,this._channelName );
  229. // return;
  230. // }
  231. //}
  232. //Debug.Log(" 未找到UID ");
  233. //for (int i = 0; i < list_UserInfo.Count; i++)
  234. //{
  235. // Debug.Log(list_UserInfo[i].uid + " " + list_UserInfo[i].userAccount);
  236. //}
  237. }
  238. public void EnableLoacalAudio( bool isAudio)
  239. {
  240. int msg = RtcEngine.MuteLocalAudioStream(!isAudio);
  241. // int msg = RtcEngine.EnableLocalAudio(isAudio);
  242. switch (msg)
  243. {
  244. case 0:
  245. Debug.Log( isAudio ? "打开本地音频成功" : "关闭本地音频成功 ");
  246. break;
  247. default:
  248. Debug.LogError("开关本地音频失败: " + msg);
  249. break;
  250. }
  251. }
  252. public void MuteRemoteAudioStream( string peerid , bool isAudio)
  253. {
  254. if (!dicPeeridAndUid.ContainsKey(peerid))
  255. return;
  256. int msg = RtcEngine.MuteRemoteAudioStream(dicPeeridAndUid[peerid], isAudio);
  257. switch (msg)
  258. {
  259. case 0:
  260. Debug.Log( isAudio ? "订阅远端音频成功" : "取订远端音频成功 ");
  261. break;
  262. default:
  263. Debug.LogError( "开关本地音频失败: " + msg);
  264. break;
  265. }
  266. }
  267. public void EnableLocalVideo( bool isVideo)
  268. {
  269. //int msg = RtcEngine.MuteLocalVideoStream(!isVideo);
  270. //RtcEngine.EnableVideo();
  271. int msg = RtcEngine.EnableLocalVideo(isVideo);
  272. switch (msg)
  273. {
  274. case 0:
  275. Debug.Log( isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ");
  276. break;
  277. default:
  278. Debug.LogError( "开关本地视频失败: " + msg);
  279. break;
  280. }
  281. }
  282. public void OnClickEnableLocalVideo( bool isVideo)
  283. {
  284. int msg = RtcEngine.EnableLocalVideo(isVideo);
  285. switch (msg)
  286. {
  287. case 0:
  288. LogText.text = isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ";
  289. break;
  290. default:
  291. LogText.text = "开关本地视频失败: " + msg;
  292. break;
  293. }
  294. }
  295. private void StopPublish()
  296. {
  297. var options = new ChannelMediaOptions();
  298. options.publishMicrophoneTrack.SetValue(false);
  299. options.publishCameraTrack.SetValue(false);
  300. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  301. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  302. }
  303. private void StartPublish()
  304. {
  305. var options = new ChannelMediaOptions();
  306. options.publishMicrophoneTrack.SetValue(true);
  307. options.publishCameraTrack.SetValue(true);
  308. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  309. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  310. }
  311. private void OnDestroy()
  312. {
  313. Debug.Log("OnDestroy");
  314. if (RtcEngine == null) return;
  315. RtcEngine.InitEventHandler(null);
  316. RtcEngine.LeaveChannel();
  317. RtcEngine.Dispose();
  318. }
  319. internal static void MakeVideoView(uint uid, RawImage rawImage, string channelId = "")
  320. {
  321. Debug.Log("MakeVideoView " + uid);
  322. //var go = GameObject.Find(uid.ToString());
  323. //if (!ReferenceEquals(go, null))
  324. //{
  325. // return; // reuse
  326. //}
  327. // create a GameObject and assign to this new user
  328. var videoSurface = MakeImageSurface(rawImage);
  329. // var videoSurface = MakePlaneSurface(uid.ToString());
  330. if (ReferenceEquals(videoSurface, null)) return;
  331. // configure videoSurface
  332. if (uid == 0)
  333. {
  334. videoSurface.SetForUser(uid, channelId);
  335. }
  336. else
  337. {
  338. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  339. }
  340. //videoSurface.OnTextureSizeModify += (int width, int height) =>
  341. //{
  342. // float scale = (float)height / (float)width;
  343. // videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  344. // Debug.Log("OnTextureSizeModify: " + width + " " + height);
  345. //};
  346. videoSurface.SetEnable(true);
  347. }
  348. //internal static void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  349. //{
  350. // Debug.Log(info.uid);
  351. // disUserPeer_Uid.Add("", info);
  352. //}
  353. internal static void OnUserJoined(uint uid)
  354. {
  355. // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  356. // Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed))
  357. Agora.Rtc.UserInfo userInfo = new Agora.Rtc.UserInfo();
  358. AgoraVideoAudioManager.Instance.RtcEngine.GetUserInfoByUid(uid, ref userInfo);
  359. }
  360. #region -- Video Render UI Logic ---
  361. // VIDEO TYPE 1: 3D Object
  362. private static VideoSurface MakePlaneSurface(string goName)
  363. {
  364. var go = GameObject.CreatePrimitive(PrimitiveType.Quad);
  365. //for (int i = 0; i < list_UserInfo.Count; i++)
  366. //{
  367. // if (list_UserInfo[i].uid.ToString() == goName)
  368. // {
  369. // string userAccount = list_UserInfo[i].userAccount;
  370. // if (AgoraVideoAudioManager.Instance.list_ShowView.ContainsKey(list_UserInfo[i].userAccount))
  371. // {
  372. // go = AgoraVideoAudioManager.Instance.list_ShowView[list_UserInfo[i].userAccount].gameObject;
  373. // }
  374. // else
  375. // Debug.LogError(" Agora ShowView is NULL ");
  376. // }
  377. //}
  378. if (go == null)
  379. {
  380. go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  381. return null;
  382. }
  383. go.name = goName;
  384. // set up transform
  385. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  386. var yPos = Random.Range(3.0f, 5.0f);
  387. var xPos = Random.Range(-2.0f, 2.0f);
  388. go.transform.position = Vector3.zero;
  389. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  390. // configure videoSurface
  391. var videoSurface = go.AddComponent<VideoSurface>();
  392. return videoSurface;
  393. }
  394. // Video TYPE 2: RawImage
  395. private static VideoSurface MakeImageSurface(RawImage rawImage)
  396. {
  397. rawImage.gameObject.AddComponent<UIElementDrag>();
  398. var videoSurface = rawImage.gameObject.AddComponent<VideoSurface>();
  399. return videoSurface;
  400. }
  401. internal static void DestroyVideoView(uint uid)
  402. {
  403. var go = GameObject.Find(uid.ToString());
  404. if (!ReferenceEquals(go, null))
  405. {
  406. Destroy(go);
  407. }
  408. }
  409. internal static void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  410. {
  411. Debug.Log(uid.ToString() + " " + info.uid + " " + info.userAccount);
  412. // list_UserInfo.Add(info);
  413. }
  414. internal static void OnLocalUserRegistered(uint uid, string userAccount)
  415. {
  416. //AgoraVideoAudioManager.Instance.uid = uid;
  417. //AgoraVideoAudioManager.Instance.userAccount = userAccount;
  418. }
  419. #endregion
  420. }
  421. #region -- Agora Event ---
  422. public class AgoraVideoManagerHandler : IRtcEngineEventHandler
  423. {
  424. private readonly AgoraVideoAudioManager _videoSample;
  425. internal AgoraVideoManagerHandler(AgoraVideoAudioManager videoSample)
  426. {
  427. _videoSample = videoSample;
  428. }
  429. public override void OnError(int err, string msg)
  430. {
  431. _videoSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  432. }
  433. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  434. {
  435. int build = 0;
  436. Debug.Log("Agora: OnJoinChannelSuccess ");
  437. _videoSample.Log.UpdateLog(string.Format("sdk version: ${0}",
  438. _videoSample.RtcEngine.GetVersion(ref build)));
  439. _videoSample.Log.UpdateLog(string.Format("sdk build: ${0}",
  440. build));
  441. _videoSample.Log.UpdateLog(
  442. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  443. connection.channelId, connection.localUid, elapsed));
  444. // _videoSample.ClickSelf();
  445. // AgoraVideoAudioManager.MakeVideoView(0);
  446. }
  447. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  448. {
  449. _videoSample.Log.UpdateLog("OnRejoinChannelSuccess");
  450. }
  451. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  452. {
  453. _videoSample.Log.UpdateLog("OnLeaveChannel");
  454. AgoraVideoAudioManager.DestroyVideoView(0);
  455. }
  456. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  457. {
  458. _videoSample.Log.UpdateLog("OnClientRoleChanged");
  459. }
  460. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  461. {
  462. Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  463. _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  464. AgoraVideoAudioManager.OnUserJoined(uid);
  465. // AgoraVideoAudioManager.MakeVideoView(uid, _videoSample.GetChannelName());
  466. }
  467. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  468. {
  469. _videoSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  470. (int)reason));
  471. AgoraVideoAudioManager.DestroyVideoView(uid);
  472. }
  473. public override void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  474. {
  475. _videoSample.Log.UpdateLog(string.Format(" 用户 :${0} 加入房间", uid));
  476. AgoraVideoAudioManager.OnUserInfoUpdated(uid, info);
  477. }
  478. public override void OnUplinkNetworkInfoUpdated(UplinkNetworkInfo info)
  479. {
  480. _videoSample.Log.UpdateLog("OnUplinkNetworkInfoUpdated");
  481. }
  482. public override void OnDownlinkNetworkInfoUpdated(DownlinkNetworkInfo info)
  483. {
  484. _videoSample.Log.UpdateLog("OnDownlinkNetworkInfoUpdated");
  485. }
  486. public override void OnLocalUserRegistered(uint uid, string userAccount)
  487. {
  488. AgoraVideoAudioManager.OnLocalUserRegistered(uid, userAccount);
  489. }
  490. }
  491. #endregion