PlayerViewComponent.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. using BeinLab.Util;
  2. using ShadowStudio.Mgr;
  3. using ShadowStudio.UI;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using XRTool.Util;
  10. using XRTool.WorldUI;
  11. using static BoundingBox;
  12. namespace ShadowStudio.Model
  13. {
  14. /// <summary>
  15. /// 用户的视频通话窗口
  16. /// </summary>
  17. public class PlayerViewComponent : UIComponent
  18. {
  19. public static string viewId = "ArtId_PlayerView";
  20. public static Dictionary<string, ArtContainerHandler> viewDic = new Dictionary<string, ArtContainerHandler>();
  21. private string peerid;
  22. private bool isInit = false;
  23. private Peer _peer;
  24. public static event Action<string, bool> ViewChange;
  25. /// <summary>
  26. /// 创建一个视频通话窗口
  27. /// 即时同步
  28. /// </summary>
  29. /// <param name="peerid">点击的用户的id</param>
  30. /// <param name="target"></param>
  31. public static void CreateViewComponent(string peerid, Transform target, bool isDel = false)
  32. {
  33. if (viewDic.ContainsKey(peerid))
  34. {
  35. return;
  36. }
  37. GoodsInfo data = new GoodsInfo();
  38. var info = ArtInfoMgr.Instance.GetArtInfo(viewId);
  39. data.art_id = viewId;
  40. data.id = UnityEngine.Random.Range(100000, 999999999);
  41. if (target)
  42. {
  43. data.goods_info = UnityUtil.TransferToString(target, 2);
  44. }
  45. else
  46. {
  47. data.goods_info = UnityUtil.ArtTransferInfo(info);
  48. }
  49. data.goods_name = peerid;
  50. data.rid = int.Parse(UserInfoMgr.Instance.userConfig.Id);
  51. data.scene_id = -1;
  52. ///创建本地窗口
  53. ArtInfoMgr.Instance.SyncGoods(data);
  54. ///
  55. ///ArtInfoMgr.Instance.SendTransfer(data);
  56. if (target && isDel)
  57. {
  58. Destroy(target.gameObject);
  59. }
  60. }
  61. private bool checkRtc()
  62. {
  63. if (_peer != null && StudioRtc.Instance != null && StudioRtc.Instance.customPeerList != null)
  64. {
  65. cPeer = (CustomPeer)StudioRtc.Instance.customPeerList.getPeerName(_peer.PeerId);
  66. if (cPeer != null)
  67. {
  68. // Debug.Log("cPeer Not NULL" + _peer.PeerId);
  69. cPeer.onChangeInfo -= OnChangeInfo;
  70. cPeer.onChangeInfo += OnChangeInfo;
  71. if (!_peer.IsSlef)
  72. {
  73. if (cPeer.isAudio && cPeer.cIdA != "")
  74. {
  75. openAudioEffect();
  76. }
  77. else
  78. {
  79. closeAudioEffect();
  80. }
  81. if (cPeer.isVideo && cPeer.cIdV != "")
  82. {
  83. openVideoEffect();
  84. }
  85. else
  86. {
  87. closeVideoEffect();
  88. }
  89. }
  90. }
  91. if (cPeer != null)
  92. {
  93. return true;
  94. }
  95. }
  96. // Debug.Log("cPeer is NULL" + _peer.PeerId);
  97. return false;
  98. }
  99. void StateUpdata()
  100. {
  101. if (cPeer == null)
  102. return;
  103. if (cPeer.isVideo)
  104. {
  105. if (cPeer.isCloseVideo)
  106. {
  107. openVideo.SetActive(false);
  108. closeVideo.SetActive(false);
  109. pauseVideo.SetActive(true);
  110. VideoEmpty.gameObject.SetActive(true);
  111. }
  112. else
  113. {
  114. openVideo.SetActive(false);
  115. closeVideo.SetActive(true);
  116. pauseVideo.SetActive(false);
  117. VideoEmpty.gameObject.SetActive(false);
  118. }
  119. }
  120. else
  121. {
  122. openVideo.SetActive(true);
  123. closeVideo.SetActive(false);
  124. pauseVideo.SetActive(false);
  125. VideoEmpty.gameObject.SetActive(true);
  126. }
  127. if (cPeer.isAudio)
  128. {
  129. if (cPeer.isCloseAudio)
  130. {
  131. openAudio.SetActive(false);
  132. closeAudio.SetActive(false);
  133. pauseAudio.SetActive(true);
  134. }
  135. else
  136. {
  137. openAudio.SetActive(false);
  138. closeAudio.SetActive(true);
  139. pauseAudio.SetActive(false);
  140. }
  141. }
  142. else
  143. {
  144. openAudio.SetActive(true);
  145. closeAudio.SetActive(false);
  146. pauseAudio.SetActive(false);
  147. }
  148. }
  149. public float volume = 0.2f;
  150. public Text textMic;
  151. public GameObject pauseCerAudio;
  152. public GameObject pauseCerVideo;
  153. public GameObject pauseAudio;
  154. public GameObject pauseVideo;
  155. public GameObject openVideo;
  156. public GameObject closeVideo;
  157. public MeshRenderer videoBG;
  158. public Text nametext;
  159. public XRIcon touxiang;
  160. public GameObject openAudio;
  161. public GameObject closeAudio;
  162. public GameObject VolumnGo;
  163. public RawImage agoraShowVideo;
  164. private void closeVideoEffect()
  165. {
  166. if(openVideo)
  167. {
  168. openVideo.SetActive(true);
  169. closeVideo.SetActive(false);
  170. VideoEmpty.gameObject.SetActive(true);
  171. if (cPeer != null)
  172. {
  173. pauseCerVideo.SetActive(true);
  174. pauseVideo.SetActive(false);
  175. }
  176. if (peerid == CommonMethod.MyPeer.PeerId)
  177. AgoraVideoAudioManager.Instance.EnableLocalVideo(false);
  178. }
  179. }
  180. private void openVideoEffect()
  181. {
  182. openVideo.SetActive(false);
  183. closeVideo.SetActive(true);
  184. if (cPeer != null)
  185. {
  186. ShowVideo.material.mainTexture = cPeer.tex;
  187. if (cPeer.isCloseVideo)
  188. {
  189. pauseCerVideo.SetActive(false);
  190. pauseVideo.SetActive(true);
  191. }
  192. }
  193. VideoEmpty.gameObject.SetActive(false);
  194. if (peerid == CommonMethod.MyPeer.PeerId)
  195. AgoraVideoAudioManager.Instance.EnableLocalVideo(true);
  196. }
  197. private void closeAudioEffect()
  198. {
  199. pauseCerAudio.SetActive(true);
  200. pauseAudio.SetActive(false);
  201. openAudio.SetActive(true);
  202. closeAudio.SetActive(false);
  203. VolumnGo.SetActive(false);
  204. if (peerid == CommonMethod.MyPeer.PeerId)
  205. AgoraVideoAudioManager.Instance.EnableLoacalAudio(false);
  206. //else
  207. // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(peerid, false);
  208. }
  209. private void openAudioEffect()
  210. {
  211. openAudio.SetActive(false);
  212. closeAudio.SetActive(true);
  213. VolumnGo.SetActive(true);
  214. if (cPeer != null)
  215. {
  216. volume = cPeer.volumn;
  217. if (cPeer.isCloseAudio)
  218. {
  219. pauseCerAudio.SetActive(false);
  220. pauseAudio.SetActive(true);
  221. }
  222. }
  223. textMic.text = volume.ToString("F2");
  224. if (peerid == CommonMethod.MyPeer.PeerId)
  225. AgoraVideoAudioManager.Instance.EnableLoacalAudio(true);
  226. //else
  227. // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(peerid, true);
  228. }
  229. private void OnChangeInfo(string type, string id)
  230. {
  231. if (type == "video")
  232. {
  233. if (cPeer.isVideo)
  234. {
  235. openVideoEffect();
  236. if (cPeer.isCloseVideo)
  237. {
  238. StudioRtc.Instance.closeRev(cPeer.cIdV);
  239. }
  240. }
  241. else
  242. {
  243. closeVideoEffect();
  244. }
  245. }
  246. else
  247. {
  248. if (cPeer.isAudio)
  249. {
  250. openAudioEffect();
  251. if (cPeer.isCloseAudio)
  252. {
  253. StudioRtc.Instance.closeRev(cPeer.cIdA);
  254. }
  255. }
  256. else
  257. {
  258. closeAudioEffect();
  259. }
  260. }
  261. }
  262. private void Update()
  263. {
  264. if (checkRtc() && !isRtcInit)
  265. {
  266. ShowVideo.material.mainTexture = cPeer.tex;
  267. isRtcInit = true;
  268. }
  269. StateUpdata();
  270. }
  271. bool isRtcInit = false;
  272. CustomPeer cPeer;
  273. public void setVideo(string peerid)
  274. {
  275. _peer = UserItemList.Instance.GetPeer(peerid);
  276. nametext.text = _peer.NickName;
  277. if (peerid == CommonMethod.MyPeer.PeerId)
  278. {
  279. if (StudioRtc.Instance.me.cIdA != "")
  280. {
  281. openAudioEffect();
  282. }
  283. else
  284. {
  285. closeAudioEffect();
  286. }
  287. if (StudioRtc.Instance.me.cIdV != "")
  288. {
  289. openVideoEffect();
  290. }
  291. else
  292. {
  293. closeVideoEffect();
  294. }
  295. ShowVideo.material.mainTexture = StudioRtc.Instance.rt;
  296. isRtcInit = true;
  297. return;
  298. }
  299. else
  300. {
  301. Debug.Log(" Peerid != MyPeer" + peerid);
  302. }
  303. if (checkRtc())
  304. {
  305. ShowVideo.material.mainTexture = cPeer.tex;
  306. }
  307. /*
  308. if (peerid == CommonMethod.MyPeer.PeerId)
  309. {
  310. ShowVideo.material.mainTexture = ConsoleDlg.Instance.vufora_camera.GetComponent<Camera>().targetTexture;
  311. return;
  312. }
  313. peer = UserItemList.Instance.GetPeer(peerid);
  314. if (peer != null)
  315. {
  316. ShowVideo.material.mainTexture = peer.CustomPeer.tex;
  317. }
  318. else
  319. {
  320. UnityLog.Instance.LogError("Cant Find peerid" + peerid);
  321. }
  322. */
  323. }
  324. public void addMic()
  325. {
  326. volume += 0.1f;
  327. if (_peer.IsSlef)
  328. {
  329. StudioRtc.Instance.setVolume(volume);
  330. }
  331. else
  332. {
  333. if (cPeer != null && cPeer.cIdA != "")
  334. {
  335. StudioRtc.Instance.setVolume(cPeer.cIdA, volume);
  336. }
  337. }
  338. textMic.text = volume.ToString("F2");
  339. }
  340. public void redMic()
  341. {
  342. volume -= 0.1f;
  343. if (_peer.IsSlef)
  344. {
  345. StudioRtc.Instance.setVolume(volume);
  346. }
  347. else
  348. {
  349. if (cPeer != null && cPeer.cIdA != "")
  350. {
  351. StudioRtc.Instance.setVolume(cPeer.cIdA, volume);
  352. }
  353. }
  354. textMic.text = volume.ToString("F2");
  355. }
  356. public void closeMic()
  357. {
  358. if (_peer.IsSlef)
  359. {
  360. if (StudioRtc.Instance.me.cIdA != "")
  361. {
  362. WSHandler.Rtc.closeProducer(StudioRtc.Instance.me.cIdA);
  363. StudioRtc.Instance.sendMic(false);
  364. closeAudioEffect();
  365. StudioRtc.Instance.me.cIdA = "";
  366. }
  367. }
  368. else
  369. {
  370. if (cPeer != null && cPeer.isAudio && cPeer.cIdA != "" && !cPeer.isCloseAudio)
  371. {
  372. pauseCerAudio.SetActive(false);
  373. pauseAudio.SetActive(true);
  374. StudioRtc.Instance.closeRev(cPeer.cIdA);
  375. cPeer.isCloseAudio = true;
  376. // cPeer.setInfo("audio", cPeer.cIdA, false);
  377. //AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(peerid, true);
  378. }
  379. AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(cPeer.name, false);
  380. }
  381. }
  382. public void openMic()
  383. {
  384. if (_peer.IsSlef)
  385. {
  386. StudioRtc.Instance.sendMic(true);
  387. volume = StudioRtc.Instance.me.volumn;
  388. openAudioEffect();
  389. }
  390. else
  391. {
  392. if (cPeer != null && cPeer.isAudio && cPeer.cIdA != "" && cPeer.isCloseAudio)
  393. {
  394. pauseCerAudio.SetActive(true);
  395. pauseAudio.SetActive(false);
  396. StudioRtc.Instance.startRev(cPeer.cIdA);
  397. cPeer.isCloseAudio = false;
  398. // cPeer.setInfo("audio", cPeer.cIdA,true);
  399. // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(peerid, false);
  400. }
  401. AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(cPeer.name, true);
  402. }
  403. }
  404. public void OpenVideo()
  405. {
  406. agoraShowVideo.gameObject.SetActive(true);
  407. if (_peer.IsSlef)
  408. {
  409. StudioRtc.Instance.startCamera();
  410. // videoBG.gameObject.SetActive(true);
  411. //videoBG.material.mainTexture = StudioRtc.Instance.rt;
  412. StudioRtc.Instance.sendVideo(true);
  413. openVideoEffect();
  414. }
  415. else
  416. {
  417. if (cPeer != null && cPeer.isVideo && cPeer.cIdV != "" && cPeer.isCloseVideo)
  418. {
  419. pauseCerVideo.SetActive(true);
  420. pauseVideo.SetActive(false);
  421. StudioRtc.Instance.startRev(cPeer.cIdV);
  422. cPeer.isCloseVideo = false;
  423. // cPeer.setInfo("audio", cPeer.cIdA,true);
  424. }
  425. AgoraVideoAudioManager.Instance.MuteRemoteVideoStream(cPeer.name, true);
  426. }
  427. }
  428. public void CloseVideo()
  429. {
  430. agoraShowVideo.gameObject.SetActive(false);
  431. if (_peer.IsSlef)
  432. {
  433. if (StudioRtc.Instance.me.cIdV != "")
  434. {
  435. WSHandler.Rtc.closeProducer(StudioRtc.Instance.me.cIdV);
  436. StudioRtc.Instance.stopCamera();
  437. StudioRtc.Instance.sendVideo(false);
  438. closeVideoEffect();
  439. StudioRtc.Instance.me.cIdV = "";
  440. }
  441. }
  442. else
  443. {
  444. if (cPeer != null && cPeer.isVideo && cPeer.cIdV != "" && !cPeer.isCloseVideo)
  445. {
  446. pauseCerVideo.SetActive(false);
  447. pauseVideo.SetActive(true);
  448. StudioRtc.Instance.closeRev(cPeer.cIdV);
  449. cPeer.isCloseVideo = true;
  450. // cPeer.setInfo("audio", cPeer.cIdA,true);
  451. }
  452. AgoraVideoAudioManager.Instance.MuteRemoteVideoStream(cPeer.name, false);
  453. }
  454. }
  455. private Button closeBtn;
  456. public Button CloseBtn
  457. {
  458. get
  459. {
  460. if (!closeBtn)
  461. {
  462. closeBtn = UnityUtil.GetBreadthChild<Button>(transform, "CloseBtn");
  463. }
  464. return closeBtn;
  465. }
  466. }
  467. private MeshRenderer showVideo;
  468. public MeshRenderer ShowVideo
  469. {
  470. get
  471. {
  472. if (!showVideo)
  473. {
  474. showVideo = UnityUtil.GetBreadthChild<MeshRenderer>(transform, "ShowVideo");
  475. }
  476. return showVideo;
  477. }
  478. }
  479. private MeshRenderer videoEmpty;
  480. public MeshRenderer VideoEmpty
  481. {
  482. get
  483. {
  484. if (!videoEmpty)
  485. {
  486. videoEmpty = UnityUtil.GetBreadthChild<MeshRenderer>(transform, "VideoEmpty");
  487. }
  488. return videoEmpty;
  489. }
  490. }
  491. private void OnDestroy()
  492. {
  493. ViewChange?.Invoke(peerid, false);
  494. if (!string.IsNullOrEmpty(peerid) && viewDic.ContainsKey(peerid))
  495. {
  496. viewDic.Remove(peerid);
  497. }
  498. if (cPeer != null)
  499. cPeer.onChangeInfo -= OnChangeInfo;
  500. }
  501. private void Start()
  502. {
  503. if (CloseBtn)
  504. {
  505. //CloseBtn.onClick.AddListener(OnClickClose);
  506. }
  507. if (ArtInfoMgr.Instance != null)
  508. {
  509. ArtInfoMgr.Instance.UserChange += OnUserChange;
  510. }
  511. XBoundingBox boxb= this.GetComponent<XBoundingBox>();
  512. if(boxb)
  513. boxb.ActiveAxis= AxisType.None;
  514. AgoraVideoAudioManager.Instance.AddListShowView(peerid, agoraShowVideo);
  515. }
  516. /// <summary>
  517. /// 用户状态发生改变的时候
  518. /// </summary>
  519. /// <param name="peerId"></param>
  520. /// <param name="isJoin"></param>
  521. private void OnUserChange(string peerId, bool isJoin)
  522. {
  523. ///用户视频列表被创建,但用户退出
  524. ///删除用户视频列表
  525. if (viewDic.ContainsKey(peerId))
  526. {
  527. if (peerId == this.peerid && !isJoin)
  528. {
  529. OnClickClose();
  530. }
  531. }
  532. else
  533. {
  534. ///用户加入
  535. if (isJoin)
  536. {
  537. base.SendTransfer(2, 0);
  538. TimerMgr.Instance.CreateTimer(() =>
  539. {
  540. base.SendTransfer(2, 0);
  541. }, 3f);
  542. }
  543. }
  544. }
  545. /// <summary>
  546. /// 点击关闭按钮
  547. /// 删除此视频通话窗口
  548. /// 如何删除?发送额外消息
  549. /// 删除本地
  550. /// </summary>
  551. public void OnClickClose()
  552. {
  553. if (Container)
  554. {
  555. Container.data.goods_extended = "DES";
  556. base.SendTransfer(2, 0);
  557. }
  558. if (cPeer != null)
  559. cPeer.onChangeInfo -= OnChangeInfo;
  560. DestroyArt();
  561. }
  562. public override void DestroyArt()
  563. {
  564. base.DestroyArt();
  565. }
  566. public override void SendTransfer(int state, int status)
  567. {
  568. base.SendTransfer(state, status);
  569. //if (Container is WorldDlgContainer)
  570. //{
  571. // TimerMgr.Instance.CreateTimer(()=>
  572. // {
  573. // (Container as WorldDlgContainer).OnTransferSelect(false);
  574. // },0.02f);
  575. //}
  576. }
  577. /// <summary>
  578. /// 设置容器
  579. /// </summary>
  580. /// <param name="container"></param>
  581. /// <param name="art"></param>
  582. /// <param name="info"></param>
  583. public override void SetContainer(ArtContainerHandler container, UnityEngine.Object art, ArtInfo info)
  584. {
  585. base.SetContainer(container, art, info);
  586. RectTransform rt = GetComponent<RectTransform>();
  587. rt.offsetMax = Vector2.zero;
  588. rt.offsetMin = Vector2.zero;
  589. }
  590. public override void TransferSyn(GoodsInfo goods, bool smooth = true)
  591. {
  592. base.TransferSyn(goods, smooth);
  593. if (!isInit)
  594. {
  595. if (!viewDic.ContainsKey(goods.goods_name))
  596. {
  597. this.peerid = goods.goods_name;
  598. viewDic.Add(goods.goods_name, Container);
  599. setVideo(peerid);
  600. ///自身点击的,需要发送同步一次
  601. if (goods.rid.ToString() == UserInfoMgr.Instance.userConfig.Id)
  602. {
  603. SendTransfer(2, 0);
  604. }
  605. ViewChange?.Invoke(goods.goods_name, true);
  606. isInit = true;
  607. }
  608. }
  609. if (!string.IsNullOrEmpty(goods.goods_extended))
  610. {
  611. if (goods.goods_extended == "DES")
  612. {
  613. DestroyArt();
  614. }
  615. }
  616. }
  617. }
  618. }