AgoraVideoAudioManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. StartCoroutine(InitVideoAndAduio(1f));
  141. }
  142. private IEnumerator InitVideoAndAduio(float times)
  143. {
  144. yield return new WaitForSeconds(times);
  145. EnableLocalVideo(CustomInfo.isSendVideo);
  146. EnableLoacalAudio(CustomInfo.isSendAudio);
  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. private IEnumerator CloseChannel( float times)
  157. {
  158. yield return new WaitForSeconds(times);
  159. LeaveChannel();
  160. }
  161. public void LeaveChannel()
  162. {
  163. Debug.Log("LeaveChannel " );
  164. int msg = RtcEngine.LeaveChannel();
  165. switch (msg)
  166. {
  167. case 0:
  168. LogText.text = "成功退出频道: " + _channelName;
  169. break;
  170. default:
  171. LogText.text = "退出频道失败: " + msg;
  172. break;
  173. }
  174. isRoom = false;
  175. myPeer = null;
  176. dicPeeridAndUid.Clear();
  177. dicPeeridAndUid = new Dictionary<string, uint>();
  178. //list_ShowView.Clear();
  179. //list_ShowView = new Dictionary<string, RawImage>();
  180. }
  181. public void VuforiaLeaveChannel()
  182. {
  183. int msg = RtcEngine.LeaveChannel();
  184. switch (msg)
  185. {
  186. case 0:
  187. LogText.text = "成功退出频道: " + _channelName;
  188. break;
  189. default:
  190. LogText.text = "退出频道失败: " + msg;
  191. break;
  192. }
  193. }
  194. public void AddPeeridUid(string peerid, uint uid)
  195. {
  196. Debug.Log(" AddPeeridUid " + peerid + " " + uid);
  197. if (dicPeeridAndUid.ContainsKey(peerid))
  198. return;
  199. dicPeeridAndUid.Add(peerid, uid);
  200. }
  201. public void RemAtPeeridUid(string peerid)
  202. {
  203. Debug.Log(" RemAtPeeridUid " + peerid + " " + uid);
  204. dicPeeridAndUid.Remove(peerid);
  205. }
  206. //public void RemAtListShowView(string peerId)
  207. //{
  208. // if (list_ShowView.ContainsKey(peerId))
  209. // list_ShowView.Remove(peerId);
  210. //}
  211. public void AddListShowView( string peerId, RawImage rawImage)
  212. {
  213. if (peerId == CommonMethod.MyPeer.PeerId)
  214. {
  215. rawImage.gameObject.SetActive(true);
  216. rawImage.rectTransform.localEulerAngles += new Vector3(0, 180, 180);
  217. MakeVideoView(0, rawImage);
  218. }
  219. if (peerId == CommonMethod.MyPeer.PeerId && !dicPeeridAndUid.ContainsKey(peerId))
  220. return;
  221. Debug.Log(" AddListShowView " + peerId);
  222. // list_ShowView.Add(peerId, rawImage);
  223. rawImage.gameObject.SetActive(true);
  224. rawImage.rectTransform.localEulerAngles += new Vector3(0, 180, 180);
  225. MakeVideoView(dicPeeridAndUid[peerId], rawImage, this._channelName);
  226. //for (int i = 0; i < list_UserInfo.Count; i++)
  227. //{
  228. // if(list_UserInfo[i].userAccount == peerId)
  229. // {
  230. // Debug.Log(" 查找到 ");
  231. // MakeVideoView(list_UserInfo[i].uid, rawImage,this._channelName );
  232. // return;
  233. // }
  234. //}
  235. //Debug.Log(" 未找到UID ");
  236. //for (int i = 0; i < list_UserInfo.Count; i++)
  237. //{
  238. // Debug.Log(list_UserInfo[i].uid + " " + list_UserInfo[i].userAccount);
  239. //}
  240. }
  241. public void EnableLoacalAudio( bool isAudio)
  242. {
  243. if (isAudio)
  244. StudioRtc.Instance.me.cIdA = "Audio";
  245. int msg = RtcEngine.MuteLocalAudioStream(!isAudio);
  246. // int msg = RtcEngine.EnableLocalAudio(isAudio);
  247. switch (msg)
  248. {
  249. case 0:
  250. Debug.Log( isAudio ? "打开本地音频成功" : "关闭本地音频成功 ");
  251. break;
  252. default:
  253. Debug.LogError("开关本地音频失败: " + msg);
  254. break;
  255. }
  256. }
  257. public void MuteRemoteAudioStream( string peerid , bool isAudio)
  258. {
  259. if (!dicPeeridAndUid.ContainsKey(peerid))
  260. return;
  261. int msg = RtcEngine.MuteRemoteAudioStream(dicPeeridAndUid[peerid], isAudio);
  262. switch (msg)
  263. {
  264. case 0:
  265. Debug.Log( isAudio ? "订阅远端音频成功" : "取订远端音频成功 ");
  266. break;
  267. default:
  268. Debug.LogError( "开关本地音频失败: " + msg);
  269. break;
  270. }
  271. }
  272. public void EnableLocalVideo( bool isVideo)
  273. {
  274. if (isVideo)
  275. StudioRtc.Instance.me.cIdV = "Video";
  276. int msg = RtcEngine.MuteLocalVideoStream(!isVideo);
  277. //RtcEngine.EnableVideo();
  278. //int msg = RtcEngine.EnableLocalVideo(isVideo);
  279. switch (msg)
  280. {
  281. case 0:
  282. Debug.Log( isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ");
  283. break;
  284. default:
  285. Debug.LogError( "开关本地视频失败: " + msg);
  286. break;
  287. }
  288. }
  289. public void OnClickEnableLocalVideo( bool isVideo)
  290. {
  291. int msg = RtcEngine.EnableLocalVideo(isVideo);
  292. switch (msg)
  293. {
  294. case 0:
  295. LogText.text = isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ";
  296. break;
  297. default:
  298. LogText.text = "开关本地视频失败: " + msg;
  299. break;
  300. }
  301. }
  302. private void StopPublish()
  303. {
  304. var options = new ChannelMediaOptions();
  305. options.publishMicrophoneTrack.SetValue(false);
  306. options.publishCameraTrack.SetValue(false);
  307. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  308. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  309. }
  310. private void StartPublish()
  311. {
  312. var options = new ChannelMediaOptions();
  313. options.publishMicrophoneTrack.SetValue(true);
  314. options.publishCameraTrack.SetValue(true);
  315. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  316. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  317. }
  318. private void OnDestroy()
  319. {
  320. Debug.Log("OnDestroy");
  321. if (RtcEngine == null) return;
  322. RtcEngine.InitEventHandler(null);
  323. RtcEngine.LeaveChannel();
  324. RtcEngine.Dispose();
  325. }
  326. public void ClickSelf()
  327. {
  328. this.menuIcon.ClickSelf2();
  329. }
  330. internal string GetChannelName()
  331. {
  332. return _channelName;
  333. }
  334. internal static void MakeVideoView(uint uid, RawImage rawImage, string channelId = "")
  335. {
  336. Debug.Log("MakeVideoView " + uid);
  337. //var go = GameObject.Find(uid.ToString());
  338. //if (!ReferenceEquals(go, null))
  339. //{
  340. // return; // reuse
  341. //}
  342. // create a GameObject and assign to this new user
  343. var videoSurface = MakeImageSurface(rawImage);
  344. // var videoSurface = MakePlaneSurface(uid.ToString());
  345. if (ReferenceEquals(videoSurface, null)) return;
  346. // configure videoSurface
  347. if (uid == 0)
  348. {
  349. videoSurface.SetForUser(uid, channelId);
  350. }
  351. else
  352. {
  353. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  354. }
  355. //videoSurface.OnTextureSizeModify += (int width, int height) =>
  356. //{
  357. // float scale = (float)height / (float)width;
  358. // videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  359. // Debug.Log("OnTextureSizeModify: " + width + " " + height);
  360. //};
  361. videoSurface.SetEnable(true);
  362. }
  363. //internal static void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  364. //{
  365. // Debug.Log(info.uid);
  366. // disUserPeer_Uid.Add("", info);
  367. //}
  368. internal static void OnUserJoined(uint uid)
  369. {
  370. // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  371. // Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed))
  372. Agora.Rtc.UserInfo userInfo = new Agora.Rtc.UserInfo();
  373. AgoraVideoAudioManager.Instance.RtcEngine.GetUserInfoByUid(uid, ref userInfo);
  374. }
  375. #region -- Video Render UI Logic ---
  376. // VIDEO TYPE 1: 3D Object
  377. private static VideoSurface MakePlaneSurface(string goName)
  378. {
  379. var go = GameObject.CreatePrimitive(PrimitiveType.Quad);
  380. //for (int i = 0; i < list_UserInfo.Count; i++)
  381. //{
  382. // if (list_UserInfo[i].uid.ToString() == goName)
  383. // {
  384. // string userAccount = list_UserInfo[i].userAccount;
  385. // if (AgoraVideoAudioManager.Instance.list_ShowView.ContainsKey(list_UserInfo[i].userAccount))
  386. // {
  387. // go = AgoraVideoAudioManager.Instance.list_ShowView[list_UserInfo[i].userAccount].gameObject;
  388. // }
  389. // else
  390. // Debug.LogError(" Agora ShowView is NULL ");
  391. // }
  392. //}
  393. if (go == null)
  394. {
  395. go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  396. return null;
  397. }
  398. go.name = goName;
  399. // set up transform
  400. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  401. var yPos = Random.Range(3.0f, 5.0f);
  402. var xPos = Random.Range(-2.0f, 2.0f);
  403. go.transform.position = Vector3.zero;
  404. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  405. // configure videoSurface
  406. var videoSurface = go.AddComponent<VideoSurface>();
  407. return videoSurface;
  408. }
  409. // Video TYPE 2: RawImage
  410. private static VideoSurface MakeImageSurface(RawImage rawImage)
  411. {
  412. //GameObject go = rawImage.gameObject;
  413. //if (go == null)
  414. //{
  415. // go = new GameObject();
  416. // //go.name = goName;
  417. // // to be renderered onto
  418. // go.AddComponent<RawImage>();
  419. // // set up transform
  420. // //go.transform.Rotate(0f, 0.0f, 180.0f);
  421. // //go.transform.localPosition = Vector3.zero;
  422. // //go.transform.localScale = new Vector3(2f, 3f, 1f);
  423. //}
  424. //if (go == null)
  425. //{
  426. // return null;
  427. //}
  428. // make the object draggable
  429. rawImage.gameObject.AddComponent<UIElementDrag>();
  430. //var canvas = GameObject.Find("VideoCanvas");
  431. //if (canvas != null)
  432. //{
  433. // go.transform.parent = canvas.transform;
  434. // Debug.Log("add video view");
  435. //}
  436. //else
  437. //{
  438. // Debug.Log("Canvas is null video view");
  439. //}
  440. // configure videoSurface
  441. var videoSurface = rawImage.gameObject.AddComponent<VideoSurface>();
  442. return videoSurface;
  443. }
  444. internal static void DestroyVideoView(uint uid)
  445. {
  446. var go = GameObject.Find(uid.ToString());
  447. if (!ReferenceEquals(go, null))
  448. {
  449. Destroy(go);
  450. }
  451. }
  452. internal static void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  453. {
  454. Debug.Log(uid.ToString() + " " + info.uid + " " + info.userAccount);
  455. // list_UserInfo.Add(info);
  456. }
  457. internal static void OnLocalUserRegistered(uint uid, string userAccount)
  458. {
  459. //AgoraVideoAudioManager.Instance.uid = uid;
  460. //AgoraVideoAudioManager.Instance.userAccount = userAccount;
  461. }
  462. #endregion
  463. }
  464. #region -- Agora Event ---
  465. public class AgoraVideoManagerHandler : IRtcEngineEventHandler
  466. {
  467. private readonly AgoraVideoAudioManager _videoSample;
  468. internal AgoraVideoManagerHandler(AgoraVideoAudioManager videoSample)
  469. {
  470. _videoSample = videoSample;
  471. }
  472. public override void OnError(int err, string msg)
  473. {
  474. _videoSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  475. }
  476. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  477. {
  478. int build = 0;
  479. Debug.Log("Agora: OnJoinChannelSuccess ");
  480. _videoSample.Log.UpdateLog(string.Format("sdk version: ${0}",
  481. _videoSample.RtcEngine.GetVersion(ref build)));
  482. _videoSample.Log.UpdateLog(string.Format("sdk build: ${0}",
  483. build));
  484. _videoSample.Log.UpdateLog(
  485. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  486. connection.channelId, connection.localUid, elapsed));
  487. // _videoSample.ClickSelf();
  488. // AgoraVideoAudioManager.MakeVideoView(0);
  489. }
  490. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  491. {
  492. _videoSample.Log.UpdateLog("OnRejoinChannelSuccess");
  493. }
  494. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  495. {
  496. _videoSample.Log.UpdateLog("OnLeaveChannel");
  497. AgoraVideoAudioManager.DestroyVideoView(0);
  498. }
  499. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  500. {
  501. _videoSample.Log.UpdateLog("OnClientRoleChanged");
  502. }
  503. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  504. {
  505. Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  506. _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  507. AgoraVideoAudioManager.OnUserJoined(uid);
  508. // AgoraVideoAudioManager.MakeVideoView(uid, _videoSample.GetChannelName());
  509. }
  510. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  511. {
  512. _videoSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  513. (int)reason));
  514. AgoraVideoAudioManager.DestroyVideoView(uid);
  515. }
  516. public override void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
  517. {
  518. _videoSample.Log.UpdateLog(string.Format(" 用户 :${0} 加入房间", uid));
  519. AgoraVideoAudioManager.OnUserInfoUpdated(uid, info);
  520. }
  521. public override void OnUplinkNetworkInfoUpdated(UplinkNetworkInfo info)
  522. {
  523. _videoSample.Log.UpdateLog("OnUplinkNetworkInfoUpdated");
  524. }
  525. public override void OnDownlinkNetworkInfoUpdated(DownlinkNetworkInfo info)
  526. {
  527. _videoSample.Log.UpdateLog("OnDownlinkNetworkInfoUpdated");
  528. }
  529. public override void OnLocalUserRegistered(uint uid, string userAccount)
  530. {
  531. AgoraVideoAudioManager.OnLocalUserRegistered(uid, userAccount);
  532. }
  533. }
  534. #endregion