ConsultationV2.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using JsonRpcLite.Rpc;
  2. using System;
  3. using System.Collections.Generic;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.vCloud.Common.FIS.Remedicals;
  6. using Vinno.vCloud.FIS.CrossPlatform.Common;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  8. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  9. using Vinno.vCloud.Protocol.Infrastructures;
  10. using WingInterfaceLibrary.DTO.Device;
  11. using WingInterfaceLibrary.Interface;
  12. using WingInterfaceLibrary.LiveConsultation;
  13. namespace Vinno.vCloud.Common.FIS.Consultation
  14. {
  15. internal class ConsultationV2 : IConsultationV2
  16. {
  17. private readonly string _deviceId;
  18. private readonly JsonRpcClient _client;
  19. private readonly string _deviceToken;
  20. private bool _disposed;
  21. public string ConsultationUrl { get; private set; }
  22. /// <summary>
  23. /// device image frame arrived
  24. /// </summary>
  25. public event EventHandler<CPVideoFrameData> UltrasoundImageFrameArrived;
  26. /// <summary>
  27. /// device camera frame arrived
  28. /// </summary>
  29. public event EventHandler<CPVideoFrameData> UltrasoundCameraFrameArrived;
  30. /// <summary>
  31. /// Local camera video frame arrived only for consultation
  32. /// </summary>
  33. public event EventHandler<ConsultationVideoFrameData> ConsultationLocalVideoFrameArrived;
  34. /// <summary>
  35. /// Remote camera frame arrived only for consultation
  36. /// </summary>
  37. public event EventHandler<ConsultationVideoFrameData> ConsultationRemoteVideoFrameArrived;
  38. /// <summary>
  39. /// Raised when receive a consultation request from server.
  40. /// </summary>
  41. public event EventHandler<ConsultationInfo> ConsultationRequestArrived;
  42. /// <summary>
  43. /// Raised when the consultation is disconnect.
  44. /// </summary>
  45. public event EventHandler<ConsultationDisconnectedType> ConsultationDisconnected;
  46. /// <summary>
  47. /// Raised when consultation Member Changed
  48. /// </summary>
  49. public event EventHandler<ConsultationMemberNotificaiton> ConsultationMemberChangedNotificationArrived;
  50. /// <summary>
  51. /// The consultation recipient
  52. /// </summary>
  53. public ConsultationRecipientV2 ConsultationRecipient { get; }
  54. /// <summary>
  55. /// Chat live video provider, handle different video
  56. /// </summary>
  57. public ConsultationLiveVideoProviderV2 ConsultationLiveVideoProvider { get; }
  58. public ConsultationManagerV2 ConsultationManager { get; }
  59. /// <summary>
  60. /// 接收到其他用户的白板信息
  61. /// </summary>
  62. public event EventHandler<InteractiveBoardInfo> InteractiveBoardInfoArrived;
  63. /// <summary>
  64. /// 接收到清除白板信息
  65. /// </summary>
  66. public event EventHandler<string> ClearInteractiveBoardArrived;
  67. /// <summary>
  68. /// 收到关闭摄像头的通知
  69. /// </summary>
  70. public event EventHandler<List<string>> MuteVideoUserListNotifyArrived;
  71. /// <summary>
  72. /// 通知切换到本地病人,当vCloudExamInfo的ExamPairInfo的PatientIdInUSMachine为空时需先创建病人及检查,否则直接跳到该病人。
  73. /// </summary>
  74. public event EventHandler<vCloudExamInfo> SwitchToLocalPatientEvent;
  75. /// <summary>
  76. /// 当开始Web会诊时
  77. /// </summary>
  78. public event EventHandler<string> StartWebConsultationEvent;
  79. /// <summary>
  80. /// 当切换病人时触发,病人信息与会诊code变更
  81. /// </summary>
  82. public event EventHandler<ChangeLiveConsultationrResult> ChangeLiveConsultationEvent;
  83. /// <summary>
  84. /// 当扫查手法摄像头开启或关闭时触发
  85. /// </summary>
  86. public event EventHandler<TerminalInfo> TerminalInfoChangedEvent;
  87. public ConsultationV2(JsonRpcClient client, string token, CacheDeviceDTO deviceInfo)
  88. {
  89. if (!string.IsNullOrEmpty(vCloudServerConfig.Instance.FISWebUrl))
  90. {
  91. ConsultationUrl = vCloudServerConfig.Instance.FISWebUrl + "#/login/";
  92. }
  93. _deviceId = deviceInfo?.DeviceCode;
  94. _client = client;
  95. _deviceToken = token;
  96. var liveConsultationService = _client?.CreateProxy<ILiveConsultationService>();
  97. var userService = _client?.CreateProxy<IUserService>();
  98. var deviceService = _client?.CreateProxy<IDeviceService>();
  99. var organizationService = _client?.CreateProxy<IOrganizationService>();
  100. ConsultationLiveVideoProvider = new ConsultationLiveVideoProviderV2(liveConsultationService, organizationService);
  101. ConsultationLiveVideoProvider.OnTRTCRoomEnterError += OnTRTCRoomEnterErrorHappened;
  102. ConsultationLiveVideoProvider.ConsultationLocalVideoFrameArrived += OnConsultationLocalVideoFrameArrived;
  103. ConsultationLiveVideoProvider.ConsultationRemoteVideoFrameArrived += OnConsultationRemoteVideoFrameArrived;
  104. ConsultationLiveVideoProvider.DeviceCameraFrameArrived += OnUltrasoundCameraFrameArrived;
  105. ConsultationManager = new ConsultationManagerV2(liveConsultationService);
  106. ConsultationManager.SwitchToLocalPatientEvent += OnSwitchToLocalPatientEvent;
  107. ConsultationRecipient = new ConsultationRecipientV2(ConsultationLiveVideoProvider, ConsultationManager, _deviceId, liveConsultationService, userService, deviceService);
  108. ConsultationRecipient.ConsultationDisconnected += OnConsultationDisconnected;
  109. ConsultationRecipient.ConsultationRequestArrived += OnConcultationRequestArrived;
  110. ConsultationRecipient.ConsultationMemberChangedNotificationArrived += OnConsultationMemberChangedNotificationArrived;
  111. ConsultationRecipient.InteractiveBoardInfoArrived += OnInteractiveBoardInfoArrived;
  112. ConsultationRecipient.ClearInteractiveBoardArrived += OnClearInteractiveBoardArrived;
  113. ConsultationRecipient.MuteVideoUserListNotifyArrived += OnMuteVideoUserListNotifyArrived;
  114. ConsultationRecipient.StartWebConsultationEvent += OnStartWebConsultationEvent;
  115. ConsultationRecipient.ChangeLiveConsultationEvent += OnChangeLiveConsultationEvent;
  116. ConsultationRecipient.TerminalInfoChangedEvent += OnTerminalInfoChangedEvent;
  117. }
  118. private void OnTerminalInfoChangedEvent(object sender, TerminalInfo e)
  119. {
  120. TerminalInfoChangedEvent?.Invoke(this, e);
  121. }
  122. private void OnStartWebConsultationEvent(object sender, string e)
  123. {
  124. StartWebConsultationEvent?.Invoke(this, e);
  125. }
  126. private void OnSwitchToLocalPatientEvent(object sender, vCloudExamInfo e)
  127. {
  128. SwitchToLocalPatientEvent?.Invoke(this, e);
  129. }
  130. private void OnMuteVideoUserListNotifyArrived(object sender, List<string> muteVideoUserList)
  131. {
  132. MuteVideoUserListNotifyArrived?.Invoke(this, muteVideoUserList);
  133. }
  134. private void OnInteractiveBoardInfoArrived(object sender, InteractiveBoardInfo e)
  135. {
  136. InteractiveBoardInfoArrived?.Invoke(this, e);
  137. }
  138. private void OnClearInteractiveBoardArrived(object sender, string e)
  139. {
  140. ClearInteractiveBoardArrived?.Invoke(this, e);
  141. }
  142. /// <summary>
  143. /// Gets the live states of the current chat.
  144. /// </summary>
  145. /// <returns></returns>
  146. public LiveStates GetCurrentConsultationLiveStates()
  147. {
  148. return ConsultationRecipient.CurrentLiveStatus;
  149. }
  150. /// <summary>
  151. /// Dispose consultation
  152. /// </summary>
  153. public void Dispose()
  154. {
  155. DoDispose();
  156. GC.SuppressFinalize(this);
  157. }
  158. private void DoDispose()
  159. {
  160. if (!_disposed)
  161. {
  162. var consultationtatus = GetCurrentConsultationLiveStates();
  163. var isConsultation = consultationtatus == LiveStates.RecipientAcceptted
  164. || consultationtatus == LiveStates.ChatRequestArrived
  165. || consultationtatus == LiveStates.InitiatorRequestingChat
  166. || consultationtatus == LiveStates.RecipientAcceptting;
  167. if (isConsultation)
  168. {
  169. HangupLiveConsultationFromFlutter(true);
  170. }
  171. ConsultationLiveVideoProvider.OnTRTCRoomEnterError -= OnTRTCRoomEnterErrorHappened;
  172. ConsultationLiveVideoProvider.ConsultationLocalVideoFrameArrived -= OnConsultationLocalVideoFrameArrived;
  173. ConsultationLiveVideoProvider.ConsultationRemoteVideoFrameArrived -= OnConsultationRemoteVideoFrameArrived;
  174. ConsultationLiveVideoProvider.DeviceCameraFrameArrived -= OnUltrasoundCameraFrameArrived;
  175. ConsultationLiveVideoProvider.Dispose();
  176. ConsultationRecipient.ConsultationDisconnected -= OnConsultationDisconnected;
  177. ConsultationRecipient.ConsultationRequestArrived -= OnConcultationRequestArrived;
  178. ConsultationRecipient.ConsultationMemberChangedNotificationArrived -= OnConsultationMemberChangedNotificationArrived;
  179. ConsultationRecipient.InteractiveBoardInfoArrived -= OnInteractiveBoardInfoArrived;
  180. ConsultationRecipient.ClearInteractiveBoardArrived -= OnClearInteractiveBoardArrived;
  181. ConsultationRecipient.MuteVideoUserListNotifyArrived -= OnMuteVideoUserListNotifyArrived;
  182. ConsultationRecipient.StartWebConsultationEvent -= OnStartWebConsultationEvent;
  183. ConsultationRecipient.ChangeLiveConsultationEvent -= OnChangeLiveConsultationEvent;
  184. ConsultationRecipient.TerminalInfoChangedEvent -= OnTerminalInfoChangedEvent;
  185. ConsultationRecipient.Dispose();
  186. ConsultationManager.SwitchToLocalPatientEvent -= OnSwitchToLocalPatientEvent;
  187. ConsultationManager.Dispose();
  188. _disposed = true;
  189. }
  190. }
  191. private void OnChangeLiveConsultationEvent(object sender, ChangeLiveConsultationrResult e)
  192. {
  193. ChangeLiveConsultationEvent?.Invoke(this, e);
  194. }
  195. private void OnConcultationRequestArrived(object sender, ConsultationInfo e)
  196. {
  197. CrossPlatformHelper.Instance.HardwareDetector.StopCameraPreview();
  198. ConsultationRequestArrived?.Invoke(this, e);
  199. }
  200. private void OnConsultationMemberChangedNotificationArrived(object sender, ConsultationMemberNotificaiton e)
  201. {
  202. ConsultationMemberChangedNotificationArrived?.Invoke(this, e);
  203. }
  204. private void OnTRTCRoomEnterErrorHappened(object sender, EnumTRTCRoomError e)
  205. {
  206. Logger.WriteLineError($"Consultation TRTCRoomEnterErrorHappened:{e}");
  207. }
  208. private void OnUltrasoundCameraFrameArrived(object sender, CPVideoFrameData e)
  209. {
  210. UltrasoundCameraFrameArrived?.Invoke(this, e);
  211. }
  212. private void OnConsultationRemoteVideoFrameArrived(object sender, ConsultationVideoFrameData e)
  213. {
  214. ConsultationRemoteVideoFrameArrived?.Invoke(this, e);
  215. }
  216. private void OnConsultationLocalVideoFrameArrived(object sender, ConsultationVideoFrameData e)
  217. {
  218. ConsultationLocalVideoFrameArrived?.Invoke(this, e);
  219. }
  220. private void OnConsultationDisconnected(object sender, ConsultationDisconnectedType e)
  221. {
  222. ConsultationDisconnected?.Invoke(this, e);
  223. }
  224. public ResultInfoDTO StartLiveConsultationFromFlutter(LiveConsultationRequestDTO liveConsultationRequestDTO, string cameraId, string micId, string speakerId)
  225. {
  226. if (liveConsultationRequestDTO == null)
  227. {
  228. return new ResultInfoDTO
  229. {
  230. IsSuccess = false,
  231. FailMessage = "ConsultationV2 StartLiveConsultationFromFlutter Fail:Args is null",
  232. };
  233. }
  234. return ConsultationRecipient.StartLiveConsultation(liveConsultationRequestDTO, cameraId, micId, speakerId);
  235. }
  236. public ResultInfoDTO StartLiveConsultationFromFlutter(StartOnlyForRtmpPushingDTO startOnlyForRtmpPushingDTO, string cameraId, string micId)
  237. {
  238. if (startOnlyForRtmpPushingDTO == null)
  239. {
  240. return new ResultInfoDTO
  241. {
  242. IsSuccess = false,
  243. FailMessage = "ConsultationV2 StartLiveConsultationFromFlutter Fail:Args is null",
  244. };
  245. }
  246. return ConsultationRecipient.StartLiveConsultation(startOnlyForRtmpPushingDTO, cameraId, micId);
  247. }
  248. public string GetUserInfo(string token)
  249. {
  250. return ConsultationRecipient.GetUserInfo(token);
  251. }
  252. public ResultInfoDTO HangupLiveConsultationFromFlutter(bool isInteractiveExit)
  253. {
  254. var result = ConsultationRecipient.Hangup();
  255. if (isInteractiveExit)
  256. {
  257. OnConsultationDisconnected(this, ConsultationDisconnectedType.HangUpBySelf);
  258. }
  259. else
  260. {
  261. OnConsultationDisconnected(this, ConsultationDisconnectedType.HangupByOther);
  262. }
  263. return result;
  264. }
  265. /// <summary>
  266. /// 切换会诊病人
  267. /// </summary>
  268. /// <param name="changeLiveConsultation0RequestDTO"></param>
  269. /// <returns></returns>
  270. public ResultInfoDTO ChangeLiveConsultationFromFlutter(ChangeLiveConsultationRequestDTO changeLiveConsultation0RequestDTO)
  271. {
  272. return ConsultationRecipient.ChangeLiveConsultation(changeLiveConsultation0RequestDTO);
  273. }
  274. public ResultInfoDTO SwitchToLocalPatientFromFlutter(SwitchToLocalPatientRequestDTO switchToLocalPatientRequestDTO)
  275. {
  276. return ConsultationManager.SwitchToLocalPatientFromFlutter(switchToLocalPatientRequestDTO);
  277. }
  278. /// <summary>
  279. /// 上报本地与云端病人的关联信息
  280. /// </summary>
  281. /// <param name="patientPairInfo"></param>
  282. /// <returns></returns>
  283. public bool UpdatePatientPairInfo(PatientPairInfo patientPairInfo)
  284. {
  285. return ConsultationManager.UpdatePatientPairInfo(patientPairInfo);
  286. }
  287. public ResultInfoDTO LiveConsultationMemberChangedFromFlutter(ConsultationMemberChangeDTO consultationMemberChangeDTO)
  288. {
  289. return ConsultationRecipient.LiveConsultationMemberChanged(consultationMemberChangeDTO);
  290. }
  291. /// <summary>
  292. /// 当收到白板信息的时候
  293. /// </summary>
  294. /// <param name="whiteBoardDataDTO"></param>
  295. /// <returns></returns>
  296. public ResultInfoDTO ReceiveWhiteBoardDataFromFlutter(WhiteBoardDataDTO whiteBoardDataDTO)
  297. {
  298. return ConsultationRecipient.ReceiveWhiteBoardData(whiteBoardDataDTO);
  299. }
  300. public ResultInfoDTO SendFlutterLiveConsultationInfo(FlutterLiveConsultationInfo flutterLiveConsultationInfo)
  301. {
  302. if (flutterLiveConsultationInfo == null)
  303. {
  304. Logger.WriteLineError($"SendFlutterLiveConsultationInfo Error:Args is null");
  305. return new ResultInfoDTO
  306. {
  307. IsSuccess = false,
  308. FailMessage = "Args is null",
  309. };
  310. }
  311. if (!flutterLiveConsultationInfo.IsStartLiveConsultation)
  312. {
  313. ConsultationRecipient.Hangup(true);
  314. OnConsultationDisconnected(this, ConsultationDisconnectedType.HangUpBySelf);
  315. return new ResultInfoDTO
  316. {
  317. IsSuccess = true,
  318. };
  319. }
  320. else
  321. {
  322. if (string.IsNullOrEmpty(flutterLiveConsultationInfo.LiveConsultationFlutterUrl))
  323. {
  324. Logger.WriteLineError($"SendFlutterLiveConsultationInfo Error:url is null");
  325. return new ResultInfoDTO
  326. {
  327. IsSuccess = false,
  328. FailMessage = "Url is null",
  329. };
  330. }
  331. if (string.IsNullOrEmpty(flutterLiveConsultationInfo.ConsulationId))
  332. {
  333. Logger.WriteLineError($"SendFlutterLiveConsultationInfo Error:consultationId is null");
  334. return new ResultInfoDTO
  335. {
  336. IsSuccess = false,
  337. FailMessage = "consultationId is null",
  338. };
  339. }
  340. else
  341. {
  342. return ConsultationRecipient.StartWebConsultation(flutterLiveConsultationInfo);
  343. }
  344. }
  345. }
  346. public ResultInfoDTO SwitchVideoPlayFromFlutter(string userId, bool isVideoOpen)
  347. {
  348. if (string.IsNullOrEmpty(userId))
  349. {
  350. return new ResultInfoDTO
  351. {
  352. IsSuccess = false,
  353. FailMessage = "UserId is null",
  354. };
  355. }
  356. return ConsultationRecipient.SwitchVideoPlay(userId, isVideoOpen);
  357. }
  358. /// <summary>
  359. /// 是否第二屏会诊或者使用FIS会诊中
  360. /// </summary>
  361. /// <returns></returns>
  362. public bool CheckLiveConsultationState()
  363. {
  364. return ConsultationRecipient.IsSecondViewMode || ConsultationRecipient.CurrentLiveStatus == LiveStates.RecipientAcceptted || ConsultationRecipient.CurrentLiveStatus == LiveStates.RecipientAcceptting;
  365. }
  366. }
  367. }