AgoraVideoAudioManager.cs 30 KB

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