AgoraVideoAudioManager.cs 28 KB

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