ConsultationClientV2.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.Json;
  5. using Vinno.IUS.Common.Log;
  6. using Vinno.vCloud.Common.FIS.Helper;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common;
  8. using Vinno.vCloud.FIS.CrossPlatform.Common.Consultation;
  9. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  10. using Vinno.vCloud.Protocol.Infrastructures;
  11. using WingInterfaceLibrary.Interface;
  12. using WingInterfaceLibrary.LiveConsultation;
  13. namespace Vinno.vCloud.Common.FIS.Consultation
  14. {
  15. internal abstract class ConsultationClientV2 : IDisposable
  16. {
  17. private ConsultationInfo _consultationInfo;
  18. private double _usInkCanvasWidth = 800;
  19. private double _usInkCanvasHeight = 600;
  20. private double _flyinsonoWidth = 10000;
  21. private double _flyinsonoHeight = 10000;
  22. private List<string> _muteVideoUserList;
  23. private readonly object _interactiveBoardLocker = new object();
  24. protected Dictionary<string, List<StrokePointList>> StrokeDict = new Dictionary<string, List<StrokePointList>>();
  25. protected readonly object ConsultationInfoLocker = new object();
  26. protected readonly ILiveConsultationService LiveConsultationService;
  27. protected readonly IUserService UserService;
  28. protected readonly IDeviceService DeviceService;
  29. protected string AccountId;
  30. protected string AccountToken;
  31. protected string AccountName;
  32. protected bool IsOnlyForRtmpPushing;
  33. protected int count = 0;
  34. protected ConsultationLiveVideoProviderV2 VideoProvider { get; }
  35. protected ConsultationManagerV2 ConsultationManager { get; }
  36. public bool IsSecondViewMode { get; protected set; }
  37. /// <summary>
  38. /// Gets or sets the current live status
  39. /// </summary>
  40. public LiveStates CurrentLiveStatus { get; set; }
  41. /// <summary>
  42. /// Raised when meeting disconnected.
  43. /// </summary>
  44. public event EventHandler<ConsultationDisconnectedType> ConsultationDisconnected;
  45. /// <summary>
  46. /// Raised when meeting member changed
  47. /// </summary>
  48. public event EventHandler<ConsultationMemberNotificaiton> ConsultationMemberChangedNotificationArrived;
  49. /// <summary>
  50. /// 接收到其他用户的白板信息
  51. /// </summary>
  52. public event EventHandler<InteractiveBoardInfo> InteractiveBoardInfoArrived;
  53. /// <summary>
  54. /// 接收到清除白板信息
  55. /// </summary>
  56. public event EventHandler<string> ClearInteractiveBoardArrived;
  57. public event EventHandler<List<string>> MuteVideoUserListNotifyArrived;
  58. public ConsultationInfo ConsultationInfo
  59. {
  60. get => _consultationInfo;
  61. set
  62. {
  63. if (_consultationInfo != value)
  64. {
  65. _consultationInfo = value;
  66. count = 0;
  67. }
  68. }
  69. }
  70. protected ConsultationClientV2(ConsultationLiveVideoProviderV2 videoProvider, ConsultationManagerV2 consultationManager, ILiveConsultationService liveConsultationService, IUserService userService, IDeviceService deviceService)
  71. {
  72. _muteVideoUserList = new List<string>();
  73. LiveConsultationService = liveConsultationService;
  74. UserService = userService;
  75. DeviceService = deviceService;
  76. CurrentLiveStatus = LiveStates.Idle;
  77. ConsultationManager = consultationManager;
  78. VideoProvider = videoProvider;
  79. VideoProvider.RemoteUserLeaveRoomArrived += OnRemoteUserLeaveRoomArrived;
  80. VideoProvider.CloseConsultation += OnCloseMeeting;
  81. VideoProvider.TryToReconnect += OnTryToReconnect;
  82. }
  83. protected void OnTryToReconnect(object sender, EventArgs e)
  84. {
  85. }
  86. private void OnRemoteUserLeaveRoomArrived(object sender, RemoteUserLeaveRoomArgs e)
  87. {
  88. }
  89. private void OnCloseMeeting(object sender, EventArgs e)
  90. {
  91. OnDisconnected(ConsultationDisconnectedType.SelfClientOffline);
  92. }
  93. protected void OnDisconnected(ConsultationDisconnectedType e)
  94. {
  95. try
  96. {
  97. StopConsultation();
  98. }
  99. catch (Exception ex)
  100. {
  101. Logger.WriteLineError($"Consultationclient- error happended while disconnectting: {ex}");
  102. }
  103. finally
  104. {
  105. if (!IsOnlyForRtmpPushing)
  106. {
  107. ConsultationDisconnected?.Invoke(this, e);
  108. }
  109. lock (ConsultationInfoLocker)
  110. {
  111. Logger.WriteLineInfo($"Current consultation info is set to null on reson: {e}");
  112. ConsultationInfo = null;
  113. }
  114. lock (_interactiveBoardLocker)
  115. {
  116. StrokeDict.Clear();
  117. }
  118. CurrentLiveStatus = LiveStates.Disconnected;
  119. }
  120. }
  121. protected void StopConsultation()
  122. {
  123. var isMeetting = CurrentLiveStatus == LiveStates.RecipientAcceptted || CurrentLiveStatus == LiveStates.RecipientAcceptting;
  124. if (isMeetting)
  125. {
  126. VideoProvider.Hangup();
  127. CurrentLiveStatus = LiveStates.Idle;
  128. }
  129. else
  130. {
  131. Logger.WriteLineInfo($"Don't do stop consultation video since no rtc consultation is not started");
  132. }
  133. }
  134. protected void StartConsultation(string cameraHardwareId, string micHardwareId, string speakerHardwareId, bool isOnlyForRtmpPushing)
  135. {
  136. try
  137. {
  138. VideoProvider.StartConsultationVideo(ConsultationInfo, AccountId, AccountName, AccountToken, cameraHardwareId, micHardwareId, speakerHardwareId, isOnlyForRtmpPushing);
  139. }
  140. catch (Exception ex)
  141. {
  142. Logger.WriteLineError($"Error happened while start meeting {ex}");
  143. }
  144. }
  145. public ResultInfoDTO Hangup(bool isFakeHangup = false)
  146. {
  147. try
  148. {
  149. if (ConsultationInfo == null)
  150. {
  151. Logger.WriteLineWarn($"ConsultationHangup Fail: Current is not Consultating");
  152. return new ResultInfoDTO
  153. {
  154. IsSuccess = false,
  155. FailMessage = "ConsultationHangup Fail: Current is not Consultating"
  156. };
  157. }
  158. if (isFakeHangup)
  159. {
  160. lock (ConsultationInfoLocker)
  161. {
  162. ConsultationInfo = null;
  163. _muteVideoUserList.Clear();
  164. }
  165. lock (_interactiveBoardLocker)
  166. {
  167. StrokeDict.Clear();
  168. }
  169. return new ResultInfoDTO
  170. {
  171. IsSuccess = true,
  172. };
  173. }
  174. var leaveLiveConsultationRequest = new LeaveLiveConsultationRequest
  175. {
  176. ConsultationCode = ConsultationInfo?.ConsultationId,
  177. Token = AccountToken,
  178. };
  179. var leaveResult = JsonRpcHelper.LeaveLiveConsultation(LiveConsultationService, leaveLiveConsultationRequest);
  180. if (leaveResult == null || leaveResult.ConsultationCode != ConsultationInfo?.ConsultationId)
  181. {
  182. Logger.WriteLineError($"Hangup Fail, leaveResult.ConsultationCode is {leaveResult?.ConsultationCode}");
  183. }
  184. lock (ConsultationInfoLocker)
  185. {
  186. ConsultationInfo = null;
  187. _muteVideoUserList.Clear();
  188. }
  189. lock (_interactiveBoardLocker)
  190. {
  191. StrokeDict.Clear();
  192. }
  193. StopConsultation();
  194. CurrentLiveStatus = LiveStates.ChatHangup;
  195. Logger.WriteLineInfo($"ConsultationLive-- {AccountName} hangup end with result {CurrentLiveStatus}");
  196. return new ResultInfoDTO
  197. {
  198. IsSuccess = true,
  199. };
  200. }
  201. catch (Exception ex)
  202. {
  203. Logger.WriteLineWarn($"ConsultationHangup error: {ex}");
  204. return new ResultInfoDTO
  205. {
  206. IsSuccess = false,
  207. FailMessage = $"ConsultationHangup Fail: {ex}"
  208. };
  209. }
  210. finally
  211. {
  212. IsOnlyForRtmpPushing = false;
  213. IsSecondViewMode = false;
  214. }
  215. }
  216. protected virtual void HandleConsultationMemberNotificationArrived(ConsultationMemberNotificaiton consultationMemberNotificaiton)
  217. {
  218. ConsultationMemberChangedNotificationArrived?.Invoke(this, consultationMemberNotificaiton);
  219. }
  220. protected void ConsultationMemberChange(ConsultationMemberChangeDTO meetingChangeMember)
  221. {
  222. VideoProvider.ConsultationMemberChange(meetingChangeMember);
  223. }
  224. internal ResultInfoDTO ReceiveWhiteBoardData(WhiteBoardDataDTO whiteBoardDataDTO)
  225. {
  226. if (whiteBoardDataDTO == null)
  227. {
  228. Logger.WriteLineError($"ReceiveWhiteBoardData Error:Args is invalid");
  229. return new ResultInfoDTO
  230. {
  231. IsSuccess = false,
  232. FailMessage = "Args is invalid",
  233. };
  234. }
  235. if (string.IsNullOrEmpty(whiteBoardDataDTO.UserId))
  236. {
  237. Logger.WriteLineError($"ReceiveWhiteBoardData Error:Args:UserId is empty");
  238. return new ResultInfoDTO
  239. {
  240. IsSuccess = false,
  241. FailMessage = "Args:UserId is empty",
  242. };
  243. }
  244. if (whiteBoardDataDTO.IsClear == "true")
  245. {
  246. lock (_interactiveBoardLocker)
  247. {
  248. if (StrokeDict.ContainsKey(whiteBoardDataDTO.UserId))
  249. {
  250. StrokeDict.Remove(whiteBoardDataDTO.UserId);
  251. }
  252. InteractiveBoardInfoArrived?.Invoke(this, new InteractiveBoardInfo(whiteBoardDataDTO.UserId, AccountId, null, ""));
  253. }
  254. return new ResultInfoDTO
  255. {
  256. IsSuccess = true
  257. };
  258. }
  259. else
  260. {
  261. if (uint.TryParse(whiteBoardDataDTO.Color, out var color))
  262. {
  263. if (whiteBoardDataDTO.Points.Length % 8 != 0)
  264. {
  265. Logger.WriteLineError($"ReceiveWhiteBoardData Error:Args:Point is invalid,is not divisible by 8");
  266. return new ResultInfoDTO
  267. {
  268. IsSuccess = false,
  269. FailMessage = "Args:Point is invalid,is not divisible by 8",
  270. };
  271. }
  272. List<StrokePointList> strokePointLists;
  273. var strokePointList = ConvertToSrokePointList(whiteBoardDataDTO.Points);
  274. if (strokePointList != null)
  275. {
  276. lock (_interactiveBoardLocker)
  277. {
  278. if (StrokeDict.ContainsKey(whiteBoardDataDTO.UserId))
  279. {
  280. StrokeDict[whiteBoardDataDTO.UserId].Add(strokePointList);
  281. }
  282. else
  283. {
  284. StrokeDict[whiteBoardDataDTO.UserId] = new List<StrokePointList>
  285. {
  286. strokePointList
  287. };
  288. }
  289. strokePointLists = StrokeDict[whiteBoardDataDTO.UserId].ToList();
  290. }
  291. if (CrossPlatformHelper.Instance.Platform == EnumPlatform.Windows)
  292. {
  293. var jsonData = CrossPlatformHelper.Instance.StrokeHelper.GetStrokeJson(strokePointLists, color);
  294. InteractiveBoardInfoArrived?.Invoke(this, new InteractiveBoardInfo(whiteBoardDataDTO.UserId, AccountId, null, jsonData));
  295. return new ResultInfoDTO
  296. {
  297. IsSuccess = true,
  298. };
  299. }
  300. else
  301. {
  302. var strokePointsInfo = new StrokePointsInfo(color, strokePointLists);
  303. var jsonData = JsonSerializer.Serialize(strokePointsInfo);
  304. InteractiveBoardInfoArrived?.Invoke(this, new InteractiveBoardInfo(whiteBoardDataDTO.UserId, AccountId, null, jsonData));
  305. return new ResultInfoDTO
  306. {
  307. IsSuccess = true,
  308. };
  309. }
  310. }
  311. else
  312. {
  313. Logger.WriteLineError($"ReceiveWhiteBoardData Error:Args is invalid");
  314. return new ResultInfoDTO
  315. {
  316. IsSuccess = false,
  317. FailMessage = "Args is invalid",
  318. };
  319. }
  320. }
  321. else
  322. {
  323. Logger.WriteLineError($"ReceiveWhiteBoardData Error:Args:Color is invalid");
  324. return new ResultInfoDTO
  325. {
  326. IsSuccess = false,
  327. FailMessage = "Args:Color is invalid",
  328. };
  329. }
  330. }
  331. }
  332. private StrokePointList ConvertToSrokePointList(string pointstring)
  333. {
  334. try
  335. {
  336. List<StrokePoint> pointsList = new List<StrokePoint>();
  337. for (int i = 0; i < pointstring.Length; i += 8)
  338. {
  339. var xString = pointstring.Substring(i, 4);
  340. var yString = pointstring.Substring(i + 4, 4);
  341. var xBeforeAdjust = double.Parse(xString);
  342. var yBeforeAdjust = double.Parse(yString);
  343. var xAfterAdjust = xBeforeAdjust / _flyinsonoWidth * _usInkCanvasWidth;
  344. var yAfterAdjust = yBeforeAdjust / _flyinsonoHeight * _usInkCanvasHeight;
  345. pointsList.Add(new StrokePoint(xAfterAdjust, yAfterAdjust));
  346. }
  347. return new StrokePointList() { StrokePoints = pointsList };
  348. }
  349. catch (Exception ex)
  350. {
  351. Logger.WriteLineError($"ConvertToSrokePointList Error:{ex}");
  352. return null;
  353. }
  354. }
  355. internal ResultInfoDTO SwitchVideoPlay(string userId, bool isVideoOpen)
  356. {
  357. var existUser = ConsultationInfo?.ConsultationMemberInfos?.FirstOrDefault(x => x.Id == userId);
  358. if (existUser != null)
  359. {
  360. if (isVideoOpen)
  361. {
  362. if (_muteVideoUserList.Contains(userId))
  363. {
  364. _muteVideoUserList.Remove(userId);
  365. MuteVideoUserListNotifyArrived?.Invoke(this, _muteVideoUserList.ToList());
  366. }
  367. }
  368. else
  369. {
  370. if (!_muteVideoUserList.Contains(userId))
  371. {
  372. _muteVideoUserList.Add(userId);
  373. MuteVideoUserListNotifyArrived?.Invoke(this, _muteVideoUserList.ToList());
  374. }
  375. }
  376. }
  377. return new ResultInfoDTO
  378. {
  379. IsSuccess = true,
  380. };
  381. }
  382. public virtual void Dispose()
  383. {
  384. VideoProvider.RemoteUserLeaveRoomArrived -= OnRemoteUserLeaveRoomArrived;
  385. VideoProvider.CloseConsultation -= OnCloseMeeting;
  386. VideoProvider.TryToReconnect -= OnTryToReconnect;
  387. }
  388. }
  389. }